lubridate icon indicating copy to clipboard operation
lubridate copied to clipboard

leap second for 1969?

Open mcfrank opened this issue 4 years ago • 2 comments

Hi lubridate folks, I found something odd when rounding a time series around lubridate::origin: in particular, it seems like rounding introduces some kind of a leap second between 1969 and 1970 (see plot).

Take a look at this example:

t <- seq(-10000,10000,33)/1000
t_date <- lubridate::origin + t
t_date_rounded <- lubridate::round_date(t_date, ".033s")
plot(t_date,t_date_rounded)

image

It doesn't seem like the ideal behavior of this rounding should be to introduce the leap second? Or maybe I'm just not understanding this functionality? Thanks for any help.

mcfrank avatar Aug 22 '19 18:08 mcfrank

This is another bug fixed by the timechange package:

library(lubridate, warn.conflicts = FALSE)

t <- seq(-10000, 10000, 33) / 1000
t_date <- origin + t
plot(t_date, round_date(t_date, ".033s"))


plot(t_date, timechange::time_round(t_date, ".033 asec"))

Created on 2019-11-19 by the reprex package (v0.3.0)

hadley avatar Nov 19 '19 23:11 hadley

This also works correctly in clock. You have to use a clock type, which truly supports sub second precision date-times.

library(clock)
library(magrittr)

t <- seq(-10000,10000,33)/1000

# Approximately split into seconds/milliseconds
secs <- floor(t)
millisecs <- round((t - secs) * 1000)

x <- year_month_day(1970, 1, 1) %>%
  as_naive_time() %>%
  add_seconds(secs) %>%
  add_milliseconds(millisecs)

x_floor <- time_point_floor(x, "millisecond", n = 33)
head(x_floor)
#> <time_point<naive><millisecond>[6]>
#> [1] "1969-12-31 23:59:49.968" "1969-12-31 23:59:50.001"
#> [3] "1969-12-31 23:59:50.034" "1969-12-31 23:59:50.067"
#> [5] "1969-12-31 23:59:50.100" "1969-12-31 23:59:50.133"

y <- x_floor %>%
  as_duration() %>%
  as.double()

plot(t, y)

Created on 2021-05-25 by the reprex package (v1.0.0)

DavisVaughan avatar May 25 '21 14:05 DavisVaughan

It's working correctly in devel

vspinu avatar Nov 05 '22 12:11 vspinu