Time format related!

Happy New Year to All!!!
I need a little help with time format in RF.
I have in the file this format of date - Sun Jan 1 14:29:11 CST 2023
So , that time stemp I am I save to the txt file , and then I have to check if I have - Day , Month , day number and an hour β€” like this β€” > Sun Jan 1 14 ( dont need to check minutes)
So , I need to convert time in RF and I am using this format -

  1. ${time}= Get Current Date time_zone=UTC
  2. ${time2_hour}= Convert Date ${time} result_format=%a %b %d %H
    – this one worked until till it hits Jan 1 , it would grep the time from txt file, because it would be looking for β€”> Sun Jan 01 14 β€” Jun 01 , so I was able to find a way how to get rid of β€œ0” in front of 1.
    like that - << result_format=%a %b %#d %H >>

but its still would grep from txt file because there is a difference between
Sun Jan 1 20 - from RF
Sun Jan 1 20 - from the system

  • less space between Jan and 1 in RF.
    How I can do in RF same output ? played << result_format=%a %b %#d %H >> giving more spaces , did not work.
    Thank you!

Hi Sam,

Unfortunately you can’t use Get Time because that time string is not a standard timestamp. If you can modify the output from the app that generates the text file to output an ISO 8601 date format, this would be the easy way to deal with this problem.

There’s a few things I can think of that you could try:

  • use ${SPACE} to handle the double space in a RF friendly way but this is likely to break on the 10th of Jan
${time2_hour}= Convert Date ${time} result_format=%a %b ${SPACE}%d %H
  • You could use Replace String to do things like

    • replace ${SPACE}${SPACE} with ${SPACE}
    • replace ${SPACE}0 with ${SPACE}
  • another option might be to not worry about converting to a time string at all since your requirement is only to check the first 4 elements you could simply use Split String to give you a list then just check the elements you care about

Hopefully one of these will be helpful,

Dave.

1 Like

Hi Dave,
One of your combinations works -
– ${time2_hour}= Replace String ${str_time2_hour} 0 ${SPACE}
this combination didn’t work for me << ${SPACE}0 with ${SPACE}>>

it does convert << Tue Jan 03 15 >> to << Tue Jan 3 15 >> with 2 spaces between Jan and 3.
I think it might fail if it hits Jan 10 or Jan 20 or Jan 30 β€” > because it still has 0 , and it will convert it like – > Jan 1 , Jan 2, Jan 3 . maybe?

I was thinking maybe do if statement , somehow , if date is 10 or greater use date format as it was before, else: use new time where o is replaces with space

Hi Sam,

did you use

${time2_hour}=    Replace String    ${str_time2_hour}    0    ${SPACE}

or

${time2_hour}=    Replace String    ${str_time2_hour}    ${SPACE}0    ${SPACE}

The reason I ask, RF uses whitespace (spaces and tabs) as a delimiter, so if you want to replace a string starting with a space you need to specify it with the Built-in variable

Consider the following examples:

Using:

${time2_hour}=    Replace String    ${str_time2_hour}    0    ${SPACE}
${str_time2_hour}=   Sun Jan 01 14:29:11 CST 2023
${time2_hour}=       Sun Jan  1 14:29:11 CST 2023 # 2 spaces before day number, because it simply matches on 0
${str_time2_hour}=   Sun Jan 10 14:29:11 CST 2023
${time2_hour}=       Sun Jan 1  14:29:11 CST 2023 # 2 spaces after day number, because it simply matches on 0

${str_time2_hour}=   Sun Jan 01 04:20:11 CST 2023
${time2_hour}=       Sun Jan  1  4:2 :11 CST 2023 # note what happened to the time

Now using:

${time2_hour}=    Replace String    ${str_time2_hour}    ${SPACE}0    ${SPACE}
${str_time2_hour}=   Sun Jan 01 14:29:11 CST 2023
${time2_hour}=       Sun Jan 1 14:29:11 CST 2023 # 1 space before day number, because it matched the space before the 0 with the 0 and replaced these 2 characters with a single space
${str_time2_hour}=   Sun Jan 10 14:29:11 CST 2023
${time2_hour}=       Sun Jan 10 14:29:11 CST 2023 # day number not changed because there was no 0 with a space before it to match

${str_time2_hour}=   Sun Jan 01 04:20:11 CST 2023
${time2_hour}=       Sun Jan 1 4:20:11 CST 2023 # note what happened to the time

Hope that helps,

Dave.

Of course it helps a lot , thank you Dave.
So , I went with :
${time2_hour}= Replace String ${str_time2_hour} 0 ${SPACE},

I have tried β€”>
${time2_hour}= Replace String ${str_time2_hour} ${SPACE}0 ${SPACE}
but it didnt work for me ) will look into [Built-in variable] . - don’t know how to use that yet.

I run this command first now - on system - ${dateNumber}= Execute Command date +β€˜%-d’

  • it gives me day as an integer without 0 in front. instead of 03β€” I would get 3,

then I am using if-else

  • lets say today is 4, then β€”>

     IF    ${dateNumber} < 10
    
     ${str_time2_hour}=         Convert Date            ${time}           result_format=%a %b %d %H
     ${time2_hour}=              Replace String    ${str_time2_hour}    0    ${SPACE}
    
     ELSE   
    ${str_time2_hour}=         Convert Date            ${time}           result_format=%a %b %d %H
    

My idea is if ${dateNumber} will be 10 and up , it will go as usuall without Replace String Command.
Not sure if that will work.
But I know for sure it will go to Else statement and run that code, did tested if
${dateNumber}= Execute Command date +β€˜%-d’ β€” output will be 10 or up
Looks ugly now, since I doubled the code ( I mean its a little different in IF statement, but anyway it more lines now).

Hi Sam,

I decided to to have a go and create a working example for you, I left in my failed attempts commented out so you can see the progression (this might help with learning):

sam-datetime.robot

*** Settings ***
Library 	DateTime
Library 	String

*** Variables ***
# Test Template	Check Date

*** Test Cases ***
T-01-14
	Check Date	Sun Jan 01 14:29:11 CST 2023

T-10-14
	Check Date	Sun Jan 10 14:29:11 CST 2023

T-01-04
	Check Date	Sun Jan 01 04:29:11 CST 2023

*** Keywords ***
Check Date
	[Arguments]		${timein}
	Log To Console	${EMPTY}
	Log To Console    timein= ${SPACE}${SPACE}${SPACE}${SPACE}${timein}
	# ${timeresult}=	Convert Date	${timein}	result_format=%a %b %d %H
	# ${timeresult}=	Convert Date	${timein}	result_format=%a %b %d %H:%M:%S %Z %Y
	# ${timeresult}=	Convert Date	${timemod}	result_format=%a %b %w %H
	# ${timeresult}=	Convert Date	${timein}	date_format=%a %b %d %H:%M:%S %Z %Y
	# ${timeresult}=	Convert Date	${timein}	date_format=%a %b %d %H
	${timeresult}=	Convert Date	${timein}	date_format=%a %b %d %H:%M:%S CST %Y	result_format=%a %b %d %H
	Log To Console    timeresult= ${timeresult}
	${timemoda}=			Replace String	${timeresult}	${SPACE}0	${SPACE}
	Log To Console    timemoda= ${SPACE}${SPACE}${timemoda}
	${timemodb}=			Replace String	${timeresult}	${SPACE}0	${SPACE}	count=1
	Log To Console    timemodb= ${SPACE}${SPACE}${timemodb}

and the output:

% robot sam-datetime.robot
==============================================================================
Sam-Datetime                                                                  
==============================================================================
T-01-14                                                               
timein=     Sun Jan 01 14:29:11 CST 2023
timeresult= Sun Jan 01 14
timemoda=   Sun Jan 1 14
timemodb=   Sun Jan 1 14
T-01-14                                                               | PASS |
------------------------------------------------------------------------------
T-10-14                                                               
timein=     Sun Jan 10 14:29:11 CST 2023
timeresult= Tue Jan 10 14
timemoda=   Tue Jan 10 14
timemodb=   Tue Jan 10 14
T-10-14                                                               | PASS |
------------------------------------------------------------------------------
T-01-04                                                               
timein=     Sun Jan 01 04:29:11 CST 2023
timeresult= Sun Jan 01 04
timemoda=   Sun Jan 1 4
timemodb=   Sun Jan 1 04
T-01-04                                                               | PASS |
------------------------------------------------------------------------------
Sam-Datetime                                                          | PASS |
3 tests, 3 passed, 0 failed
==============================================================================
Output:  /Users/dave/tmp/sam/output.xml
Log:     /Users/dave/tmp/sam/log.html
Report:  /Users/dave/tmp/sam/report.html

Is this what you wanted to achieve?

Dave.

1 Like

Hi Dave,
Thank you for your time spending on this for me. I appreciate it ! :+1:
I need some time to see how you did it. I am going to copy your code and run in my pycharm.
To see how you did it step by step.
Want to see if that would take correct date with data from txt file, with diff dates.
Will keep you posted ! :+1:

1 Like

Hi Dave,
Sorry for the long wait!
I was trying to bring real date into your code , where you hardcoded it
*** Test Cases ***

T-01-14
	Check Date	Sun Jan 01 14:29:11 CST 2023

this one

Sun Jan 01 14:29:11 CST 2023

but wansn’t able to actually do it.

*** Test Cases ***

T-01-04
${getTime}= Get Current Date
Log To Console ${getTime}
Check Date ${getTime}

*** Keywords ***
Check Date
[Arguments] ${timein}
Log To Console ${EMPTY}
Log To Console timein= ${SPACE}${SPACE}${SPACE}${SPACE}${timein}
${timeresult}= Convert Date ${timein} date_format=%a %b %d %H:%M:%S CST %Y result_format=%a %b %d %H
Log To Console timeresult= ${timeresult}
${timemoda}= Replace String ${timeresult} ${SPACE}0 ${SPACE}
Log To Console timemoda= ${SPACE}${SPACE}${timemoda}
${timemodb}= Replace String ${timeresult} ${SPACE}0 ${SPACE} count=1
Log To Console timemodb= ${SPACE}${SPACE}${timemodb}

So, taking a real time and put in variable , then I was trying use your code to make it with spaces .

Hi Sam,

My example was for dealing with the non-standard date format that you showed in your first post, I assumed this was the format you were getting from the file

if you are using ${getTime}= Get Current Date you don’t need a custom keyword like Check Date as the standard DateTime keywords can handle it

You could modify the date_format= option of Convert Date to parse the standard ISO format date given by Get Current Date if you want, you could also make the date_format a second argument to Check Date if you want ti to be able to check several date formats.

Hope that explains it,

Dave.