将字符串转换为秒-Robot Framework [英] Convert string to seconds - Robot framework

查看:211
本文介绍了将字符串转换为秒-Robot Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法从页面获取时间戳:

I am getting a time stamp from a page using:

|| ${value}= |  Get Text |  //span[@class='time']

它将以以下格式返回值 Apr 2015年2月28日上午3:03。我想将其转换为秒,以便可以将时间戳与测试案例中先前发生的另一个事件进行比较。

it is returning a value in this format "Apr 28, 2015 03:03 AM AST". I want to convert it to seconds so that I can compare the time stamp with another event that has occurred previously in my test cases.

对于先前的事件,我使用了以下内容并且我在几秒钟内得到结果:

For previous event I have used the following and I am getting results in seconds:

||  ${secs} = |  Get Time  |    epoch

请让我知道上面缺少的信息,我将编辑我的问题。如果您还可以指导我,在获得两个时间戳后如何判断两个时间戳之间的差异是否小于5分钟,我将不胜感激。

Please let me know which info I am missing in above and I will edit my question. I would also appreciate if you can also guide me that after having the two stamps how can I tell if the difference of the two time stamps is less than 5 minutes or not.

推荐答案

您可以使用DateTime库

You can use the DateTime library

该库具有一个convert date关键字,可以将其转换为时代时间。

The library has a convert date keyword that can convert into epoch time.

但是请注意,一旦时区开始发挥作用,您的测试就可能会失败,因为有人在不同时区的主机上运行了它。

Note however, that once time-zones come into play, your tests would probably break down, Simply because someone ran it on a host in a different timezone.

*** Settings ***
Library DateTime

*** Keywords ***
Convert The date to epoch
  [arguments] ${date}
  ${epoch_date}=  Convert Date  ${date}  epoch
  [return]  ${epoch_date}

转换日期也可以接受格式参数(美国东部时间2015年4月28日上午03:03)

Convert date can also accept a format argument (Apr 28, 2015 03:03 AM AST)

  ${epoch_date}=  Convert Date  ${date}  epoch  date_format=%bbb %dd, %Y %H:%M %p %Z

此处解释格式:
https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

这篇关于将字符串转换为秒-Robot Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆