xref: /netbsd-src/lib/libc/time/Theory (revision 5bd15d6468dac28a2ee1d6c6a8caea7766fab5a4)
1@(#)Theory	7.5
2
3
4----- Outline -----
5
6	Time and date functions
7	Names of time zone regions
8	Time zone abbreviations
9
10
11----- Time and date functions -----
12
13These time and date functions are upwards compatible with POSIX.1,
14an international standard for Unix-like systems.
15As of this writing, the current edition of POSIX.1 is:
16
17  Information technology --Portable Operating System Interface (POSIX (R))
18  -- Part 1: System Application Program Interface (API) [C Language]
19  ISO/IEC 9945-1:1996
20  ANSI/IEEE Std 1003.1, 1996 Edition
21  1996-07-12
22
23POSIX.1 has the following properties and limitations.
24
25*	In POSIX.1, time display in a process is controlled by the
26	environment variable TZ.  Unfortunately, the POSIX.1 TZ string takes
27	a form that is hard to describe and is error-prone in practice.
28	Also, POSIX.1 TZ strings can't deal with other (for example, Israeli)
29	daylight saving time rules, or situations where more than two
30	time zone abbreviations are used in an area.
31
32	The POSIX.1 TZ string takes the following form:
33
34		stdoffset[dst[offset],date[/time],date[/time]]
35
36	where:
37
38	std and dst
39		are 3 or more characters specifying the standard
40		and daylight saving time (DST) zone names.
41	offset
42		is of the form `[-]hh:[mm[:ss]]' and specifies the
43		offset west of UTC.  The default DST offset is one hour
44		ahead of standard time.
45	date[/time],date[/time]
46		specifies the beginning and end of DST.  If this is absent,
47		the system supplies its own rules for DST, and these can
48		differ from year to year; typically US DST rules are used.
49	time
50		takes the form `hh:[mm[:ss]]' and defaults to 02:00.
51	date
52		takes one of the following forms:
53		Jn (1<=n<=365)
54			origin-1 day number not counting February 29
55		n (0<=n<=365)
56			origin-0 day number counting February 29 if present
57		Mm.n.d (0[Sunday]<=d<=6[Saturday], 1<=n<=5, 1<=m<=12)
58			for the dth day of week n of month m of the year,
59			where week 1 is the first week in which day d appears,
60			and `5' stands for the last week in which day d appears
61			(which may be either the 4th or 5th week).
62
63*	In POSIX.1, when a TZ value like "EST5EDT" is parsed,
64	typically the current US DST rules are used,
65	but this means that the US DST rules are compiled into each program
66	that does time conversion.  This means that when US time conversion
67	rules change (as in the United States in 1987), all programs that
68	do time conversion must be recompiled to ensure proper results.
69
70*	In POSIX.1, there's no tamper-proof way for a process to learn the
71	system's best idea of local wall clock.  (This is important for
72	applications that an administrator wants used only at certain times--
73	without regard to whether the user has fiddled the "TZ" environment
74	variable.  While an administrator can "do everything in GMT" to get
75	around the problem, doing so is inconvenient and precludes handling
76	daylight saving time shifts--as might be required to limit phone
77	calls to off-peak hours.)
78
79*	POSIX.1 requires that systems ignore leap seconds.
80
81These are the extensions that have been made to the POSIX.1 functions:
82
83*	The "TZ" environment variable is used in generating the name of a file
84	from which time zone information is read (or is interpreted a la
85	POSIX); "TZ" is no longer constrained to be a three-letter time zone
86	name followed by a number of hours and an optional three-letter
87	daylight time zone name.  The daylight saving time rules to be used
88	for a particular time zone are encoded in the time zone file;
89	the format of the file allows U.S., Australian, and other rules to be
90	encoded, and allows for situations where more than two time zone
91	abbreviations are used.
92
93	It was recognized that allowing the "TZ" environment variable to
94	take on values such as "America/New_York" might cause "old" programs
95	(that expect "TZ" to have a certain form) to operate incorrectly;
96	consideration was given to using some other environment variable
97	(for example, "TIMEZONE") to hold the string used to generate the
98	time zone information file name.  In the end, however, it was decided
99	to continue using "TZ":  it is widely used for time zone purposes;
100	separately maintaining both "TZ" and "TIMEZONE" seemed a nuisance;
101	and systems where "new" forms of "TZ" might cause problems can simply
102	use TZ values such as "EST5EDT" which can be used both by
103	"new" programs (a la POSIX) and "old" programs (as zone names and
104	offsets).
105
106*	To handle places where more than two time zone abbreviations are used,
107	the functions "localtime" and "gmtime" set tzname[tmp->tm_isdst]
108	(where "tmp" is the value the function returns) to the time zone
109	abbreviation to be used.  This differs from POSIX.1, where the elements
110	of tzname are only changed as a result of calls to tzset.
111
112*	Since the "TZ" environment variable can now be used to control time
113	conversion, the "daylight" and "timezone" variables are no longer
114	needed.  (These variables are defined and set by "tzset"; however, their
115	values will not be used by "localtime.")
116
117*	The "localtime" function has been set up to deliver correct results
118	for near-minimum or near-maximum time_t values.  (A comment in the
119	source code tells how to get compatibly wrong results).
120
121*	A function "tzsetwall" has been added to arrange for the system's
122	best approximation to local wall clock time to be delivered by
123	subsequent calls to "localtime."  Source code for portable
124	applications that "must" run on local wall clock time should call
125	"tzsetwall();" if such code is moved to "old" systems that don't
126	provide tzsetwall, you won't be able to generate an executable program.
127	(These time zone functions also arrange for local wall clock time to be
128	used if tzset is called--directly or indirectly--and there's no "TZ"
129	environment variable; portable applications should not, however, rely
130	on this behavior since it's not the way SVR2 systems behave.)
131
132*	These functions can account for leap seconds, thanks to Bradley White
133	(bww@k.cs.cmu.edu).
134
135Points of interest to folks with other systems:
136
137*	This package is already part of many POSIX-compliant hosts,
138	including BSD, HP, Linux, Network Appliance, SCO, SGI, and Sun.
139	On such hosts, the primary use of this package
140	is to update obsolete time zone rule tables.
141	To do this, you may need to compile the time zone compiler
142	`zic' supplied with this package instead of using the system `zic',
143	since the format of zic's input changed slightly in late 1994,
144	and many vendors still do not support the new input format.
145
146*	The Unix Version 7 "timezone" function is not present in this package;
147	it's impossible to reliably map timezone's arguments (a "minutes west
148	of GMT" value and a "daylight saving time in effect" flag) to a
149	time zone abbreviation, and we refuse to guess.
150	Programs that in the past used the timezone function may now examine
151	tzname[localtime(&clock)->tm_isdst] to learn the correct time
152	zone abbreviation to use.  Alternatively, use
153	localtime(&clock)->tm_zone if this has been enabled.
154
155*	The 4.2BSD gettimeofday function is not used in this package.
156	This formerly let users obtain the current UTC offset and DST flag,
157	but this functionality was removed in later versions of BSD.
158
159*	In SVR2, time conversion fails for near-minimum or near-maximum
160	time_t values when doing conversions for places that don't use GMT.
161	This package takes care to do these conversions correctly.
162
163The functions that are conditionally compiled if STD_INSPIRED is defined
164should, at this point, be looked on primarily as food for thought.  They are
165not in any sense "standard compatible"--some are not, in fact, specified in
166*any* standard.  They do, however, represent responses of various authors to
167standardization proposals.
168
169Other time conversion proposals, in particular the one developed by folks at
170Hewlett Packard, offer a wider selection of functions that provide capabilities
171beyond those provided here.  The absence of such functions from this package
172is not meant to discourage the development, standardization, or use of such
173functions.  Rather, their absence reflects the decision to make this package
174contain valid extensions to POSIX.1, to ensure its broad
175acceptability.  If more powerful time conversion functions can be standardized,
176so much the better.
177
178
179----- Names of time zone rule files -----
180
181The names of this package's installed time zone rule files are chosen to
182help minimize possible future incompatibilities due to political events.
183Ordinarily, names of countries are not used, to avoid incompatibilities
184when countries change their name (e.g. Zaire->Congo) or
185when locations change countries (e.g. Hong Kong from UK colony to China).
186
187Names normally have the form AREA/LOCATION, where AREA is the name
188of a continent or ocean, and LOCATION is the name of a specific
189location within that region.  North and South America share the same
190area, `America'.  Typical names are `Africa/Cairo', `America/New_York',
191and `Pacific/Honolulu'.
192
193Here are the general rules used for choosing location names,
194in decreasing order of importance:
195
196	Use only valid Posix file names.  Use only Ascii letters, digits, `.',
197		`-' and `_'.  Do not exceed 14 characters or start with `-'.
198		E.g. prefer `Brunei' to `Bandar_Seri_Begawan'.
199	Include at least one location per time zone rule set per country.
200		One such location is enough.
201	If all the clocks in a country's region have agreed since 1970,
202		don't bother to include more than one location
203		even if subregions' clocks disagreed before 1970.
204		Otherwise these tables would become annoyingly large.
205	If a name is ambiguous, use a less ambiguous alternative;
206		e.g. many cities are named San Jose and Georgetown, so
207		prefer `Costa_Rica' to `San_Jose' and `Guyana' to `Georgetown'.
208	Keep locations compact.  Use cities or small islands, not countries
209		or regions, so that any future time zone changes do not split
210		locations into different time zones.  E.g. prefer `Paris'
211		to `France', since France has had multiple time zones.
212	Use traditional English spelling, e.g. prefer `Rome' to `Roma', and
213		prefer `Athens' to the true name (which uses Greek letters).
214		The Posix file name restrictions encourage this rule.
215	Use the most populous among locations in a country's time zone,
216		e.g. prefer `Shanghai' to `Beijing'.  Among locations with
217		similar populations, pick the best-known location,
218		e.g. prefer `Rome' to `Milan'.
219	Use the singular form, e.g. prefer `Canary' to `Canaries'.
220	Omit common suffixes like `_Islands' and `_City', unless that
221		would lead to ambiguity.  E.g. prefer `Cayman' to
222		`Cayman_Islands' and `Guatemala' to `Guatemala_City',
223		but prefer `Mexico_City' to `Mexico' because the country
224		of Mexico has several time zones.
225	Use `_' to represent a space.
226	Omit `.' from abbreviations in names, e.g. prefer `St_Helena'
227		to `St._Helena'.
228
229The file `zone.tab' lists the geographical locations used to name
230time zone rule files.
231
232Older versions of this package used a different naming scheme,
233and these older names are still supported.
234See the file `backwards' for most of these older names
235(e.g. `US/Eastern' instead of `America/New_York').
236The other old-fashioned names still supported are
237`WET', `CET', `MET', `EET' (see the file `europe'),
238and `Factory' (see the file `factory').
239
240
241----- Time zone abbreviations -----
242
243When this package is installed, it generates time zone abbreviations
244like `EST' to be compatible with human tradition and POSIX.1.
245Here are the general rules used for choosing time zone abbreviations,
246in decreasing order of importance:
247
248	Use abbreviations that consist of 3 or more upper-case Ascii letters,
249		except use "___" for locations while uninhabited.
250		Posix.1 requires at least 3 characters, and the restriction to
251		upper-case Ascii letters follows most traditions.
252		Previous editions of this database also used characters like
253		' ' and '?', but these characters have a special meaning to
254		the shell and cause commands like
255			set `date`
256		to have unexpected effects.  In theory, the character set could
257		be !%./@A-Z^_a-z{}, but these tables use only upper-case
258		Ascii letters (and "___").
259	Use abbreviations that are in common use among English-speakers,
260		e.g. `EST' for Eastern Standard Time in North America.
261		We assume that applications translate them to other languages
262		as part of the normal localization process; for example,
263		a French application might translate `EST' to `HNE'.
264	For zones whose times are taken from a city's longitude, use the
265		traditional xMT notation, e.g. `PMT' for Paris Mean Time.
266		The only name like this in current use is `GMT'.
267	If there is no common English abbreviation, abbreviate the English
268		translation of the usual phrase used by native speakers.
269		If this is not available or is a phrase mentioning the country
270		(e.g. ``Cape Verde Time''), then:
271
272		When a country has a single or principal time zone region,
273			append `T' to the country's ISO	code, e.g. `CVT' for
274			Cape Verde Time.  For summer time append `ST';
275			for double summer time append `DST'; etc.
276		When a country has multiple time zones, take the first three
277			letters of an English place name identifying each zone
278			and then append `T', `ST', etc. as before;
279			e.g. `VLAST' for VLAdivostok Summer Time.
280
281Application writers should note that these abbreviations are ambiguous
282in practice: e.g. `EST' has a different meaning in Australia than
283it does in the United States.  In new applications, it's often better
284to use numeric GMT offsets like `-0500' instead of time zone
285abbreviations like `EST'; this avoids the ambiguity.
286