Get startedGet started for free

Time zones

1. Time zones

In this Chapter you'll look at a few more aspects of working with datetimes that you may run into in practice, starting with handling time zones. Time zones are a way to keep track of the differences between local times in different locations. As soon as you are dealing with times from more than one place, you'll need to worry about these differences. You'll often have seen them as offsets from some global standard like UTC or Greenwich meantime. For example if you ask google

2. Time zones

for the time in Auckland it says it's GMT + 13, or 13 hours ahead of Greenwich Meantime. But in R, to specify the timezone for a datetime you use a character string. You can see the timezone of your system from R with the function Sys (dot) timezone. On my laptop I get "America/Los_Angeles". The time zones R knows about can be listed with the function OlsonNames.

3. IANA Timezones

It may seem weird to use a region and city to label a timezone rather than just an offset, but remember offsets might change through the year due to daylight savings, and when, and if, daylight savings occurred might have changed through history. To convert one set of datetimes to another timezone requires a complete record of this history, and since that history often isn't country specific, or country borders have changed through time, a city is the easiest way to label it. This database of timezones is managed by Internet Assigned Numbers Authority, or IANA, the same organization that is also responsible for handing out blocks of IP addresses to Regional Internet Registries. The database was orginally compiled by Arthur David Olson, hence the name of the R function, OlsonNames. You've already seen that you can set

4. Setting and extracting

the timezone of datetimes when you import them. For example you saw this one in chapter 3. All the functions for parsing dates in lubridate like parse_date_time, ymd and friends, and make_date as well as the base R functions as (dot) POSIXct and as (dot) Date can take a tz argument. In lubridate you can use the tz function to extract the timezone of a datetime.

5. Manipulating timezones

Once you have datetimes in R there are generally two things you might want to do: set the timezone to something different, or view the same moment in a different timezone. The current timezone might be listed incorrectly, in which case you want to set it to something else, without changing the components of the datetime like the hours and minutes. In lubridate this is done with the function force_tz. Simply pass in the datetime and the new timezone in the tzone argument. We can force our mar_11 datetime have the New York timezone instead. It now represents the same clock time, noon on March 11th, but in New York. On the other hand sometimes the timezone is correct, but you'd like to view the datetime in a different timezone. This is very common when you are combining data from many zones, and want all the datetimes in a common time zone. In lubridate this is handled by the with_tz function, and you use it just like force_tz. If we view our mar_11 datetime in the New York timezone we see when its noon in the Los Angeles timezone its already 3pm in New York.

6. Let's practice!

Alright, time for you to try this out.