r-lib / clock

A Date-Time Library for R
https://clock.r-lib.org
Other
97 stars 4 forks source link

Implement `date_time_info()` and `zoned_time_info()` #295

Closed DavisVaughan closed 1 year ago

DavisVaughan commented 2 years ago
library(clock)

# Add input type checking
zoned_time_info <- function(x) {
  zone <- clock:::zoned_time_zone_attribute(x)
  x <- as_sys_time(x)
  out <- sys_time_info(x, zone)
  out$begin <- as_zoned_time(out$begin, zone = zone)
  out$end <- as_zoned_time(out$end, zone = zone)
  out
}

date_time_info <- function(x) {
  x <- as_zoned_time(x)
  out <- zoned_time_info(x)
  out$begin <- as_date_time(out$begin)
  out$end <- as_date_time(out$end)
  out
}

x <- date_time_parse("1970-10-25 00:00:00", "America/New_York")
x <- x + c(3600, 7200)

date_time_info(x)
#>                 begin                 end offset   dst abbreviation
#> 1 1970-04-26 03:00:00 1970-10-25 01:00:00 -14400  TRUE          EDT
#> 2 1970-10-25 01:00:00 1971-04-25 03:00:00 -18000 FALSE          EST
zoned_time_info(as_zoned_time(x))
#>                                         begin
#> 1 1970-04-26T03:00:00-04:00[America/New_York]
#> 2 1970-10-25T01:00:00-05:00[America/New_York]
#>                                           end offset   dst abbreviation
#> 1 1970-10-25T01:00:00-05:00[America/New_York] -14400  TRUE          EDT
#> 2 1971-04-25T03:00:00-04:00[America/New_York] -18000 FALSE          EST

These won't actually conflict with or be ambiguous with naive_time_info() in any way because we already know the time zone to look the information up in, and we know it is a unique type because the times exist already.

Follow up to https://github.com/r-lib/clock/pull/170 that does add zoned_time_info() back in. I feel like these wrappers are actually fairly useful and unambiguous.

Should make sure the begin/end limits are working right (year 32767 and whatnot)

Crosslink between these and naive_time_info() and sys_time_info()