Comparing two date values in RobotFramework - datetime

I'm trying to compare two date values using conditional statement. My plan is to validate DueDate against CompletionDate and determine if the value of CompletionDate is greater than DueDate which is not valid date value. Tried different resolution but didn't work for my desired result.
Test Date
${a} Convert Date 12/30/2023 result_format=%Y/%m/%d date_format=%m/%d/%Y
${b} Convert Date 12/16/2022 result_format=%Y/%m/%d date_format=%m/%d/%Y
IF ${a} > ${b}
Log To Console A:${a} is greater than B:${b}
ELSE
Log To Console B:${b} is greater than A:${a}
END
The result is ${b}
My conclusion here is that the IF condition here is treating the date values differently
Is there any way to do this, I have browse already in the RobotFramework documentation but nothing fits to my problem. Thank you!

I think RF tries to do arithmetic on your dates. You can test this by trying date `1/1/2022'. There are two workarounds:
Put quotes around variables: IF '${a}' > '${b}'
Convert dates to epoch: result_format=epoch
Here is a code sample:
*** Settings ***
Library DateTime
*** Variables ***
${a_date} 12/30/2023
${b_date} 12/16/2022
*** Test Cases ***
Test Date with Epoch
${a_epoch} Convert Date ${a_date} result_format=epoch date_format=%m/%d/%Y
${b_epoch} Convert Date ${b_date} result_format=epoch date_format=%m/%d/%Y
IF ${a_epoch} > ${b_epoch}
Log To Console \nA:${a_epoch} is greater than B:${b_epoch}
ELSE
Log To Console \nB:${b_epoch} is greater than A:${b_epoch}
Fail A should be greater than B
END
Test Date with String
${a_str} Convert Date ${a_date} result_format=%Y/%m/%d date_format=%m/%d/%Y
${b_str} Convert Date ${b_date} result_format=%Y/%m/%d date_format=%m/%d/%Y
IF '${a_str}' > '${b_str}'
Log To Console \nA:${a_str} is greater than B:${b_str}
ELSE
Log To Console \nB:${b_str} is greater than A:${b_str}
Fail A should be greater than B
END

Related

controlM variable for YYYYMM?

I'm using ControlM and in a command, I would like to find a variable that gives me the date in this format : YYYYMM
I found there is %%$DATE variable but it gives YYYYMMDD
Thanks for you help
It is possible to define and concatenate a variable that will represent the date in such a format.
These are available:
Day DD, %%DAY,
Month MM, %%MONTH,
Year YY, %%YEAR,
Year YYYY, %%$YEAR
Prefer %%$OYEAR AND %%OMONTH over %%$YEAR and %%MONTH
I suggest using the variables %%$OYEAR and %%OMONTH over %%$YEAR and %%MONTH. The reason is that date variables beginning with O refer to processing dates and do not necessarily coincide with the system date. For this case you could use any of the following options:
1. YYYYMM = %%$OYEAR.%%OMONTH
2. YYYYMM = %%SUBSTRING %%$ODATE 1 6
The $ symbol preceding the variable %%$OYEAR or %%$ODATE indicates that the year is returned in 4-digit format, instead of OYEAR or ODATE which print the year with only 2 digits.
The dot (.) character is used for concatenate variables.
For example: For the order day May 29, 2020.
1. %%$ODATE would print 20200529
2. %%ODATE would print 200529

Robotframework - Day of the month without zero-padded decimal number

I use this:
${today}= Get Time
${today_formated}= Convert Date ${today} result_format=%d
The result is 01 for the 1st day of the month but I need 1.
%d is Day of the month as a zero-padded decimal number. 01, 02, …, 31
How to remove 0 at the start?
My question is on the day of the month not on the month number
full robotframework script:
*** Settings ***
Library SeleniumLibrary
Library DateTime
*** Keywords ***
test
${today}= Get Time
${today_formated}= Convert Date ${today} result_format=%d
Log To Console ${today_formated}
*** Test Cases ***
PLFT
[Tags] foo|AC0
Given test
01
Use Replace String Using Regexp from standard library String
${today}= Get Time
${today_formated}= Convert Date ${today} result_format=%d
${today_no_padding}= Replace String Using Regexp ${today_formated} ^0 ${EMPTY}
This transforms values with leading zeros like 01 to 1 but values just containing zeros like 10 would remain the same.
To use the library, add a declaration to your settings:
*** Settings ***
Library SeleniumLibrary
Library DateTime
Library String

How to convert chararray to datetime with milliseconds in pig latin

I wish to convert following value which is a chararray in pig
2016-05-11 23:59:57.628197
to
2016-05-11T23:59:57.628-05:00
How can I do it ?
Following is what I tried considering alias 'a2' contains list of datetime values in chararray in the column named 'input_date_value'
FOREACH a2 GENERATE input_date_value AS input_date:chararray,
ToDate(input_date_value,'YYYY-MM-DD HH:mm:ss.SSSSSS') AS modification_datetime:datetime;
For input -
2002-07-11 16:58:40.249764
Output is -
2002-01-11T16:58:40.249-05:00
The month values like '07' are not getting picked up,
The created timestamp has month set to 01' i.e. January everytime for all dates.
Can someone help. What am I doing wrong ?
https://pig.apache.org/docs/r0.11.1/func.html#to-date ToDate takes SimpleDateFormat only supports milliseconds http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
The -05:00 you see is the time zone ToDate is actually truncating to 3 digits as it supports only milliseconds
Use lowercase character d instead of uppercase D for parsing date values.
Now, I have managed to fix it myself on (In Pig 0.11)
Apparently Pig 0.11 does not support the date format components I used earlier for parsing the month and date.
I found below inference which hints on the incompatibility as mentioned https://www.w3.org/TR/NOTE-datetime
Use:
'YYYY-MM-dd HH:mm:ss.SSSSSS'
instead of 'YYYY-MM-DD HH:mm:ss.SSSSSS'
It now gives correct output.
Input:
2001-11-28 16:04:49.22388
Output:
2001-11-28T16:04:49.223-05:00

Converting Date time using respective tz values in another column

I have tried to look up previous answers, if anyone thinks this has been answered before please direct me to link.
I have a dataframe (Tickets) with 4 Date time values - and in another column I have timezone values.
sample dataset as below.
TIMEZONE,Creation_Datetime
US/Mountain,2013-07-09 10:08:00
US/Central,2014-03-24 05:37:13
US/Pacific,2013-01-26 04:30:57
US/Eastern,2013-01-21 02:59:18
US/Eastern,2014-02-24 08:39:17
US/Eastern,2013-02-05 02:30:36
I need to convert creation_Datetime in all rows as per the timezone in corresponding row.
Tickets$Creation_Datetime= with_tz(
Tickets$Creation_Datetime,
as.character(Tickets$TIMEZONE)
)
...throws an error "invalid tz value".
It works with a single tz Tickets$TIMEZONE[1] value or character value 'US/Mountain' passed as timezone.
Help with applying 'apply' function to all rows.

convert string to time in r

I have an array of time strings, for example 115521.45 which corresponds to 11:55:21.45 in terms of an actual clock.
I have another array of time strings in the standard format (HH:MM:SS.0) and I need to compare the two.
I can't find any way to convert the original time format into something useable.
I've tried using strptime but all it does is add a date (the wrong date) and get rid of time decimal places. I don't care about the date and I need the decimal places:
for example
t <- strptime(105748.35, '%H%M%OS') = ... 10:57:48
using %OSn (n = 1,2 etc) gives NA.
Alternatively, is there a way to convert a time such as 10:57:48 to 105748?
Set the options to allow digits in seconds, and then add the date you wish before converting (so that the start date is meaningful).
options(digits.secs=3)
strptime(paste0('2013-01-01 ',105748.35), '%Y-%M-%d %H%M%OS')

Resources