Skip to content

Commit c3e5685

Browse files
committed
Add vec-ptype method for iso-year-week-day
1 parent adf4b72 commit c3e5685

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ S3method(vec_proxy,clock_year_quarter_day)
430430
S3method(vec_proxy,clock_zoned_time)
431431
S3method(vec_proxy_compare,clock_weekday)
432432
S3method(vec_proxy_compare,clock_year_month_weekday)
433+
S3method(vec_ptype,clock_iso_year_week_day)
433434
S3method(vec_ptype,clock_year_month_day)
434435
S3method(vec_ptype,clock_year_month_weekday)
435436
S3method(vec_ptype2,clock_duration.clock_duration)

R/iso-year-week-day.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ is_iso_year_week_day <- function(x) {
152152

153153
# ------------------------------------------------------------------------------
154154

155+
#' @export
156+
vec_ptype.clock_iso_year_week_day <- function(x, ...) {
157+
switch(
158+
calendar_precision(x) + 1L,
159+
clock_empty_iso_year_week_day_year,
160+
abort("Internal error: Invalid precision"),
161+
abort("Internal error: Invalid precision"),
162+
clock_empty_iso_year_week_day_week,
163+
clock_empty_iso_year_week_day_day,
164+
clock_empty_iso_year_week_day_hour,
165+
clock_empty_iso_year_week_day_minute,
166+
clock_empty_iso_year_week_day_second,
167+
clock_empty_iso_year_week_day_millisecond,
168+
clock_empty_iso_year_week_day_microsecond,
169+
clock_empty_iso_year_week_day_nanosecond,
170+
abort("Internal error: Invalid precision.")
171+
)
172+
}
173+
155174
#' @export
156175
vec_ptype2.clock_iso_year_week_day.clock_iso_year_week_day <- function(x, y, ...) {
157176
ptype2_calendar_and_calendar(x, y, ...)
@@ -916,3 +935,21 @@ seq.clock_iso_year_week_day <- function(from,
916935
...
917936
)
918937
}
938+
939+
# ------------------------------------------------------------------------------
940+
941+
clock_init_iso_year_week_day_utils <- function(env) {
942+
year <- iso_year_week_day(integer())
943+
944+
assign("clock_empty_iso_year_week_day_year", year, envir = env)
945+
assign("clock_empty_iso_year_week_day_week", calendar_widen(year, "week"), envir = env)
946+
assign("clock_empty_iso_year_week_day_day", calendar_widen(year, "day"), envir = env)
947+
assign("clock_empty_iso_year_week_day_hour", calendar_widen(year, "hour"), envir = env)
948+
assign("clock_empty_iso_year_week_day_minute", calendar_widen(year, "minute"), envir = env)
949+
assign("clock_empty_iso_year_week_day_second", calendar_widen(year, "second"), envir = env)
950+
assign("clock_empty_iso_year_week_day_millisecond", calendar_widen(year, "millisecond"), envir = env)
951+
assign("clock_empty_iso_year_week_day_microsecond", calendar_widen(year, "microsecond"), envir = env)
952+
assign("clock_empty_iso_year_week_day_nanosecond", calendar_widen(year, "nanosecond"), envir = env)
953+
954+
invisible(NULL)
955+
}

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# Initializers must run after initializing C++ utils and setting tzdata
1212
clock_init_year_month_day_utils(clock_ns)
1313
clock_init_year_month_weekday_utils(clock_ns)
14+
clock_init_iso_year_week_day_utils(clock_ns)
1415

1516
vctrs::s3_register("pillar::pillar_shaft", "clock_calendar", pillar_shaft.clock_calendar)
1617
vctrs::s3_register("pillar::pillar_shaft", "clock_time_point", pillar_shaft.clock_time_point)

tests/testthat/test-iso-year-week-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 <- iso_year_week_day(1)
55+
ptype <- iso_year_week_day(integer())
56+
57+
for (precision in precision_names()) {
58+
if (precision == "quarter" || precision == "month") {
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)