Theory revision 1.2
11.2Sperry#	$NetBSD: Theory,v 1.2 1998/01/09 04:11:55 perry Exp $
21.2Sperryfrom: @(#)Theory	7.5
31.1Sjtc
41.2Sperry
51.2Sperry----- Outline -----
61.2Sperry
71.2Sperry	Time and date functions
81.2Sperry	Names of time zone regions
91.2Sperry	Time zone abbreviations
101.2Sperry
111.2Sperry
121.2Sperry----- Time and date functions -----
131.2Sperry
141.2SperryThese time and date functions are upwards compatible with POSIX.1,
151.2Sperryan international standard for Unix-like systems.
161.2SperryAs of this writing, the current edition of POSIX.1 is:
171.2Sperry
181.2Sperry  Information technology --Portable Operating System Interface (POSIX (R))
191.2Sperry  -- Part 1: System Application Program Interface (API) [C Language]
201.2Sperry  ISO/IEC 9945-1:1996
211.2Sperry  ANSI/IEEE Std 1003.1, 1996 Edition
221.2Sperry  1996-07-12
231.2Sperry
241.2SperryPOSIX.1 has the following properties and limitations.
251.2Sperry
261.2Sperry*	In POSIX.1, time display in a process is controlled by the
271.2Sperry	environment variable TZ.  Unfortunately, the POSIX.1 TZ string takes
281.2Sperry	a form that is hard to describe and is error-prone in practice.
291.2Sperry	Also, POSIX.1 TZ strings can't deal with other (for example, Israeli)
301.2Sperry	daylight saving time rules, or situations where more than two
311.1Sjtc	time zone abbreviations are used in an area.
321.1Sjtc
331.2Sperry	The POSIX.1 TZ string takes the following form:
341.2Sperry
351.2Sperry		stdoffset[dst[offset],date[/time],date[/time]]
361.2Sperry
371.2Sperry	where:
381.2Sperry	
391.2Sperry	std and dst
401.2Sperry		are 3 or more characters specifying the standard
411.2Sperry		and daylight saving time (DST) zone names.
421.2Sperry	offset
431.2Sperry		is of the form `[-]hh:[mm[:ss]]' and specifies the
441.2Sperry		offset west of UTC.  The default DST offset is one hour
451.2Sperry		ahead of standard time.
461.2Sperry	date[/time],date[/time]
471.2Sperry		specifies the beginning and end of DST.  If this is absent,
481.2Sperry		the system supplies its own rules for DST, and these can
491.2Sperry		differ from year to year; typically US DST rules are used.
501.2Sperry	time
511.2Sperry		takes the form `hh:[mm[:ss]]' and defaults to 02:00.
521.2Sperry	date
531.2Sperry		takes one of the following forms:
541.2Sperry		Jn (1<=n<=365)
551.2Sperry			origin-1 day number not counting February 29
561.2Sperry		n (0<=n<=365)
571.2Sperry			origin-0 day number counting February 29 if present
581.2Sperry		Mm.n.d (0[Sunday]<=d<=6[Saturday], 1<=n<=5, 1<=m<=12)
591.2Sperry			for the dth day of week n of month m of the year,
601.2Sperry			where week 1 is the first week in which day d appears,
611.2Sperry			and `5' stands for the last week in which day d appears
621.2Sperry			(which may be either the 4th or 5th week).
631.2Sperry
641.2Sperry*	In POSIX.1, when a TZ value like "EST5EDT" is parsed,
651.2Sperry	typically the current US DST rules are used,
661.2Sperry	but this means that the US DST rules are compiled into each program
671.2Sperry	that does time conversion.  This means that when US time conversion
681.1Sjtc	rules change (as in the United States in 1987), all programs that
691.1Sjtc	do time conversion must be recompiled to ensure proper results.
701.1Sjtc
711.2Sperry*	In POSIX.1, there's no tamper-proof way for a process to learn the
721.1Sjtc	system's best idea of local wall clock.  (This is important for
731.1Sjtc	applications that an administrator wants used only at certain times--
741.1Sjtc	without regard to whether the user has fiddled the "TZ" environment
751.1Sjtc	variable.  While an administrator can "do everything in GMT" to get
761.1Sjtc	around the problem, doing so is inconvenient and precludes handling
771.2Sperry	daylight saving time shifts--as might be required to limit phone
781.1Sjtc	calls to off-peak hours.)
791.1Sjtc
801.2Sperry*	POSIX.1 requires that systems ignore leap seconds.
811.1Sjtc
821.2SperryThese are the extensions that have been made to the POSIX.1 functions:
831.1Sjtc
841.1Sjtc*	The "TZ" environment variable is used in generating the name of a file
851.1Sjtc	from which time zone information is read (or is interpreted a la
861.1Sjtc	POSIX); "TZ" is no longer constrained to be a three-letter time zone
871.1Sjtc	name followed by a number of hours and an optional three-letter
881.1Sjtc	daylight time zone name.  The daylight saving time rules to be used
891.1Sjtc	for a particular time zone are encoded in the time zone file;
901.1Sjtc	the format of the file allows U.S., Australian, and other rules to be
911.1Sjtc	encoded, and allows for situations where more than two time zone
921.1Sjtc	abbreviations are used.
931.1Sjtc
941.1Sjtc	It was recognized that allowing the "TZ" environment variable to
951.2Sperry	take on values such as "America/New_York" might cause "old" programs
961.1Sjtc	(that expect "TZ" to have a certain form) to operate incorrectly;
971.1Sjtc	consideration was given to using some other environment variable
981.1Sjtc	(for example, "TIMEZONE") to hold the string used to generate the
991.1Sjtc	time zone information file name.  In the end, however, it was decided
1001.1Sjtc	to continue using "TZ":  it is widely used for time zone purposes;
1011.1Sjtc	separately maintaining both "TZ" and "TIMEZONE" seemed a nuisance;
1021.1Sjtc	and systems where "new" forms of "TZ" might cause problems can simply
1031.1Sjtc	use TZ values such as "EST5EDT" which can be used both by
1041.1Sjtc	"new" programs (a la POSIX) and "old" programs (as zone names and
1051.1Sjtc	offsets).
1061.1Sjtc
1071.1Sjtc*	To handle places where more than two time zone abbreviations are used,
1081.1Sjtc	the functions "localtime" and "gmtime" set tzname[tmp->tm_isdst]
1091.1Sjtc	(where "tmp" is the value the function returns) to the time zone
1101.2Sperry	abbreviation to be used.  This differs from POSIX.1, where the elements
1111.1Sjtc	of tzname are only changed as a result of calls to tzset.
1121.1Sjtc
1131.1Sjtc*	Since the "TZ" environment variable can now be used to control time
1141.1Sjtc	conversion, the "daylight" and "timezone" variables are no longer
1151.2Sperry	needed.  (These variables are defined and set by "tzset"; however, their
1161.1Sjtc	values will not be used by "localtime.")
1171.1Sjtc
1181.1Sjtc*	The "localtime" function has been set up to deliver correct results
1191.1Sjtc	for near-minimum or near-maximum time_t values.  (A comment in the
1201.1Sjtc	source code tells how to get compatibly wrong results).
1211.1Sjtc
1221.1Sjtc*	A function "tzsetwall" has been added to arrange for the system's
1231.1Sjtc	best approximation to local wall clock time to be delivered by
1241.1Sjtc	subsequent calls to "localtime."  Source code for portable
1251.1Sjtc	applications that "must" run on local wall clock time should call
1261.2Sperry	"tzsetwall();" if such code is moved to "old" systems that don't
1271.2Sperry	provide tzsetwall, you won't be able to generate an executable program.
1281.1Sjtc	(These time zone functions also arrange for local wall clock time to be
1291.1Sjtc	used if tzset is called--directly or indirectly--and there's no "TZ"
1301.1Sjtc	environment variable; portable applications should not, however, rely
1311.1Sjtc	on this behavior since it's not the way SVR2 systems behave.)
1321.1Sjtc
1331.2Sperry*	These functions can account for leap seconds, thanks to Bradley White
1341.2Sperry	(bww@k.cs.cmu.edu).
1351.1Sjtc
1361.2SperryPoints of interest to folks with other systems:
1371.2Sperry
1381.2Sperry*	This package is already part of many POSIX-compliant hosts,
1391.2Sperry	including BSD, HP, Linux, Network Appliance, SCO, SGI, and Sun.
1401.2Sperry	On such hosts, the primary use of this package
1411.2Sperry	is to update obsolete time zone rule tables.
1421.2Sperry	To do this, you may need to compile the time zone compiler
1431.2Sperry	`zic' supplied with this package instead of using the system `zic',
1441.2Sperry	since the format of zic's input changed slightly in late 1994,
1451.2Sperry	and many vendors still do not support the new input format.
1461.2Sperry
1471.2Sperry*	The Unix Version 7 "timezone" function is not present in this package;
1481.1Sjtc	it's impossible to reliably map timezone's arguments (a "minutes west
1491.1Sjtc	of GMT" value and a "daylight saving time in effect" flag) to a
1501.1Sjtc	time zone abbreviation, and we refuse to guess.
1511.1Sjtc	Programs that in the past used the timezone function may now examine
1521.1Sjtc	tzname[localtime(&clock)->tm_isdst] to learn the correct time
1531.2Sperry	zone abbreviation to use.  Alternatively, use
1541.2Sperry	localtime(&clock)->tm_zone if this has been enabled.
1551.2Sperry
1561.2Sperry*	The 4.2BSD gettimeofday function is not used in this package.
1571.2Sperry	This formerly let users obtain the current UTC offset and DST flag,
1581.2Sperry	but this functionality was removed in later versions of BSD.
1591.1Sjtc
1601.2Sperry*	In SVR2, time conversion fails for near-minimum or near-maximum
1611.2Sperry	time_t values when doing conversions for places that don't use GMT.
1621.2Sperry	This package takes care to do these conversions correctly.
1631.2Sperry
1641.2SperryThe functions that are conditionally compiled if STD_INSPIRED is defined
1651.2Sperryshould, at this point, be looked on primarily as food for thought.  They are
1661.2Sperrynot in any sense "standard compatible"--some are not, in fact, specified in
1671.2Sperry*any* standard.  They do, however, represent responses of various authors to
1681.1Sjtcstandardization proposals.
1691.1Sjtc
1701.1SjtcOther time conversion proposals, in particular the one developed by folks at
1711.1SjtcHewlett Packard, offer a wider selection of functions that provide capabilities
1721.1Sjtcbeyond those provided here.  The absence of such functions from this package
1731.1Sjtcis not meant to discourage the development, standardization, or use of such
1741.1Sjtcfunctions.  Rather, their absence reflects the decision to make this package
1751.2Sperrycontain valid extensions to POSIX.1, to ensure its broad
1761.1Sjtcacceptability.  If more powerful time conversion functions can be standardized,
1771.1Sjtcso much the better.
1781.2Sperry
1791.2Sperry
1801.2Sperry----- Names of time zone rule files -----
1811.2Sperry
1821.2SperryThe names of this package's installed time zone rule files are chosen to
1831.2Sperryhelp minimize possible future incompatibilities due to political events.
1841.2SperryOrdinarily, names of countries are not used, to avoid incompatibilities
1851.2Sperrywhen countries change their name (e.g. Zaire->Congo) or
1861.2Sperrywhen locations change countries (e.g. Hong Kong from UK colony to China).
1871.2Sperry
1881.2SperryNames normally have the form AREA/LOCATION, where AREA is the name
1891.2Sperryof a continent or ocean, and LOCATION is the name of a specific
1901.2Sperrylocation within that region.  North and South America share the same
1911.2Sperryarea, `America'.  Typical names are `Africa/Cairo', `America/New_York',
1921.2Sperryand `Pacific/Honolulu'.
1931.2Sperry
1941.2SperryHere are the general rules used for choosing location names,
1951.2Sperryin decreasing order of importance:
1961.2Sperry
1971.2Sperry	Use only valid Posix file names.  Use only Ascii letters, digits, `.',
1981.2Sperry		`-' and `_'.  Do not exceed 14 characters or start with `-'.
1991.2Sperry		E.g. prefer `Brunei' to `Bandar_Seri_Begawan'.
2001.2Sperry	Include at least one location per time zone rule set per country.
2011.2Sperry		One such location is enough.
2021.2Sperry	If all the clocks in a country's region have agreed since 1970,
2031.2Sperry		don't bother to include more than one location
2041.2Sperry		even if subregions' clocks disagreed before 1970.
2051.2Sperry		Otherwise these tables would become annoyingly large.
2061.2Sperry	If a name is ambiguous, use a less ambiguous alternative;
2071.2Sperry		e.g. many cities are named San Jose and Georgetown, so
2081.2Sperry		prefer `Costa_Rica' to `San_Jose' and `Guyana' to `Georgetown'.
2091.2Sperry	Keep locations compact.  Use cities or small islands, not countries
2101.2Sperry		or regions, so that any future time zone changes do not split
2111.2Sperry		locations into different time zones.  E.g. prefer `Paris'
2121.2Sperry		to `France', since France has had multiple time zones.
2131.2Sperry	Use traditional English spelling, e.g. prefer `Rome' to `Roma', and
2141.2Sperry		prefer `Athens' to the true name (which uses Greek letters).
2151.2Sperry		The Posix file name restrictions encourage this rule.
2161.2Sperry	Use the most populous among locations in a country's time zone,
2171.2Sperry		e.g. prefer `Shanghai' to `Beijing'.  Among locations with
2181.2Sperry		similar populations, pick the best-known location,
2191.2Sperry		e.g. prefer `Rome' to `Milan'.
2201.2Sperry	Use the singular form, e.g. prefer `Canary' to `Canaries'.
2211.2Sperry	Omit common suffixes like `_Islands' and `_City', unless that
2221.2Sperry		would lead to ambiguity.  E.g. prefer `Cayman' to
2231.2Sperry		`Cayman_Islands' and `Guatemala' to `Guatemala_City',
2241.2Sperry		but prefer `Mexico_City' to `Mexico' because the country
2251.2Sperry		of Mexico has several time zones.
2261.2Sperry	Use `_' to represent a space.
2271.2Sperry	Omit `.' from abbreviations in names, e.g. prefer `St_Helena'
2281.2Sperry		to `St._Helena'.
2291.2Sperry
2301.2SperryThe file `zone.tab' lists the geographical locations used to name
2311.2Sperrytime zone rule files.
2321.2Sperry
2331.2SperryOlder versions of this package used a different naming scheme,
2341.2Sperryand these older names are still supported.
2351.2SperrySee the file `backwards' for most of these older names
2361.2Sperry(e.g. `US/Eastern' instead of `America/New_York').
2371.2SperryThe other old-fashioned names still supported are
2381.2Sperry`WET', `CET', `MET', `EET' (see the file `europe'),
2391.2Sperryand `Factory' (see the file `factory').
2401.2Sperry
2411.2Sperry
2421.2Sperry----- Time zone abbreviations -----
2431.2Sperry
2441.2SperryWhen this package is installed, it generates time zone abbreviations
2451.2Sperrylike `EST' to be compatible with human tradition and POSIX.1.
2461.2SperryHere are the general rules used for choosing time zone abbreviations,
2471.2Sperryin decreasing order of importance:
2481.2Sperry
2491.2Sperry	Use abbreviations that consist of 3 or more upper-case Ascii letters,
2501.2Sperry		except use "___" for locations while uninhabited.
2511.2Sperry		Posix.1 requires at least 3 characters, and the restriction to
2521.2Sperry		upper-case Ascii letters follows most traditions.
2531.2Sperry		Previous editions of this database also used characters like
2541.2Sperry		' ' and '?', but these characters have a special meaning to
2551.2Sperry		the shell and cause commands like
2561.2Sperry			set `date`
2571.2Sperry		to have unexpected effects.  In theory, the character set could
2581.2Sperry		be !%./@A-Z^_a-z{}, but these tables use only upper-case
2591.2Sperry		Ascii letters (and "___").
2601.2Sperry	Use abbreviations that are in common use among English-speakers,
2611.2Sperry		e.g. `EST' for Eastern Standard Time in North America.
2621.2Sperry		We assume that applications translate them to other languages
2631.2Sperry		as part of the normal localization process; for example,
2641.2Sperry		a French application might translate `EST' to `HNE'.
2651.2Sperry	For zones whose times are taken from a city's longitude, use the
2661.2Sperry		traditional xMT notation, e.g. `PMT' for Paris Mean Time.
2671.2Sperry		The only name like this in current use is `GMT'.
2681.2Sperry	If there is no common English abbreviation, abbreviate the English
2691.2Sperry		translation of the usual phrase used by native speakers.
2701.2Sperry		If this is not available or is a phrase mentioning the country
2711.2Sperry		(e.g. ``Cape Verde Time''), then:
2721.2Sperry
2731.2Sperry		When a country has a single or principal time zone region,
2741.2Sperry			append `T' to the country's ISO	code, e.g. `CVT' for
2751.2Sperry			Cape Verde Time.  For summer time append `ST';
2761.2Sperry			for double summer time append `DST'; etc.
2771.2Sperry		When a country has multiple time zones, take the first three
2781.2Sperry			letters of an English place name identifying each zone
2791.2Sperry			and then append `T', `ST', etc. as before;
2801.2Sperry			e.g. `VLAST' for VLAdivostok Summer Time.
2811.2Sperry
2821.2SperryApplication writers should note that these abbreviations are ambiguous
2831.2Sperryin practice: e.g. `EST' has a different meaning in Australia than
2841.2Sperryit does in the United States.  In new applications, it's often better
2851.2Sperryto use numeric GMT offsets like `-0500' instead of time zone
2861.2Sperryabbreviations like `EST'; this avoids the ambiguity.
287