-
Notifications
You must be signed in to change notification settings - Fork 25
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
hms class dropped when doing arithmetic on hms objects #18
Comments
The following works for the test case, but generates a warning and incorrect results when doing arithmetic with Date objects. Ops.hms <- function(e1, e2) {
res <- NextMethod("Ops")
mostattributes(res) <- attributes(e1)
res
}
late_night <- hms::hms(seconds = 22 * 3600 + 20 * 60)
str(late_night + 5)
#> Classes 'hms', 'difftime' atomic [1:1] 80405
#> ..- attr(*, "units")= chr "secs"
as.Date("2016-03-31") + hms(minutes = 1)
#> Warning: Incompatible methods ("+.Date", "Ops.hms") for "+"
#> [1] "2016-05-30" |
It's an annoyance, but I'm not sure it can be fixed easily. If I define |
I'm giving up. R has some special handling for the difftime class in its internals, it seems impossible to implement this without breaking cases where we add difftime or hms to a date or date-time object: |
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary. |
Finally found a way that seems to work: #' @export
Ops.hms <- function(e1, e2) {
out <- ...
if (inherits(out, "difftime")) {
out <- vec_cast(out, new_hms())
}
out
}
# Registered on .onLoad() to avoid warning when loading the package
`+.Date` <- Ops.hms If I override
With this I think we can finally get full arith and generic support. Related: #97. |
Prior to implementing, need to add tests for all combinations of adding and subtracting dates, datetimes and difftimes. These must work unchanged after the implementation. |
I think you just need to define
Ops.hms
that callsOps.difftime
and preserves the hms class.The text was updated successfully, but these errors were encountered: