Skip to content

Commit

Permalink
Merge pull request #480 from shrektan/patch-1
Browse files Browse the repository at this point in the history
Fix ceiling_date return type when unit = "week"
  • Loading branch information
vspinu committed Sep 28, 2016
2 parents d561bf1 + 14774c8 commit 35cb3ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -36,6 +36,7 @@ Version 1.6.0

### BUG FIXES

* [#479](https://github.com/hadley/lubridate/issues/479) Fix the inconsistent behavior in `ceiling_date` when `unit = "week"`
* [#463](https://github.com/hadley/lubridate/issues/463) Fix NA subscripting error in %m+% when rollback is involved.
* [#462](https://github.com/hadley/lubridate/issues/462) Non-numeric or non-character arguments are disallowed as arguments to `period` and `duration` constructors.
* [#458](https://github.com/hadley/lubridate/issues/458) When year is missing in parsing, return consistently year 0.
Expand Down
2 changes: 1 addition & 1 deletion R/round.r
Expand Up @@ -137,7 +137,7 @@ round_date <- function(x, unit = "second") {
}

reclass_date_maybe <- function(new, orig, unit){
if(is.Date(orig) && !unit %in% c("day", "month", "year")) as.POSIXct(new)
if(is.Date(orig) && !unit %in% c("day", "week", "month", "year")) as.POSIXct(new)
else reclass_date(new, orig)
}

Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-round.R
Expand Up @@ -260,6 +260,32 @@ test_that("Ceiling for partials (Date) rounds up on boundary", {
expect_identical(ceiling_date(as.Date("2012-09-01"), '2 days'), ymd("2012-09-03"))
})

test_that("Ceiling for Date returns date when unit level is higher than day", {
expect_true(is.Date(ceiling_date(ymd("20160927"), "year")))
expect_true(is.Date(ceiling_date(ymd("20160927"), "halfyear")))
expect_true(is.Date(ceiling_date(ymd("20160927"), "quarter")))
expect_true(is.Date(ceiling_date(ymd("20160927"), "bimonth")))
expect_true(is.Date(ceiling_date(ymd("20160927"), "month")))
expect_true(is.Date(ceiling_date(ymd("20160927"), "week")))
expect_true(is.Date(ceiling_date(ymd("20160927"), "day")))
expect_true(is.POSIXct(ceiling_date(ymd("20160927"), "hour")))
expect_true(is.POSIXct(ceiling_date(ymd("20160927"), "minute")))
expect_true(is.POSIXct(ceiling_date(ymd("20160927"), "second")))
})

test_that("Ceiling for POSIXct always returns POSIXct", {
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "year")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "halfyear")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "quarter")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "bimonth")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "month")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "week")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "day")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "hour")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "minute")))
expect_true(is.POSIXct(ceiling_date(ymd_hms("20160927 00:00:00"), "second")))
})

test_that("ceiling_date does not round up dates that are already on a boundary",{
expect_equal(ceiling_date(ymd_hms("2012-09-01 00:00:00"), 'month'), as.POSIXct("2012-09-01", tz = "UTC"))
expect_equal(ceiling_date(ymd_hms("2012-01-01 00:00:00"), 'year'), as.POSIXct("2012-01-01", tz = "UTC"))
Expand Down

0 comments on commit 35cb3ea

Please sign in to comment.