Convert string to seconds - Robot framework

萝らか妹 提交于 2019-12-23 04:58:18

问题


I am getting a time stamp from a page using:

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

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

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.


回答1:


You can use the DateTime library

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}

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

The format is explained here: https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior



来源:https://stackoverflow.com/questions/29924390/convert-string-to-seconds-robot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!