Update.
[glibc.git] / time / tzset.c
blobd4e3037260eda49743ba4d092d9dacec07fcc329
1 /* Copyright (C) 1991,92,93,94,95,96,97,98 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 <errno.h>
21 #include <bits/libc-lock.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
28 /* Defined in mktime.c. */
29 extern const unsigned short int __mon_yday[2][13];
31 /* Defined in localtime.c. */
32 extern struct tm _tmbuf;
34 #define NOID
35 #include <timezone/tzfile.h>
37 extern int __use_tzfile;
38 extern void __tzfile_read __P ((const char *file));
39 extern int __tzfile_compute __P ((time_t timer, int use_localtime,
40 long int *leap_correct, int *leap_hit,
41 struct tm *tp));
42 extern void __tzfile_default __P ((const char *std, const char *dst,
43 long int stdoff, long int dstoff));
44 extern char *__tzstring __P ((const char *string));
46 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
47 int __daylight = 0;
48 long int __timezone = 0L;
50 weak_alias (__tzname, tzname)
51 weak_alias (__daylight, daylight)
52 weak_alias (__timezone, timezone)
54 /* This locks all the state variables in tzfile.c and this file. */
55 __libc_lock_define (static, tzset_lock)
58 #define min(a, b) ((a) < (b) ? (a) : (b))
59 #define max(a, b) ((a) > (b) ? (a) : (b))
60 #define sign(x) ((x) < 0 ? -1 : 1)
63 /* This structure contains all the information about a
64 timezone given in the POSIX standard TZ envariable. */
65 typedef struct
67 const char *name;
69 /* When to change. */
70 enum { J0, J1, M } type; /* Interpretation of: */
71 unsigned short int m, n, d; /* Month, week, day. */
72 unsigned int secs; /* Time of day. */
74 long int offset; /* Seconds east of GMT (west if < 0). */
76 /* We cache the computed time of change for a
77 given year so we don't have to recompute it. */
78 time_t change; /* When to change to this zone. */
79 int computed_for; /* Year above is computed for. */
80 } tz_rule;
82 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
83 static tz_rule tz_rules[2];
86 static int compute_change __P ((tz_rule *rule, int year)) internal_function;
87 static int tz_compute __P ((time_t timer, const struct tm *tm))
88 internal_function;
89 static void tzset_internal __P ((int always)) internal_function;
91 /* List of buffers containing time zone strings. */
92 struct tzstring_l
94 struct tzstring_l *next;
95 size_t len; /* strlen(data) - doesn't count terminating NUL! */
96 char data[0];
99 struct tzstring_l *tzstring_list;
101 /* Allocate a permanent home for S. It will never be moved or deallocated,
102 but may share space with other strings.
103 Don't modify the returned string. */
104 char *
105 __tzstring (const char *s)
107 char *p;
108 struct tzstring_l *t, *u, *new;
109 size_t len = strlen(s);
111 /* Walk the list and look for a match. If this string is the same
112 as the end of an already-allocated string, it can share space. */
113 for (u = t = tzstring_list; t; u = t, t = t->next)
114 if (len <= t->len)
116 p = &t->data[t->len - len];
117 if (strcmp (s, p) == 0)
118 return p;
121 /* Not found; allocate a new buffer. */
122 new = malloc (sizeof (struct tzstring_l) + len + 1);
123 if (!new)
124 return NULL;
126 new->next = NULL;
127 new->len = len;
128 strcpy (new->data, s);
130 if (u)
131 u->next = new;
132 else
133 tzstring_list = new;
135 return new->data;
138 static char *old_tz = NULL;
140 /* Interpret the TZ envariable. */
141 static void
142 internal_function
143 tzset_internal (always)
144 int always;
146 static int is_initialized = 0;
147 register const char *tz;
148 register size_t l;
149 char *tzbuf;
150 unsigned short int hh, mm, ss;
151 unsigned short int whichrule;
153 if (is_initialized && !always)
154 return;
155 is_initialized = 1;
157 /* Examine the TZ environment variable. */
158 tz = getenv ("TZ");
159 if (tz == NULL)
160 /* No user specification; use the site-wide default. */
161 tz = TZDEFAULT;
162 else if (*tz == '\0')
163 /* User specified the empty string; use UTC explicitly. */
164 tz = "Universal";
166 /* A leading colon means "implementation defined syntax".
167 We ignore the colon and always use the same algorithm:
168 try a data file, and if none exists parse the 1003.1 syntax. */
169 if (tz && *tz == ':')
170 ++tz;
172 /* Check whether the value changes since the last run. */
173 if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0)
174 /* No change, simply return. */
175 return;
177 tz_rules[0].name = NULL;
178 tz_rules[1].name = NULL;
180 /* Save the value of `tz'. */
181 if (old_tz != NULL)
182 free (old_tz);
183 old_tz = tz ? __strdup (tz) : NULL;
185 /* Try to read a data file. */
186 __tzfile_read (tz);
187 if (__use_tzfile)
188 return;
190 /* No data file found. Default to UTC if nothing specified. */
192 if (tz == NULL || *tz == '\0')
194 tz_rules[0].name = tz_rules[1].name = "UTC";
195 tz_rules[0].type = tz_rules[1].type = J0;
196 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
197 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
198 tz_rules[0].secs = tz_rules[1].secs = 0;
199 tz_rules[0].offset = tz_rules[1].offset = 0L;
200 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
201 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
202 return;
205 /* Clear out old state and reset to unnamed UTC. */
206 memset (tz_rules, 0, sizeof tz_rules);
207 tz_rules[0].name = tz_rules[1].name = "";
209 /* Get the standard timezone name. */
210 tzbuf = malloc (strlen (tz) + 1);
211 if (! tzbuf)
213 /* Clear the old tz name so we will try again. */
214 free (old_tz);
215 old_tz = NULL;
216 return;
219 if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
220 (l = strlen (tzbuf)) < 3)
222 free (tzbuf);
223 return;
226 tz_rules[0].name = __tzstring (tzbuf);
228 tz += l;
230 /* Figure out the standard offset from UTC. */
231 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
233 free (tzbuf);
234 return;
237 if (*tz == '-' || *tz == '+')
238 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
239 else
240 tz_rules[0].offset = -1L;
241 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
243 default:
244 free (tzbuf);
245 return;
246 case 1:
247 mm = 0;
248 case 2:
249 ss = 0;
250 case 3:
251 break;
253 tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
254 (min (hh, 23) * 60 * 60));
256 for (l = 0; l < 3; ++l)
258 while (isdigit(*tz))
259 ++tz;
260 if (l < 2 && *tz == ':')
261 ++tz;
264 /* Get the DST timezone name (if any). */
265 if (*tz != '\0')
267 char *n = tzbuf + strlen (tzbuf) + 1;
268 if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
269 (l = strlen (n)) < 3)
270 goto done_names; /* Punt on name, set up the offsets. */
272 tz_rules[1].name = __tzstring (n);
274 tz += l;
276 /* Figure out the DST offset from GMT. */
277 if (*tz == '-' || *tz == '+')
278 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
279 else
280 tz_rules[1].offset = -1L;
282 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
284 default:
285 /* Default to one hour later than standard time. */
286 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
287 break;
289 case 1:
290 mm = 0;
291 case 2:
292 ss = 0;
293 case 3:
294 tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
295 (min (hh, 23) * (60 * 60)));
296 break;
298 for (l = 0; l < 3; ++l)
300 while (isdigit (*tz))
301 ++tz;
302 if (l < 2 && *tz == ':')
303 ++tz;
305 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
307 /* There is no rule. See if there is a default rule file. */
308 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
309 tz_rules[0].offset, tz_rules[1].offset);
310 if (__use_tzfile)
312 free (old_tz);
313 old_tz = NULL;
314 free (tzbuf);
315 return;
319 else
321 /* There is no DST. */
322 tz_rules[1].name = tz_rules[0].name;
323 tz_rules[1].offset = tz_rules[0].offset;
324 free (tzbuf);
325 return;
328 done_names:
329 free (tzbuf);
331 /* Figure out the standard <-> DST rules. */
332 for (whichrule = 0; whichrule < 2; ++whichrule)
334 register tz_rule *tzr = &tz_rules[whichrule];
336 /* Ignore comma to support string following the incorrect
337 specification in early POSIX.1 printings. */
338 tz += *tz == ',';
340 /* Get the date of the change. */
341 if (*tz == 'J' || isdigit (*tz))
343 char *end;
344 tzr->type = *tz == 'J' ? J1 : J0;
345 if (tzr->type == J1 && !isdigit (*++tz))
346 return;
347 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
348 if (end == tz || tzr->d > 365)
349 return;
350 else if (tzr->type == J1 && tzr->d == 0)
351 return;
352 tz = end;
354 else if (*tz == 'M')
356 int n;
357 tzr->type = M;
358 if (sscanf (tz, "M%hu.%hu.%hu%n",
359 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
360 tzr->m < 1 || tzr->m > 12 ||
361 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
362 return;
363 tz += n;
365 else if (*tz == '\0')
367 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
368 tzr->type = M;
369 if (tzr == &tz_rules[0])
371 tzr->m = 4;
372 tzr->n = 1;
373 tzr->d = 0;
375 else
377 tzr->m = 10;
378 tzr->n = 5;
379 tzr->d = 0;
382 else
383 return;
385 if (*tz != '\0' && *tz != '/' && *tz != ',')
386 return;
387 else if (*tz == '/')
389 /* Get the time of day of the change. */
390 ++tz;
391 if (*tz == '\0')
392 return;
393 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
395 default:
396 hh = 2; /* Default to 2:00 AM. */
397 case 1:
398 mm = 0;
399 case 2:
400 ss = 0;
401 case 3:
402 break;
404 for (l = 0; l < 3; ++l)
406 while (isdigit (*tz))
407 ++tz;
408 if (l < 2 && *tz == ':')
409 ++tz;
411 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
413 else
414 /* Default to 2:00 AM. */
415 tzr->secs = 2 * 60 * 60;
417 tzr->computed_for = -1;
421 /* Maximum length of a timezone name. __tz_compute keeps this up to date
422 (never decreasing it) when ! __use_tzfile.
423 tzfile.c keeps it up to date when __use_tzfile. */
424 size_t __tzname_cur_max;
426 long int
427 __tzname_max ()
429 __libc_lock_lock (tzset_lock);
431 tzset_internal (0);
433 __libc_lock_unlock (tzset_lock);
435 return __tzname_cur_max;
438 /* Figure out the exact time (as a time_t) in YEAR
439 when the change described by RULE will occur and
440 put it in RULE->change, saving YEAR in RULE->computed_for.
441 Return nonzero if successful, zero on failure. */
442 static int
443 internal_function
444 compute_change (rule, year)
445 tz_rule *rule;
446 int year;
448 register time_t t;
449 int y;
451 if (year != -1 && rule->computed_for == year)
452 /* Operations on times in 1969 will be slower. Oh well. */
453 return 1;
455 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
456 t = 0;
457 for (y = 1970; y < year; ++y)
458 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
460 switch (rule->type)
462 case J1:
463 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
464 In non-leap years, or if the day number is 59 or less, just
465 add SECSPERDAY times the day number-1 to the time of
466 January 1, midnight, to get the day. */
467 t += (rule->d - 1) * SECSPERDAY;
468 if (rule->d >= 60 && __isleap (year))
469 t += SECSPERDAY;
470 break;
472 case J0:
473 /* n - Day of year.
474 Just add SECSPERDAY times the day number to the time of Jan 1st. */
475 t += rule->d * SECSPERDAY;
476 break;
478 case M:
479 /* Mm.n.d - Nth "Dth day" of month M. */
481 unsigned int i;
482 int d, m1, yy0, yy1, yy2, dow;
483 const unsigned short int *myday =
484 &__mon_yday[__isleap (year)][rule->m];
486 /* First add SECSPERDAY for each day in months before M. */
487 t += myday[-1] * SECSPERDAY;
489 /* Use Zeller's Congruence to get day-of-week of first day of month. */
490 m1 = (rule->m + 9) % 12 + 1;
491 yy0 = (rule->m <= 2) ? (year - 1) : year;
492 yy1 = yy0 / 100;
493 yy2 = yy0 % 100;
494 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
495 if (dow < 0)
496 dow += 7;
498 /* DOW is the day-of-week of the first day of the month. Get the
499 day-of-month (zero-origin) of the first DOW day of the month. */
500 d = rule->d - dow;
501 if (d < 0)
502 d += 7;
503 for (i = 1; i < rule->n; ++i)
505 if (d + 7 >= (int) myday[0] - myday[-1])
506 break;
507 d += 7;
510 /* D is the day-of-month (zero-origin) of the day we want. */
511 t += d * SECSPERDAY;
513 break;
516 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
517 Just add the time of day and local offset from GMT, and we're done. */
519 rule->change = t - rule->offset + rule->secs;
520 rule->computed_for = year;
521 return 1;
525 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
526 and set `__tzname', `__timezone', and `__daylight' accordingly.
527 Return nonzero on success, zero on failure. */
528 static int
529 internal_function
530 tz_compute (timer, tm)
531 time_t timer;
532 const struct tm *tm;
534 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
535 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
536 return 0;
538 __daylight = tz_rules[0].offset != tz_rules[1].offset;
539 __timezone = -tz_rules[0].offset;
540 __tzname[0] = (char *) tz_rules[0].name;
541 __tzname[1] = (char *) tz_rules[1].name;
544 /* Keep __tzname_cur_max up to date. */
545 size_t len0 = strlen (__tzname[0]);
546 size_t len1 = strlen (__tzname[1]);
547 if (len0 > __tzname_cur_max)
548 __tzname_cur_max = len0;
549 if (len1 > __tzname_cur_max)
550 __tzname_cur_max = len1;
553 return 1;
556 /* Reinterpret the TZ environment variable and set `tzname'. */
557 #undef tzset
559 void
560 __tzset (void)
562 __libc_lock_lock (tzset_lock);
564 tzset_internal (1);
566 if (!__use_tzfile)
568 /* Set `tzname'. */
569 __tzname[0] = (char *) tz_rules[0].name;
570 __tzname[1] = (char *) tz_rules[1].name;
573 __libc_lock_unlock (tzset_lock);
575 weak_alias (__tzset, tzset)
577 /* Return the `struct tm' representation of *TIMER in the local timezone.
578 Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */
579 struct tm *
580 __tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
582 long int leap_correction;
583 int leap_extra_secs;
585 if (timer == NULL)
587 __set_errno (EINVAL);
588 return NULL;
591 __libc_lock_lock (tzset_lock);
593 /* Update internal database according to current TZ setting.
594 POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
595 This is a good idea since this allows at least a bit more parallelism.
596 By analogy we apply the same rule to gmtime_r. */
597 tzset_internal (tp == &_tmbuf);
599 if (__use_tzfile)
601 if (! __tzfile_compute (*timer, use_localtime,
602 &leap_correction, &leap_extra_secs, tp))
603 tp = NULL;
605 else
607 if (! (__offtime (timer, 0, tp) && tz_compute (*timer, tp)))
608 tp = NULL;
609 leap_correction = 0L;
610 leap_extra_secs = 0;
613 if (tp)
615 if (use_localtime)
617 if (!__use_tzfile)
619 int isdst = (*timer >= tz_rules[0].change
620 && *timer < tz_rules[1].change);
621 tp->tm_isdst = isdst;
622 tp->tm_zone = __tzname[isdst];
623 tp->tm_gmtoff = tz_rules[isdst].offset;
626 else
628 tp->tm_isdst = 0;
629 tp->tm_zone = "GMT";
630 tp->tm_gmtoff = 0L;
633 if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp))
634 tp->tm_sec += leap_extra_secs;
635 else
636 tp = NULL;
639 __libc_lock_unlock (tzset_lock);
641 return tp;