-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31a37a0
commit b5864d3
Showing
34 changed files
with
1,346 additions
and
1,359 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
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 |
---|---|---|
@@ -1,45 +1,45 @@ | ||
change_date <- function(x, date) { | ||
classes <- c("Date", "POSIXct", "POSIXlt") | ||
checkmate::assert_multi_class(x, classes) | ||
classes <- c("Date", "POSIXct", "POSIXlt") | ||
checkmate::assert_multi_class(x, classes) | ||
|
||
classes <- c("character", "Date") | ||
checkmate::assert_multi_class(date, classes) | ||
gutils:::assert_length_one(date) | ||
classes <- c("character", "Date") | ||
checkmate::assert_multi_class(date, classes) | ||
gutils:::assert_length_one(date) | ||
|
||
lubridate::date(x) <- date | ||
lubridate::date(x) <- date | ||
|
||
x | ||
x | ||
} | ||
|
||
change_day <- function(x, day) { | ||
classes <- c("Date", "POSIXct", "POSIXlt") | ||
|
||
checkmate::assert_multi_class(x, classes, null.ok = FALSE) | ||
checkmate::assert_number(day, lower = 1, upper = 31) | ||
|
||
if (any(lubridate::month(x) %in% c(4, 6, 9, 11), na.rm = TRUE) | ||
&& day > 30) { | ||
cli::cli_abort(paste0( | ||
"You can't assign more than 30 days to April, June, ", | ||
"September, or November." | ||
)) | ||
} | ||
|
||
if (any(lubridate::month(x) == 2 & !lubridate::leap_year(x)) && day > 28) { | ||
cli::cli_abort(paste0( | ||
"You can't assign more than 28 days to February in ", | ||
"non-leap years." | ||
)) | ||
} | ||
|
||
if (any(lubridate::month(x) == 2 & lubridate::leap_year(x), na.rm = TRUE) && | ||
classes <- c("Date", "POSIXct", "POSIXlt") | ||
|
||
checkmate::assert_multi_class(x, classes, null.ok = FALSE) | ||
checkmate::assert_number(day, lower = 1, upper = 31) | ||
|
||
if (any(lubridate::month(x) %in% c(4, 6, 9, 11), na.rm = TRUE) | ||
&& day > 30) { | ||
cli::cli_abort(paste0( | ||
"You can't assign more than 30 days to April, June, ", | ||
"September, or November." | ||
)) | ||
} | ||
|
||
if (any(lubridate::month(x) == 2 & !lubridate::leap_year(x)) && day > 28) { | ||
cli::cli_abort(paste0( | ||
"You can't assign more than 28 days to February in ", | ||
"non-leap years." | ||
)) | ||
} | ||
|
||
if (any(lubridate::month(x) == 2 & lubridate::leap_year(x), na.rm = TRUE) && | ||
day > 29) { | ||
cli::cli_abort(paste0( | ||
"You can't assign more than 29 days to February in a leap year." | ||
)) | ||
} | ||
cli::cli_abort(paste0( | ||
"You can't assign more than 29 days to February in a leap year." | ||
)) | ||
} | ||
|
||
lubridate::day(x) <- day | ||
lubridate::day(x) <- day | ||
|
||
x | ||
x | ||
} |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
extract_seconds <- function(x) { | ||
classes <- c("Duration", "difftime", "hms", "POSIXct", "POSIXlt", | ||
"Interval") | ||
classes <- c("Duration", "difftime", "hms", "POSIXct", "POSIXlt", | ||
"Interval") | ||
|
||
checkmate::assert_multi_class(x, classes) | ||
checkmate::assert_multi_class(x, classes) | ||
|
||
if (lubridate::is.POSIXt(x) || lubridate::is.difftime(x)) { | ||
as.numeric(hms::as_hms(x)) | ||
} else { | ||
as.numeric(x) | ||
} | ||
if (lubridate::is.POSIXt(x) || lubridate::is.difftime(x)) { | ||
as.numeric(hms::as_hms(x)) | ||
} else { | ||
as.numeric(x) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
fix_hms <- function(x) { | ||
checkmate::assert_class(x, "hms") | ||
checkmate::assert_class(x, "hms") | ||
|
||
dplyr::case_when( | ||
x == hms::parse_hm("24:00") ~ hms::hms(0), | ||
TRUE ~ x | ||
) | ||
dplyr::case_when( | ||
x == hms::parse_hm("24:00") ~ hms::hms(0), | ||
TRUE ~ x | ||
) | ||
} |
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 |
---|---|---|
@@ -1,32 +1,32 @@ | ||
flat_posixt <- function(posixt, base = as.Date("1970-01-01"), | ||
force_tz = TRUE, tz = "UTC") { | ||
gutils:::assert_posixt(posixt, null.ok = FALSE) | ||
checkmate::assert_date(base, len = 1, all.missing = FALSE) | ||
checkmate::assert_flag(force_tz) | ||
checkmate::assert_choice(tz, OlsonNames()) | ||
gutils:::assert_posixt(posixt, null.ok = FALSE) | ||
checkmate::assert_date(base, len = 1, all.missing = FALSE) | ||
checkmate::assert_flag(force_tz) | ||
checkmate::assert_choice(tz, OlsonNames()) | ||
|
||
lubridate::date(posixt) <- base | ||
lubridate::date(posixt) <- base | ||
|
||
if (isTRUE(force_tz)) { | ||
lubridate::force_tz(posixt, tz) | ||
} else { | ||
posixt | ||
} | ||
if (isTRUE(force_tz)) { | ||
lubridate::force_tz(posixt, tz) | ||
} else { | ||
posixt | ||
} | ||
} | ||
|
||
flat_posixt_date <- function(posixt, base = as.Date("1970-01-01")) { | ||
gutils:::assert_posixt(posixt, null.ok = FALSE) | ||
checkmate::assert_date(base, len = 1, any.missing = FALSE) | ||
gutils:::assert_posixt(posixt, null.ok = FALSE) | ||
checkmate::assert_date(base, len = 1, any.missing = FALSE) | ||
|
||
posixt %>% lubridate::`date<-`(base) %>% c() | ||
posixt |> lubridate::`date<-`(base) |> c() | ||
} | ||
|
||
flat_posixt_hour <- function(posixt, base = hms::parse_hms("00:00:00")) { | ||
gutils:::assert_posixt(posixt) | ||
gutils:::assert_hms(base, any.missing = FALSE) | ||
gutils:::assert_posixt(posixt) | ||
gutils:::assert_hms(base, any.missing = FALSE) | ||
|
||
posixt %>% | ||
lubridate::date() %>% | ||
paste0(" ", base) %>% | ||
lubridate::as_datetime(tz = lubridate::tz(posixt)) | ||
posixt |> | ||
lubridate::date() |> | ||
paste0(" ", base) |> | ||
lubridate::as_datetime(tz = lubridate::tz(posixt)) | ||
} |
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,16 @@ | ||
int_mean <- function(start, end, ambiguity = 24) { | ||
classes <- c("Duration", "difftime", "hms", "POSIXct", "POSIXlt") | ||
|
||
checkmate::assert_multi_class(start, classes) | ||
checkmate::assert_multi_class(end, classes) | ||
checkmate::assert_choice(ambiguity, c(0, 24, NA)) | ||
|
||
start <- cycle_time(hms::hms(extract_seconds(start)), | ||
cycle = lubridate::ddays()) | ||
end <- cycle_time(hms::hms(extract_seconds(end)), | ||
cycle = lubridate::ddays()) | ||
interval <- gutils:::shush(assign_date(start, end, ambiguity = ambiguity)) | ||
mean <- as.numeric(start) + (as.numeric(interval) / 2) | ||
|
||
hms::hms(mean) | ||
} |
Oops, something went wrong.