How to use keyword "Get time" to convert date "MM DD YY" ... Ex: "May 22 14"

2,546 views
Skip to first unread message

Phuoc Ha

unread,
May 25, 2014, 11:17:47 PM5/25/14
to robotframe...@googlegroups.com
Hello guys,

I want to get system date and convert date to format "May 22 14" in Robot FW.
I know some way to format date with keyword "Get time" from BuiltIn:
http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html?r=2.6.1

but I haven't found out the way to format date: "May 22 14".
I have attached my picture below.
Please help me to solve this issue.
Thanks for your help.






Get format time.PNG
Message has been deleted

Trey Duskin

unread,
May 25, 2014, 11:49:11 PM5/25/14
to robotframe...@googlegroups.com
I started work on an RF library to handle dates and times, since I also found the Builtin Library lacking with date handling.

https://github.com/tduskin/robotframework-datetimelibrary

I hope it helps... Please feel free to leave any feedback as an issue on Github.

Bryan Oakley

unread,
May 26, 2014, 10:17:34 PM5/26/14
to phuo...@gmail.com, robotframework-users
The built-in keyword won't give you time in the format you want. You can get the month, day and year, but the year will have the century (eg: 2014, rather than 14) Of course, you could write a keyword that calls the built-in keyword and then does some math, but there's an easier way. Use robot's ability to call inline python code. You can then call the strftime function which will give you complete control of the format. 

For example:

    *** Test Cases ***
    | Example of getting the time in a specific format
    | | ${time}= | Evaluate | time.strftime("%b %d %y", time.localtime()) | time
    | | log | time: ${time} | warn

The documentation for what %b, %d and %y mean can be found here: 




--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Phuoc Ha

unread,
May 26, 2014, 11:39:21 PM5/26/14
to robotframe...@googlegroups.com, phuo...@gmail.com
Dear Bryan,

Thanks so much for your help.
Let me try to use your extend library.
But I want to know how to convert month too. For example I would like convert month "05" to "May".
Do you have any ideas for this case ?
 
I found some ways to format date by python:

 def format_my_date(self, mydate)   
        date = mydate
        date = date.split('/')
        day = date[0] # date is, for example, [1,2,1998]. A list, because you have use split()
        monthDict = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun',
                    7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
        month = date[1] # Notice how I have changed this as well
                        # because the length of date is only 3
        month = monthDict[int(month)]
        year = date[2] # Also changed this, otherwise it would be an IndexError
        print day, month, "," , year


If you have free time, please add my skype: erit.ht02. I think I need more information from you.
Thanks you very much!

Vào 09:17:34 UTC+7 Thứ ba, ngày 27 tháng năm năm 2014, Bryan Oakley đã viết:
The built-in keyword won't give you time in the format you want. You can get the month, day and year, but the year will have the century (eg: 2014, rather than 14) Of course, you could write a keyword that calls the built-in keyword and then does some math, but there's an easier way. Use robot's ability to call inline python code. You can then call the strftime function which will give you complete control of the format. 

For example:

    *** Test Cases ***
    | Example of getting the time in a specific format
    | | ${time}= | Evaluate | time.strftime("%b %d %y", time.localtime()) | time
    | | log | time: ${time} | warn

The documentation for what %b, %d and %y mean can be found here: 


On Sun, May 25, 2014 at 10:17 PM, Phuoc Ha <phuo...@gmail.com> wrote:
Hello guys,

I want to get system date and convert date to format "May 22 14" in Robot FW.
I know some way to format date with keyword "Get time" from BuiltIn:
http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html?r=2.6.1

but I haven't found out the way to format date: "May 22 14".
I have attached my picture below.
Please help me to solve this issue.
Thanks for your help.






--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages