update from main archive 961220
[glibc.git] / time / tzset.c
blob886ac6c0c811db20e73f9b5cce085881bf9e7c66
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <ctype.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
26 /* Defined in mktime.c. */
27 extern const unsigned short int __mon_yday[2][13];
29 #define NOID
30 #include "tzfile.h"
32 extern int __use_tzfile;
33 extern void __tzfile_read __P ((const char *file));
34 extern void __tzfile_default __P ((char *std, char *dst,
35 long int stdoff, long int dstoff));
36 extern int __tz_compute __P ((time_t timer, const struct tm *tm));
38 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
39 int __daylight = 0;
40 long int __timezone = 0L;
42 weak_alias (__tzname, tzname)
43 weak_alias (__daylight, daylight)
44 weak_alias (__timezone, timezone)
47 #define min(a, b) ((a) < (b) ? (a) : (b))
48 #define max(a, b) ((a) > (b) ? (a) : (b))
49 #define sign(x) ((x) < 0 ? -1 : 1)
52 /* This structure contains all the information about a
53 timezone given in the POSIX standard TZ envariable. */
54 typedef struct
56 char *name;
58 /* When to change. */
59 enum { J0, J1, M } type; /* Interpretation of: */
60 unsigned short int m, n, d; /* Month, week, day. */
61 unsigned int secs; /* Time of day. */
63 long int offset; /* Seconds east of GMT (west if < 0). */
65 /* We cache the computed time of change for a
66 given year so we don't have to recompute it. */
67 time_t change; /* When to change to this zone. */
68 int computed_for; /* Year above is computed for. */
69 } tz_rule;
71 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
72 static tz_rule tz_rules[2];
75 static int compute_change __P ((tz_rule *rule, int year));
77 static char *old_tz = NULL;
79 /* Interpret the TZ envariable. */
80 void __tzset_internal __P ((void));
81 void
82 __tzset_internal ()
84 register const char *tz;
85 register size_t l;
86 unsigned short int hh, mm, ss;
87 unsigned short int whichrule;
89 /* Examine the TZ environment variable. */
90 tz = getenv ("TZ");
92 /* A leading colon means "implementation defined syntax".
93 We ignore the colon and always use the same algorithm:
94 try a data file, and if none exists parse the 1003.1 syntax. */
95 if (tz && *tz == ':')
96 ++tz;
98 /* Check whether the value changes since the last run. */
99 if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0)
100 /* No change, simply return. */
101 return;
103 /* Free old storage. */
104 if (tz_rules[0].name != NULL && *tz_rules[0].name != '\0')
106 free((void *) tz_rules[0].name);
107 tz_rules[0].name = NULL;
109 if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
110 tz_rules[1].name != tz_rules[0].name)
112 free((void *) tz_rules[1].name);
113 tz_rules[1].name = NULL;
116 /* Save the value of `tz'. */
117 if (old_tz != NULL)
118 free (old_tz);
119 old_tz = tz ? __strdup (tz) : NULL;
121 /* Try to read a data file. */
122 __tzfile_read (tz);
123 if (__use_tzfile)
124 return;
126 /* No data file found. Default to UTC if nothing specified. */
128 if (tz == NULL || *tz == '\0')
130 static const char UTC[] = "UTC";
131 size_t len = sizeof UTC;
132 tz_rules[0].name = (char *) malloc (len);
133 if (tz_rules[0].name == NULL)
134 return;
135 tz_rules[1].name = (char *) malloc (len);
136 if (tz_rules[1].name == NULL)
137 return;
138 memcpy ((void *) tz_rules[0].name, UTC, len);
139 memcpy ((void *) tz_rules[1].name, UTC, len);
140 tz_rules[0].type = tz_rules[1].type = J0;
141 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
142 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
143 tz_rules[0].secs = tz_rules[1].secs = 0;
144 tz_rules[0].offset = tz_rules[1].offset = 0L;
145 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
146 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
147 return;
150 /* Clear out old state and reset to unnamed UTC. */
151 memset (tz_rules, 0, sizeof tz_rules);
152 tz_rules[0].name = tz_rules[1].name = (char *) "";
154 /* Get the standard timezone name. */
155 tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
156 if (tz_rules[0].name == NULL)
158 /* Clear the old tz name so we will try again. */
159 free (old_tz);
160 old_tz = NULL;
161 return;
164 if (sscanf(tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
165 (l = strlen(tz_rules[0].name)) < 3)
167 free (tz_rules[0].name);
168 tz_rules[0].name = (char *) "";
169 return;
173 char *n = realloc ((void *) tz_rules[0].name, l + 1);
174 if (n != NULL)
175 tz_rules[0].name = n;
178 tz += l;
180 /* Figure out the standard offset from UTC. */
181 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit(*tz)))
182 return;
184 if (*tz == '-' || *tz == '+')
185 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
186 else
187 tz_rules[0].offset = -1L;
188 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
190 default:
191 return;
192 case 1:
193 mm = 0;
194 case 2:
195 ss = 0;
196 case 3:
197 break;
199 tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
200 (min (hh, 23) * 60 * 60));
202 for (l = 0; l < 3; ++l)
204 while (isdigit(*tz))
205 ++tz;
206 if (l < 2 && *tz == ':')
207 ++tz;
210 /* Get the DST timezone name (if any). */
211 if (*tz != '\0')
213 char *n = malloc (strlen (tz) + 1);
214 if (n != NULL)
216 tz_rules[1].name = n;
217 if (sscanf (tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
218 (l = strlen (tz_rules[1].name)) < 3)
220 free (n);
221 tz_rules[1].name = (char *) "";
222 goto done_names; /* Punt on name, set up the offsets. */
224 n = realloc ((void *) tz_rules[1].name, l + 1);
225 if (n != NULL)
226 tz_rules[1].name = n;
228 tz += l;
232 /* Figure out the DST offset from GMT. */
233 if (*tz == '-' || *tz == '+')
234 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
235 else
236 tz_rules[1].offset = -1L;
238 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
240 default:
241 /* Default to one hour later than standard time. */
242 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
243 break;
245 case 1:
246 mm = 0;
247 case 2:
248 ss = 0;
249 case 3:
250 tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
251 (min (hh, 23) * (60 * 60)));
252 break;
254 for (l = 0; l < 3; ++l)
256 while (isdigit (*tz))
257 ++tz;
258 if (l < 2 && *tz == ':')
259 ++tz;
262 done_names:
264 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
266 /* There is no rule. See if there is a default rule file. */
267 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
268 tz_rules[0].offset, tz_rules[1].offset);
269 if (__use_tzfile)
271 free (old_tz);
272 old_tz = NULL;
273 return;
277 /* Figure out the standard <-> DST rules. */
278 for (whichrule = 0; whichrule < 2; ++whichrule)
280 register tz_rule *tzr = &tz_rules[whichrule];
282 if (*tz == ',')
284 ++tz;
285 if (*tz == '\0')
286 return;
289 /* Get the date of the change. */
290 if (*tz == 'J' || isdigit (*tz))
292 char *end;
293 tzr->type = *tz == 'J' ? J1 : J0;
294 if (tzr->type == J1 && !isdigit (*++tz))
295 return;
296 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
297 if (end == tz || tzr->d > 365)
298 return;
299 else if (tzr->type == J1 && tzr->d == 0)
300 return;
301 tz = end;
303 else if (*tz == 'M')
305 int n;
306 tzr->type = M;
307 if (sscanf (tz, "M%hu.%hu.%hu%n",
308 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
309 tzr->m < 1 || tzr->m > 12 ||
310 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
311 return;
312 tz += n;
314 else if (*tz == '\0')
316 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
317 tzr->type = M;
318 if (tzr == &tz_rules[0])
320 tzr->m = 4;
321 tzr->n = 1;
322 tzr->d = 0;
324 else
326 tzr->m = 10;
327 tzr->n = 5;
328 tzr->d = 0;
331 else
332 return;
334 if (*tz != '\0' && *tz != '/' && *tz != ',')
335 return;
336 else if (*tz == '/')
338 /* Get the time of day of the change. */
339 ++tz;
340 if (*tz == '\0')
341 return;
342 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
344 default:
345 hh = 2; /* Default to 2:00 AM. */
346 case 1:
347 mm = 0;
348 case 2:
349 ss = 0;
350 case 3:
351 break;
353 for (l = 0; l < 3; ++l)
355 while (isdigit (*tz))
356 ++tz;
357 if (l < 2 && *tz == ':')
358 ++tz;
360 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
362 else
363 /* Default to 2:00 AM. */
364 tzr->secs = 2 * 60 * 60;
366 tzr->computed_for = -1;
370 /* Maximum length of a timezone name. __tz_compute keeps this up to date
371 (never decreasing it) when ! __use_tzfile.
372 tzfile.c keeps it up to date when __use_tzfile. */
373 size_t __tzname_cur_max;
375 long int
376 __tzname_max ()
378 __tzset_internal ();
380 return __tzname_cur_max;
383 /* Figure out the exact time (as a time_t) in YEAR
384 when the change described by RULE will occur and
385 put it in RULE->change, saving YEAR in RULE->computed_for.
386 Return nonzero if successful, zero on failure. */
387 static int
388 compute_change (rule, year)
389 tz_rule *rule;
390 int year;
392 register time_t t;
393 int y;
395 if (year != -1 && rule->computed_for == year)
396 /* Operations on times in 1969 will be slower. Oh well. */
397 return 1;
399 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
400 t = 0;
401 for (y = 1970; y < year; ++y)
402 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
404 switch (rule->type)
406 case J1:
407 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
408 In non-leap years, or if the day number is 59 or less, just
409 add SECSPERDAY times the day number-1 to the time of
410 January 1, midnight, to get the day. */
411 t += (rule->d - 1) * SECSPERDAY;
412 if (rule->d >= 60 && __isleap (year))
413 t += SECSPERDAY;
414 break;
416 case J0:
417 /* n - Day of year.
418 Just add SECSPERDAY times the day number to the time of Jan 1st. */
419 t += rule->d * SECSPERDAY;
420 break;
422 case M:
423 /* Mm.n.d - Nth "Dth day" of month M. */
425 register int i, d, m1, yy0, yy1, yy2, dow;
426 register const unsigned short int *myday =
427 &__mon_yday[__isleap (year)][rule->m];
429 /* First add SECSPERDAY for each day in months before M. */
430 t += myday[-1] * SECSPERDAY;
432 /* Use Zeller's Congruence to get day-of-week of first day of month. */
433 m1 = (rule->m + 9) % 12 + 1;
434 yy0 = (rule->m <= 2) ? (year - 1) : year;
435 yy1 = yy0 / 100;
436 yy2 = yy0 % 100;
437 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
438 if (dow < 0)
439 dow += 7;
441 /* DOW is the day-of-week of the first day of the month. Get the
442 day-of-month (zero-origin) of the first DOW day of the month. */
443 d = rule->d - dow;
444 if (d < 0)
445 d += 7;
446 for (i = 1; i < rule->n; ++i)
448 if (d + 7 >= myday[0] - myday[-1])
449 break;
450 d += 7;
453 /* D is the day-of-month (zero-origin) of the day we want. */
454 t += d * SECSPERDAY;
456 break;
459 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
460 Just add the time of day and local offset from GMT, and we're done. */
462 rule->change = t - rule->offset + rule->secs;
463 rule->computed_for = year;
464 return 1;
468 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
469 and set `__tzname', `__timezone', and `__daylight' accordingly.
470 Return nonzero on success, zero on failure. */
472 __tz_compute (timer, tm)
473 time_t timer;
474 const struct tm *tm;
476 __tzset_internal ();
478 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
479 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
480 return 0;
482 __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
483 __timezone = tz_rules[__daylight ? 1 : 0].offset;
484 __tzname[0] = (char *) tz_rules[0].name;
485 __tzname[1] = (char *) tz_rules[1].name;
488 /* Keep __tzname_cur_max up to date. */
489 size_t len0 = strlen (__tzname[0]);
490 size_t len1 = strlen (__tzname[1]);
491 if (len0 > __tzname_cur_max)
492 __tzname_cur_max = len0;
493 if (len1 > __tzname_cur_max)
494 __tzname_cur_max = len1;
497 return 1;
500 #include <libc-lock.h>
502 /* This locks all the state variables in tzfile.c and this file. */
503 __libc_lock_define (, __tzset_lock)
505 /* Reinterpret the TZ environment variable and set `tzname'. */
506 #undef tzset
508 void
509 __tzset (void)
511 __libc_lock_lock (__tzset_lock);
513 __tzset_internal ();
515 if (!__use_tzfile)
517 /* Set `tzname'. */
518 __tzname[0] = (char *) tz_rules[0].name;
519 __tzname[1] = (char *) tz_rules[1].name;
522 __libc_lock_unlock (__tzset_lock);
524 weak_alias (__tzset, tzset)