Update.
[glibc.git] / time / tzset.c
blobdcee9dd66cb58255da56323baed37b03aec95584
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 <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 "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)) internal_function;
86 static int tz_compute __P ((time_t timer, const struct tm *tm))
87 internal_function;
88 static void tzset_internal __P ((int always)) internal_function;
90 /* Header for a list of buffers containing time zone strings. */
91 struct tzstring_head
93 struct tzstring_head *next;
94 /* The buffer itself immediately follows the header.
95 The buffer contains zero or more (possibly overlapping) strings.
96 The last string is followed by 2 '\0's instead of the usual 1. */
99 /* First in a list of buffers containing time zone strings.
100 All the buffers but the last are read-only. */
101 static struct
103 struct tzstring_head head;
104 char data[48];
105 } tzstring_list;
107 /* Size of the last buffer in the list, not counting its header. */
108 static size_t tzstring_last_buffer_size = sizeof tzstring_list.data;
110 /* Allocate a time zone string with given contents.
111 The string will never be moved or deallocated.
112 However, its contents may be shared with other such strings. */
113 char *
114 __tzstring (string)
115 const char *string;
117 struct tzstring_head *h = &tzstring_list.head;
118 size_t needed;
119 char *p;
121 /* Look through time zone string list for a duplicate of this one. */
122 for (h = &tzstring_list.head; ; h = h->next)
124 for (p = (char *) (h + 1); p[0] | p[1]; ++p)
125 if (strcmp (p, string) == 0)
126 return p;
127 if (! h->next)
128 break;
131 /* No duplicate was found. Copy to the end of this buffer if there's room;
132 otherwise, append a large-enough new buffer to the list and use it. */
133 ++p;
134 needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string. */
136 if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed)
138 size_t buffer_size = tzstring_last_buffer_size;
139 while ((buffer_size *= 2) < needed)
140 continue;
141 if (! (h = h->next = malloc (sizeof *h + buffer_size)))
142 return NULL;
143 h->next = NULL;
144 tzstring_last_buffer_size = buffer_size;
145 p = (char *) (h + 1);
148 return strncpy (p, string, needed);
151 static char *old_tz = NULL;
153 /* Interpret the TZ envariable. */
154 static void
155 internal_function
156 tzset_internal (always)
157 int always;
159 static int is_initialized = 0;
160 register const char *tz;
161 register size_t l;
162 char *tzbuf;
163 unsigned short int hh, mm, ss;
164 unsigned short int whichrule;
166 if (is_initialized && !always)
167 return;
168 is_initialized = 1;
170 /* Examine the TZ environment variable. */
171 tz = getenv ("TZ");
172 if (tz == NULL)
173 /* No user specification; use the site-wide default. */
174 tz = TZDEFAULT;
175 else if (*tz == '\0')
176 /* User specified the empty string; use UTC explicitly. */
177 tz = "Universal";
179 /* A leading colon means "implementation defined syntax".
180 We ignore the colon and always use the same algorithm:
181 try a data file, and if none exists parse the 1003.1 syntax. */
182 if (tz && *tz == ':')
183 ++tz;
185 /* Check whether the value changes since the last run. */
186 if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0)
187 /* No change, simply return. */
188 return;
190 tz_rules[0].name = NULL;
191 tz_rules[1].name = NULL;
193 /* Save the value of `tz'. */
194 if (old_tz != NULL)
195 free (old_tz);
196 old_tz = tz ? __strdup (tz) : NULL;
198 /* Try to read a data file. */
199 __tzfile_read (tz);
200 if (__use_tzfile)
201 return;
203 /* No data file found. Default to UTC if nothing specified. */
205 if (tz == NULL || *tz == '\0')
207 tz_rules[0].name = tz_rules[1].name = "UTC";
208 tz_rules[0].type = tz_rules[1].type = J0;
209 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
210 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
211 tz_rules[0].secs = tz_rules[1].secs = 0;
212 tz_rules[0].offset = tz_rules[1].offset = 0L;
213 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
214 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
215 return;
218 /* Clear out old state and reset to unnamed UTC. */
219 memset (tz_rules, 0, sizeof tz_rules);
220 tz_rules[0].name = tz_rules[1].name = "";
222 /* Get the standard timezone name. */
223 tzbuf = malloc (strlen (tz) + 1);
224 if (! tzbuf)
226 /* Clear the old tz name so we will try again. */
227 free (old_tz);
228 old_tz = NULL;
229 return;
232 if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
233 (l = strlen (tzbuf)) < 3)
235 free (tzbuf);
236 return;
239 tz_rules[0].name = __tzstring (tzbuf);
241 tz += l;
243 /* Figure out the standard offset from UTC. */
244 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
246 free (tzbuf);
247 return;
250 if (*tz == '-' || *tz == '+')
251 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
252 else
253 tz_rules[0].offset = -1L;
254 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
256 default:
257 free (tzbuf);
258 return;
259 case 1:
260 mm = 0;
261 case 2:
262 ss = 0;
263 case 3:
264 break;
266 tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
267 (min (hh, 23) * 60 * 60));
269 for (l = 0; l < 3; ++l)
271 while (isdigit(*tz))
272 ++tz;
273 if (l < 2 && *tz == ':')
274 ++tz;
277 /* Get the DST timezone name (if any). */
278 if (*tz != '\0')
280 char *n = tzbuf + strlen (tzbuf) + 1;
281 if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
282 (l = strlen (n)) < 3)
283 goto done_names; /* Punt on name, set up the offsets. */
285 tz_rules[1].name = __tzstring (n);
287 tz += l;
289 /* Figure out the DST offset from GMT. */
290 if (*tz == '-' || *tz == '+')
291 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
292 else
293 tz_rules[1].offset = -1L;
295 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
297 default:
298 /* Default to one hour later than standard time. */
299 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
300 break;
302 case 1:
303 mm = 0;
304 case 2:
305 ss = 0;
306 case 3:
307 tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
308 (min (hh, 23) * (60 * 60)));
309 break;
311 for (l = 0; l < 3; ++l)
313 while (isdigit (*tz))
314 ++tz;
315 if (l < 2 && *tz == ':')
316 ++tz;
318 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
320 /* There is no rule. See if there is a default rule file. */
321 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
322 tz_rules[0].offset, tz_rules[1].offset);
323 if (__use_tzfile)
325 free (old_tz);
326 old_tz = NULL;
327 free (tzbuf);
328 return;
332 else
334 /* There is no DST. */
335 tz_rules[1].name = tz_rules[0].name;
336 free (tzbuf);
337 return;
340 done_names:
341 free (tzbuf);
343 /* Figure out the standard <-> DST rules. */
344 for (whichrule = 0; whichrule < 2; ++whichrule)
346 register tz_rule *tzr = &tz_rules[whichrule];
348 /* Ignore comma to support string following the incorrect
349 specification in early POSIX.1 printings. */
350 tz += *tz == ',';
352 /* Get the date of the change. */
353 if (*tz == 'J' || isdigit (*tz))
355 char *end;
356 tzr->type = *tz == 'J' ? J1 : J0;
357 if (tzr->type == J1 && !isdigit (*++tz))
358 return;
359 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
360 if (end == tz || tzr->d > 365)
361 return;
362 else if (tzr->type == J1 && tzr->d == 0)
363 return;
364 tz = end;
366 else if (*tz == 'M')
368 int n;
369 tzr->type = M;
370 if (sscanf (tz, "M%hu.%hu.%hu%n",
371 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
372 tzr->m < 1 || tzr->m > 12 ||
373 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
374 return;
375 tz += n;
377 else if (*tz == '\0')
379 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
380 tzr->type = M;
381 if (tzr == &tz_rules[0])
383 tzr->m = 4;
384 tzr->n = 1;
385 tzr->d = 0;
387 else
389 tzr->m = 10;
390 tzr->n = 5;
391 tzr->d = 0;
394 else
395 return;
397 if (*tz != '\0' && *tz != '/' && *tz != ',')
398 return;
399 else if (*tz == '/')
401 /* Get the time of day of the change. */
402 ++tz;
403 if (*tz == '\0')
404 return;
405 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
407 default:
408 hh = 2; /* Default to 2:00 AM. */
409 case 1:
410 mm = 0;
411 case 2:
412 ss = 0;
413 case 3:
414 break;
416 for (l = 0; l < 3; ++l)
418 while (isdigit (*tz))
419 ++tz;
420 if (l < 2 && *tz == ':')
421 ++tz;
423 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
425 else
426 /* Default to 2:00 AM. */
427 tzr->secs = 2 * 60 * 60;
429 tzr->computed_for = -1;
433 /* Maximum length of a timezone name. __tz_compute keeps this up to date
434 (never decreasing it) when ! __use_tzfile.
435 tzfile.c keeps it up to date when __use_tzfile. */
436 size_t __tzname_cur_max;
438 long int
439 __tzname_max ()
441 __libc_lock_lock (tzset_lock);
443 tzset_internal (0);
445 __libc_lock_unlock (tzset_lock);
447 return __tzname_cur_max;
450 /* Figure out the exact time (as a time_t) in YEAR
451 when the change described by RULE will occur and
452 put it in RULE->change, saving YEAR in RULE->computed_for.
453 Return nonzero if successful, zero on failure. */
454 static int
455 internal_function
456 compute_change (rule, year)
457 tz_rule *rule;
458 int year;
460 register time_t t;
461 int y;
463 if (year != -1 && rule->computed_for == year)
464 /* Operations on times in 1969 will be slower. Oh well. */
465 return 1;
467 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
468 t = 0;
469 for (y = 1970; y < year; ++y)
470 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
472 switch (rule->type)
474 case J1:
475 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
476 In non-leap years, or if the day number is 59 or less, just
477 add SECSPERDAY times the day number-1 to the time of
478 January 1, midnight, to get the day. */
479 t += (rule->d - 1) * SECSPERDAY;
480 if (rule->d >= 60 && __isleap (year))
481 t += SECSPERDAY;
482 break;
484 case J0:
485 /* n - Day of year.
486 Just add SECSPERDAY times the day number to the time of Jan 1st. */
487 t += rule->d * SECSPERDAY;
488 break;
490 case M:
491 /* Mm.n.d - Nth "Dth day" of month M. */
493 unsigned int i;
494 int d, m1, yy0, yy1, yy2, dow;
495 const unsigned short int *myday =
496 &__mon_yday[__isleap (year)][rule->m];
498 /* First add SECSPERDAY for each day in months before M. */
499 t += myday[-1] * SECSPERDAY;
501 /* Use Zeller's Congruence to get day-of-week of first day of month. */
502 m1 = (rule->m + 9) % 12 + 1;
503 yy0 = (rule->m <= 2) ? (year - 1) : year;
504 yy1 = yy0 / 100;
505 yy2 = yy0 % 100;
506 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
507 if (dow < 0)
508 dow += 7;
510 /* DOW is the day-of-week of the first day of the month. Get the
511 day-of-month (zero-origin) of the first DOW day of the month. */
512 d = rule->d - dow;
513 if (d < 0)
514 d += 7;
515 for (i = 1; i < rule->n; ++i)
517 if (d + 7 >= (int) myday[0] - myday[-1])
518 break;
519 d += 7;
522 /* D is the day-of-month (zero-origin) of the day we want. */
523 t += d * SECSPERDAY;
525 break;
528 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
529 Just add the time of day and local offset from GMT, and we're done. */
531 rule->change = t - rule->offset + rule->secs;
532 rule->computed_for = year;
533 return 1;
537 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
538 and set `__tzname', `__timezone', and `__daylight' accordingly.
539 Return nonzero on success, zero on failure. */
540 static int
541 internal_function
542 tz_compute (timer, tm)
543 time_t timer;
544 const struct tm *tm;
546 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
547 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
548 return 0;
550 __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
551 __timezone = tz_rules[__daylight ? 1 : 0].offset;
552 __tzname[0] = (char *) tz_rules[0].name;
553 __tzname[1] = (char *) tz_rules[1].name;
556 /* Keep __tzname_cur_max up to date. */
557 size_t len0 = strlen (__tzname[0]);
558 size_t len1 = strlen (__tzname[1]);
559 if (len0 > __tzname_cur_max)
560 __tzname_cur_max = len0;
561 if (len1 > __tzname_cur_max)
562 __tzname_cur_max = len1;
565 return 1;
568 /* Reinterpret the TZ environment variable and set `tzname'. */
569 #undef tzset
571 void
572 __tzset (void)
574 __libc_lock_lock (tzset_lock);
576 tzset_internal (1);
578 if (!__use_tzfile)
580 /* Set `tzname'. */
581 __tzname[0] = (char *) tz_rules[0].name;
582 __tzname[1] = (char *) tz_rules[1].name;
585 __libc_lock_unlock (tzset_lock);
587 weak_alias (__tzset, tzset)
589 /* Return the `struct tm' representation of *TIMER in the local timezone.
590 Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */
591 struct tm *
592 __tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
594 long int leap_correction;
595 int leap_extra_secs;
597 if (timer == NULL)
599 __set_errno (EINVAL);
600 return NULL;
603 __libc_lock_lock (tzset_lock);
605 /* Update internal database according to current TZ setting.
606 POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
607 This is a good idea since this allows at least a bit more parallelism.
608 By analogy we apply the same rule to gmtime_r. */
609 tzset_internal (tp == &_tmbuf);
611 if (__use_tzfile)
613 if (! __tzfile_compute (*timer, use_localtime,
614 &leap_correction, &leap_extra_secs))
615 tp = NULL;
617 else
619 __offtime (timer, 0, tp);
620 if (! tz_compute (*timer, tp))
621 tp = NULL;
622 leap_correction = 0L;
623 leap_extra_secs = 0;
626 if (tp)
628 if (use_localtime)
630 tp->tm_isdst = __daylight;
631 tp->tm_zone = __tzname[__daylight];
632 tp->tm_gmtoff = __timezone;
634 else
636 tp->tm_isdst = 0;
637 tp->tm_zone = "GMT";
638 tp->tm_gmtoff = 0L;
641 __offtime (timer, tp->tm_gmtoff - leap_correction, tp);
642 tp->tm_sec += leap_extra_secs;
645 __libc_lock_unlock (tzset_lock);
647 return tp;