roger referabal
roger referabal

Reputation: 89

Convert string to seconds - Robot framework

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.

Upvotes: 0

Views: 5554

Answers (1)

Uri Shtand
Uri Shtand

Reputation: 1737

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

Upvotes: 1

Related Questions