Skip to content

Instantly share code, notes, and snippets.

@elipousson
Created March 4, 2024 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elipousson/31795a79408b78d30af3dd2c15014fc5 to your computer and use it in GitHub Desktop.
Save elipousson/31795a79408b78d30af3dd2c15014fc5 to your computer and use it in GitHub Desktop.
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