4

I started playing with the lubridate package in R. I noticed that now(tzone="EST") computed as:

[1] "2015-08-25 13:01:08 EST"

while now(tzone="PST") resulted in warning:

[1] "2015-08-25 18:02:16 GMT"
Warning message:
In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'PST'

So what are the known timezones? Valid Timezones in Lubridate has an answer. But I'd like to see how I can answer this for myself (i.e., by digging through the package itself). I look at the now() function:

> now
function (tzone = "") 
with_tz(Sys.time(), tzone)
<environment: namespace:lubridate>

So then I look at the with_tz function:

> with_tz
function (time, tzone = "") 
{
    check_tz(tzone)
    if (is.POSIXlt(time)) 
        new <- as.POSIXct(time)
    else new <- time
    attr(new, "tzone") <- tzone
    reclass_date(new, time)
}
<environment: namespace:lubridate>
> 

So then I check the check_tz function:

> check_tz
Error: object 'check_tz' not found

Not there. I search my local lubridate R library files for check_tz. I don't find anything. I do a Google Search and find this GitHub page. There it is! It appears that olson_time_zones() lists the known time zones. (UPDATE: olson_time_zones() only returns a subset of the available time zones. See my comments below for more details.) In particular,

> now(tzone="America/Los_Angeles")
[1] "2015-08-25 11:11:14 PDT"

Q: How could I have answered my question about the list of known time zones if there weren't a nice file up on GitHub or post on StackOverflow with the answer? In other words, could I have found my answer by digging through my local lubridate library files?

Q: Is there a more general principle to digging through R packages which is worth pointing out?

Community
  • 1
  • 1
lowndrul
  • 3,715
  • 7
  • 36
  • 54
  • I, too, would like to know how to find this sort of thing out in R in general. In particular, here, is a particular lead on your question. http://stackoverflow.com/questions/29784310/valid-time-zones-in-lubridate – Shawn Mehan Aug 25 '15 at 18:28
  • I think that *might* be happening because right now (late August) is always Pacific Daylight Time (PDT), and at this time during the year Pacific Standard Time (PST) does not exist. Just a thought, no clue if it's correct or not. – Rich Scriven Aug 26 '15 at 05:28
  • @RichardScriven, I think it's even weirder that "EST" works at all since `stringr::str_subset(olson_time_zones(), "EST")` returns nothing. It appears `lubridate` also uses `OlsonNames()` for `tzone`. `str_subset(OlsonNames(), "EST")` returns "EST" and "EST5EDT". `str_subset(OlsonNames(), "PDT")` returns "PST8PDT". – lowndrul Aug 26 '15 at 16:57
  • A side-note here, that as of *lubridate 1.6* (Sept 2016), there is no longer `olson_time_zones` - '*`here` and `olson_time_zones` were deprecated in favor of `new` and base `OlsonNames` respectively.*' – thelatemail Jul 09 '20 at 01:39

1 Answers1

9

You can find a list of the timezones available by lubridate with:

lubridate::olson_time_zones()

This information is easily found by typing

??timezones

in the console. The link describing this function is the second entry in the list of the help page in my case:

enter image description here

Another possibility is to search the manual of lubridate for "available time zones".

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • Certainly right that `??` ferrets out the right answer in this case. – lowndrul Aug 25 '15 at 21:15
  • I conceded too quickly that this worked. See my comments above in response to @RichardScriven. Time zones like "EST" and "PST8PDT" work in the `now()` function but are not returned by `olson_time_zones()`. So it appears `olson_time_zones()` only returns a subset of the available time zones. – lowndrul Aug 26 '15 at 17:02