Convert string to seconds - Robot framework - datetime

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.

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

Related

How to subtract a date from another date using Robot Framework?

i'm using Robot Framework Keyword Subtract Date From Date to test that a date is between 2 dates(For example 11/06/2020 is between 07/06/2020 and 20/06/2020), but i'm getting an error in my log file ValueError: Invalid timestamp. The date is a String extracted from a Span Text and i convert it to date This is my code :
${date}= Get Current Date result_format=datetime
${postdate}= Get Text xpath://app-post[1]//div[1]//div[1]//div[1]//div[2]//div[1]//div[2]//div[1]//div[2]//span[2]
${postdate}= Convert Date ${postdate} date_format=%d/%m/%y
${date}= Convert Date ${date} result_format=%d/%m/%y
${temp} = Subtract Date From Date ${postdate} ${date}
Should Be True ${temp}>=0
The problem is you don't follow the allowed format for the keyword Subtract Date From Date mentioned here: https://robotframework.org/robotframework/latest/libraries/DateTime.html#Date%20formats
%y will give you 20, but that obviously is not enough. If I change it to %Y, it will give me 2020 for the current year, and the keyword Subtract Date From Date doesn't complain anymore about invalid timestamp. You'd also need to change it to something like date_format=%Y/%m/%d, that is to change the order of %Y, %m, and %d.
EDIT:
I can now see you're also using date_format as well as result_format with Convert Date. That's another bug in your code, you need to use result_format with both.
EDIT 2:
The full working example:
(And you're not using BuiltIn library here, these datetime keywords are from DateTime library)
The below script worked (Used the static PAST_DATE to check the sample run ):
${date}= Set Variable 21
${month}= Set Variable 12
${year}= Set Variable 2019
${PAST_DATE}= BuiltIn.Catenate SEPARATOR=/ ${year} ${month} ${date}
Log ${PAST_DATE}
${CURRENT_DATE}= Get Current Date result_format=%Y/%m/%d
Log ${CURRENT_DATE}
${DIFFERNCE_IN_DAYS}= Subtract Date from Date ${CURRENT_DATE} ${PAST_DATE} verbose
Log ${DIFFERNCE_IN_DAYS}
#{total_days}= Split String ${DIFFERNCE_IN_DAYS}
${final_days}= Set Variable ${total_days[0]}
Log ${final_days}
Should be true ${final_days} >= 0

How to get a date in milliseconds with moment.js

I'm trying to get a date in milliseconds with moment.js. The problem is, in their docs in unix timestamp section they have only unix timestampt to momentjs format. How can I get a unix timestamp in milliseconds from date?
Eg: moment.unix(2010-01-01T05:06:07) => 1262318767000
You can get the date in milliseconds also with format().
moment("2010-01-01T05:06:07").format('x');
You said that "in their docs in unix timestamp section they have only unix timestampt to momentjs format", but that's not true.
According to https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/:
moment#valueOf simply outputs the number of milliseconds since the Unix Epoch
So, use the valueOf method:
moment("2010-01-01T05:06:07").valueOf();
But in my machine it returns 1262329567000. That's because moment.js is using my browser's timezone - this milliseconds value corresponds to January 1st 2010, at 05:06:07 AM in my browser's timezone.
The value you mention in your question (1262318767000) corresponds to January 1st 2010, at 05:06:07 AM in a timezone where the offset +01:00 is used: in some place that is one hour ahead of UTC, in January 1st 2010: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets#UTC+01:00,_A
If you want to be specific about what timezone the date/time corresponds to, you can use moment timezone: https://momentjs.com/timezone/
valueOf function https://momentjs.com/docs/#/parsing/utc/
console.log(moment('12/09/2021').valueOf());
In above example, you will not if it is 12 September or 09 January. It is better to provide the format, which format you're sending the value.
Checkout the below example,
moment(dateValue, dateFormat).valueOf();
So here we provide the date format which we are sending to moment and again converting it to a format moment understands and then taking out the milliseconds from 1 January 1970. If your date is less than that date, the value will be negative
Example:
console.log(moment('09/12/2021', 'DD/MM/YYYY').valueOf());
You can use
moment( date ).toDate().getTime();
And if you like to use timezone
let timezone = moment.tz.guess() || 'America/Los_Angeles';
moment( date, timezone ).toDate().getTime();

TeradataSQL: Time to String, Add to Date and Compare to Another Time and Data

I'm trying to figure out the cleanest way to do a comparison in Teradata SQL Assistant. I have the scheduled start date (TimeStamp), the Schedule start time (varchar), actual start and end times (TimeStamp). I need to consolidate the scheduled start date and time and be able to compare it to the actual start and end date and time without modifying the original data (because it's not mine). I realize that the Scheduled Start Time [SST] is in a 24 hour time format with a AM/PM suffix, but like I said before, I can't change that.
I tried to do select cast(substr(scheduled_start_date,1,5) as TIMESTAMP(0)) from DB.TBL but am getting the "Invalid timestamp" error. There is example table data below.
Sch Start Date Sch Start Time Actual Start Actual End
09/11/2017 00:00:00 11:30 AM 09/11/2017 11:34:16 09/11/2017 11:58:00
05/26/2017 00:00:00 15:30 PM 05/26/2017 15:40:00 05/26/2017 15:55:15
11/06/2017 00:00:00 19:30 PM 11/06/2017 21:25:00 11/06/2017 21:45:00
Thanks!
You need to cast the schedule start time as an Interval, then you can easily add it to the start date:
scheduled_start_date
+ Cast(Substr(scheduled_start_time, 1,5) AS INTERVAL HOUR TO MINUTE)
A start date which is a timestamp seems to indicate this was ported from Oracle/SQL Server?
And a 24 hour time format with a AM/PM suffix is also quite strange.
A couple things to try:
Convert the separate Scheduled Date and Scheduled Time fields into strings, concatenate them, and feed that into a TIMESTAMP CAST. Something like:
SELECT
CAST(CAST(Scheduled_Date AS DATE) AS VARCHAR(25)) AS Date_String,
CAST(CAST(Scheduled_Time AS TIME FORMAT 'HH:MM BB') AS VARCHAR(25)) AS Time_String,
CAST(TRIM(Date_String) || ' ' || TRIM(Time_String) AS TIMESTAMP(0)) AS MyTimestamp
Cast the Scheduled Time field as a TIME data type. Cast the Scheduled Date field as a DATE data type. Then somehow combine the two into a TIMESTAMP field -- either with a CAST or some kind of timestamp constructor function (not sure if this is possible)
Option 1 should work for sure as long as you properly format the strings. Try to avoid using SUBSTRING and instead use FORMAT to cast as DATE/TIME fields. Not sure about Option 2. Take a look at these link for how to format DATE/TIME fields using the FORMAT clause:
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference%2FB035-1143-160K%2Fmuq1472241377538.html%23wwID0EPHKR
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference/B035-1143-160K/cmy1472241389785.html
Sorry, I don't have access to a TD system to test it out. Let me know if you have any luck.

Format UTC Time To Local Time

When you have a string that is formatted in UTC Time, how can I format it to a DateTime but local time?
For example, if I have the below code, it improperly formats my code (meaning incorrect time)
string dateformatted = "2017-01-10T11:13:00-07:00"
DateTime.Parse(Convert.ToString(dateformatted));
However, the output from this is
01/10/2017 1:13:00 PM
Which is 2 hours ahead of the actual time of 11:13:00. How can I convert the string to the proper timezone time?
I believe you are looking for the Parse(String, IFormatProvider, DateTimeStyles) overload of the Parse method. The third parameter, DateTimeStyles, will allow for force or prohibit conversion between local and UTC times. Options include: AdjustToUniversal, AssumeLocal, and AssumeUniversal (among others).
According MSDN docs:
Return value Type: DateTime
An object that is equivalent to the date and time contained in s.
Generally, the Parse method returns a DateTime object whose Kind
property is DateTimeKind.Unspecified. However, the Parse method
may also perform time zone conversion and set the value of the Kind
property differently,depending on the values of the s and styles
parameters:
If:
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| If | Time zone conversion | Kind property |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains time zone information. | The date and time is converted to the time in the local time zone. | DateTimeKind.Local |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains time zone information, and styles includes the AdjustToUniversalflag. | The date and time is converted to Coordinated Universal Time (UTC). | DateTimeKind.Utc |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains the Z or GMT time zone designator, and styles includes the RoundtripKind flag. | The date and time are interpreted as UTC. | DateTimeKind.Utc |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
You should add timezone.

AngularJS date filter incorrect time

I have a date object formatted to isotime. I'm using the |date filter to nicely format this in my template, but it incorrectly changing the time.
This Code...
<td>[[ user.last_online | date:'dd MMM yyyy - hh:mm a' ]]</td>
<td>[[ user.last_online ]]</td>
Results in this...
Now I know that the 1 hour difference is because of the Timezone, this is what I'm expecting. The Minutes however is incorrect.
In the first row, 13 minutes gets added when the filter is applied.
In the second row, 5 minutes gets added.
Not only are these two values wrong, but they are also inconsistent.
If you check ISO8601, you can see the correct time stamp format is
yyyy-MM-ddTHH:mm:ss.SSSZ
The milliseconds should consists of 3 digits. I did a simple test and you can see after correcting the milliseconds part, the dates will be rendered correctly.
{{"2013-08-09T15:36:31.764546+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />
{{"2013-08-09T15:34:14.318753+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />
{{"2013-08-09T15:36:31.764+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />
{{"2013-08-09T15:34:14.318+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />
The result is
09 Aug 2013 - 09:49 AM
09 Aug 2013 - 09:39 AM
09 Aug 2013 - 09:36 AM
09 Aug 2013 - 09:34 AM
Demo
Update
Python's datetime.isoformat() return the time with microseconds 0 <= microsecond < 1000000. Angularjs doesn't like, though this format is correct according to ISO8601, since ISO8601 only requires one or more digits representing a decimal fraction of a second
So I guess you can use strftime to format it.
I think the value of user.last_online is incorrect or has a bad format. If you check ISO8601, you can see the correct time stamp format is:
yyyy-MM-ddTHH:mm:ss.SSSZ
My plunker
Your dates are correctly formatted. ISO8601 doesn't actually require any particular number of decimals. There could be anywhere from zero to 7 decimals or more. If you look at an actual copy of the ISO8601 spec, section 4.2.2.4 says the following:
... with as many digits as necessary following the decimal sign ...
There are a few older browsers where this mattered when passed directly to the new Date() constructor, but AFAIK those were consider bugs and were fixed.
You are experiencing a bug in AngularJS, which was fixed in version 1.1.5. You can find it referenced in their change log as follows:
date filter: correctly format dates with more than 3 sub-second digits (4f2e3606)

Resources