Rev 86182 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/timezones.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2024 R Core Team% Distributed under GPL 2 or later\name{timezones}\alias{Sys.timezone}\alias{OlsonNames}\alias{timezone}\alias{timezones}\alias{time zone}\alias{time zones}\alias{TZ}\alias{TZDIR}\alias{.sys.timezone}\title{Time Zones}\description{Information about time zones in \R. \code{Sys.timezone} returnsthe name of the current time zone.}\usage{Sys.timezone(location = TRUE)OlsonNames(tzdir = NULL)}\arguments{\item{location}{logical. Defunct, with a warning if \code{FALSE}.}\item{tzdir}{the time-zone database to be used: the default is to tryknown locations until one is found.}}\details{Time zones are a system-specific topic, but these days almost all \Rplatforms use similar underlying code, used by Linux, macOS, Solaris,\I{AIX} and FreeBSD, and installed with \R on Windows. (Unfortunatelythere are many system-specific errors in the implementations.) It ispossible to use the \R sources' version of the code on Unix-alikes aswell as on Windows: this is the default on macOS.It should be possible to set the current time zone via the environmentvariable \env{TZ}: see the section on \sQuote{Time zone names} forsuitable values. \code{Sys.timezone()} will return the value of\env{TZ} if set initially (and on some OSes it is always set),otherwise it will try to retrieve from the OS a value which if set for\env{TZ} would give the initial time zone. (\sQuote{Initially} meansbefore any time-zone functions are used: if \env{TZ} is being set tooverride the OS setting or if the \sQuote{try} does not get thisright, it should be set before the \R process is started or (probablyearly enough) in file \code{\link{.Rprofile}}).%% glibc silently uses UTC but uses the invalid name as the%% abbreviations. tzcode as used by R warns and uses UTC.If \env{TZ} is set but invalid, most platforms default to \samp{UTC},the time zone colloquially known as \samp{GMT} (see\url{https://en.wikipedia.org/wiki/Coordinated_Universal_Time}).(Some but not all platforms will give a warning for invalid values.)If it is unset or empty the \emph{system} time zone is used (the onereturned by \code{Sys.timezone}).%% arguably, 'railway time' in the UK in 1840 was earliest.Time zones did not come into use until the middle of the nineteenthcentury and were not widely adopted until the twentieth, and\emph{daylight saving time} (DST, also known as \emph{summer time})was first introduced in the early twentieth century, most widely in1916. Over the last 100 years places have changed their affiliationbetween major time zones, have opted out of (or in to) DST in variousyears or adopted DST rule changes late or not at all. (For example,the UK experimented with DST throughout 1971, only.) In a fewcountries (one is the Irish Republic) it is the summer time which isthe \sQuote{standard} time and a different name is used in winter.And there can be multiple changes during a year, for example forRamadan.A quite common system implementation of \code{POSIXct} was as signed32-bit integers and so only went back to the end of 1901: on suchsystems \R assumes that dates prior to that are in the same time zoneas they were in 1902. Most of the world had not adopted time zones by1902 (so used local \sQuote{mean time} based on longitude) but for afew places there had been time-zone changes before then. 64-bitrepresentations are becoming by far the most common; unfortunately onsome 64-bit OSes the database information is 32-bit and so onlyavailable for the range 1901--2038, and incompletely for the endyears.When a time zone location is first found in a session its value iscached in object \code{.sys.timezone} in the base environment.}\value{\code{Sys.timezone} returns an OS-specific character string, possibly\code{NA} or an empty string (which on some OSes means \samp{UTC}).This will be a location such as \code{"Europe/London"} if one can beascertained.A time zone region may be known by several names: for example\samp{"Europe/London"} may also be known as \samp{GB}, \samp{GB-Eire},\samp{Europe/Belfast}, \samp{Europe/Guernsey},\samp{Europe/Isle_of_Man} and \samp{Europe/Jersey}. A few regions arealso known by a summary of their time zone,e.g.\sspace{}\samp{PST8PDT} is (on most but not all systems) an aliasfor \samp{America/Los_Angeles}.\code{OlsonNames} returns a character vector, see the examples fortypical cases. It may have an attribute \code{"Version"}, somethinglike \samp{"2023a"}. (It does on systems using\option{--with-internal-tzcode} and those like Fedora distributingfile \file{tzdata.zi}.)}\section{Time zone names}{Names \code{"UTC"} and its synonym \code{"GMT"} are accepted on allplatforms.Where OSes describe their valid time zones can be obscure. The helpfor the C function \code{tzset} can be helpful, but it can also beinaccurate. There is a cumbersome POSIX specification (listed underenvironment variable \env{TZ} at\url{https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08}),which is often at least partially supported, but there are other moreuser-friendly ways to specify time zones.Almost all \R platforms make use of a time-zone database originallycompiled by Arthur David Olson and now managed by \abbr{IANA}, in which thepreferred way to refer to a time zone is by a location (typically of acity), e.g., \code{Europe/London}, \code{America/Los_Angeles},\code{Pacific/Easter} within a \sQuote{time zone region}. Sometraditional designations are also allowed such as \code{EST5EDT} or\code{GB}. (Beware that some of these designations may not be whatyou expect: in particular \code{EST} is a time zone used in Canada\emph{without} daylight saving time, and not \code{EST5EDT} nor(Australian) Eastern Standard Time.) The designation can also be anoptional colon prepended to the path to a file giving complied zoneinformation (and the examples above are all files in a system-specificlocation). See \url{https://data.iana.org/time-zones/tz-link.html}for more details and references. By convention, regions with a uniquetime-zone history since 1970 have specific names in the database, butthose with different earlier histories may not. Each time zone hasone or two (the second for \sQuote{summer}) \emph{abbreviations} used whenformatting times.%% Debian/Ubuntu: https://mm.icann.org/pipermail/tz/2024-March/058812.htmlIncreasingly OSes are (optionally or always) not including\sQuote{legacy} names such as \code{US/Eastern}: only names of theforms \code{Continent/City} and \code{Etc/...} are fully portable.The abbreviations used have changed over the years: for example Franceused \samp{PMT} (\sQuote{Paris Mean Time}) from 1891 to 1911 then\samp{WET/WEST} up to 1940 and \samp{CET/CEST} from 1946. (In almostall time zones the abbreviations have been stable since 1970.) ThePOSIX standard allows only one or two abbreviations per time zone, soyou may see the current abbreviation(s) used for older times.For some time zones abbreviations are like \samp{-03} and\samp{+0845}: this is done when there is no official abbreviation.(Negative values are behind (West of) UTC, as for the \code{"\%z"}format for \code{\link{strftime}}.)The function \code{OlsonNames} returns the time-zone names known tothe currently selected Olson/\abbr{IANA} database. The system-specificlocation in the file system varies,e.g.\sspace{}\file{/usr/share/zoneinfo} (Linux, macOS, FreeBSD),\file{/usr/share/lib/zoneinfo} (Solaris, \I{AIX}), \ldots. It is likelythat there is a file named something like \file{zone1970.tab} or(older) \file{zone.tab} under that directory listing the locationsknown as time-zone names (but not for example \code{EST5EDT}). Seealso \url{https://en.wikipedia.org/wiki/Zone.tab}.Where \R was configured with option \option{--with-internal-tzcode}(the default on Windows), the database at\code{file.path(R.home("share"), "zoneinfo")} is used by default: file\file{VERSION} in that directory states the version. That option isalso the default on macOS but there whichever is more recent of thesystem database at \file{/var/db/timezone/zoneinfo} and thatdistributed with \R is used by default. Environment variable\env{TZDIR} can be used to give the full path to a different\file{zoneinfo} database: value \code{"internal"} indicates thedatabase from the \R sources and \code{"macOS"} indicates the systemdatabase. (Setting either of those values would not be recognized byother software using \env{TZDIR}.)Setting \env{TZDIR} is also supported by the native services on someOSes, e.g.\sspace{}Linux using \code{glibc} except in secure modes.%% But not Linux with muslTime zones given by name (\emph{via} environment variable \env{TZ}, in\code{tz} arguments to functions such as \code{\link{as.POSIXlt}} andperhaps the system time zone) are loaded from the currently selected\file{zoneinfo} database.\emph{On Windows only:}An attempt is made (once only per session) to map Windows' idea of thecurrent time zone to a location, following a version of\url{http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml}with additional values deduced from the Windows Registry and documentation.It can be overridden by setting the \env{TZ} environment variablebefore any date-times are used in the session.Most platforms support time zones of the form \samp{Etc/GMT+n} and\samp{Etc/GMT-n} (possibly also without prefix \samp{Etc/}), whichassume a fixed offset from UTC (hence no DST). Contrary to someexpectations (but consistent with names such as \samp{PST8PDT}),negative offsets are times ahead of (East of) UTC, positive offsetsare times behind (West of) UTC.Immediately prior to the advent of legislated time zones, most peopleused time based on their longitude (or that of a nearby town), knownas \sQuote{Local Mean Time} and abbreviated as \samp{LMT} in thedatabases: in many countries that was codified with a specific namebefore the switch to a standard time. For example, Paris codified its\abbr{LMT} as \sQuote{Paris Mean Time} in 1891 (to be used throughoutmainland France) and switched to \samp{GMT+0} in 1911.%% it is a ksh script so could well pop up elsewhere.Some systems (notably Linux) have a \command{tzselect} command whichallows the interactive selection of a supported time zone name. Onsystems using \command{systemd} (notably Linux), the OS command\command{timedatectl list-timezones} will list all available time zonenames.}\section{Warnings}{%% glibc and macOS have _POSIX_TZNAME_MAX and define it as 6.%% Earlier versions of R's code assumed 10, and it was discovered%% that some implementations did not abbreviate unusual names, thereby%% exceeding this.%% Olson's tzcode has a limit of 255 and does not check: this has been%% corrected in R's copy.%% sysconf(_SC_TZNAME_MAX) might allow it to be checked:%% that gives 27 on macOS. However, seems it is dynamic on glibc.There is a system-specific upper limit on the number of bytes in(abbreviated) time-zone names which can be as low as 6 (as required byPOSIX). Some OSes allow the setting of time zones with names whichexceed their limit, and that can crash the \R session.Information about future times is speculative (\sQuote{proleptic}):the database provides the best-known information based on currentrules set by civil authorities. For the period 1900--1970 those rules(and which of any authority's rules were enacted) are often obscure,and the databases do get corrected frequently.\code{OlsonNames} tries to find an Olson database in known locations.It might not succeed (when it returns an empty vector with a warning)and even if it does it might not locate the database used by thedate-time code linked into \R. Fortunately names are added rarelyand most databases are pretty complete. On the other hand, many nameswhich duplicate other named timezones have been moved to the\file{backward} list -- these are regarded as optional and omitted onminimal installations. Similarly, there are timezones named in file\file{backzone} which differ only from those in the main lists priorto 1970 -- these are usually included but may not be in minimalistsystems.%% https://mm.icann.org/pipermail/tz/2024-March/025162.html%% https://mm.icann.org/pipermail/tz/2024-March/025166.htmlFor many years, the legacy names \code{EST5EDT} and \code{PST8PDT}were portable, but \code{musl} (the C runtime used by Alpine Linux)does not use DST with those names.}\note{Since 2007 there has been considerable disruption over changes to thetimings of the DST transitions; these often have short notice andtime-zone databases may not be up to date. (Morocco in 2013 announceda change to the end of DST at \emph{a day's} notice. In2023 there was chaos in Lebanon as the authorities changed their mindsrepeatedly and some changes were not widely implemented.)There have also been changes to the \sQuote{standard} time with littlenotice (Kazakhstan switched to a single time zone in Mar 2024 with sixweeks' notice), and to whether \sQuote{summer} or \sQuote{winter}time is regarded as \sQuote{standard} (and hence to abbreviations).%% Irish Standard time is used in summer.On platforms with case-insensitive file systems, time zone names will becase-insensitive. They may or may not be on other platforms and so,for example, \code{"gmt"} is valid on some platforms and not on others.Note that except where replaced, the operation of time zones is an OSservice, and even where replaced a third-party database is used andcan be updated (see the section on \sQuote{Time zone names}).Incorrect results will never be an \R issue, so please ensure that youhave the courtesy not to blame \R for them.}\section{How the system time zone is found -- on Unix-alikes}{This section is of background interest for users of a Unix-alike, butmay help if an \code{NA} value is returned unexpectedly.Commercial Unixen such as Solaris and \I{AIX} set \env{TZ}, so the valuewhen \R is started is used.All other common platforms (Linux, macOS, *BSD) use similar schemes,either derived from \code{tzcode} (currently distributed from\url{https://www.iana.org/time-zones}) or independently coded(\code{glibc}, \code{musl-libc}). Such systems read the time-zoneinformation from a file \file{localtime}, usually under \file{/etc}(but possibly under \file{/usr/local/etc} or\file{/usr/local/etc/zoneinfo}). As the usual Linux manual page for\code{localtime} says\sQuote{Because the time zone identifier is extracted from the symlinktarget name of \file{/etc/localtime}, this file may not be a normalfile or hardlink.}Nevertheless, some Linux distributions (including the one from whichthat quote was taken) or sysadmins have chosen to copy a time-zone fileto \file{localtime}. For a non-symlink, the ultimate fallback is tocompare that file to all files in the time-zone database.Some Linux platforms provide two other mechanisms which are tried inturn before looking at \file{/etc/localtime}.\itemize{\item \sQuote{Modern} Linux systems use \code{systemd} whichprovides mechanisms to set and retrieve the time zone (amongst otherthings). There is a command \command{timedatectl} to give details.(Unfortunately \I{RHEL}/\I{Centos} 6.x were not \sQuote{modern}.)\item Debian-derived systems since \emph{ca} 2007 have supplied afile \file{/etc/timezone}. Its format is undocumented butempirically it contains a single line of text naming the time zone.}In each case a sanity check is performed that the time-zone name is thename of a file in the time-zone database. (The systems probably usethe time-zone file (symlinked to) \file{/etc/localtime}, but the\code{Sys.timezone} code does not check that is the same as the namedfile in the database. This is deliberate as they may be fromdifferent dates.)}\seealso{\code{\link{Sys.time}}, \code{\link{as.POSIXlt}}.\url{https://en.wikipedia.org/wiki/Time_zone} and\url{https://data.iana.org/time-zones/tz-link.html}for extensive sets of links.\url{https://data.iana.org/time-zones/theory.html} for the\sQuote{rules} of the Olson/\abbr{IANA} database.}\examples{Sys.timezone()str(OlsonNames()) ## typically around six hundred names,## typically some acronyms/aliases such as "UTC", "NZ", "MET", "Eire", ..., but## mostly pairs (and triplets) such as "Pacific/Auckland"table(sl <- grepl("/", OlsonNames()))OlsonNames()[ !sl ] # the simple oneshead(Osl <- strsplit(OlsonNames()[sl], "/"))(tOS1 <- table(vapply(Osl, `[[`, "", 1))) # Continents, countries, ...table(lengths(Osl))# most are pairs, some tripletsstr(Osl[lengths(Osl) >= 3])# "America" South and North ...}\keyword{utilities}\keyword{chron}