ymd

An R package that handles common dates operations using Rust

OTHER License

Downloads
310
Stars
33
Committers
2

output: github_document

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

ymd

Convert 'YMD' format number or string to Date efficiently, e.g., 211225 to as.Date("2021-12-25"), using Rust's standard library. It also provides helper functions to handle Date, e.g., quick finding the beginning or end of the given period, adding months to Date, etc.

It's similar to the lubridate package but is much lighter and focuses only on Date objects.

Installation

Binary version (no Rust toolchain required)

CRAN provides the binary package. So, if you are on Windows or macOS, the package can be installed via:

install.packages("ymd")

If you are on Linux, you can try to use the RSPM (RStudio Package Manager) repo provided by RStudio PBC, via (remember to choose the correct binary repo URL for your platform):

install.packages("ymd", repos = "{RSPM-Repo-URL}")

Source version (Rust toolchain required)

If you want to build the dev version from source, you'll need the Rust toolchain, which can be installed following the instructions from the Rust book.

After that, you can build the package via:

remotes::install_github("ymd")

Use Cases and Benchmarks

print_bmk <- function(x) {
  x[[1]] <- format(x[[1]])
  x[[5]] <- format(x[[5]])
  rnd <- \(v) if (is.numeric(v)) round(v, 1) else v
  x[, 1:8] |>
    lapply(rnd) |>
    as.data.frame() |>
    knitr::kable() |>
    print()
}
run_bmk <- function(..., time_unit = "us") {
  bench::mark(..., time_unit = time_unit) |> print_bmk()
}

ymd

x <- c("210101", "21/02/03", "89-1-03", "1989.03.05", "01 02 03")
x <- rep(x, 100)
run_bmk(
  ymd::ymd(x),
  lubridate::ymd(x)
)

x <- c(210101, 210224, 211231, 19890103)
x <- rep(x, 100)
run_bmk(
  ymd::ymd(x),
  lubridate::ymd(x)
)

x <- c("2021-01-01", "2022-12-31", "1995-03-22")
x <- rep(x, 100)
run_bmk(
  ymd::ymd(x),
  lubridate::ymd(x),
  as.Date(x)
)

x <- ymd::ymd(210515) + 1:100
run_bmk(
  ymd::eop$tm(x),
  lubridate::ceiling_date(x, "month") - 1
)

edate

`%m+%` <- lubridate::`%m+%`
x <- ymd::ymd(c(200115, 200131, 200229, 200331, 200401))
x <- rep(x, 100)
run_bmk(
  ymd::edate(x, 2),
  x %m+% months(2)
)
run_bmk(
  ymd::edate(x, -12),
  x %m+% months(-12)
)

Extract Date Part

# tweak from https://github.com/Rdatatable/data.table/pull/5300
set.seed(373L)
x <- as.Date(data.table::as.IDate(sample(seq(-25000, 45000), 1e6, TRUE)))

run_bmk(
  data.table::year(x),
  lubridate::year(x),
  funchir::quick_year(x),
  ymd::year(x)
)
run_bmk(
  data.table::month(x),
  lubridate::month(x),
  ymd::month(x)
)
run_bmk(
  data.table::quarter(x),
  lubridate::quarter(x),
  ymd::quarter(x)
)
run_bmk(
  data.table::yday(x),
  lubridate::yday(x),
  funchir::quick_yday(x),
  ymd::yday(x)
)
run_bmk(
  data.table::mday(x),
  lubridate::mday(x),
  funchir::quick_mday(x),
  ymd::mday(x)
)
run_bmk(
  data.table::wday(x),
  lubridate::wday(x),
  ymd::wday(x)
)
run_bmk(
  data.table::isoweek(x),
  lubridate::isoweek(x),
  ymd::isoweek(x)
)

Session Info

xfun::session_info()