Update.
[glibc.git] / time / tzset.c
blobf651c3f7ac6cd4e9ddd9b358735d8716a583b1af
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 <errno.h>
21 #include <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 "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 extern void __tzfile_default __P ((const char *std, const char *dst,
42 long int stdoff, long int dstoff));
43 extern char * __tzstring __P ((const char *string));
45 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
46 int __daylight = 0;
47 long int __timezone = 0L;
49 weak_alias (__tzname, tzname)
50 weak_alias (__daylight, daylight)
51 weak_alias (__timezone, timezone)
53 /* This locks all the state variables in tzfile.c and this file. */
54 __libc_lock_define (static, tzset_lock)
57 #define min(a, b) ((a) < (b) ? (a) : (b))
58 #define max(a, b) ((a) > (b) ? (a) : (b))
59 #define sign(x) ((x) < 0 ? -1 : 1)
62 /* This structure contains all the information about a
63 timezone given in the POSIX standard TZ envariable. */
64 typedef struct
66 const char *name;
68 /* When to change. */
69 enum { J0, J1, M } type; /* Interpretation of: */
70 unsigned short int m, n, d; /* Month, week, day. */
71 unsigned int secs; /* Time of day. */
73 long int offset; /* Seconds east of GMT (west if < 0). */
75 /* We cache the computed time of change for a
76 given year so we don't have to recompute it. */
77 time_t change; /* When to change to this zone. */
78 int computed_for; /* Year above is computed for. */
79 } tz_rule;
81 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
82 static tz_rule tz_rules[2];
85 static int compute_change __P ((tz_rule *rule, int year));
86 static int tz_compute __P ((time_t timer, const struct tm *tm));
87 static void tzset_internal __P ((int always));
89 /* Header for a list of buffers containing time zone strings. */
90 struct tzstring_head
92 struct tzstring_head *next;
93 /* The buffer itself immediately follows the header.
94 The buffer contains zero or more (possibly overlapping) strings.
95 The last string is followed by 2 '\0's instead of the usual 1. */
98 /* First in a list of buffers containing time zone strings.
99 All the buffers but the last are read-only. */
100 static struct
102 struct tzstring_head head;
103 char data[48];
104 } tzstring_list;
106 /* Size of the last buffer in the list, not counting its header. */
107 static size_t tzstring_last_buffer_size = sizeof tzstring_list.data;
109 /* Allocate a time zone string with given contents.
110 The string will never be moved or deallocated.
111 However, its contents may be shared with other such strings. */
112 char *
113 __tzstring (string)
114 const char *string;
116 struct tzstring_head *h = &tzstring_list.head;
117 size_t needed;
118 char *p;
120 /* Look through time zone string list for a duplicate of this one. */
121 for (h = &tzstring_list.head; ; h = h->next)
123 for (p = (char *) (h + 1); p[0] | p[1]; ++p)
124 if (strcmp (p, string) == 0)
125 return p;
126 if (! h->next)
127 break;
130 /* No duplicate was found. Copy to the end of this buffer if there's room;
131 otherwise, append a large-enough new buffer to the list and use it. */
132 ++p;
133 needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string. */
135 if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed)
137 size_t buffer_size = tzstring_last_buffer_size;
138 while ((buffer_size *= 2) < needed)
139 continue;
140 if (! (h = h->next = malloc (sizeof *h + buffer_size)))
141 return NULL;
142 h->next = NULL;
143 tzstring_last_buffer_size = buffer_size;
144 p = (char *) (h + 1);
147 return strncpy (p, string, needed);
150 static char *old_tz = NULL;
152 /* Interpret the TZ envariable. */
153 static void
154 tzset_internal (always)
155 int always;
157 static int is_initialized = 0;
158 register const char *tz;
159 register size_t l;
160 char *tzbuf;
161 unsigned short int hh, mm, ss;
162 unsigned short int whichrule;
164 if (is_initialized && !always)
165 return;
166 is_initialized = 1;
168 /* Examine the TZ environment variable. */
169 tz = getenv ("TZ");
170 if (tz == NULL)
171 /* No user specification; use the site-wide default. */
172 tz = TZDEFAULT;
173 else if (*tz == '\0')
174 /* User specified the empty string; use UTC explicitly. */
175 tz = "Universal";
177 /* A leading colon means "implementation defined syntax".
178 We ignore the colon and always use the same algorithm:
179 try a data file, and if none exists parse the 1003.1 syntax. */
180 if (tz && *tz == ':')
181 ++tz;
183 /* Check whether the value changes since the last run. */
184 if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0)
185 /* No change, simply return. */
186 return;
188 tz_rules[0].name = NULL;
189 tz_rules[1].name = NULL;
191 /* Save the value of `tz'. */
192 if (old_tz != NULL)
193 free (old_tz);
194 old_tz = tz ? __strdup (tz) : NULL;
196 /* Try to read a data file. */
197 __tzfile_read (tz);
198 if (__use_tzfile)
199 return;
201 /* No data file found. Default to UTC if nothing specified. */
203 if (tz == NULL || *tz == '\0')
205 tz_rules[0].name = tz_rules[1].name = "UTC";
206 tz_rules[0].type = tz_rules[1].type = J0;
207 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
208 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
209 tz_rules[0].secs = tz_rules[1].secs = 0;
210 tz_rules[0].offset = tz_rules[1].offset = 0L;
211 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
212 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
213 return;
216 /* Clear out old state and reset to unnamed UTC. */
217 memset (tz_rules, 0, sizeof tz_rules);
218 tz_rules[0].name = tz_rules[1].name = "";
220 /* Get the standard timezone name. */
221 tzbuf = malloc (strlen (tz) + 1);
222 if (! tzbuf)
224 /* Clear the old tz name so we will try again. */
225 free (old_tz);
226 old_tz = NULL;
227 return;
230 if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
231 (l = strlen (tzbuf)) < 3)
233 free (tzbuf);
234 return;
237 tz_rules[0].name = __tzstring (tzbuf);
239 tz += l;
241 /* Figure out the standard offset from UTC. */
242 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
244 free (tzbuf);
245 return;
248 if (*tz == '-' || *tz == '+')
249 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
250 else
251 tz_rules[0].offset = -1L;
252 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
254 default:
255 free (tzbuf);
256 return;
257 case 1:
258 mm = 0;
259 case 2:
260 ss = 0;
261 case 3:
262 break;
264 tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
265 (min (hh, 23) * 60 * 60));
267 for (l = 0; l < 3; ++l)
269 while (isdigit(*tz))
270 ++tz;
271 if (l < 2 && *tz == ':')
272 ++tz;
275 /* Get the DST timezone name (if any). */
276 if (*tz != '\0')
278 char *n = tzbuf + strlen (tzbuf) + 1;
279 if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
280 (l = strlen (n)) < 3)
281 goto done_names; /* Punt on name, set up the offsets. */
283 tz_rules[1].name = __tzstring (n);
285 tz += l;
287 /* Figure out the DST offset from GMT. */
288 if (*tz == '-' || *tz == '+')
289 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
290 else
291 tz_rules[1].offset = -1L;
293 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
295 default:
296 /* Default to one hour later than standard time. */
297 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
298 break;
300 case 1:
301 mm = 0;
302 case 2:
303 ss = 0;
304 case 3:
305 tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
306 (min (hh, 23) * (60 * 60)));
307 break;
309 for (l = 0; l < 3; ++l)
311 while (isdigit (*tz))
312 ++tz;
313 if (l < 2 && *tz == ':')
314 ++tz;
316 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
318 /* There is no rule. See if there is a default rule file. */
319 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
320 tz_rules[0].offset, tz_rules[1].offset);
321 if (__use_tzfile)
323 free (old_tz);
324 old_tz = NULL;
325 free (tzbuf);
326 return;
330 else
332 /* There is no DST. */
333 tz_rules[1].name = tz_rules[0].name;
334 free (tzbuf);
335 return;
338 done_names:
339 free (tzbuf);
341 /* Figure out the standard <-> DST rules. */
342 for (whichrule = 0; whichrule < 2; ++whichrule)
344 register tz_rule *tzr = &tz_rules[whichrule];
346 /* Ignore comma to support string following the incorrect
347 specification in early POSIX.1 printings. */
348 tz += *tz == ',';
350 /* Get the date of the change. */
351 if (*tz == 'J' || isdigit (*tz))
353 char *end;
354 tzr->type = *tz == 'J' ? J1 : J0;
355 if (tzr->type == J1 && !isdigit (*++tz))
356 return;
357 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
358 if (end == tz || tzr->d > 365)
359 return;
360 else if (tzr->type == J1 && tzr->d == 0)
361 return;
362 tz = end;
364 else if (*tz == 'M')
366 int n;
367 tzr->type = M;
368 if (sscanf (tz, "M%hu.%hu.%hu%n",
369 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
370 tzr->m < 1 || tzr->m > 12 ||
371 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
372 return;
373 tz += n;
375 else if (*tz == '\0')
377 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
378 tzr->type = M;
379 if (tzr == &tz_rules[0])
381 tzr->m = 4;
382 tzr->n = 1;
383 tzr->d = 0;
385 else
387 tzr->m = 10;
388 tzr->n = 5;
389 tzr->d = 0;
392 else
393 return;
395 if (*tz != '\0' && *tz != '/' && *tz != ',')
396 return;
397 else if (*tz == '/')
399 /* Get the time of day of the change. */
400 ++tz;
401 if (*tz == '\0')
402 return;
403 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
405 default:
406 hh = 2; /* Default to 2:00 AM. */
407 case 1:
408 mm = 0;
409 case 2:
410 ss = 0;
411 case 3:
412 break;
414 for (l = 0; l < 3; ++l)
416 while (isdigit (*tz))
417 ++tz;
418 if (l < 2 && *tz == ':')
419 ++tz;
421 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
423 else
424 /* Default to 2:00 AM. */
425 tzr->secs = 2 * 60 * 60;
427 tzr->computed_for = -1;
431 /* Maximum length of a timezone name. __tz_compute keeps this up to date
432 (never decreasing it) when ! __use_tzfile.
433 tzfile.c keeps it up to date when __use_tzfile. */
434 size_t __tzname_cur_max;
436 long int
437 __tzname_max ()
439 __libc_lock_lock (tzset_lock);
441 tzset_internal (0);
443 __libc_lock_unlock (tzset_lock);
445 return __tzname_cur_max;
448 /* Figure out the exact time (as a time_t) in YEAR
449 when the change described by RULE will occur and
450 put it in RULE->change, saving YEAR in RULE->computed_for.
451 Return nonzero if successful, zero on failure. */
452 static int
453 compute_change (rule, year)
454 tz_rule *rule;
455 int year;
457 register time_t t;
458 int y;
460 if (year != -1 && rule->computed_for == year)
461 /* Operations on times in 1969 will be slower. Oh well. */
462 return 1;
464 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
465 t = 0;
466 for (y = 1970; y < year; ++y)
467 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
469 switch (rule->type)
471 case J1:
472 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
473 In non-leap years, or if the day number is 59 or less, just
474 add SECSPERDAY times the day number-1 to the time of
475 January 1, midnight, to get the day. */
476 t += (rule->d - 1) * SECSPERDAY;
477 if (rule->d >= 60 && __isleap (year))
478 t += SECSPERDAY;
479 break;
481 case J0:
482 /* n - Day of year.
483 Just add SECSPERDAY times the day number to the time of Jan 1st. */
484 t += rule->d * SECSPERDAY;
485 break;
487 case M:
488 /* Mm.n.d - Nth "Dth day" of month M. */
490 register int i, d, m1, yy0, yy1, yy2, dow;
491 register const unsigned short int *myday =
492 &__mon_yday[__isleap (year)][rule->m];
494 /* First add SECSPERDAY for each day in months before M. */
495 t += myday[-1] * SECSPERDAY;
497 /* Use Zeller's Congruence to get day-of-week of first day of month. */
498 m1 = (rule->m + 9) % 12 + 1;
499 yy0 = (rule->m <= 2) ? (year - 1) : year;
500 yy1 = yy0 / 100;
501 yy2 = yy0 % 100;
502 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
503 if (dow < 0)
504 dow += 7;
506 /* DOW is the day-of-week of the first day of the month. Get the
507 day-of-month (zero-origin) of the first DOW day of the month. */
508 d = rule->d - dow;
509 if (d < 0)
510 d += 7;
511 for (i = 1; i < rule->n; ++i)
513 if (d + 7 >= myday[0] - myday[-1])
514 break;
515 d += 7;
518 /* D is the day-of-month (zero-origin) of the day we want. */
519 t += d * SECSPERDAY;
521 break;
524 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
525 Just add the time of day and local offset from GMT, and we're done. */
527 rule->change = t - rule->offset + rule->secs;
528 rule->computed_for = year;
529 return 1;
533 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
534 and set `__tzname', `__timezone', and `__daylight' accordingly.
535 Return nonzero on success, zero on failure. */
536 static int
537 tz_compute (timer, tm)
538 time_t timer;
539 const struct tm *tm;
541 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
542 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
543 return 0;
545 __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
546 __timezone = -tz_rules[__daylight].offset;
547 __tzname[0] = (char *) tz_rules[0].name;
548 __tzname[1] = (char *) tz_rules[1].name;
551 /* Keep __tzname_cur_max up to date. */
552 size_t len0 = strlen (__tzname[0]);
553 size_t len1 = strlen (__tzname[1]);
554 if (len0 > __tzname_cur_max)
555 __tzname_cur_max = len0;
556 if (len1 > __tzname_cur_max)
557 __tzname_cur_max = len1;
560 return 1;
563 /* Reinterpret the TZ environment variable and set `tzname'. */
564 #undef tzset
566 void
567 __tzset (void)
569 __libc_lock_lock (tzset_lock);
571 tzset_internal (1);
573 if (!__use_tzfile)
575 /* Set `tzname'. */
576 __tzname[0] = (char *) tz_rules[0].name;
577 __tzname[1] = (char *) tz_rules[1].name;
580 __libc_lock_unlock (tzset_lock);
582 weak_alias (__tzset, tzset)
584 /* Return the `struct tm' representation of *TIMER in the local timezone.
585 Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */
586 struct tm *
587 __tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
589 long int leap_correction;
590 int leap_extra_secs;
592 if (timer == NULL)
594 __set_errno (EINVAL);
595 return NULL;
598 __libc_lock_lock (tzset_lock);
600 /* Update internal database according to current TZ setting.
601 POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
602 This is a good idea since this allows at least a bit more parallelism.
603 By analogy we apply the same rule to gmtime_r. */
604 tzset_internal (tp == &_tmbuf);
606 if (__use_tzfile)
608 if (! __tzfile_compute (*timer, use_localtime,
609 &leap_correction, &leap_extra_secs))
610 tp = NULL;
612 else
614 __offtime (timer, 0, tp);
615 if (! tz_compute (*timer, tp))
616 tp = NULL;
617 leap_correction = 0L;
618 leap_extra_secs = 0;
621 if (tp)
623 if (use_localtime)
625 tp->tm_isdst = __daylight;
626 tp->tm_zone = __tzname[__daylight];
627 tp->tm_gmtoff = -__timezone;
629 else
631 tp->tm_isdst = 0;
632 tp->tm_zone = "GMT";
633 tp->tm_gmtoff = 0L;
636 __offtime (timer, tp->tm_gmtoff - leap_correction, tp);
637 tp->tm_sec += leap_extra_secs;
640 __libc_lock_unlock (tzset_lock);
642 return tp;