Skip to content

Commit

Permalink
Add support for more UTC-equivalent time zones
Browse files Browse the repository at this point in the history
There are several other ways to specify UTC and GMT time zones:
  Etc/UTC, Etc/GMT, GMT-0, GMT+0, GMT0

Thanks to Michael Chirico for the report!

Fixes #319.
  • Loading branch information
joshuaulrich committed Jul 22, 2020
1 parent 6287228 commit a9e6573
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion R/endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ function (year = 1970,
}
# strptime has an issue (bug?) which returns NA when passed
# 1969-12-31-23-59-59; pass 58.9 secs instead.
sysTZ <- Sys.getenv("TZ")

This comment has been minimized.

Copy link
@MichaelChirico

MichaelChirico Jul 22, 2020

Should we add unset=NA here? AIUI TZ= means UTC, but TZ unset means local?

Came up here: Rdatatable/data.table#4608

This comment has been minimized.

Copy link
@joshuaulrich

joshuaulrich Jul 22, 2020

Author Owner

Thanks for bringing this up! It looks like your understanding is based on the POSIX implementation. This code was most likely written to be consistent with how R treats an empty string for the tz argument in most functions (e.g. strptime). I'm also not sure how much code in the wild works as if TZ="" is local time.

This comment has been minimized.

Copy link
@MichaelChirico

MichaelChirico Jul 22, 2020

yea makes sense. I would certainly use TZ=UTC myself for clarity. Makes sense to leave it as is until there would be a bug report then, I guess

if (length(c(year, month, day, hour, min, sec)) == 6 &&
all(c(year, month, day, hour, min, sec) == c(1969, 12, 31, 23, 59, 59)) &&
Sys.getenv("TZ") %in% c("", "GMT", "UTC"))
(sysTZ == "" || isUTC(sysTZ)))
sec <- sec-1
ISOdatetime(year, month, day, hour, min, sec, tz)
}
17 changes: 16 additions & 1 deletion R/tzone.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ function(x, ...)

.classesWithoutTZ <- c("chron","dates","times","Date","yearmon","yearqtr")

isUTC <- function(tz = NULL) {
if (is.null(tz)) {
tz <- Sys.timezone()
}
switch(tz,
"UTC" = ,
"GMT" = ,
"Etc/UTC" = ,
"Etc/GMT" = ,
"GMT-0" = ,
"GMT+0" = ,
"GMT0" = TRUE,
FALSE)
}

check.TZ <- function(x, ...)
{
#if( !getOption("xts_check_TZ", FALSE))
Expand All @@ -103,7 +118,7 @@ check.TZ <- function(x, ...)
if(any(tclass(x) %in% .classesWithoutTZ)) {
# warn if tzone is not UTC or GMT (GMT is not technically correct, since
# it *is* a timezone, but it should work for all practical purposes)
if (!(tzone(x) %in% c("UTC","GMT")))
if (!isUTC(tzone(x)))
warning(paste0("index class is ", paste(class(index(x)), collapse=", "),
", which does not support timezones.\nExpected 'UTC' timezone",
", but tzone is ", sQuote(tzone(x))), call.=FALSE)
Expand Down

0 comments on commit a9e6573

Please sign in to comment.