Skip to content

Commit

Permalink
ymd() supports ... making it more convinient
Browse files Browse the repository at this point in the history
  • Loading branch information
shrektan committed Jan 11, 2022
1 parent 9ed2ee7 commit 3a53e09
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ymd
Title: Parse 'YMD' Format Number or String to Date
Version: 0.0.1.9000
Version: 0.0.2
Authors@R: c(
person("Xianying", "Tan", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-6072-3521")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ymd 0.1.0

* `ymd()` now supports `...` arguments, which is convinient for interactive use, e.g., `ymd(210101, 220201)`.

# ymd 0.0.1

* Added a `NEWS.md` file to track changes to the package.
Expand Down
18 changes: 1 addition & 17 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,7 @@
#' @useDynLib ymd, .registration = TRUE
NULL

#' Convert 'YMD' format integer or string to Date
#'
#' Transform integer or strings vectors in 'YMD' format to Date objects.
#' It intends to only support limited formats (no separator or one of
#' '.', ' ', '-' and '/' separators). See the possible formats in examples.
#'
#' @param x An integer or string vector in 'YMD' format. Double
#' values without the decimal part are allowed.
#' @return A Date object. When the parse fails for certain input,
#' the value returned would be `NA`, silently.
#'
#' @examples
#' ymd(c(210326, 19981225))
#' ymd(c("2020/1/8", "20 1 7", "1998.7.1", "1990-02-03"))
#'
#' @export
ymd <- function(x) .Call(wrap__ymd, x)
rust_ymd <- function(x) .Call(wrap__rust_ymd, x)

period_begin <- function(x, unit) .Call(wrap__period_begin, x, unit)

Expand Down
26 changes: 26 additions & 0 deletions R/ymd.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Convert 'YMD' format integer or string to Date
#'
#' Transform integer or strings vectors in 'YMD' format to Date objects.
#' It intends to only support limited formats (no separator or one of
#' '.', ' ', '-' and '/' separators). See the possible formats in examples.
#'
#' @param x An integer or string vector in 'YMD' format. Double
#' values without the decimal part are allowed.
#' @param ... The same as `x`. It will be merged into one vector with `x`.
#' It's convinient for interactive use.
#'
#' @return A Date object. When the parse fails for certain input,
#' the value returned would be `NA`, silently.
#'
#' @examples
#' ymd(c(210326, 19981225))
#' ymd(c("2020/1/8", "20 1 7", "1998.7.1", "1990-02-03"))
#' ymd(210420, 180322)
#'
#' @export
ymd <- function(x, ...) {
if (...length()) {
x <- c(x, unlist(list(...)))
}
rust_ymd(x)
}
8 changes: 6 additions & 2 deletions man/ymd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 4 additions & 20 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,8 @@ fn make_rdate2(x: Vec<Option<NaiveDate>>) -> Robj {
make_rdate(v)
}

/// Convert 'YMD' format integer or string to Date
///
/// Transform integer or strings vectors in 'YMD' format to Date objects.
/// It intends to only support limited formats (no separator or one of
/// '.', ' ', '-' and '/' separators). See the possible formats in examples.
///
/// @param x An integer or string vector in 'YMD' format. Double
/// values without the decimal part are allowed.
/// @return A Date object. When the parse fails for certain input,
/// the value returned would be `NA`, silently.
///
/// @examples
/// ymd(c(210326, 19981225))
/// ymd(c("2020/1/8", "20 1 7", "1998.7.1", "1990-02-03"))
///
/// @export
#[extendr]
fn ymd(x: Robj) -> Robj {
fn rust_ymd(x: Robj) -> Robj {
if x.inherits("Date") {
return x;
}
Expand Down Expand Up @@ -167,7 +151,7 @@ fn beop(x: Robj, unit: &str, fun: fn(&NaiveDate, period::Period) -> NaiveDate) -
Some(i) => i,
None => return make_rdate(vec![None; x.len()]),
};
let x = robj2date(ymd(x));
let x = robj2date(rust_ymd(x));
let out = x
.iter()
.map(|v| match v {
Expand Down Expand Up @@ -203,7 +187,7 @@ fn period_end(x: Robj, unit: &str) -> Robj {
/// @export
#[extendr]
fn edate(ref_date: Robj, months: i32) -> Robj {
let out = robj2date(ymd(ref_date))
let out = robj2date(rust_ymd(ref_date))
.iter()
.map(|v| match v {
Some(date) => Some(period::add_months(date, months)),
Expand Down Expand Up @@ -336,7 +320,7 @@ mod test {
// See corresponding C code in `entrypoint.c`.
extendr_module! {
mod ymd;
fn ymd;
fn rust_ymd;
fn period_begin;
fn period_end;
fn edate;
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-ymd.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ test_that("parse short year dates correctly", {
expect_equal(ymd("0098-03-05"), as.Date("0098-03-05"))
expect_equal(ymd("98-3-05"), as.Date("1998-03-05"))
})

test_that("ymd ... works", {
expect_equal(ymd(210101, 220101), ymd(c(210101, 220101)))
})

0 comments on commit 3a53e09

Please sign in to comment.