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