Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Digging into R package: Time Zones in lubridate

Tags:

r

lubridate

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?

like image 320
lowndrul Avatar asked Jan 07 '23 16:01

lowndrul


1 Answers

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".

like image 84
RHertel Avatar answered Jan 15 '23 04:01

RHertel