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

Adding difftime to date truncates the difftime to full dates #192

Merged
merged 4 commits into from Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -5,3 +5,5 @@
* Character indexing is now only supported for named objects, an error is raised for unnamed objects (#171).

* `vec_c()` and `vec_rbind()` now handle data.frame columns properly (@yutannihilation, #182).

* `vec_arith()` is consistent with base R when combining `difftime` and `date`, with a warning if casts are lossy (#192).
5 changes: 5 additions & 0 deletions R/cast.R
Expand Up @@ -354,3 +354,8 @@ report_lossy_cast <- function(x, y, lossy, details = NULL) {
}
}

lossy_floor <- function(x, to) {
x_floor <- floor(x)
report_lossy_cast(x, to, x != x_floor)
x_floor
}
7 changes: 3 additions & 4 deletions R/type-date-time.R
Expand Up @@ -427,12 +427,11 @@ vec_arith.difftime.POSIXct <- function(op, x, y) {
#' @method vec_arith.Date difftime
#' @export
vec_arith.Date.difftime <- function(op, x, y) {
# Need to warn if non-integer number of days
y <- vec_cast(y, new_duration(units = "days"))

switch(op,
`+` = vec_restore(vec_arith_base(op, x, y), x),
`-` = vec_restore(vec_arith_base(op, x, y), x),
`+` = ,
`-` = vec_restore(vec_arith_base(op, x, lossy_floor(y, x)), x),
stop_incompatible_op(op, x, y)
)
}
Expand All @@ -442,7 +441,7 @@ vec_arith.difftime.Date <- function(op, x, y) {
x <- vec_cast(x, new_duration(units = "days"))

switch(op,
`+` = vec_restore(vec_arith_base(op, x, y), y),
`+` = vec_restore(vec_arith_base(op, lossy_floor(x, y), y), y),
stop_incompatible_op(op, x, y)
)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-type-date-time.R
Expand Up @@ -207,14 +207,22 @@ test_that("date-time vs difftime", {
d <- as.Date("2018-01-01")
dt <- as.POSIXct(d)
t <- as.difftime(1, units = "days")
th <- as.difftime(c(1, 24), units = "hours")

expect_equal(vec_arith("+", dt, t), dt + t)
expect_equal(vec_arith("+", d, t), d + t)
expect_equal(vec_arith("+", dt, th), dt + th)
expect_warning(expect_equal(vec_arith("+", d, th), d + th), class = "warning_lossy_cast")
expect_equal(vec_arith("-", dt, t), dt - t)
expect_equal(vec_arith("-", d, t), d - t)
expect_equal(vec_arith("-", dt, th), dt - th)
expect_warning(expect_equal(vec_arith("-", d, th), d - th), class = "warning_lossy_cast")

expect_equal(vec_arith("+", t, dt), dt + t)
expect_equal(vec_arith("+", t, d), d + t)
expect_equal(vec_arith("+", th, dt), dt + th)
expect_warning(expect_equal(vec_arith("+", th, d), d + th), class = "warning_lossy_cast")

expect_error(vec_arith("-", t, dt), class = "vctrs_error_incompatible_op")
expect_error(vec_arith("-", t, d), class = "vctrs_error_incompatible_op")
})
Expand Down