Skip to content

Commit 279b377

Browse files
committed
Add vec-ptype method for year-day
1 parent 3fbd211 commit 279b377

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ S3method(vec_ptype,clock_iso_year_week_day)
481481
S3method(vec_ptype,clock_naive_time)
482482
S3method(vec_ptype,clock_sys_time)
483483
S3method(vec_ptype,clock_weekday)
484+
S3method(vec_ptype,clock_year_day)
484485
S3method(vec_ptype,clock_year_month_day)
485486
S3method(vec_ptype,clock_year_month_weekday)
486487
S3method(vec_ptype,clock_year_quarter_day)

R/gregorian-year-day.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ is_year_day <- function(x) {
137137

138138
# ------------------------------------------------------------------------------
139139

140+
#' @export
141+
vec_ptype.clock_year_day <- function(x, ...) {
142+
switch(
143+
calendar_precision(x) + 1L,
144+
clock_empty_year_day_year,
145+
abort("Internal error: Invalid precision"),
146+
abort("Internal error: Invalid precision"),
147+
abort("Internal error: Invalid precision"),
148+
clock_empty_year_day_day,
149+
clock_empty_year_day_hour,
150+
clock_empty_year_day_minute,
151+
clock_empty_year_day_second,
152+
clock_empty_year_day_millisecond,
153+
clock_empty_year_day_microsecond,
154+
clock_empty_year_day_nanosecond,
155+
abort("Internal error: Invalid precision.")
156+
)
157+
}
158+
140159
#' @export
141160
vec_ptype2.clock_year_day.clock_year_day <- function(x, y, ...) {
142161
ptype2_calendar_and_calendar(x, y, ...)
@@ -903,3 +922,20 @@ seq.clock_year_day <- function(from,
903922
...
904923
)
905924
}
925+
926+
# ------------------------------------------------------------------------------
927+
928+
clock_init_year_day_utils <- function(env) {
929+
year <- year_day(integer())
930+
931+
assign("clock_empty_year_day_year", year, envir = env)
932+
assign("clock_empty_year_day_day", calendar_widen(year, "day"), envir = env)
933+
assign("clock_empty_year_day_hour", calendar_widen(year, "hour"), envir = env)
934+
assign("clock_empty_year_day_minute", calendar_widen(year, "minute"), envir = env)
935+
assign("clock_empty_year_day_second", calendar_widen(year, "second"), envir = env)
936+
assign("clock_empty_year_day_millisecond", calendar_widen(year, "millisecond"), envir = env)
937+
assign("clock_empty_year_day_microsecond", calendar_widen(year, "microsecond"), envir = env)
938+
assign("clock_empty_year_day_nanosecond", calendar_widen(year, "nanosecond"), envir = env)
939+
940+
invisible(NULL)
941+
}

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
clock_init_year_month_day_utils(clock_ns)
1313
clock_init_year_month_weekday_utils(clock_ns)
1414
clock_init_iso_year_week_day_utils(clock_ns)
15+
clock_init_year_day_utils(clock_ns)
1516
clock_init_sys_time_utils(clock_ns)
1617
clock_init_naive_time_utils(clock_ns)
1718
clock_init_zoned_time_utils(clock_ns)

tests/testthat/test-gregorian-year-day.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ test_that("names of `year` are not retained", {
5858
expect_named(year_day(c(x = 1)), NULL)
5959
})
6060

61+
# ------------------------------------------------------------------------------
62+
# vec_ptype()
63+
64+
test_that("ptype is correct", {
65+
base <- year_day(1)
66+
ptype <- year_day(integer())
67+
68+
for (precision in precision_names()) {
69+
if (precision == "quarter" || precision == "month" || precision == "week") {
70+
next
71+
}
72+
73+
x <- calendar_widen(base, precision)
74+
expect <- calendar_widen(ptype, precision)
75+
76+
expect_identical(vec_ptype(x), expect)
77+
}
78+
})
79+
6180
# ------------------------------------------------------------------------------
6281
# vec_proxy() / vec_restore()
6382

0 commit comments

Comments
 (0)