lubridate parse_date_time() sometimes off by 1 usec

Viewed 31

parse_date_time() is giving me some results that differ in their usec location by 1 unit, for example:

options(digits.secs=10)
 
> parse_date_time("2022-06-14 10:08:00.000242", "Y-m-d H:M:OS", tz="" )
[1] "2022-06-14 10:08:00.000241 CEST"
> parse_date_time("2022-06-14 10:08:00.000435", "Y-m-d H:M:OS", tz="" )
[1] "2022-06-14 10:08:00.000434 CEST"
> parse_date_time("2022-06-14 10:08:00.021961", "Y-m-d H:M:OS", tz="" )
[1] "2022-06-14 10:08:00.02196 CEST"

I'm guessing this is round off error somewhere? Is there anyway to tell parse_date_time() to use greater precision when calculating the fractional seconds value?

1 Answers

Using strptime might be superior.

options(digits.secs=6)

strptime(x, "%Y-%m-%d %H:%M:%OS", tz="")
# [1] "2022-06-14 10:08:00.000242 CEST" "2022-06-14 10:08:00.000435 CEST"
# [3] "2022-06-14 10:08:00.021961 CEST"

Data:

x <- c("2022-06-14 10:08:00.000242", "2022-06-14 10:08:00.000435", "2022-06-14 10:08:00.021961")
Related