Update.
[glibc.git] / time / tzset.c
blob9c8e5d7b9c73ca70f253fc9823bcfc85a0d5bd38
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 /* Header for a list of buffers containing time zone strings. */
92 struct tzstring_head
94 struct tzstring_head *next;
95 /* The buffer itself immediately follows the header.
96 The buffer contains zero or more (possibly overlapping) strings.
97 The last string is followed by 2 '\0's instead of the usual 1. */
100 /* First in a list of buffers containing time zone strings.
101 All the buffers but the last are read-only. */
102 static struct
104 struct tzstring_head head;
105 char data[48];
106 } tzstring_list;
108 /* Size of the last buffer in the list, not counting its header. */
109 static size_t tzstring_last_buffer_size = sizeof tzstring_list.data;
111 /* Allocate a time zone string with given contents.
112 The string will never be moved or deallocated.
113 However, its contents may be shared with other such strings. */
114 char *
115 __tzstring (string)
116 const char *string;
118 struct tzstring_head *h;
119 size_t needed;
120 char *p;
122 /* Look through time zone string list for a duplicate of this one. */
123 for (h = &tzstring_list.head; ; h = h->next)
125 for (p = (char *) (h + 1); p[0] | p[1]; ++p)
126 if (strcmp (p, string) == 0)
127 return p;
128 if (! h->next)
129 break;
132 /* No duplicate was found. Copy to the end of this buffer if there's room;
133 otherwise, append a large-enough new buffer to the list and use it. */
134 ++p;
135 needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string. */
137 if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed)
139 size_t buffer_size = tzstring_last_buffer_size;
140 while ((buffer_size *= 2) < needed)
141 continue;
142 h = malloc (sizeof *h + buffer_size);
143 if (h == NULL)
144 return NULL;
145 h->next = NULL;
146 tzstring_last_buffer_size = buffer_size;
147 p = (char *) (h + 1);
150 return strncpy (p, string, needed);
153 static char *old_tz = NULL;
155 /* Interpret the TZ envariable. */
156 static void
157 internal_function
158 tzset_internal (always)
159 int always;
161 static int is_initialized = 0;
162 register const char *tz;
163 register size_t l;
164 char *tzbuf;
165 unsigned short int hh, mm, ss;
166 unsigned short int whichrule;
168 if (is_initialized && !always)
169 return;
170 is_initialized = 1;
172 /* Examine the TZ environment variable. */
173 tz = getenv ("TZ");
174 if (tz == NULL)
175 /* No user specification; use the site-wide default. */
176 tz = TZDEFAULT;
177 else if (*tz == '\0')
178 /* User specified the empty string; use UTC explicitly. */
179 tz = "Universal";
181 /* A leading colon means "implementation defined syntax".
182 We ignore the colon and always use the same algorithm:
183 try a data file, and if none exists parse the 1003.1 syntax. */
184 if (tz && *tz == ':')
185 ++tz;
187 /* Check whether the value changes since the last run. */
188 if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0)
189 /* No change, simply return. */
190 return;
192 tz_rules[0].name = NULL;
193 tz_rules[1].name = NULL;
195 /* Save the value of `tz'. */
196 if (old_tz != NULL)
197 free (old_tz);
198 old_tz = tz ? __strdup (tz) : NULL;
200 /* Try to read a data file. */
201 __tzfile_read (tz);
202 if (__use_tzfile)
203 return;
205 /* No data file found. Default to UTC if nothing specified. */
207 if (tz == NULL || *tz == '\0')
209 tz_rules[0].name = tz_rules[1].name = "UTC";
210 tz_rules[0].type = tz_rules[1].type = J0;
211 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
212 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
213 tz_rules[0].secs = tz_rules[1].secs = 0;
214 tz_rules[0].offset = tz_rules[1].offset = 0L;
215 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
216 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
217 return;
220 /* Clear out old state and reset to unnamed UTC. */
221 memset (tz_rules, 0, sizeof tz_rules);
222 tz_rules[0].name = tz_rules[1].name = "";
224 /* Get the standard timezone name. */
225 tzbuf = malloc (strlen (tz) + 1);
226 if (! tzbuf)
228 /* Clear the old tz name so we will try again. */
229 free (old_tz);
230 old_tz = NULL;
231 return;
234 if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
235 (l = strlen (tzbuf)) < 3)
237 free (tzbuf);
238 return;
241 tz_rules[0].name = __tzstring (tzbuf);
243 tz += l;
245 /* Figure out the standard offset from UTC. */
246 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
248 free (tzbuf);
249 return;
252 if (*tz == '-' || *tz == '+')
253 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
254 else
255 tz_rules[0].offset = -1L;
256 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
258 default:
259 free (tzbuf);
260 return;
261 case 1:
262 mm = 0;
263 case 2:
264 ss = 0;
265 case 3:
266 break;
268 tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
269 (min (hh, 23) * 60 * 60));
271 for (l = 0; l < 3; ++l)
273 while (isdigit(*tz))
274 ++tz;
275 if (l < 2 && *tz == ':')
276 ++tz;
279 /* Get the DST timezone name (if any). */
280 if (*tz != '\0')
282 char *n = tzbuf + strlen (tzbuf) + 1;
283 if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
284 (l = strlen (n)) < 3)
285 goto done_names; /* Punt on name, set up the offsets. */
287 tz_rules[1].name = __tzstring (n);
289 tz += l;
291 /* Figure out the DST offset from GMT. */
292 if (*tz == '-' || *tz == '+')
293 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
294 else
295 tz_rules[1].offset = -1L;
297 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
299 default:
300 /* Default to one hour later than standard time. */
301 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
302 break;
304 case 1:
305 mm = 0;
306 case 2:
307 ss = 0;
308 case 3:
309 tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
310 (min (hh, 23) * (60 * 60)));
311 break;
313 for (l = 0; l < 3; ++l)
315 while (isdigit (*tz))
316 ++tz;
317 if (l < 2 && *tz == ':')
318 ++tz;
320 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
322 /* There is no rule. See if there is a default rule file. */
323 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
324 tz_rules[0].offset, tz_rules[1].offset);
325 if (__use_tzfile)
327 free (old_tz);
328 old_tz = NULL;
329 free (tzbuf);
330 return;
334 else
336 /* There is no DST. */
337 tz_rules[1].name = tz_rules[0].name;
338 tz_rules[1].offset = tz_rules[0].offset;
339 free (tzbuf);
340 return;
343 done_names:
344 free (tzbuf);
346 /* Figure out the standard <-> DST rules. */
347 for (whichrule = 0; whichrule < 2; ++whichrule)
349 register tz_rule *tzr = &tz_rules[whichrule];
351 /* Ignore comma to support string following the incorrect
352 specification in early POSIX.1 printings. */
353 tz += *tz == ',';
355 /* Get the date of the change. */
356 if (*tz == 'J' || isdigit (*tz))
358 char *end;
359 tzr->type = *tz == 'J' ? J1 : J0;
360 if (tzr->type == J1 && !isdigit (*++tz))
361 return;
362 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
363 if (end == tz || tzr->d > 365)
364 return;
365 else if (tzr->type == J1 && tzr->d == 0)
366 return;
367 tz = end;
369 else if (*tz == 'M')
371 int n;
372 tzr->type = M;
373 if (sscanf (tz, "M%hu.%hu.%hu%n",
374 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
375 tzr->m < 1 || tzr->m > 12 ||
376 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
377 return;
378 tz += n;
380 else if (*tz == '\0')
382 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
383 tzr->type = M;
384 if (tzr == &tz_rules[0])
386 tzr->m = 4;
387 tzr->n = 1;
388 tzr->d = 0;
390 else
392 tzr->m = 10;
393 tzr->n = 5;
394 tzr->d = 0;
397 else
398 return;
400 if (*tz != '\0' && *tz != '/' && *tz != ',')
401 return;
402 else if (*tz == '/')
404 /* Get the time of day of the change. */
405 ++tz;
406 if (*tz == '\0')
407 return;
408 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
410 default:
411 hh = 2; /* Default to 2:00 AM. */
412 case 1:
413 mm = 0;
414 case 2:
415 ss = 0;
416 case 3:
417 break;
419 for (l = 0; l < 3; ++l)
421 while (isdigit (*tz))
422 ++tz;
423 if (l < 2 && *tz == ':')
424 ++tz;
426 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
428 else
429 /* Default to 2:00 AM. */
430 tzr->secs = 2 * 60 * 60;
432 tzr->computed_for = -1;
436 /* Maximum length of a timezone name. __tz_compute keeps this up to date
437 (never decreasing it) when ! __use_tzfile.
438 tzfile.c keeps it up to date when __use_tzfile. */
439 size_t __tzname_cur_max;
441 long int
442 __tzname_max ()
444 __libc_lock_lock (tzset_lock);
446 tzset_internal (0);
448 __libc_lock_unlock (tzset_lock);
450 return __tzname_cur_max;
453 /* Figure out the exact time (as a time_t) in YEAR
454 when the change described by RULE will occur and
455 put it in RULE->change, saving YEAR in RULE->computed_for.
456 Return nonzero if successful, zero on failure. */
457 static int
458 internal_function
459 compute_change (rule, year)
460 tz_rule *rule;
461 int year;
463 register time_t t;
464 int y;
466 if (year != -1 && rule->computed_for == year)
467 /* Operations on times in 1969 will be slower. Oh well. */
468 return 1;
470 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
471 t = 0;
472 for (y = 1970; y < year; ++y)
473 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
475 switch (rule->type)
477 case J1:
478 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
479 In non-leap years, or if the day number is 59 or less, just
480 add SECSPERDAY times the day number-1 to the time of
481 January 1, midnight, to get the day. */
482 t += (rule->d - 1) * SECSPERDAY;
483 if (rule->d >= 60 && __isleap (year))
484 t += SECSPERDAY;
485 break;
487 case J0:
488 /* n - Day of year.
489 Just add SECSPERDAY times the day number to the time of Jan 1st. */
490 t += rule->d * SECSPERDAY;
491 break;
493 case M:
494 /* Mm.n.d - Nth "Dth day" of month M. */
496 unsigned int i;
497 int d, m1, yy0, yy1, yy2, dow;
498 const unsigned short int *myday =
499 &__mon_yday[__isleap (year)][rule->m];
501 /* First add SECSPERDAY for each day in months before M. */
502 t += myday[-1] * SECSPERDAY;
504 /* Use Zeller's Congruence to get day-of-week of first day of month. */
505 m1 = (rule->m + 9) % 12 + 1;
506 yy0 = (rule->m <= 2) ? (year - 1) : year;
507 yy1 = yy0 / 100;
508 yy2 = yy0 % 100;
509 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
510 if (dow < 0)
511 dow += 7;
513 /* DOW is the day-of-week of the first day of the month. Get the
514 day-of-month (zero-origin) of the first DOW day of the month. */
515 d = rule->d - dow;
516 if (d < 0)
517 d += 7;
518 for (i = 1; i < rule->n; ++i)
520 if (d + 7 >= (int) myday[0] - myday[-1])
521 break;
522 d += 7;
525 /* D is the day-of-month (zero-origin) of the day we want. */
526 t += d * SECSPERDAY;
528 break;
531 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
532 Just add the time of day and local offset from GMT, and we're done. */
534 rule->change = t - rule->offset + rule->secs;
535 rule->computed_for = year;
536 return 1;
540 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
541 and set `__tzname', `__timezone', and `__daylight' accordingly.
542 Return nonzero on success, zero on failure. */
543 static int
544 internal_function
545 tz_compute (timer, tm)
546 time_t timer;
547 const struct tm *tm;
549 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
550 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
551 return 0;
553 __daylight = tz_rules[0].offset != tz_rules[1].offset;
554 __timezone = -tz_rules[0].offset;
555 __tzname[0] = (char *) tz_rules[0].name;
556 __tzname[1] = (char *) tz_rules[1].name;
559 /* Keep __tzname_cur_max up to date. */
560 size_t len0 = strlen (__tzname[0]);
561 size_t len1 = strlen (__tzname[1]);
562 if (len0 > __tzname_cur_max)
563 __tzname_cur_max = len0;
564 if (len1 > __tzname_cur_max)
565 __tzname_cur_max = len1;
568 return 1;
571 /* Reinterpret the TZ environment variable and set `tzname'. */
572 #undef tzset
574 void
575 __tzset (void)
577 __libc_lock_lock (tzset_lock);
579 tzset_internal (1);
581 if (!__use_tzfile)
583 /* Set `tzname'. */
584 __tzname[0] = (char *) tz_rules[0].name;
585 __tzname[1] = (char *) tz_rules[1].name;
588 __libc_lock_unlock (tzset_lock);
590 weak_alias (__tzset, tzset)
592 /* Return the `struct tm' representation of *TIMER in the local timezone.
593 Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */
594 struct tm *
595 __tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
597 long int leap_correction;
598 int leap_extra_secs;
600 if (timer == NULL)
602 __set_errno (EINVAL);
603 return NULL;
606 __libc_lock_lock (tzset_lock);
608 /* Update internal database according to current TZ setting.
609 POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
610 This is a good idea since this allows at least a bit more parallelism.
611 By analogy we apply the same rule to gmtime_r. */
612 tzset_internal (tp == &_tmbuf);
614 if (__use_tzfile)
616 if (! __tzfile_compute (*timer, use_localtime,
617 &leap_correction, &leap_extra_secs, tp))
618 tp = NULL;
620 else
622 if (! (__offtime (timer, 0, tp) && tz_compute (*timer, tp)))
623 tp = NULL;
624 leap_correction = 0L;
625 leap_extra_secs = 0;
628 if (tp)
630 if (use_localtime)
632 if (!__use_tzfile)
634 int isdst = (*timer >= tz_rules[0].change
635 && *timer < tz_rules[1].change);
636 tp->tm_isdst = isdst;
637 tp->tm_zone = __tzname[isdst];
638 tp->tm_gmtoff = tz_rules[isdst].offset;
641 else
643 tp->tm_isdst = 0;
644 tp->tm_zone = "GMT";
645 tp->tm_gmtoff = 0L;
648 if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp))
649 tp->tm_sec += leap_extra_secs;
650 else
651 tp = NULL;
654 __libc_lock_unlock (tzset_lock);
656 return tp;