Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 439
guzu92 Avatar asked Aug 30 '25 17:08

guzu92


2 Answers

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 offset from UTC (i.e., the +0200). 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.

like image 200
aichao Avatar answered Sep 02 '25 06:09

aichao


You could use the (pretty new) anytime package which does this without formats:

R> anytime("2016-09-12T09:47:00.000+0200")
[1] "2016-09-12 09:47:00 CDT"
R> 

I may try to extend it to also recognize the trailing TZ offset as the underlying Boost date_time code supports it. However, I have so far followed R and taken to interpret the time as local time for which it also (automatically) finds the local timezone.

anytime also supports fractional seconds automatically (but you need to ensure you display them):

R> anytime("2016-09-12T09:47:00.123456+0200")
[1] "2016-09-12 09:47:00.123456 CDT"
R> 

I tend to work with microsecond data so I tend to have six digits on all the anyway as shown here.

like image 28
Dirk Eddelbuettel Avatar answered Sep 02 '25 06:09

Dirk Eddelbuettel