Learn R Programming

base (version 3.0.3)

timezones: Time Zones

Description

Information about time zones in R. Sys.timezone returns the current time zone.

Usage

Sys.timezone()

Arguments

Value

Sys.timezone returns an OS-specific character string, possibly NA or an empty string (which on some OSes means UTC).. For Windows this is an abbreviation such as "EST".

Time zone names

Where OSes describe their valid time zones can be obscure. The help for the C function tzset can be helpful, but it can also be inaccurate. There is a cumbersome POSIX specification (listed under environment variable TZ at http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08), which is often at least partially supported, but there are other more user-friendly ways to specify time zones. Many systems make use of a time-zone database originally compiled by Arthur David Olson (and now managed by IANA), in which the preferred way to refer to a time zone is by a location (typically of a city) e.g. Europe/London, America/Los_Angeles, Pacific/Easter. Some traditional designations are also allowed such as EST5EDT or GB. (Beware that some of these designations may not be what you think: in particular EST is a time zone used in Canada without daylight savings time, and not EST5EDT nor (Australian) Eastern Standard Time.) The designation can also be an optional colon prepended to the path to a file giving complied zone information (and the examples above are all files in a system-specific location). See http://www.twinsun.com/tz/tz-link.htm for more details and references. By convention, regions with a unique time-zone history since 1970 have specific names, but those with different earlier histories may not. Each time zone has one or two (the second for DST) abbreviations used when formatting times. Note that that the abbreviations have changed over the years: for example France used PMT (‘Paris Mean Time’) from 1891 to 1911 then CET/CEST, butWET/WEST for parts of 1940--5. Software will generally use the current abbreviation(s), and the POSIX standard allows only one or two abbreviations per time zone. unix Most Unix-alikes use the Olson database. The system-specific location in the file system varies, e.g. ‘/usr/share/zoneinfo’ (Linux, OS X, FreeBSD), ‘/usr/share/lib/zoneinfo’ (Solaris, AIX), .... It is likely that there is a file named something like ‘zone.tab’ under that directory listing the locations known as time-zone names (but not for example EST5EDT). See also http://en.wikipedia.org/wiki/Zone.tab. windows R under Windows uses the Olson database. The current version of the database will be given in file ‘R_HOME\share\zoneinfo\VERSION’. Environment variable TZDIR can be used to point to a later ‘zoneinfo’ directory. A file listing most known time zones can be found at ‘R_HOME\share\zoneinfo\zone.tab’ (see ‘Examples’). By convention, regions with a unique time-zone history since 1970 have specific names, but those with different earlier histories may not. An attempt is made (once only per session) to map Windows' idea of the current time zone to a location, following an earlier version of http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml It can be overridden by setting the TZ environment variable. Windows documents a specification of the form GST-1GDT: this is interpreted as POSIX-like and hence the ‘US rules’ for changing to/from DST are applied (and are incorrect for Germany). Versions of R prior to 2.7.0 used Windows' system functions and hence this form: it is still accepted for backwards compatibility but was (and remains) unreliable and gives a warning. Many systems support time zones of the form GMT+n and GMT-n, which are at a fixed offset from UTC (hence no DST). Contrary to some usage (but consistent with names such as PST8PDT), negative offsets are times ahead of (east of) UTC, positive offsets are times behind (west of) UTC.

Details

Time zones are a system-specific topic, but these days almost all R platforms use similar underlying code, used by Linux, OS X, Solaris, AIX, FreeBSD, Sun Java >= 1.4 and Tcl >= 8.5, and installed with R on Windows. Unfortunately there are many system-specific errors in the implementation.

It should be possible to set the time zone via the environment variable TZ: see the section on ‘Time zone names’ for suitable values.

It is not in general possible to retrieve the system's own name(s) for the current time zone if environment variable TZ is not set, but on Windows Sys.timezone will retrieve the name it uses for the current time (and the name may differ depending on whether daylight saving time is in effect). (Some hints for retrieving the name from a Unix-alike OS are in the examples.)

Time zones did not come into use until the second half of the nineteenth century and were not widely adopted until the twentieth, and daylight saving time (DST, also known as summer time) was first introduced in the early twentieth century, most widely in 1916. Over the last 100 years places have changed their affiliation between major time zones, have opted out of (or in to) DST in various years or adopted rule changes late or not at all. The most common system implementation of POSIXct is as signed 32-bit integers and so only goes back to the end of 1901: on such systems R assumes that dates prior to that are in the same time zone as they were in 1902. Most of the world had not adopted time zones by 1902 but for some places there had been time-zone changes before 1902. 64-bit representations are becoming common but are still pretty unreliable.

See Also

Sys.time, as.POSIXlt.

http://en.wikipedia.org/wiki/Time_zone and http://www.twinsun.com/tz/tz-link.htm for extensive sets of links.

Examples

Run this code
Sys.timezone()

 windows
tzfile <- file.path(R.home("share"), "zoneinfo", "zone.tab")
tzones <- read.delim(tzfile, row.names = NULL, header = FALSE,
    col.names = c("country", "coords", "name", "comments"),
    as.is = TRUE, fill = TRUE, comment.char = "#")
str(tzones$name)
 unix
## need to find a suitable file path (if any) for your system
tzdirs <- c("/usr/share/zoneinfo", # Linux, OS X, FreeBSD
            "/usr/lib/zoneinfo",   # early glibc
            "/usr/share/lib/zoneinfo", # AIX?
            "/usr/local/etc/zoneinfo", # tzcode default
            "/etc/zoneinfo", "/usr/etc/zoneinfo")
tzfiles <- c(file.path(tzdirs, "zone.tab"),
            "/usr/share/lib/zoneinfo/tab/zone_sun.tab") # Solaris
if (any(have <- file.exists(tzfiles))) {
tzfile <- tzfiles[have][1]
tzones <- read.delim(tzfile, row.names = NULL, header = FALSE,
    col.names = c("country", "coords", "name", "comments"),
    as.is = TRUE, fill = TRUE, comment.char = "#")
str(tzones$name)
}

## Try to find the system's internal name for the current time zone.
if(nzchar(lt <- Sys.getenv("TZ"))) { # always set on Solaris, AIX
  print(lt)
} else {
  lt <- normalizePath("/etc/localtime") # Linux, OS X, ...
  if (grepl(pat <- "^/usr/share/zoneinfo/", lt))
     print(sub(pat, "", lt))
}

Run the code above in your browser using DataLab