guzu92 guzu92 - 2 months ago 15
R Question

R: date/time "YYYY-MM-DDThh:mm:ss.sTZD" import

How can I import the folowing date/time format example in R ? I'm willing to keep all information within this format.

2016-09-12T09:47:00.000+0200


where:

YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)


I've tried
strptime
without success since I cannot find how to match
s
and
TZD
, example:

> strptime("2016-09-12T09:47:00.000+0200", format = '%Y-%m-%dT%H:%M:%S.000%z')
[1] "2016-09-12 09:47:00

Answer

To match the decimal fraction of a second (from the docs ?strptime in Examples) use:

format = '%Y-%m-%dT%H:%M:%OS%z'

Then, to see the 3-digits:

op <- options(digits.secs = 3)
strptime("2016-09-12T09:47:00.123+0200", format = '%Y-%m-%dT%H:%M:%OS%z')
##[1] "2016-09-12 03:47:00.123"

To go back to not seeing the 3-digits:

options(op)

I believe this does parse the time zone. I'm on the east coast of the United States, and it is EDT (-0400). Therefore, I'm 6 hours behind (+0200) so that 09:47:00.123+0200 becomes 03:47:00.123 EDT.

Comments