Skip to content

Commit

Permalink
fix the bug about the integer Date
Browse files Browse the repository at this point in the history
  • Loading branch information
shrektan committed Nov 6, 2024
1 parent 093de4c commit 0639598
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
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.3

* Fix the bug inside of `period_begin()` function to support both integer and double dates.

# ymd 0.1.2

* Added rustc version reporting in the installation log as per CRAN policy.
Expand Down
8 changes: 5 additions & 3 deletions src/rust/src/rdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ pub fn robj2date(x: Robj, var: &str) -> Result<Vec<Option<NaiveDate>>> {
})
.collect(),
Rtype::Integers => x
.as_integer_slice()
.as_integer_vector()
.unwrap()
.iter()
.map(|d| {
if d[0].is_na() {
if d.is_na() {
None
} else {
NaiveDate::from_num_days_from_ce_opt(d[0] as i32 + R_DATE_FROM_CE)
NaiveDate::from_num_days_from_ce_opt(d + R_DATE_FROM_CE)
}
})
.into_iter()
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-beop.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
test_that("period_begin returns right begining date", {

dates <- seq.Date(from = as.Date("2016-03-07") - 1, to = as.Date("2016-03-13") + 1, by = "1 day")
out <- period_begin(dates, unit = "week") == as.Date("2016-03-07")

# check both integer dates and double dates
dates_int <- structure(as.integer(dates), class = "Date")
out <- period_begin(dates_int, unit = "week") == as.Date("2016-03-07")
expect_equal(out, c(FALSE, rep(TRUE, length(out) - 2L), FALSE))

dates_dbl <- structure(as.numeric(dates), class = "Date")
out <- period_begin(dates_dbl, unit = "week") == as.Date("2016-03-07")
expect_equal(out, c(FALSE, rep(TRUE, length(out) - 2L), FALSE))

# other checks: such as month, quarter, year
dates <- seq.Date(from = as.Date("2015-01-01") - 1, to = as.Date("2015-01-31") + 1, by = "1 day")
out <- period_begin(dates, unit = "month") == as.Date("2015-01-01")
expect_equal(out, c(FALSE, rep(TRUE, length(out) - 2L), FALSE))
Expand Down

0 comments on commit 0639598

Please sign in to comment.