[R] Detecting invalid time zone names

Christian Lederer christianlederer at t-online.de
Sat Nov 14 04:36:14 CET 2009


Hi folks,

i have to deal with data sets, which have a column with UTC seconds
and a second column containing the timezone.
However this timezone is not always valid in R, e.g. misspelled.
Unfortunately, the date/time functions like ISOdatetime or as.POSIXlt
silently accept invalid timezone arguments, and consequently e.g.
truncating to days gives wrong results.

After experimenting with invalid timezone names and as.POSIXlt,
i found that i could use this simple function to check if a
string is a valid time zone name:

checktz <- function(s)
  ## Returns TRUE if s is a valid time zone name, otherwise FALSE
{
  t <- as.POSIXlt(ISOdatetime(1970, 1, 1, 0, 0, 12345, tz = s))
  tzone <- attributes(t)$tzone
  valid <- s != "" &&
           (length(tzone) == 1 || tzone[2]!= "UTC")
  return(valid)
}

However this relies on undocumented behavior of a.POSIXlt.

Is there a clean way to to check if a string represents a valid
timezone name in R?

Christian




More information about the R-help mailing list