DavisVaughan / warp

Group Dates

Home Page:https://davisvaughan.github.io/warp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider always using TZ of x

DavisVaughan opened this issue · comments

So we remove the origin parameter and always use either 1970-01-01 or 0001-01-01 in the time zone of x We need the origin still

Construct with as.POSIXct(“0001-01-01”, tz =)?

This would mean that if you ever wanted to use those values as offsets then you’d have to know the original time zone, but that seems ok

This would make it less of a hassle because you don’t get the annoying origin warning, and it would automatically do the thing you generally would have done anyways

Or, from POSIXct x

  • 0 element slice
  • Convert to lt to get gmtoffset (if null or na use utc with warning?)
  • Wrap double gmtoffset with attributes of x to get origin

This way we don’t have to tinker with the tzone attribute

Might make sense to use 0001-01-01 since we only used the Unix epoch because it was the UTC epoch

Here is a way to get the 1970-01-01 epoch in the time zone of interest

time_zone <- function(x) {
  UseMethod("time_zone")
}

time_zone.default <- function(x) {
  stop("Can't extract the time zone from `x`.", call. = FALSE)
}

time_zone.Date <- function(x) {
  "UTC"
}

time_zone.POSIXct <- function(x) {
  tzone <- attr(x, "tzone")
  
  # Local
  if (is.null(tzone)) {
    return("")
  }
  
  tzone
}

time_zone.POSIXlt <- function(x) {
  tzone <- attr(x, "tzone")
  
  # Local
  if (is.null(tzone)) {
    return("")
  }
  
  # Generally a vector of length 3
  # According to docs, possible to be length 1 (UTC)
  tzone <- tzone[[1]]
  
  tzone
}

epoch_in_tzone <- function(x) {
  time_zone <- time_zone(x)
  posixct_attributes <- list(tzone = time_zone, class = c("POSIXct", "POSIXt"))
  
  dummy <- 0
  attributes(dummy) <- posixct_attributes
  
  if (time_zone == "UTC" || time_zone == "GMT") {
    return(dummy)
  }
  
  dummy <- as.POSIXlt.POSIXct(dummy, time_zone)
  
  offset <- dummy$gmtoff
  
  # If unknown, assume UTC
  if (is.na(offset)) {
    return(dummy)
  }
  
  out <- offset * -1
  attributes(out) <- posixct_attributes
  
  out
}