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

rounding invalidated by daylight savings time #240

Closed
couthcommander opened this issue May 12, 2014 · 2 comments
Closed

rounding invalidated by daylight savings time #240

couthcommander opened this issue May 12, 2014 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@couthcommander
Copy link

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

@vspinu vspinu added the bug an unexpected problem or unintended behavior label Dec 14, 2014
@vspinu vspinu closed this as completed in 071078c Apr 27, 2015
@vspinu
Copy link
Member

vspinu commented Apr 27, 2015

I would prefer the answer "2014-03-09 03:00:00".

You have it:

> times <- ymd_hms("2013-03-31 01:00:00 CET", "2013-03-31 01:15:00 CEST",
+                  "2013-03-31 01:30:00 CEST", "2013-03-31 01:45:00 CEST",
+                  "2013-03-31 03:00:00 CEST", "2013-03-31 03:15:00 CEST",
+                  tz = "Europe/Amsterdam")
> round_date(times, unit = "hour")
[1] "2013-03-31 01:00:00 CET"  "2013-03-31 01:00:00 CET"  "2013-03-31 01:00:00 CET" 
[4] "2013-03-31 03:00:00 CEST" "2013-03-31 03:00:00 CEST" "2013-03-31 03:00:00 CEST"
> ceiling_date(times, unit = "hour")
[1] "2013-03-31 01:00:00 CET"  "2013-03-31 03:00:00 CEST" "2013-03-31 03:00:00 CEST"
[4] "2013-03-31 03:00:00 CEST" "2013-03-31 03:00:00 CEST" "2013-03-31 04:00:00 CEST"
> 

The only difference is that rounding of half hour is floored.

@vspinu
Copy link
Member

vspinu commented Apr 27, 2015

The only difference is that rounding of half hour is floored.

Not anymore. Changed that to comply with base::round.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants