Skip to content

Commit 40d710d

Browse files
committed
Add vec-ptype method for zoned-time
1 parent f69a897 commit 40d710d

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ S3method(vec_ptype,clock_sys_time)
436436
S3method(vec_ptype,clock_year_month_day)
437437
S3method(vec_ptype,clock_year_month_weekday)
438438
S3method(vec_ptype,clock_year_quarter_day)
439+
S3method(vec_ptype,clock_zoned_time)
439440
S3method(vec_ptype2,clock_duration.clock_duration)
440441
S3method(vec_ptype2,clock_iso_year_week_day.clock_iso_year_week_day)
441442
S3method(vec_ptype2,clock_naive_time.clock_naive_time)

R/zoned-time.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,31 @@ zone_pretty <- function(zone) {
675675

676676
# ------------------------------------------------------------------------------
677677

678+
#' @export
679+
vec_ptype.clock_zoned_time <- function(x, ...) {
680+
zone <- zoned_time_zone(x)
681+
682+
ptype_utc <- switch(
683+
zoned_time_precision(x) + 1L,
684+
abort("Internal error: Invalid precision"),
685+
abort("Internal error: Invalid precision"),
686+
abort("Internal error: Invalid precision"),
687+
abort("Internal error: Invalid precision"),
688+
abort("Internal error: Invalid precision"),
689+
abort("Internal error: Invalid precision"),
690+
abort("Internal error: Invalid precision"),
691+
clock_empty_zoned_time_utc_second,
692+
clock_empty_zoned_time_utc_millisecond,
693+
clock_empty_zoned_time_utc_microsecond,
694+
clock_empty_zoned_time_utc_nanosecond,
695+
abort("Internal error: Invalid precision.")
696+
)
697+
698+
ptype <- zoned_time_set_zone(ptype_utc, zone)
699+
700+
ptype
701+
}
702+
678703
#' @export
679704
vec_ptype2.clock_zoned_time.clock_zoned_time <- function(x, y, ...) {
680705
x_zone <- zoned_time_zone(x)
@@ -1015,3 +1040,14 @@ validate_zoned_time_precision_string <- function(precision) {
10151040
is_valid_zoned_time_precision <- function(precision) {
10161041
precision >= PRECISION_SECOND
10171042
}
1043+
1044+
# ------------------------------------------------------------------------------
1045+
1046+
clock_init_zoned_time_utils <- function(env) {
1047+
assign("clock_empty_zoned_time_utc_second", as_zoned(as_sys(duration_seconds()), "UTC"), envir = env)
1048+
assign("clock_empty_zoned_time_utc_millisecond", as_zoned(as_sys(duration_milliseconds()), "UTC"), envir = env)
1049+
assign("clock_empty_zoned_time_utc_microsecond", as_zoned(as_sys(duration_microseconds()), "UTC"), envir = env)
1050+
assign("clock_empty_zoned_time_utc_nanosecond", as_zoned(as_sys(duration_nanoseconds()), "UTC"), envir = env)
1051+
1052+
invisible(NULL)
1053+
}

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
clock_init_iso_year_week_day_utils(clock_ns)
1515
clock_init_sys_time_utils(clock_ns)
1616
clock_init_naive_time_utils(clock_ns)
17+
clock_init_zoned_time_utils(clock_ns)
1718

1819
vctrs::s3_register("pillar::pillar_shaft", "clock_calendar", pillar_shaft.clock_calendar)
1920
vctrs::s3_register("pillar::pillar_shaft", "clock_time_point", pillar_shaft.clock_time_point)

tests/testthat/test-zoned-time.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,28 @@ test_that("zoned-times don't support arithmetic", {
407407
expect_snapshot_error(add_microseconds(x, 1))
408408
expect_snapshot_error(add_nanoseconds(x, 1))
409409
})
410+
411+
# ------------------------------------------------------------------------------
412+
# vec_ptype()
413+
414+
test_that("ptype is correct", {
415+
zones <- c("UTC", "America/New_York", "")
416+
417+
for (zone in zones) {
418+
for (precision in precision_names()) {
419+
precision <- validate_precision_string(precision)
420+
421+
if (precision < PRECISION_SECOND) {
422+
next
423+
}
424+
425+
x <- duration_helper(0L, precision)
426+
x <- as_zoned(as_naive(x), zone)
427+
428+
ptype <- duration_helper(integer(), precision)
429+
ptype <- as_zoned(as_naive(ptype), zone)
430+
431+
expect_identical(vec_ptype(x), ptype)
432+
}
433+
}
434+
})

0 commit comments

Comments
 (0)