Skip to content

Commit 5056b56

Browse files
committed
feat: add get_interval_duration()
1 parent 505e2ca commit 5056b56

13 files changed

Lines changed: 195 additions & 0 deletions

pkg/NAMESPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ S3method(get_firstday,grates_yearweek_sunday)
198198
S3method(get_firstday,grates_yearweek_thursday)
199199
S3method(get_firstday,grates_yearweek_tuesday)
200200
S3method(get_firstday,grates_yearweek_wednesday)
201+
S3method(get_interval_duration,default)
202+
S3method(get_interval_duration,grates_epiweek)
203+
S3method(get_interval_duration,grates_int_period)
204+
S3method(get_interval_duration,grates_isoweek)
205+
S3method(get_interval_duration,grates_month)
206+
S3method(get_interval_duration,grates_period)
207+
S3method(get_interval_duration,grates_year)
208+
S3method(get_interval_duration,grates_yearmonth)
209+
S3method(get_interval_duration,grates_yearquarter)
210+
S3method(get_interval_duration,grates_yearweek)
201211
S3method(get_n,default)
202212
S3method(get_n,grates_int_period)
203213
S3method(get_n,grates_month)
@@ -343,6 +353,7 @@ export(date_end)
343353
export(date_start)
344354
export(epiweek)
345355
export(get_firstday)
356+
export(get_interval_duration)
346357
export(get_n)
347358
export(get_offset)
348359
export(get_week)

pkg/NEWS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# grates (development version)
22

3+
* New function `get_interval_duration()` which returns the number of days
4+
covered by an element of a grates object, e.g. the following is TRUE
5+
6+
```
7+
identical(
8+
get_interval_duration(yearmonth(2020, 1:3)),
9+
c(31, 29, 31)
10+
)
11+
```
12+
13+
314
## breaking change
415

516
* `as.double()` for `<grates_int_period>` objects now returns the same result

pkg/R/accessors.R

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,68 @@ get_offset.default <- function(x, ...) {
207207
get_offset.grates_period <- function(x, ...) {
208208
attr(x, "offset")
209209
}
210+
211+
# -------------------------------------------------------------------------
212+
#' @name grouped_date_accessors
213+
#' @export
214+
get_interval_duration <- function(x, ...) {
215+
UseMethod("get_interval_duration")
216+
}
217+
218+
# -------------------------------------------------------------------------
219+
#' @rdname grouped_date_accessors
220+
#' @export
221+
get_interval_duration.default <- function(x, ...) {
222+
stopf("Not implemented for class [%s].", toString(class(x)))
223+
}
224+
225+
# -------------------------------------------------------------------------
226+
#' @rdname grouped_date_accessors
227+
#' @export
228+
get_interval_duration.grates_yearweek <- function(x, ...) {
229+
rep(7, length(x))
230+
}
231+
232+
# -------------------------------------------------------------------------
233+
#' @rdname grouped_date_accessors
234+
#' @export
235+
get_interval_duration.grates_int_period <- function(x, ...) {
236+
rep(attr(x, "n"), length(x))
237+
}
238+
239+
# -------------------------------------------------------------------------
240+
#' @rdname grouped_date_accessors
241+
#' @export
242+
get_interval_duration.grates_isoweek <- get_interval_duration.grates_yearweek
243+
244+
# -------------------------------------------------------------------------
245+
#' @rdname grouped_date_accessors
246+
#' @export
247+
get_interval_duration.grates_epiweek <- get_interval_duration.grates_yearweek
248+
249+
# -------------------------------------------------------------------------
250+
#' @rdname grouped_date_accessors
251+
#' @export
252+
get_interval_duration.grates_yearmonth <- function(x, ...) {
253+
as.double(as.Date(x + 1) - as.Date(x), units = "days")
254+
}
255+
256+
# -------------------------------------------------------------------------
257+
#' @rdname grouped_date_accessors
258+
#' @export
259+
get_interval_duration.grates_month <- get_interval_duration.grates_yearmonth
260+
261+
# -------------------------------------------------------------------------
262+
#' @rdname grouped_date_accessors
263+
#' @export
264+
get_interval_duration.grates_yearquarter <- get_interval_duration.grates_yearmonth
265+
266+
# -------------------------------------------------------------------------
267+
#' @rdname grouped_date_accessors
268+
#' @export
269+
get_interval_duration.grates_year <- get_interval_duration.grates_yearmonth
270+
271+
# -------------------------------------------------------------------------
272+
#' @rdname grouped_date_accessors
273+
#' @export
274+
get_interval_duration.grates_period <- get_interval_duration.grates_int_period

pkg/man/grouped_date_accessors.Rd

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/tests/testthat/test-epiweek.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,14 @@ test_that("epiweek boundary functions work", {
314314
expect_identical(date_start(weeks), starts)
315315
expect_identical(date_end(weeks), ends)
316316
})
317+
318+
test_that("epiweek get_interval_duration works", {
319+
dates <- as.Date("2020-01-01") + 0:14
320+
weeks <- as_epiweek(dates)
321+
expect_identical(
322+
get_interval_duration(weeks),
323+
expected = rep(7, length(dates))
324+
)
325+
})
326+
327+

pkg/tests/testthat/test-int_period.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,11 @@ test_that("int_period, miscellaneous work", {
255255
)
256256
expect_false(is.numeric(new_int_period(1)))
257257
})
258+
259+
test_that("int_period get_interval_duration works", {
260+
dates <- as_int_period(0:14, 3)
261+
expect_identical(
262+
get_interval_duration(dates),
263+
expected = rep(3L, length(dates))
264+
)
265+
})

pkg/tests/testthat/test-isoweek.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,12 @@ test_that("isoweek boundary functions work", {
313313
expect_identical(date_start(weeks), starts)
314314
expect_identical(date_end(weeks), ends)
315315
})
316+
317+
test_that("isoweek get_interval_duration works", {
318+
dates <- as.Date("2020-01-01") + 0:14
319+
weeks <- as_isoweek(dates)
320+
expect_identical(
321+
get_interval_duration(weeks),
322+
expected = rep(7, length(dates))
323+
)
324+
})

pkg/tests/testthat/test-month.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,11 @@ test_that("month boundary functions work", {
319319
expect_identical(date_start(months), starts)
320320
expect_identical(date_end(months), ends)
321321
})
322+
323+
test_that("month get_interval_duration works", {
324+
dates <- c(fastymd::fymd(2020,seq(1,12,2),1), fastymd::fymd(2021,seq(1,12,2),1))
325+
months <- as_month(dates, n = 2)
326+
leap <- c(31 + 29, 31 + 30, 31 + 30, 31 + 31, 30 + 31, 30 + 31)
327+
normal <- c(31 + 28, 31 + 30, 31 + 30, 31 + 31, 30 + 31, 30 + 31)
328+
expect_identical(get_interval_duration(months), c(leap,normal))
329+
})

pkg/tests/testthat/test-period.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,12 @@ test_that("period, character offset works", {
427427
dat <- as_period(dates, n = 2, offset = "2020-01-02")
428428
expect_identical(as_period(dates, n = 2), expected = dat)
429429
})
430+
431+
test_that("period get_interval_duration works", {
432+
dates <- as.Date("2020-01-01") + (0:61)
433+
dat <- as_period(dates, n = 2, offset = "2020-01-01")
434+
expect_identical(
435+
get_interval_duration(dat),
436+
expected = rep(2L, length(dates))
437+
)
438+
})

pkg/tests/testthat/test-year.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,10 @@ test_that("year boundary functions work", {
272272
expect_identical(date_start(years), starts)
273273
expect_identical(date_end(years), ends)
274274
})
275+
276+
test_that("year get_interval_duration works", {
277+
years <- grates::year(2020:2021)
278+
leap <- c(31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31)
279+
normal <- c(31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31)
280+
expect_identical(get_interval_duration(years), c(leap, normal))
281+
})

0 commit comments

Comments
 (0)