Skip to content

Commit 77336d3

Browse files
committed
Add vec-ptype method for year-quarter-day
1 parent c3e5685 commit 77336d3

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ S3method(vec_proxy_compare,clock_year_month_weekday)
433433
S3method(vec_ptype,clock_iso_year_week_day)
434434
S3method(vec_ptype,clock_year_month_day)
435435
S3method(vec_ptype,clock_year_month_weekday)
436+
S3method(vec_ptype,clock_year_quarter_day)
436437
S3method(vec_ptype2,clock_duration.clock_duration)
437438
S3method(vec_ptype2,clock_iso_year_week_day.clock_iso_year_week_day)
438439
S3method(vec_ptype2,clock_naive_time.clock_naive_time)

R/quarterly-year-quarter-day.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,33 @@ is_year_quarter_day <- function(x) {
175175

176176
# ------------------------------------------------------------------------------
177177

178+
#' @export
179+
vec_ptype.clock_year_quarter_day <- function(x, ...) {
180+
names <- NULL
181+
precision <- calendar_precision(x)
182+
start <- quarterly_start(x)
183+
184+
f <- integer()
185+
186+
fields <- switch(
187+
precision + 1L,
188+
list(year = f),
189+
list(year = f, quarter = f),
190+
abort("Internal error: Invalid precision"),
191+
abort("Internal error: Invalid precision"),
192+
list(year = f, quarter = f, day = f),
193+
list(year = f, quarter = f, day = f, hour = f),
194+
list(year = f, quarter = f, day = f, hour = f, minute = f),
195+
list(year = f, quarter = f, day = f, hour = f, minute = f, second = f),
196+
list(year = f, quarter = f, day = f, hour = f, minute = f, second = f, subsecond = f),
197+
list(year = f, quarter = f, day = f, hour = f, minute = f, second = f, subsecond = f),
198+
list(year = f, quarter = f, day = f, hour = f, minute = f, second = f, subsecond = f),
199+
abort("Internal error: Invalid precision.")
200+
)
201+
202+
new_year_quarter_day_from_fields(fields, precision, start, names)
203+
}
204+
178205
#' @export
179206
vec_ptype2.clock_year_quarter_day.clock_year_quarter_day <- function(x, y, ...) {
180207
if (quarterly_start(x) != quarterly_start(y)) {

tests/testthat/test-quarterly-year-quarter-day.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ test_that("NA values propagate", {
4747
expect_identical(is.na(x), c(TRUE, FALSE, TRUE))
4848
})
4949

50+
# ------------------------------------------------------------------------------
51+
# vec_ptype()
52+
53+
test_that("ptype is correct", {
54+
base <- year_quarter_day(1)
55+
ptype <- year_quarter_day(integer())
56+
57+
for (precision in precision_names()) {
58+
if (precision == "month" || precision == "week") {
59+
next
60+
}
61+
62+
x <- calendar_widen(base, precision)
63+
expect <- calendar_widen(ptype, precision)
64+
65+
expect_identical(vec_ptype(x), expect)
66+
}
67+
})
68+
5069
# ------------------------------------------------------------------------------
5170
# vec_proxy() / vec_restore()
5271

0 commit comments

Comments
 (0)