Update.
[glibc.git] / time / tzset.c
blob05760b2c626619c1782385d65697a1bd3426a362
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 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 ((int always));
81 void
82 __tzset_internal (always)
83 int always;
85 static int is_initialized = 0;
86 register const char *tz;
87 register size_t l;
88 unsigned short int hh, mm, ss;
89 unsigned short int whichrule;
91 if (is_initialized && !always)
92 return;
93 is_initialized = 1;
95 /* Examine the TZ environment variable. */
96 tz = getenv ("TZ");
97 if (tz == NULL)
98 /* No user specification; use the site-wide default. */
99 tz = TZDEFAULT;
100 else if (*tz == '\0')
101 /* User specified the empty string; use UTC explicitly. */
102 tz = "Universal";
104 /* A leading colon means "implementation defined syntax".
105 We ignore the colon and always use the same algorithm:
106 try a data file, and if none exists parse the 1003.1 syntax. */
107 if (tz && *tz == ':')
108 ++tz;
110 /* Check whether the value changes since the last run. */
111 if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0)
112 /* No change, simply return. */
113 return;
115 /* Free old storage. */
116 if (tz_rules[0].name != NULL && *tz_rules[0].name != '\0')
117 free ((void *) tz_rules[0].name);
118 if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
119 tz_rules[1].name != tz_rules[0].name)
120 free ((void *) tz_rules[1].name);
121 tz_rules[0].name = NULL;
122 tz_rules[1].name = NULL;
124 /* Save the value of `tz'. */
125 if (old_tz != NULL)
126 free (old_tz);
127 old_tz = tz ? __strdup (tz) : NULL;
129 /* Try to read a data file. */
130 __tzfile_read (tz);
131 if (__use_tzfile)
132 return;
134 /* No data file found. Default to UTC if nothing specified. */
136 if (tz == NULL || *tz == '\0')
138 static const char UTC[] = "UTC";
139 size_t len = sizeof UTC;
140 tz_rules[0].name = (char *) malloc (len);
141 if (tz_rules[0].name == NULL)
142 return;
143 tz_rules[1].name = (char *) malloc (len);
144 if (tz_rules[1].name == NULL)
145 return;
146 memcpy ((void *) tz_rules[0].name, UTC, len);
147 memcpy ((void *) tz_rules[1].name, UTC, len);
148 tz_rules[0].type = tz_rules[1].type = J0;
149 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
150 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
151 tz_rules[0].secs = tz_rules[1].secs = 0;
152 tz_rules[0].offset = tz_rules[1].offset = 0L;
153 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
154 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
155 return;
158 /* Clear out old state and reset to unnamed UTC. */
159 memset (tz_rules, 0, sizeof tz_rules);
160 tz_rules[0].name = tz_rules[1].name = (char *) "";
162 /* Get the standard timezone name. */
163 tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
164 if (tz_rules[0].name == NULL)
166 /* Clear the old tz name so we will try again. */
167 free (old_tz);
168 old_tz = NULL;
169 return;
172 if (sscanf (tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
173 (l = strlen(tz_rules[0].name)) < 3)
175 free (tz_rules[0].name);
176 tz_rules[0].name = (char *) "";
177 return;
181 char *n = realloc ((void *) tz_rules[0].name, l + 1);
182 if (n != NULL)
183 tz_rules[0].name = n;
186 tz += l;
188 /* Figure out the standard offset from UTC. */
189 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
190 return;
192 if (*tz == '-' || *tz == '+')
193 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
194 else
195 tz_rules[0].offset = -1L;
196 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
198 default:
199 return;
200 case 1:
201 mm = 0;
202 case 2:
203 ss = 0;
204 case 3:
205 break;
207 tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
208 (min (hh, 23) * 60 * 60));
210 for (l = 0; l < 3; ++l)
212 while (isdigit(*tz))
213 ++tz;
214 if (l < 2 && *tz == ':')
215 ++tz;
218 /* Get the DST timezone name (if any). */
219 if (*tz != '\0')
221 char *n = malloc (strlen (tz) + 1);
222 if (n != NULL)
224 tz_rules[1].name = n;
225 if (sscanf (tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
226 (l = strlen (tz_rules[1].name)) < 3)
228 free (n);
229 tz_rules[1].name = (char *) "";
230 goto done_names; /* Punt on name, set up the offsets. */
232 n = realloc ((void *) tz_rules[1].name, l + 1);
233 if (n != NULL)
234 tz_rules[1].name = n;
236 tz += l;
239 /* Figure out the DST offset from GMT. */
240 if (*tz == '-' || *tz == '+')
241 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
242 else
243 tz_rules[1].offset = -1L;
245 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
247 default:
248 /* Default to one hour later than standard time. */
249 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
250 break;
252 case 1:
253 mm = 0;
254 case 2:
255 ss = 0;
256 case 3:
257 tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
258 (min (hh, 23) * (60 * 60)));
259 break;
261 for (l = 0; l < 3; ++l)
263 while (isdigit (*tz))
264 ++tz;
265 if (l < 2 && *tz == ':')
266 ++tz;
269 else
270 /* There is no DST. */
271 tz_rules[1].name = tz_rules[0].name;
273 done_names:
275 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
277 /* There is no rule. See if there is a default rule file. */
278 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
279 tz_rules[0].offset, tz_rules[1].offset);
280 if (__use_tzfile)
282 free (old_tz);
283 old_tz = NULL;
284 return;
288 /* Figure out the standard <-> DST rules. */
289 for (whichrule = 0; whichrule < 2; ++whichrule)
291 register tz_rule *tzr = &tz_rules[whichrule];
293 if (*tz == ',')
295 ++tz;
296 if (*tz == '\0')
297 return;
300 /* Get the date of the change. */
301 if (*tz == 'J' || isdigit (*tz))
303 char *end;
304 tzr->type = *tz == 'J' ? J1 : J0;
305 if (tzr->type == J1 && !isdigit (*++tz))
306 return;
307 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
308 if (end == tz || tzr->d > 365)
309 return;
310 else if (tzr->type == J1 && tzr->d == 0)
311 return;
312 tz = end;
314 else if (*tz == 'M')
316 int n;
317 tzr->type = M;
318 if (sscanf (tz, "M%hu.%hu.%hu%n",
319 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
320 tzr->m < 1 || tzr->m > 12 ||
321 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
322 return;
323 tz += n;
325 else if (*tz == '\0')
327 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
328 tzr->type = M;
329 if (tzr == &tz_rules[0])
331 tzr->m = 4;
332 tzr->n = 1;
333 tzr->d = 0;
335 else
337 tzr->m = 10;
338 tzr->n = 5;
339 tzr->d = 0;
342 else
343 return;
345 if (*tz != '\0' && *tz != '/' && *tz != ',')
346 return;
347 else if (*tz == '/')
349 /* Get the time of day of the change. */
350 ++tz;
351 if (*tz == '\0')
352 return;
353 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
355 default:
356 hh = 2; /* Default to 2:00 AM. */
357 case 1:
358 mm = 0;
359 case 2:
360 ss = 0;
361 case 3:
362 break;
364 for (l = 0; l < 3; ++l)
366 while (isdigit (*tz))
367 ++tz;
368 if (l < 2 && *tz == ':')
369 ++tz;
371 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
373 else
374 /* Default to 2:00 AM. */
375 tzr->secs = 2 * 60 * 60;
377 tzr->computed_for = -1;
381 /* Maximum length of a timezone name. __tz_compute keeps this up to date
382 (never decreasing it) when ! __use_tzfile.
383 tzfile.c keeps it up to date when __use_tzfile. */
384 size_t __tzname_cur_max;
386 long int
387 __tzname_max ()
389 __tzset_internal (0);
391 return __tzname_cur_max;
394 /* Figure out the exact time (as a time_t) in YEAR
395 when the change described by RULE will occur and
396 put it in RULE->change, saving YEAR in RULE->computed_for.
397 Return nonzero if successful, zero on failure. */
398 static int
399 compute_change (rule, year)
400 tz_rule *rule;
401 int year;
403 register time_t t;
404 int y;
406 if (year != -1 && rule->computed_for == year)
407 /* Operations on times in 1969 will be slower. Oh well. */
408 return 1;
410 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
411 t = 0;
412 for (y = 1970; y < year; ++y)
413 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
415 switch (rule->type)
417 case J1:
418 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
419 In non-leap years, or if the day number is 59 or less, just
420 add SECSPERDAY times the day number-1 to the time of
421 January 1, midnight, to get the day. */
422 t += (rule->d - 1) * SECSPERDAY;
423 if (rule->d >= 60 && __isleap (year))
424 t += SECSPERDAY;
425 break;
427 case J0:
428 /* n - Day of year.
429 Just add SECSPERDAY times the day number to the time of Jan 1st. */
430 t += rule->d * SECSPERDAY;
431 break;
433 case M:
434 /* Mm.n.d - Nth "Dth day" of month M. */
436 register int i, d, m1, yy0, yy1, yy2, dow;
437 register const unsigned short int *myday =
438 &__mon_yday[__isleap (year)][rule->m];
440 /* First add SECSPERDAY for each day in months before M. */
441 t += myday[-1] * SECSPERDAY;
443 /* Use Zeller's Congruence to get day-of-week of first day of month. */
444 m1 = (rule->m + 9) % 12 + 1;
445 yy0 = (rule->m <= 2) ? (year - 1) : year;
446 yy1 = yy0 / 100;
447 yy2 = yy0 % 100;
448 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
449 if (dow < 0)
450 dow += 7;
452 /* DOW is the day-of-week of the first day of the month. Get the
453 day-of-month (zero-origin) of the first DOW day of the month. */
454 d = rule->d - dow;
455 if (d < 0)
456 d += 7;
457 for (i = 1; i < rule->n; ++i)
459 if (d + 7 >= myday[0] - myday[-1])
460 break;
461 d += 7;
464 /* D is the day-of-month (zero-origin) of the day we want. */
465 t += d * SECSPERDAY;
467 break;
470 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
471 Just add the time of day and local offset from GMT, and we're done. */
473 rule->change = t - rule->offset + rule->secs;
474 rule->computed_for = year;
475 return 1;
479 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
480 and set `__tzname', `__timezone', and `__daylight' accordingly.
481 Return nonzero on success, zero on failure. */
483 __tz_compute (timer, tm)
484 time_t timer;
485 const struct tm *tm;
487 __tzset_internal (0);
489 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
490 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
491 return 0;
493 __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
494 __timezone = tz_rules[__daylight ? 1 : 0].offset;
495 __tzname[0] = (char *) tz_rules[0].name;
496 __tzname[1] = (char *) tz_rules[1].name;
499 /* Keep __tzname_cur_max up to date. */
500 size_t len0 = strlen (__tzname[0]);
501 size_t len1 = strlen (__tzname[1]);
502 if (len0 > __tzname_cur_max)
503 __tzname_cur_max = len0;
504 if (len1 > __tzname_cur_max)
505 __tzname_cur_max = len1;
508 return 1;
511 #include <libc-lock.h>
513 /* This locks all the state variables in tzfile.c and this file. */
514 __libc_lock_define (, __tzset_lock)
516 /* Reinterpret the TZ environment variable and set `tzname'. */
517 #undef tzset
519 void
520 __tzset (void)
522 __libc_lock_lock (__tzset_lock);
524 __tzset_internal (1);
526 if (!__use_tzfile)
528 /* Set `tzname'. */
529 __tzname[0] = (char *) tz_rules[0].name;
530 __tzname[1] = (char *) tz_rules[1].name;
533 __libc_lock_unlock (__tzset_lock);
535 weak_alias (__tzset, tzset)