0

The extra T and Z are throwing me off:

strptime("2017-06-08T11:55:53.179000Z", "%Y-%m-%dT%H:%M:%SZ")

Returns NA

Zach
  • 29,791
  • 35
  • 142
  • 201
  • 1
    The answer by @Psidom fixes the seconds part (which gets rid of the NA response). However, I don't see any answer that deals with the Z. I think that part of the problem is that Z is not a proper ISO time zone. It is US Military time. – G5W Jun 08 '17 at 21:56

2 Answers2

4

Just use the anytime() function of the anytime package:

R> anytime("2017-06-08T11:55:53.179000Z")
[1] "2017-06-08 11:55:53.178 CDT"
R> 

The whole point of the anytime package is to parse such common formats without requiring a format. Dealing with the trailing Z comes for free via the Boost parser.

If you want it interpreted as UTC use the corresponding utctime() package:

R> utctime("2017-06-08T11:55:53.179000Z")
[1] "2017-06-08 06:55:53.178 CDT"
R> 

If you want it stored as UTC, set the timezone accordingly:

R> utctime("2017-06-08T11:55:53.179000Z", tz="UTC")
[1] "2017-06-08 11:55:53.178 UTC"
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • But it does not appear to have correctly dealt with the Z. Should be [Zulu = GMT](https://www.timeanddate.com/time/zones/z) – G5W Jun 08 '17 at 21:47
  • It all depends on what exactly you want. Default is localtime, see my third call in the answer. – Dirk Eddelbuettel Jun 08 '17 at 21:49
  • But this answer would force all times to UTC, not just ones ending in Z . Since the data specifies Z, it would be nice to be able to have the code figure that out. – G5W Jun 08 '17 at 21:52
  • See the documentation of the package which points that Boost itself only uses the timezone symbol for _output_ and does not support parsing it. – Dirk Eddelbuettel Jun 08 '17 at 21:57
  • 1
    Of course. `anytime` should have been my first thought! – Zach Jun 09 '17 at 12:41
1

According to ?strptime, you can parse it with %OS parameter by setting the digits.secs option:

Specific to R is %OSn, which for output gives the seconds truncated to 0 <= n <= 6 decimal places (and if %OS is not followed by a digit, it uses the setting of getOption("digits.secs"), or if that is unset, n = 0). Further, for strptime %OS will input seconds including fractional seconds. Note that %S does not read fractional parts on output.

options(digits.secs = 6)
strptime("2017-06-08T11:55:53.179000Z", "%Y-%m-%dT%H:%M:%OSZ")
# [1] "2017-06-08 11:55:53.179 EDT"
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • That gets by the NA, but it is also getting EDT, not Zulu. – G5W Jun 08 '17 at 21:33
  • @G5W I guess `Z` doesn't stand for timezone here, so you can parse it as any time zone by pass the timezone to the `tz` parameter? Something like `strptime("2017-06-08T11:55:53.179000Z", "%Y-%m-%dT%H:%M:%OSZ", tz = "UTC")`? – Psidom Jun 08 '17 at 21:39
  • I thought it meant [Zulu = GMT](https://www.timeanddate.com/time/zones/z) – G5W Jun 08 '17 at 21:44
  • @G5W Maybe we should wait for OP to clairfy this, I doubt Z stands for `Zulu` here. You can see [here](http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm) and [here](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html). It doesn't seem like it's a standard way to use `Z` as the short cut for `GMT`. – Psidom Jun 08 '17 at 21:58
  • 2
    It is standard in the US Military See [here](https://www.timeanddate.com/time/zones/z) But I concur about getting a response from the OP. – G5W Jun 08 '17 at 22:00
  • I actually think the `Z` stands for UTC, but am not 100% sure: https://en.wikipedia.org/wiki/ISO_8601 – Zach Jun 09 '17 at 13:31
  • See also: https://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist – Zach Jun 09 '17 at 13:31
  • AHA! Z stands for `Zulu` time, or UTC: "UTC time is also known as Zulu time, since Zulu is the NATO phonetic alphabet word for Z". This is a little confusing, lol. – Zach Jun 09 '17 at 13:32