Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In split_period, why not use lubridate::as.period instead of lubridate::seconds_to_period? #288

Open
jerlev opened this issue Oct 10, 2022 · 0 comments

Comments

@jerlev
Copy link

jerlev commented Oct 10, 2022

I'm trying to understand what appears to be a limitation of tsibble. The function tsibble:::split_period is used for example by tsibble:::interval_pull.POSIXt, to handle objects of base class difftime:

# Excerpts from tsibble/R/interval.R

split_period <- function(x) {
  output <- seconds_to_period(x)
  list(
    year = output$year, month = output$month, day = output$day,
    hour = output$hour, minute = output$minute, second = output$second
  )
}

#' @export
# Assume date is regularly spaced
interval_pull.POSIXt <- function(x) {
  dttm <- as.double(x)
  nhms <- gcd_interval(dttm)
  period <- split_period(nhms)
  new_interval(
    hour = period$hour,
    minute = period$minute,
    second = period$second %/% 1,
    millisecond = (period$second %% 1 * 1e3) %/% 1,
    microsecond = (period$second %% 1 * 1e3) %% 1 * 1e3
  )
}

In order to transform the base difftime object as a lubridate period object, split_period calls lubridate::seconds_to_period. This assumes that the difftime object will return a number in seconds once cast as a double. However that assumption seems to fail for any difftime object equal to, or longer than one minute. Is that right? If so, why not use lubridate::as.period instead of lubridate::seconds_to_period?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant