-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform
clock_roll()
to an internal S3 generic
- Loading branch information
1 parent
367cef0
commit ae4f3a9
Showing
5 changed files
with
127 additions
and
67 deletions.
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
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,59 @@ | ||
clock_roll <- function(x) { | ||
UseMethod("clock_roll") | ||
} | ||
|
||
#' @export | ||
clock_roll.Duration <- function(x) { | ||
if (all(as.numeric(x) > 0 & as.numeric(x) < 86400, na.rm = TRUE)) { | ||
x | ||
} else { | ||
x %>% lubridate::as_datetime() %>% | ||
flat_posixt() %>% | ||
hms::as_hms() %>% | ||
lubridate::as.duration() | ||
} | ||
} | ||
|
||
#' @export | ||
clock_roll.Period <- function(x) { | ||
if (all(as.numeric(x) > 0 & as.numeric(x) < 86400, na.rm = TRUE)) { | ||
x | ||
} else { | ||
x %>% lubridate::as_datetime() %>% | ||
flat_posixt() %>% | ||
hms::as_hms() %>% | ||
lubridate::as.period() | ||
} | ||
} | ||
|
||
#' @export | ||
clock_roll.difftime <- function(x) { | ||
out <- x | ||
units(out) <- "secs" | ||
|
||
if (all(as.numeric(out) > 0 & as.numeric(out) < 86400, na.rm = TRUE)) { | ||
units(out) <- units(x) | ||
out | ||
} else { | ||
out <- out %>% hms::as_hms() %>% | ||
lubridate::as_datetime() %>% | ||
flat_posixt() %>% | ||
hms::as_hms() %>% | ||
as.numeric() %>% | ||
lubridate::as.difftime(units = "secs") | ||
|
||
units(out) <- units(x) | ||
out | ||
} | ||
} | ||
|
||
#' @export | ||
clock_roll.hms <- function(x) { | ||
if (all(as.numeric(x) > 0 & as.numeric(x) < 86400, na.rm = TRUE)) { | ||
x | ||
} else { | ||
x %>% lubridate::as_datetime() %>% | ||
flat_posixt() %>% | ||
hms::as_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
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,45 @@ | ||
test_that("clock_roll() | general test", { | ||
# Nonexistent method error | ||
expect_error(clock_roll(list())) | ||
}) | ||
|
||
test_that("clock_roll.Duration() | general test", { | ||
expect_equal(clock_roll(lubridate::dhours(6)), lubridate::dhours(6)) | ||
expect_equal(clock_roll(lubridate::dhours(24)), lubridate::dhours(0)) | ||
expect_equal(clock_roll(lubridate::dhours(36)), lubridate::dhours(12)) | ||
|
||
expect_equal(clock_roll(c(lubridate::dhours(1), lubridate::dhours(48))), | ||
c(lubridate::dhours(1), lubridate::dhours(0))) | ||
}) | ||
|
||
test_that("clock_roll.Period() | general test", { | ||
expect_equal(clock_roll(lubridate::hours(6)), lubridate::hours(6)) | ||
expect_equal(clock_roll(lubridate::hours(24)), lubridate::hours(0)) | ||
expect_equal(clock_roll(lubridate::hours(36)), lubridate::hours(12)) | ||
|
||
expect_equal(clock_roll(c(lubridate::hours(1), lubridate::hours(48))), | ||
c(lubridate::hours(1), lubridate::hours(0))) | ||
}) | ||
|
||
test_that("clock_roll.difftime() | general test", { | ||
expect_equal(clock_roll(as.difftime(6, units = "mins")), | ||
as.difftime(6, units = "mins")) | ||
expect_equal(clock_roll(as.difftime(24, units = "hours")), | ||
as.difftime(0, units = "hours")) | ||
expect_equal(clock_roll(as.difftime(36, units = "hours")), | ||
as.difftime(12, units = "hours")) | ||
|
||
expect_equal(clock_roll(c(as.difftime(1, units = "hours"), | ||
as.difftime(48, units = "hours"))), | ||
c(as.difftime(1, units = "hours"), | ||
as.difftime(0, units = "hours"))) | ||
}) | ||
|
||
test_that("clock_roll.hms() | general test", { | ||
expect_equal(clock_roll(hms::parse_hm("06:00")), hms::parse_hm("06:00")) | ||
expect_equal(clock_roll(hms::parse_hm("24:00")), hms::parse_hm("00:00")) | ||
expect_equal(clock_roll(hms::hms(129600)), hms::parse_hm("12:00")) | ||
|
||
expect_equal(clock_roll(c(hms::parse_hm("01:00"), hms::hms(172800))), | ||
c(hms::parse_hm("01:00"), hms::parse_hm("00:00"))) | ||
}) |
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