MyNixOS website logo
Description

Parse 'YMD' Format Number or String to Date.

Convert 'YMD' format number or string to Date efficiently, 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.

ymd

R-CMD-check CRANstatus Downloads from the RStudio CRANmirror Rust CodeCoverage

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)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::ymd(x)32.032.830037.1812.97KB0.0100000
lubridate::ymd(x)1376.41457.9671.38.71MB13.23066

x <- c(210101, 210224, 211231, 19890103)
x <- rep(x, 100)
run_bmk(
  ymd::ymd(x),
  lubridate::ymd(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::ymd(x)13.113.770210.93.17KB0.0100000
lubridate::ymd(x)1705.41785.1556.1362.21KB17.52548

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)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::ymd(x)24.425.937791.92.39KB3.899991
lubridate::ymd(x)791.8822.21200.3193.52KB17.35548
as.Date(x)641.1672.41488.487.54KB0.07450

x <- ymd::ymd(210515) + 1:100
run_bmk(
  ymd::eop$tm(x),
  lubridate::ceiling_date(x, "month") - 1
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::eop$tm(x)5.96.6149381.819.3KB0100000
lubridate::ceiling_date(x, “month”) - 132.834.728141.0155.4KB31998911

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)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::edate(x, 2)14.114.966499.96.2KB0.0100000
x %m+% months(2)311.9324.53029.7459.8KB23.6141211
run_bmk(
  ymd::edate(x, -12),
  x %m+% months(-12)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::edate(x, -12)14.215.066097.63.95KB0.0100000
x %m+% months(-12)647.6666.21471.8286.83KB28.168213

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)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::year(x)84590.685742.510.641.97MB14.168
lubridate::year(x)84620.686198.411.145.78MB16.669
funchir::quick_year(x)28513.729036.130.226.76MB11.3166
ymd::year(x)8005.88410.3115.33.82MB8.0584
run_bmk(
  data.table::month(x),
  lubridate::month(x),
  ymd::month(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::month(x)84417.186762.911.541.97MB7.764
lubridate::month(x)106067.5107216.58.983.92MB21.3512
ymd::month(x)9007.59828.8101.63.82MB8.0514
run_bmk(
  data.table::quarter(x),
  lubridate::quarter(x),
  ymd::quarter(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::quarter(x)87365.493648.310.341.97MB8.665
lubridate::quarter(x)124762.3129507.27.699.21MB11.446
ymd::quarter(x)16361.017152.957.53.82MB4.0292
run_bmk(
  data.table::yday(x),
  lubridate::yday(x),
  funchir::quick_yday(x),
  ymd::yday(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::yday(x)84184.184490.611.841.97MB41.427
lubridate::yday(x)89058.989058.911.245.78MB89.818
funchir::quick_yday(x)22964.323207.342.919.08MB39.01110
ymd::yday(x)9580.89902.899.63.82MB8.9454
run_bmk(
  data.table::mday(x),
  lubridate::mday(x),
  funchir::quick_mday(x),
  ymd::mday(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::mday(x)81525.082372.612.238.15MB12.233
lubridate::mday(x)81730.881730.812.238.15MB61.215
funchir::quick_mday(x)9127.19380.6106.415.28MB21.3357
ymd::mday(x)9145.710157.8100.03.82MB4.2482
run_bmk(
  data.table::wday(x),
  lubridate::wday(x),
  ymd::wday(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::wday(x)10252.410449.195.53.82MB4.2452
lubridate::wday(x)83525.284106.411.945.78MB29.725
ymd::wday(x)9127.310192.698.83.82MB9.0444
run_bmk(
  data.table::isoweek(x),
  lubridate::isoweek(x),
  ymd::isoweek(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::isoweek(x)2737848.32737848.30.4225.14MB1.815
lubridate::isoweek(x)265307.8269441.43.7248MB18.6210
ymd::isoweek(x)10436.810700.490.23.82MB3.9462

Session Info

xfun::session_info()
#> R version 4.2.1 (2022-06-23)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Ventura 13.4.1
#> 
#> Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
#> 
#> Package version:
#>   base64enc_0.1.3   bench_1.1.3       bslib_0.5.1       cachem_1.0.8     
#>   cli_3.6.1         compiler_4.2.1    cpp11_0.4.6       data.table_1.14.8
#>   digest_0.6.33     ellipsis_0.3.2    evaluate_0.21     fansi_1.0.4      
#>   fastmap_1.1.1     fontawesome_0.5.2 fs_1.6.3          funchir_0.2.2    
#>   generics_0.1.3    glue_1.6.2        graphics_4.2.1    grDevices_4.2.1  
#>   highr_0.10        htmltools_0.5.6   jquerylib_0.1.4   jsonlite_1.8.7   
#>   knitr_1.43        lifecycle_1.0.3   lubridate_1.9.2   magrittr_2.0.3   
#>   memoise_2.0.1     methods_4.2.1     mime_0.12         pillar_1.9.0     
#>   pkgconfig_2.0.3   profmem_0.6.0     R6_2.5.1          rappdirs_0.3.3   
#>   rlang_1.1.1       rmarkdown_2.24    sass_0.4.7        stats_4.2.1      
#>   stringi_1.7.12    stringr_1.5.0     tibble_3.2.1      timechange_0.2.0 
#>   tinytex_0.46      tools_4.2.1       utf8_1.2.3        utils_4.2.1      
#>   vctrs_0.6.3       xfun_0.40         yaml_2.3.7        ymd_0.1.0
Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows