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