Next: Time Zone Functions, Previous: Parsing Date and Time, Up: Calendar Time

#### 21.4.7 Specifying the Time Zone with `TZ`

In POSIX systems, a user can specify the time zone by means of the `TZ` environment variable. For information about how to set environment variables, see Environment Variables. The functions for accessing the time zone are declared in `time.h`. You should not normally need to set `TZ`. If the system is configured properly, the default time zone will be correct. You might set `TZ` if you are using a computer over a network from a different time zone, and would like times reported to you in the time zone local to you, rather than what is local to the computer.

In POSIX.1 systems the value of the `TZ` variable can be in one of three formats. With the GNU C Library, the most common format is the last one, which can specify a selection from a large database of time zone information for many regions of the world. The first two formats are used to describe the time zone information directly, which is both more cumbersome and less precise. But the POSIX.1 standard only specifies the details of the first two formats, so it is good to be familiar with them in case you come across a POSIX.1 system that doesn't support a time zone information database.

The first format is used when there is no Daylight Saving Time (or summer time) in the local time zone:

         std offset

The std string specifies the name of the time zone. It must be three or more characters long and must not contain a leading colon, embedded digits, commas, nor plus and minus signs. There is no space character separating the time zone name from the offset, so these restrictions are necessary to parse the specification correctly.

The offset specifies the time value you must add to the local time to get a Coordinated Universal Time value. It has syntax like [`+`|`-`]hh[`:`mm[`:`ss]]. This is positive if the local time zone is west of the Prime Meridian and negative if it is east. The hour must be between `0` and `23`, and the minute and seconds between `0` and `59`.

For example, here is how we would specify Eastern Standard Time, but without any Daylight Saving Time alternative:

         EST+5

The second format is used when there is Daylight Saving Time:

         std offset dst [offset],start[/time],end[/time]

The initial std and offset specify the standard time zone, as described above. The dst string and offset specify the name and offset for the corresponding Daylight Saving Time zone; if the offset is omitted, it defaults to one hour ahead of standard time.

The remainder of the specification describes when Daylight Saving Time is in effect. The start field is when Daylight Saving Time goes into effect and the end field is when the change is made back to standard time. The following formats are recognized for these fields:

`J` n
  This specifies the Julian day, with n between `1` and `365`. February 29 is never counted, even in leap years.
n
  This specifies the Julian day, with n between `0` and `365`. February 29 is counted in leap years.
`M` m`.`w`.`d
  This specifies day d of week w of month m. The day d must be between `0` (Sunday) and `6`. The week w must be between `1` and `5`; week `1` is the first week in which day d occurs, and week `5` specifies the last d day in the month. The month m should be between `1` and `12`.

The time fields specify when, in the local time currently in effect, the change to the other time occurs. If omitted, the default is `02:00:00`.

For example, here is how you would specify the Eastern time zone in the United States, including the appropriate Daylight Saving Time and its dates of applicability. The normal offset from UTC is 5 hours; since this is west of the prime meridian, the sign is positive. Summer time begins on the first Sunday in April at 2:00am, and ends on the last Sunday in October at 2:00am.

         EST+5EDT,M4.1.0/2,M10.5.0/2

The schedule of Daylight Saving Time in any particular jurisdiction has changed over the years. To be strictly correct, the conversion of dates and times in the past should be based on the schedule that was in effect then. However, this format has no facilities to let you specify how the schedule has changed from year to year. The most you can do is specify one particular schedule—usually the present day schedule—and this is used to convert any date, no matter when. For precise time zone specifications, it is best to use the time zone information database (see below).

The third format looks like this:

         :characters

Each operating system interprets this format differently; in the GNU C Library, characters is the name of a file which describes the time zone.

If the `TZ` environment variable does not have a value, the operation chooses a time zone by default. In the GNU C Library, the default time zone is like the specification ‘`TZ=:/etc/localtime`’ (or ‘`TZ=:/usr/local/etc/localtime`’, depending on how the GNU C Library was configured; see Installation). Other C libraries use their own rule for choosing the default time zone, so there is little we can say about them.

If characters begins with a slash, it is an absolute file name; otherwise the library looks for the file `/share/lib/zoneinfo/characters`. The `zoneinfo` directory contains data files describing local time zones in many different parts of the world. The names represent major cities, with subdirectories for geographical areas; for example, `America/New_York`, `Europe/London`, `Asia/Hong_Kong`. These data files are installed by the system administrator, who also sets `/etc/localtime` to point to the data file for the local time zone. The GNU C Library comes with a large database of time zone information for most regions of the world, which is maintained by a community of volunteers and put in the public domain.