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)
})
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)
})