0

Why does the ymd_hms function from R's lubridate package return "2018-01-09 15:43:44.843 UTC" for ymd_hms('2018-01-09T15:43:44.844Z')?

I naively would have expected "2018-01-09 15:43:44.844 UTC".

ymd_hms('2018-01-09T15:43:44.822Z') returns "2018-01-09 15:43:44.822 UTC".

Since this is GMT/UTC, I don't believe daylight savings would be a factor, and different values for the truncated = option don't seem to make a difference.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50

2 Answers2

0

From the ymd_hms documention:

NOTE: The ymd family of functions are based on strptime()

As described here, the issue seems to lie in how strptime truncates rather than rounds fractions of a second. Play around with options(digits.secs = n) to see how it handles various numbers of decimal places.

icj
  • 719
  • 6
  • 12
0

I would rather use:

format(strptime("2018-01-09T15:43:44.844Z", "%Y-%m-%dT%H:%M:%OS", tz = "EST"), format="%Y-%m-%d %H:%M:%OS3 %Z", tz = "EST") 

[1] "2018-01-09 15:43:44.844 EST"

Note that the milliseconds are passed in the format using the argument %OS

Edgar Santos
  • 3,426
  • 2
  • 17
  • 29