Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Suggests:
magrittr,
pillar,
rmarkdown,
slider (>= 0.3.0),
testthat (>= 3.0.0),
withr
LinkingTo:
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# clock (development version)

* Duration vectors now work as `.before` and `.after` arguments of
`slider::slide_index()` and friends (#306).

* R >=3.5.0 is now required, which is in line with tidyverse standards.

* vctrs >=0.6.1 and rlang >=1.1.0 are now required.
Expand Down
12 changes: 12 additions & 0 deletions R/date.R
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ arith_duration_and_date <- function(op, x, y, ...) {

# ------------------------------------------------------------------------------

# @export - .onLoad()
slider_plus.Date.clock_duration <- function(x, y) {
vec_arith("+", x, y)
}

# @export - .onLoad()
slider_minus.Date.clock_duration <- function(x, y) {
vec_arith("-", x, y)
}

# ------------------------------------------------------------------------------

#' Arithmetic: date
#'
#' @description
Expand Down
22 changes: 22 additions & 0 deletions R/posixt.R
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,28 @@ arith_duration_and_posixt <- function(op, x, y, ...) {

# ------------------------------------------------------------------------------

# @export - .onLoad()
slider_plus.POSIXct.clock_duration <- function(x, y) {
vec_arith("+", x, y)
}

# @export - .onLoad()
slider_plus.POSIXlt.clock_duration <- function(x, y) {
vec_arith("+", x, y)
}

# @export - .onLoad()
slider_minus.POSIXct.clock_duration <- function(x, y) {
vec_arith("-", x, y)
}

# @export - .onLoad()
slider_minus.POSIXlt.clock_duration <- function(x, y) {
vec_arith("-", x, y)
}

# ------------------------------------------------------------------------------

#' Arithmetic: date-time
#'
#' @description
Expand Down
8 changes: 8 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
vctrs::s3_register("pillar::pillar_shaft", "clock_calendar", pillar_shaft.clock_calendar)
vctrs::s3_register("pillar::pillar_shaft", "clock_time_point", pillar_shaft.clock_time_point)
vctrs::s3_register("pillar::pillar_shaft", "clock_zoned_time", pillar_shaft.clock_zoned_time)

vctrs::s3_register("slider::slider_plus", "Date.clock_duration", slider_plus.Date.clock_duration)
vctrs::s3_register("slider::slider_plus", "POSIXct.clock_duration", slider_plus.POSIXct.clock_duration)
vctrs::s3_register("slider::slider_plus", "POSIXlt.clock_duration", slider_plus.POSIXlt.clock_duration)

vctrs::s3_register("slider::slider_minus", "Date.clock_duration", slider_minus.Date.clock_duration)
vctrs::s3_register("slider::slider_minus", "POSIXct.clock_duration", slider_minus.POSIXct.clock_duration)
vctrs::s3_register("slider::slider_minus", "POSIXlt.clock_duration", slider_minus.POSIXlt.clock_duration)
}

# nocov end
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,12 @@

<duration<year>> * <date> is not permitted

# `slide_index()` will error on calendrical arithmetic and invalid dates

Code
slider::slide_index(x, i, identity, .after = after)
Condition
Error in `stop_clock()`:
! Invalid date found at location 2.
i Resolve invalid date issues by specifying the `invalid` argument.

36 changes: 36 additions & 0 deletions tests/testthat/_snaps/posixt.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,39 @@

<duration<year>> * <POSIXlt<America/New_York>> is not permitted

# `slide_index()` will error on naive-time based arithmetic and ambiguous times

Code
slider::slide_index(x, i, identity, .after = after)
Condition
Error in `stop_clock()`:
! Ambiguous time due to daylight saving time at location 1.
i Resolve ambiguous time issues by specifying the `ambiguous` argument.

# `slide_index()` will error on naive-time based arithmetic and nonexistent times

Code
slider::slide_index(x, i, identity, .after = after)
Condition
Error in `stop_clock()`:
! Nonexistent time due to daylight saving time at location 1.
i Resolve nonexistent time issues by specifying the `nonexistent` argument.

# `slide_index()` will error on calendrical arithmetic and ambiguous times

Code
slider::slide_index(x, i, identity, .after = after)
Condition
Error in `stop_clock()`:
! Ambiguous time due to daylight saving time at location 1.
i Resolve ambiguous time issues by specifying the `ambiguous` argument.

# `slide_index()` will error on calendrical arithmetic and nonexistent times

Code
slider::slide_index(x, i, identity, .after = after)
Condition
Error in `stop_clock()`:
! Nonexistent time due to daylight saving time at location 1.
i Resolve nonexistent time issues by specifying the `nonexistent` argument.

72 changes: 72 additions & 0 deletions tests/testthat/test-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,75 @@ test_that("<duration> op <date>", {
expect_snapshot_error(vec_arith("+", duration_hours(1), new_date(0)))
expect_snapshot_error(vec_arith("*", duration_years(1), new_date(0)))
})

# ------------------------------------------------------------------------------
# slider_plus() / slider_minus()

test_that("`slider_plus()` method is registered", {
skip_if_not_installed("slider", minimum_version = "0.3.0")

x <- date_build(2019, 1, 1:2)

y <- duration_days(2)
expect_identical(
slider::slider_plus(x, y),
date_build(2019, 1, 3:4)
)

y <- duration_years(1)
expect_identical(
slider::slider_plus(x, y),
date_build(2020, 1, 1:2)
)
})

test_that("`slider_minus()` method is registered", {
skip_if_not_installed("slider", minimum_version = "0.3.0")

x <- date_build(2019, 1, 1:2)

y <- duration_days(2)
expect_identical(
slider::slider_minus(x, y),
date_build(2018, 12, 30:31)
)

y <- duration_years(1)
expect_identical(
slider::slider_minus(x, y),
date_build(2018, 1, 1:2)
)
})

test_that("`slide_index()` works with dates and durations", {
skip_if_not_installed("slider", minimum_version = "0.3.0")

i <- date_build(2019, 1, 1:4)
x <- seq_along(i)

before <- duration_days(1)
after <- duration_days(2)

expect_identical(
slider::slide_index(x, i, identity, .before = before, .after = after),
list(
1:3,
1:4,
2:4,
3:4
)
)
})

test_that("`slide_index()` will error on calendrical arithmetic and invalid dates", {
skip_if_not_installed("slider", minimum_version = "0.3.0")

i <- date_build(2019, 1, 28:31)
x <- seq_along(i)

after <- duration_months(1)

expect_snapshot(error = TRUE, {
slider::slide_index(x, i, identity, .after = after)
})
})
Loading