Convert current date to ISO 8601 <date>T<time>

772 views
Skip to first unread message

Geert Peeters

unread,
Aug 19, 2015, 11:25:10 AM8/19/15
to robotframework-users
Hi,

I'm struggling with converting "now" into the format YYYY-MM-DDTHH:MM:SS : 2008-09-29T03:49:45

by using the DateTime library I'm able to get the current date

${now}=    Get Current Date    time_zone=UTC    result_format=datetime    exclude_millis=true
${convertDate}=    Convert Date    ${now}    date_format=%m.%d.%YT%H:%M
log    ${convertDate}

But date_format doesn't seem to accept the "T"

How can I get this done?
The time stamp needs to end up in an XML for a REST post, which is standardized practice for timestamps in web services.

Kind regards,

Geert

Geert Peeters

unread,
Aug 20, 2015, 6:47:22 AM8/20/15
to robotframework-users
Ok I did find the solution : 

${now}=    Get Current Date    time_zone=UTC
${convertDate}=    Convert Date    ${now}    result_format=%Y-%m-%dT%H:%M:%S    exclude_millis=true
log    ${convertDate}
[Return]    ${convertDate}

This results in : 2015-08-20T10:30:21

But my issues is somewhat more complex

What I actually need is the first coming quarter derived from UTC current date.

That would mean : 2015-08-20T10:45:00

This would end up as a start time stamp for my test.
I also need to have an end time stamp 30 minutes later.
That I can do with the keyword Add Time To Date, so no issue there.

However I have no idea how I can compute the first next quarter time stamp of "now"

Does anybody has a suggestion?

Geert Peeters

unread,
Aug 21, 2015, 4:04:28 PM8/21/15
to robotframework-users
I thought I might share the solution I found.

Apparently the solution to my question was not an easy one, regarding the number of responses...

    def get_next_quarter_timestamp(self):
        now = datetime.utcnow()
        later = self._next_quarter(now)
        lateriso = later.isoformat()
        return lateriso


    def _next_quarter(self,dt):
        nsecs = dt.minute*60+dt.second+dt.microsecond*1e-6
        delta = (nsecs//900)*900+900-nsecs
        return dt + timedelta(seconds=delta)

${start}=    Get Next Quarter Timestamp
${end}=    Add Time To Date    ${start}    00:30:00:000    result_format=%Y-%m-%dT%H:%M:%S    exclude_millis=true

20150821 22:04:04.775 :  INFO : ${start} = 2015-08-21T20:15:00
20150821 22:04:04.779 :  INFO : ${end} = 2015-08-21T20:45:00

Reply all
Reply to author
Forward
0 new messages