-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add generic operations for hms
#120
Open
AshesITR
wants to merge
6
commits into
tidyverse:main
Choose a base branch
from
AshesITR:feature/hms-ops
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f4a5ae
add Ops.hms and tests for {+-*/} {integer,numeric} and {+-} Date
AshesITR 32c65fe
add {+-}.POSIXt and tests for {+-} POSIXct
AshesITR 7cbe1f8
only export for R >= 4.1
AshesITR 8759889
revert removal of expect_difftime_equal() test helper for R < 4.1
AshesITR 1f38b73
Merge branch 'main' into feature/hms-ops
krlmlr f030dd0
Merge branch 'main' into feature/hms-ops
krlmlr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
CRAN-RELEASE | ||
docs | ||
.vscode | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' @rawNamespace | ||
#' if (getRversion() >= "4.1") { | ||
#' S3method("+",Date) | ||
#' S3method("+",POSIXt) | ||
#' S3method("-",Date) | ||
#' S3method("-",POSIXt) | ||
#' S3method(Ops,hms) | ||
#' } | ||
Ops.hms <- function(e1, e2) { | ||
# This logic is hard-coded in R for difftime | ||
# cf. https://github.com/wch/r-source/blob/a46559e8f728317da979be60e401899ae60086b2/src/main/eval.c#L3406-L3419 | ||
if (.Generic == "+" && (inherits(e1, "Date") || inherits(e2, "Date"))) { | ||
return(base::`+.Date`(e1, e2)) | ||
} else if (.Generic == "-" && inherits(e1, "Date")) { | ||
return(base::`-.Date`(e1, e2)) | ||
} else if (.Generic == "+" && (inherits(e1, "POSIXt") || inherits(e2, "POSIXt"))) { | ||
return(base::`+.POSIXt`(e1, e2)) | ||
} else if (.Generic == "-" && inherits(e1, "POSIXt")) { | ||
return(base::`-.POSIXt`(e1, e2)) | ||
} | ||
|
||
# delegate to Ops.difftime | ||
res <- NextMethod(.Generic) | ||
if (inherits(res, "difftime")) { | ||
as_hms(res) | ||
} else { | ||
res | ||
} | ||
} | ||
|
||
`+.Date` <- Ops.hms | ||
`-.Date` <- Ops.hms | ||
`+.POSIXt` <- Ops.hms | ||
`-.POSIXt` <- Ops.hms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,19 @@ test_that("arithmetics work", { | |
expect_equal(hms(days = 1) + as.Date("2016-03-31"), as.Date("2016-04-01")) | ||
expect_equal(empty_tz(hms(hours = 1) + as.POSIXct("2016-03-31")), as.POSIXct("2016-03-31 01:00:00")) | ||
|
||
expect_difftime_equal(hms(1) + hms(2), hms(3)) | ||
expect_difftime_equal(hms(1) - hms(2), hms(-1)) | ||
expect_difftime_equal(2 * hms(1), hms(2)) | ||
expect_difftime_equal(hms(hours = 1) / 2, hms(minutes = 30)) | ||
expect_difftime_equal(-hms(1), hms(-1)) | ||
if (getRversion() < "4.1") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can |
||
expect_difftime_equal(hms(1) + hms(2), hms(3)) | ||
expect_difftime_equal(hms(1) - hms(2), hms(-1)) | ||
expect_difftime_equal(2 * hms(1), hms(2)) | ||
expect_difftime_equal(hms(hours = 1) / 2, hms(minutes = 30)) | ||
expect_difftime_equal(-hms(1), hms(-1)) | ||
} else { | ||
expect_hms_equal(hms(1) + hms(2), hms(3)) | ||
expect_hms_equal(hms(1) - hms(2), hms(-1)) | ||
expect_hms_equal(2 * hms(1), hms(2)) | ||
expect_hms_equal(hms(hours = 1) / 2, hms(minutes = 30)) | ||
expect_hms_equal(-hms(1), hms(-1)) | ||
} | ||
}) | ||
|
||
test_that("component extraction work", { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
test_that("generic operations work as intended", { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test chunk is too large, can you please split by topic? |
||
skip_if(getRversion() < "4.1") | ||
# Test that all binary operations involving hms and one of Date, POSIXct, POSIXlt, difftime, hms, numeric, integer | ||
# behave the same as the operation would with a difftime of the same length, except for difftime results, wich should | ||
# be hms of the same value instead. | ||
|
||
hms_test_vec <- new_hms(c(0.0, NA_real_, 10.0)) | ||
|
||
# hms {+-*/} integer | ||
expect_equal(hms_test_vec + 1L, new_hms(c(1.0, NA_real_, 11.0))) | ||
expect_equal(hms_test_vec + (1L:3L), new_hms(c(1.0, NA_real_, 13.0))) | ||
expect_equal(1L + hms_test_vec, new_hms(c(1.0, NA_real_, 11.0))) | ||
expect_equal((1L:3L) + hms_test_vec, new_hms(c(1.0, NA_real_, 13.0))) | ||
|
||
expect_equal(hms_test_vec - 1L, new_hms(c(-1.0, NA_real_, 9.0))) | ||
expect_equal(hms_test_vec - (1L:3L), new_hms(c(-1.0, NA_real_, 7.0))) | ||
expect_equal(1L - hms_test_vec, new_hms(c(1.0, NA_real_, -9.0))) | ||
expect_equal((1L:3L) - hms_test_vec, new_hms(c(1.0, NA_real_, -7.0))) | ||
|
||
expect_equal(hms_test_vec * 2L, new_hms(c(0.0, NA_real_, 20.0))) | ||
expect_equal(hms_test_vec * (1L:3L), new_hms(c(0.0, NA_real_, 30.0))) | ||
expect_equal(2L * hms_test_vec, new_hms(c(0.0, NA_real_, 20.0))) | ||
expect_equal((1L:3L) * hms_test_vec, new_hms(c(0.0, NA_real_, 30.0))) | ||
|
||
expect_equal(hms_test_vec / 2L, new_hms(c(0.0, NA_real_, 5.0))) | ||
expect_equal(hms_test_vec / c(1L, 2L, 4L), new_hms(c(0.0, NA_real_, 2.5))) | ||
expect_error(1L / hms_test_vec, "second argument of / cannot be a \"difftime\" object") | ||
|
||
# hms {+-*/} numeric | ||
expect_equal(hms_test_vec + 1.0, new_hms(c(1.0, NA_real_, 11.0))) | ||
expect_equal(hms_test_vec + (1.0:3.0), new_hms(c(1.0, NA_real_, 13.0))) | ||
expect_equal(1.0 + hms_test_vec, new_hms(c(1.0, NA_real_, 11.0))) | ||
expect_equal((1.0:3.0) + hms_test_vec, new_hms(c(1.0, NA_real_, 13.0))) | ||
|
||
expect_equal(hms_test_vec - 1.0, new_hms(c(-1.0, NA_real_, 9.0))) | ||
expect_equal(hms_test_vec - (1.0:3.0), new_hms(c(-1.0, NA_real_, 7.0))) | ||
expect_equal(1.0 - hms_test_vec, new_hms(c(1.0, NA_real_, -9.0))) | ||
expect_equal((1.0:3.0) - hms_test_vec, new_hms(c(1.0, NA_real_, -7.0))) | ||
|
||
expect_equal(hms_test_vec * 2.0, new_hms(c(0.0, NA_real_, 20.0))) | ||
expect_equal(hms_test_vec * (1.0:3.0), new_hms(c(0.0, NA_real_, 30.0))) | ||
expect_equal(2.0 * hms_test_vec, new_hms(c(0.0, NA_real_, 20.0))) | ||
expect_equal((1.0:3.0) * hms_test_vec, new_hms(c(0.0, NA_real_, 30.0))) | ||
|
||
expect_equal(hms_test_vec / 2.0, new_hms(c(0.0, NA_real_, 5.0))) | ||
expect_equal(hms_test_vec / c(1.0, 2.0, 4.0), new_hms(c(0.0, NA_real_, 2.5))) | ||
expect_error(1.0 / hms_test_vec, "second argument of / cannot be a \"difftime\" object") | ||
|
||
# hms {+-} Date | ||
# 86400.0 secs = 24 hours | ||
hms_test_vec <- new_hms(86400.0 * c(1.0, NA_real_, 10.0)) | ||
difftime_test_vec <- as.difftime(86400.0 * c(1.0, NA_real_, 10.0), units = "secs") | ||
date_vec <- as.Date(c("1970-01-01", NA_character_, "2023-08-21")) | ||
|
||
expect_equal(hms_test_vec + date_vec, difftime_test_vec + date_vec) | ||
expect_equal(date_vec + hms_test_vec, date_vec + difftime_test_vec) | ||
|
||
# Can't reproduce this warning because we forced compatibility | ||
hms_result <- hms_test_vec - date_vec | ||
expect_warning( | ||
difftime_result <- as_hms(difftime_test_vec - date_vec), | ||
regexp = "Incompatible methods \\(\"Ops.difftime\", \"-.Date\") for \"-\"" | ||
) | ||
expect_equal(hms_result, difftime_result, info = "hms - Date not equal to difftime - Date") | ||
|
||
expect_equal(date_vec - hms_test_vec, date_vec - difftime_test_vec) | ||
|
||
# hms {+-} POSIXt | ||
posixct_vec <- as.POSIXct( | ||
c("2023-03-26 03:00:00", "2023-10-29 02:00:00", NA_character_, "1970-01-01 00:00:00"), | ||
tz = "Europe/Berlin" | ||
) | ||
|
||
posix_difftime_result <- posixct_vec + as.difftime(1.0, units = "hours") | ||
expect_equal( | ||
format(posix_difftime_result, usetz = TRUE), | ||
c("2023-03-26 04:00:00 CEST", "2023-10-29 02:00:00 CET", NA_character_, "1970-01-01 01:00:00 CET") | ||
) | ||
expect_equal(posixct_vec + hms(hours = 1L), posix_difftime_result) | ||
expect_equal(hms(hours = 1L) + posixct_vec, posix_difftime_result) | ||
expect_equal(as.difftime(1.0, units = "hours") + posixct_vec, posix_difftime_result) | ||
|
||
posix_difftime_result <- posixct_vec - as.difftime(1.0, units = "hours") | ||
expect_equal( | ||
format(posix_difftime_result, usetz = TRUE), | ||
c("2023-03-26 01:00:00 CET", "2023-10-29 01:00:00 CEST", NA, "1969-12-31 23:00:00 CET"), | ||
info = "POSIXct - difftime" | ||
) | ||
expect_equal(posixct_vec - hms(hours = 1L), posix_difftime_result) | ||
|
||
hms_result <- hms(hours = 1L) - posixct_vec | ||
expect_warning( | ||
difftime_result <- as.difftime(3600.0, units = "secs") - posixct_vec, | ||
regexp = "Incompatible methods \\(\"Ops.difftime\", \"-.POSIXt\") for \"-\"" | ||
) | ||
# FIXME this currently returns a different result than difftime - POSIXt. | ||
# hms - POSIXt produces a hms | ||
# difftime - POSIXt produces a POSIXt | ||
expect_equal( | ||
as.numeric(hms_result), as.numeric(difftime_result), | ||
info = "hms - POSIXt not equal to difftime - POSIXt" | ||
) | ||
expect_hms_equal(hms_result, new_hms(c(-1679788800.0, -1698534000.0, NA_real_, 7200.0))) | ||
expect_equal( | ||
difftime_result, | ||
as.POSIXct(c(-1679788800.0, -1698534000.0, NA_real_, 7200.0), origin = "1970-01-01", tz = "Europe/Berlin") | ||
) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this wrap as hms?