Skip to content

Commit f69a897

Browse files
committed
Add vec-ptype method for sys-time and naive-time
1 parent 77336d3 commit f69a897

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ S3method(vec_proxy,clock_zoned_time)
431431
S3method(vec_proxy_compare,clock_weekday)
432432
S3method(vec_proxy_compare,clock_year_month_weekday)
433433
S3method(vec_ptype,clock_iso_year_week_day)
434+
S3method(vec_ptype,clock_naive_time)
435+
S3method(vec_ptype,clock_sys_time)
434436
S3method(vec_ptype,clock_year_month_day)
435437
S3method(vec_ptype,clock_year_month_weekday)
436438
S3method(vec_ptype,clock_year_quarter_day)

R/naive-time.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,25 @@ new_naive_info_from_fields <- function(fields) {
697697

698698
# ------------------------------------------------------------------------------
699699

700+
#' @export
701+
vec_ptype.clock_naive_time <- function(x, ...) {
702+
switch(
703+
time_point_precision(x) + 1L,
704+
abort("Internal error: Invalid precision"),
705+
abort("Internal error: Invalid precision"),
706+
abort("Internal error: Invalid precision"),
707+
abort("Internal error: Invalid precision"),
708+
clock_empty_naive_time_day,
709+
clock_empty_naive_time_hour,
710+
clock_empty_naive_time_minute,
711+
clock_empty_naive_time_second,
712+
clock_empty_naive_time_millisecond,
713+
clock_empty_naive_time_microsecond,
714+
clock_empty_naive_time_nanosecond,
715+
abort("Internal error: Invalid precision.")
716+
)
717+
}
718+
700719
#' @export
701720
vec_ptype2.clock_naive_time.clock_naive_time <- function(x, y, ...) {
702721
ptype2_time_point_and_time_point(x, y, ...)
@@ -751,3 +770,19 @@ vec_arith.clock_naive_time.numeric <- function(op, x, y, ...) {
751770
vec_arith.numeric.clock_naive_time <- function(op, x, y, ...) {
752771
arith_numeric_and_time_point(op, x, y, ...)
753772
}
773+
774+
# ------------------------------------------------------------------------------
775+
776+
clock_init_naive_time_utils <- function(env) {
777+
day <- as_naive(year_month_day(integer(), integer(), integer()))
778+
779+
assign("clock_empty_naive_time_day", day, envir = env)
780+
assign("clock_empty_naive_time_hour", time_point_cast(day, "hour"), envir = env)
781+
assign("clock_empty_naive_time_minute", time_point_cast(day, "minute"), envir = env)
782+
assign("clock_empty_naive_time_second", time_point_cast(day, "second"), envir = env)
783+
assign("clock_empty_naive_time_millisecond", time_point_cast(day, "millisecond"), envir = env)
784+
assign("clock_empty_naive_time_microsecond", time_point_cast(day, "microsecond"), envir = env)
785+
assign("clock_empty_naive_time_nanosecond", time_point_cast(day, "nanosecond"), envir = env)
786+
787+
invisible(NULL)
788+
}

R/sys-time.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,25 @@ new_sys_info_from_fields <- function(fields) {
416416

417417
# ------------------------------------------------------------------------------
418418

419+
#' @export
420+
vec_ptype.clock_sys_time <- function(x, ...) {
421+
switch(
422+
time_point_precision(x) + 1L,
423+
abort("Internal error: Invalid precision"),
424+
abort("Internal error: Invalid precision"),
425+
abort("Internal error: Invalid precision"),
426+
abort("Internal error: Invalid precision"),
427+
clock_empty_sys_time_day,
428+
clock_empty_sys_time_hour,
429+
clock_empty_sys_time_minute,
430+
clock_empty_sys_time_second,
431+
clock_empty_sys_time_millisecond,
432+
clock_empty_sys_time_microsecond,
433+
clock_empty_sys_time_nanosecond,
434+
abort("Internal error: Invalid precision.")
435+
)
436+
}
437+
419438
#' @export
420439
vec_ptype2.clock_sys_time.clock_sys_time <- function(x, y, ...) {
421440
ptype2_time_point_and_time_point(x, y, ...)
@@ -471,3 +490,18 @@ vec_arith.numeric.clock_sys_time <- function(op, x, y, ...) {
471490
arith_numeric_and_time_point(op, x, y, ...)
472491
}
473492

493+
# ------------------------------------------------------------------------------
494+
495+
clock_init_sys_time_utils <- function(env) {
496+
day <- as_sys(year_month_day(integer(), integer(), integer()))
497+
498+
assign("clock_empty_sys_time_day", day, envir = env)
499+
assign("clock_empty_sys_time_hour", time_point_cast(day, "hour"), envir = env)
500+
assign("clock_empty_sys_time_minute", time_point_cast(day, "minute"), envir = env)
501+
assign("clock_empty_sys_time_second", time_point_cast(day, "second"), envir = env)
502+
assign("clock_empty_sys_time_millisecond", time_point_cast(day, "millisecond"), envir = env)
503+
assign("clock_empty_sys_time_microsecond", time_point_cast(day, "microsecond"), envir = env)
504+
assign("clock_empty_sys_time_nanosecond", time_point_cast(day, "nanosecond"), envir = env)
505+
506+
invisible(NULL)
507+
}

R/zzz.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
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_sys_time_utils(clock_ns)
16+
clock_init_naive_time_utils(clock_ns)
1517

1618
vctrs::s3_register("pillar::pillar_shaft", "clock_calendar", pillar_shaft.clock_calendar)
1719
vctrs::s3_register("pillar::pillar_shaft", "clock_time_point", pillar_shaft.clock_time_point)

tests/testthat/test-naive-time.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,22 @@ test_that("strict mode can be activated - ambiguous", {
495495
expect_snapshot_error(as_zoned(naive_seconds(), zone, nonexistent = "roll-forward", ambiguous = zt))
496496
expect_snapshot_error(as_zoned(naive_seconds(), zone, nonexistent = "roll-forward", ambiguous = list(zt, NULL)))
497497
})
498+
499+
# ------------------------------------------------------------------------------
500+
# vec_ptype()
501+
502+
test_that("ptype is correct", {
503+
base <- naive_days(0)
504+
ptype <- naive_days(integer())
505+
506+
for (precision in precision_names()) {
507+
if (validate_precision_string(precision) < PRECISION_DAY) {
508+
next
509+
}
510+
511+
x <- time_point_cast(base, precision)
512+
expect <- time_point_cast(ptype, precision)
513+
514+
expect_identical(vec_ptype(x), expect)
515+
}
516+
})

tests/testthat/test-sys-time.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,22 @@ test_that("failure to parse throws a warning", {
119119
expect_warning(sys_parse("foo"), class = "clock_warning_parse_failures")
120120
expect_snapshot(sys_parse("foo"))
121121
})
122+
123+
# ------------------------------------------------------------------------------
124+
# vec_ptype()
125+
126+
test_that("ptype is correct", {
127+
base <- sys_days(0)
128+
ptype <- sys_days(integer())
129+
130+
for (precision in precision_names()) {
131+
if (validate_precision_string(precision) < PRECISION_DAY) {
132+
next
133+
}
134+
135+
x <- time_point_cast(base, precision)
136+
expect <- time_point_cast(ptype, precision)
137+
138+
expect_identical(vec_ptype(x), expect)
139+
}
140+
})

0 commit comments

Comments
 (0)