Skip to content

Commit

Permalink
Change code indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvartan committed Jul 27, 2023
1 parent 31a37a0 commit b5864d3
Show file tree
Hide file tree
Showing 34 changed files with 1,346 additions and 1,359 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ S3method(round_time,hms)
export(assign_date)
export(cycle_time)
export(longer_duration)
export(longer_interval)
export(longer_int)
export(round_time)
export(shorter_duration)
export(shorter_interval)
export(shorter_int)
export(sum_time)
export(vct_sum_time)
importFrom(dplyr,"%>%")
Expand Down
48 changes: 24 additions & 24 deletions R/assign_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,32 @@
#' assign_date(start, end, ambiguity = 24)
#' #> [1] 1970-01-01 12:00:00 UTC--1970-01-02 12:00:00 UTC # Expected
assign_date <- function(start, end, ambiguity = 0) {
checkmate::assert_multi_class(start, c("hms", "POSIXt"))
checkmate::assert_numeric(as.numeric(hms::as_hms(start)),
lower = 0, upper = 86400)
checkmate::assert_multi_class(end, c("hms", "POSIXt"))
checkmate::assert_numeric(as.numeric(hms::as_hms(end)),
lower = 0, upper = 86400)
gutils:::assert_identical(start, end, type = "length")
checkmate::assert_choice(ambiguity, c(0, 24, NA))
checkmate::assert_multi_class(start, c("hms", "POSIXt"))
checkmate::assert_numeric(as.numeric(hms::as_hms(start)),
lower = 0, upper = 86400)
checkmate::assert_multi_class(end, c("hms", "POSIXt"))
checkmate::assert_numeric(as.numeric(hms::as_hms(end)),
lower = 0, upper = 86400)
gutils:::assert_identical(start, end, type = "length")
checkmate::assert_choice(ambiguity, c(0, 24, NA))

start <- start %>%
hms::as_hms() %>%
as.POSIXct() %>%
flat_posixt()
start <- start |>
hms::as_hms() |>
as.POSIXct() |>
flat_posixt()

end <- end %>%
hms::as_hms() %>%
as.POSIXct() %>%
flat_posixt()
end <- end |>
hms::as_hms() |>
as.POSIXct() |>
flat_posixt()

out <- dplyr::case_when(
is.na(start) | is.na(end) ~ lubridate::as.interval(NA),
start < end ~ lubridate::interval(start, end),
start > end ~ lubridate::interval(start, end + lubridate::days()),
is.na(ambiguity) ~ lubridate::as.interval(NA),
TRUE ~ lubridate::as.interval(lubridate::hours(ambiguity), start)
)
out <- dplyr::case_when(
is.na(start) | is.na(end) ~ lubridate::as.interval(NA),
start < end ~ lubridate::interval(start, end),
start > end ~ lubridate::interval(start, end + lubridate::days()),
is.na(ambiguity) ~ lubridate::as.interval(NA),
TRUE ~ lubridate::as.interval(lubridate::hours(ambiguity), start)
)

out
out
}
68 changes: 34 additions & 34 deletions R/change_date.R
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
}
64 changes: 32 additions & 32 deletions R/cycle_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,65 +195,65 @@
#' cycle_time(time, cycle, reverse)
#' #> [1] "0s" "28927800s (~47.83 weeks)" # Expected
cycle_time <- function(time, cycle, reverse = TRUE) {
UseMethod("cycle_time")
UseMethod("cycle_time")
}

#' @rdname cycle_time
#' @export
cycle_time.numeric <- function(time, cycle, reverse = TRUE) {
time %>% cycle_time_build(cycle, reverse)
time |> cycle_time_build(cycle, reverse)
}

#' @rdname cycle_time
#' @export
cycle_time.Duration <- function(time, cycle, reverse = TRUE) {
time %>%
cycle_time_build(cycle, reverse) %>%
lubridate::dseconds()
time |>
cycle_time_build(cycle, reverse) |>
lubridate::dseconds()
}

#' @rdname cycle_time
#' @export
cycle_time.difftime <- function(time, cycle, reverse = TRUE) {
out <- time
units(out) <- "secs"
out <- time
units(out) <- "secs"

out <- out %>%
cycle_time_build(cycle, reverse) %>%
lubridate::seconds() %>%
lubridate::as.difftime(units = "secs")
out <- out |>
cycle_time_build(cycle, reverse) |>
lubridate::seconds() |>
lubridate::as.difftime(units = "secs")

units(out) <- units(time)
out
units(out) <- units(time)
out
}

#' @rdname cycle_time
#' @export
cycle_time.hms <- function(time, cycle, reverse = TRUE) {
time %>%
cycle_time_build(cycle, reverse) %>%
hms::hms()
time |>
cycle_time_build(cycle, reverse) |>
hms::hms()
}

cycle_time_build <- function(time, cycle, reverse) {
checkmate::assert_multi_class(cycle, c("numeric", "Duration"))
checkmate::assert_number(as.numeric(cycle), lower = 0, null.ok = FALSE)
checkmate::assert_flag(reverse)
checkmate::assert_multi_class(cycle, c("numeric", "Duration"))
checkmate::assert_number(as.numeric(cycle), lower = 0, null.ok = FALSE)
checkmate::assert_flag(reverse)

cycle <- cycle %>% as.numeric()
cycle <- cycle |> as.numeric()

sign <- time %>%
as.numeric() %>%
sign()
sign <- time |>
as.numeric() |>
sign()

out <- time %>%
as.numeric() %>%
abs() %>%
`%%`(cycle)
out <- time |>
as.numeric() |>
abs() %>% # Don't change
`%%`(cycle)

if (isTRUE(reverse)) {
dplyr::if_else(sign < 0, cycle - out, out)
} else {
dplyr::if_else(sign < 0, - out, out)
}
if (isTRUE(reverse)) {
dplyr::if_else(sign < 0, cycle - out, out)
} else {
dplyr::if_else(sign < 0, - out, out)
}
}
16 changes: 8 additions & 8 deletions R/extract_seconds.R
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)
}
}
10 changes: 5 additions & 5 deletions R/fix_hms.R
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
)
}
38 changes: 19 additions & 19 deletions R/flat_posixt.R
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))
}
16 changes: 16 additions & 0 deletions R/int_mean.R
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)
}
Loading

0 comments on commit b5864d3

Please sign in to comment.