Skip to content

rounding invalidated by daylight savings time #240

Description

@couthcommander

From the following test, see that "2014-03-09 01:35:00" rounded by hour becomes
NA because "2014-03-09 02:00:00" is an invalid time in Chicago's time zone. I would prefer the answer "2014-03-09 03:00:00". A solution similar to round_hours found below would be great. Thanks for considering.

test_that("round_date works around DST", {
tz <- "America/Chicago"
x <- ymd_hms(c("2014-03-09 00:00:00", "2014-03-09 00:29:59",
"2014-03-09 00:30:00", "2014-03-09 00:59:59", "2014-03-09 01:35:00",
"2014-03-09 03:15:00", "2014-11-02 00:30:00", "2014-11-02 00:59:59",
"2014-11-02 01:35:00", "2014-11-02 02:15:00", "2014-11-02 02:45:00"
), tz = tz)
y <- as.POSIXct(c("2014-03-09 00:00:00", "2014-03-09 00:00:00",
"2014-03-09 01:00:00", "2014-03-09 01:00:00", "2014-03-09 03:00:00",
"2014-03-09 03:00:00", "2014-11-02 01:00:00", "2014-11-02 01:00:00",
"2014-11-02 02:00:00", "2014-11-02 02:00:00", "2014-11-02 03:00:00"), tz = tz)
expect_equal(round_date(x, unit = 'hour'), y)
})

round_hours <- function(x) {
toAdd <- ifelse(minute(x) >= 30, dhours(1), dhours(0))
floor_date(x, unit = 'hour') + toAdd
}

test_that("round_hours works around DST", {
tz <- "America/Chicago"
x <- ymd_hms(c("2014-03-09 00:00:00", "2014-03-09 00:29:59",
"2014-03-09 00:30:00", "2014-03-09 00:59:59", "2014-03-09 01:35:00",
"2014-03-09 03:15:00", "2014-11-02 00:30:00", "2014-11-02 00:59:59",
"2014-11-02 01:35:00", "2014-11-02 02:15:00", "2014-11-02 02:45:00"
), tz = tz)
y <- as.POSIXct(c("2014-03-09 00:00:00", "2014-03-09 00:00:00",
"2014-03-09 01:00:00", "2014-03-09 01:00:00", "2014-03-09 03:00:00",
"2014-03-09 03:00:00", "2014-11-02 01:00:00", "2014-11-02 01:00:00",
"2014-11-02 02:00:00", "2014-11-02 02:00:00", "2014-11-02 03:00:00"), tz = tz)
expect_equal(round_hours(x), y)
})

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions