Created
March 4, 2024 17:43
-
-
Save elipousson/31795a79408b78d30af3dd2c15014fc5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
vague_dt_interval <- function(start, | |
end = NULL, | |
use_now = TRUE, | |
unit = "second", | |
format = c("default", "short", "terse")) { | |
if (!is.interval(start)) { | |
if (use_now) { | |
end <- end %||% lubridate::now() | |
} | |
x <- lubridate::interval( | |
start = start, | |
end = end | |
) | |
} else { | |
x <- start | |
} | |
if (is.interval(x)) { | |
x <- lubridate::time_length(x, unit = unit) | |
} | |
if (!inherits(x, "difftime")) { | |
units <- switch(unit, | |
second = "secs" | |
) | |
} | |
purrr::map_chr( | |
x, | |
\(dt) { | |
prettyunits::vague_dt( | |
as.difftime(dt, units = units), | |
format = format | |
) | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment