helloB
helloB

Reputation: 3582

How to retrieve POSIX time stamp from this string?

I have strings like the following:

"{u'iso': u'2015-05-12T12:43:17.512Z', u'__type': u'Date'}"

I'd like to convert them to times. I tried the following:

time_format_string = "{u'iso': u'%Y-%m-%dT%H:%M:%S', u'__type': u'Date'}"
strptime(test_string, time_format_string)

This gives back an NA with no error message. What am I doing wrong?

Upvotes: 2

Views: 102

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

You omitted the "Z" after the seconds in your format string. However, the result is still NA if you add the "Z", which looks like it might be because you have fractional seconds and you use "%S" instead of "%OS".

options(digits.secs=3)  # print fractional seconds
test_string = "{u'iso': u'2015-05-12T12:43:17.512Z', u'__type': u'Date'}"
time_format_string = "{u'iso': u'%Y-%m-%dT%H:%M:%SZ', u'__type': u'Date'}"
strptime(test_string, time_format_string)
#[1] NA
time_format_string = "{u'iso': u'%Y-%m-%dT%H:%M:%OSZ', u'__type': u'Date'}"
strptime(test_string, time_format_string)
#[1] "2015-05-12 12:43:17.512 CDT"

Regardless, this is easily avoidable because you do not need to specify anything in your format string after the last time component you want (see Details in ?strptime: "Each input string is processed as far as necessary for the format specified: any trailing characters are ignored").

time_format_string = "{u'iso': u'%Y-%m-%dT%H:%M:%S"
strptime(test_string, time_format_string)
#[1] "2015-05-12 12:43:17 CDT"

If you want to retain the fractional seconds, you should use "%OS" instead of "%S". You should also explicitly set the timezone to avoid the times being parsed in your machine's local timezone. I set the timezone to "UTC" because that's most likely what the "Z" after the seconds represents.

time_format_string = "{u'iso': u'%Y-%m-%dT%H:%M:%OS"
strptime(test_string, time_format_string, tz="UTC")
#[1] "2015-05-12 12:43:17.512 UTC"

Upvotes: 2

Related Questions