1 /* Copyright (C) 1991-2002,2003,2004 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <bits/libc-lock.h>
30 #include <timezone/tzfile.h>
32 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
34 long int __timezone = 0L;
36 weak_alias (__tzname, tzname)
37 weak_alias (__daylight, daylight)
38 weak_alias (__timezone, timezone)
40 /* This locks all the state variables in tzfile.c and this file. */
41 __libc_lock_define_initialized (static, tzset_lock)
44 #define min(a, b) ((a) < (b) ? (a) : (b))
45 #define max(a, b) ((a) > (b) ? (a) : (b))
46 #define sign(x) ((x) < 0 ? -1 : 1)
49 /* This structure contains all the information about a
50 timezone given in the POSIX standard TZ envariable. */
56 enum { J0, J1, M } type; /* Interpretation of: */
57 unsigned short int m, n, d; /* Month, week, day. */
58 unsigned int secs; /* Time of day. */
60 long int offset; /* Seconds east of GMT (west if < 0). */
62 /* We cache the computed time of change for a
63 given year so we don't have to recompute it. */
64 time_t change; /* When to change to this zone. */
65 int computed_for; /* Year above is computed for. */
68 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
69 static tz_rule tz_rules[2];
72 static void compute_change __P ((tz_rule *rule, int year)) internal_function;
73 static void tz_compute __P ((const struct tm *tm))
75 static void tzset_internal __P ((int always)) internal_function;
77 /* List of buffers containing time zone strings. */
80 struct tzstring_l *next;
81 size_t len; /* strlen(data) - doesn't count terminating NUL! */
85 static struct tzstring_l *tzstring_list;
87 /* Allocate a permanent home for S. It will never be moved or deallocated,
88 but may share space with other strings.
89 Don't modify the returned string. */
91 __tzstring (const char *s)
94 struct tzstring_l *t, *u, *new;
95 size_t len = strlen(s);
97 /* Walk the list and look for a match. If this string is the same
98 as the end of an already-allocated string, it can share space. */
99 for (u = t = tzstring_list; t; u = t, t = t->next)
102 p = &t->data[t->len - len];
103 if (strcmp (s, p) == 0)
107 /* Not found; allocate a new buffer. */
108 new = malloc (sizeof (struct tzstring_l) + len + 1);
114 strcpy (new->data, s);
124 /* Maximum length of a timezone name. tzset_internal keeps this up to date
125 (never decreasing it) when ! __use_tzfile.
126 tzfile.c keeps it up to date when __use_tzfile. */
127 size_t __tzname_cur_max;
132 __libc_lock_lock (tzset_lock);
136 __libc_lock_unlock (tzset_lock);
138 return __tzname_cur_max;
143 /* Interpret the TZ envariable. */
146 tzset_internal (always)
149 static int is_initialized;
150 register const char *tz;
153 unsigned short int hh, mm, ss;
154 unsigned short int whichrule;
156 if (is_initialized && !always)
160 /* Examine the TZ environment variable. */
162 if (tz && *tz == '\0')
163 /* User specified the empty string; use UTC explicitly. */
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 == ':')
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. */
178 /* No user specification; use the site-wide default. */
181 tz_rules[0].name = NULL;
182 tz_rules[1].name = NULL;
184 /* Save the value of `tz'. */
187 old_tz = tz ? __strdup (tz) : NULL;
189 /* Try to read a data file. */
190 __tzfile_read (tz, 0, NULL);
194 /* No data file found. Default to UTC if nothing specified. */
196 if (tz == NULL || *tz == '\0'
197 || (TZDEFAULT != NULL && strcmp (tz, TZDEFAULT) == 0))
199 tz_rules[0].name = tz_rules[1].name = "UTC";
200 tz_rules[0].type = tz_rules[1].type = J0;
201 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
202 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
203 tz_rules[0].secs = tz_rules[1].secs = 0;
204 tz_rules[0].offset = tz_rules[1].offset = 0L;
205 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
206 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
210 /* Clear out old state and reset to unnamed UTC. */
211 memset (tz_rules, 0, sizeof tz_rules);
212 tz_rules[0].name = tz_rules[1].name = "";
214 /* Get the standard timezone name. */
215 tzbuf = strdupa (tz);
217 if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
218 (l = strlen (tzbuf)) < 3)
221 tz_rules[0].name = __tzstring (tzbuf);
225 /* Figure out the standard offset from UTC. */
226 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
229 if (*tz == '-' || *tz == '+')
230 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
232 tz_rules[0].offset = -1L;
233 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
244 tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
245 (min (hh, 24) * 60 * 60));
247 for (l = 0; l < 3; ++l)
251 if (l < 2 && *tz == ':')
255 /* Get the DST timezone name (if any). */
258 char *n = tzbuf + strlen (tzbuf) + 1;
259 if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
260 (l = strlen (n)) < 3)
261 goto done_names; /* Punt on name, set up the offsets. */
263 tz_rules[1].name = __tzstring (n);
267 /* Figure out the DST offset from GMT. */
268 if (*tz == '-' || *tz == '+')
269 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
271 tz_rules[1].offset = -1L;
273 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
276 /* Default to one hour later than standard time. */
277 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
285 tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
286 (min (hh, 23) * (60 * 60)));
289 for (l = 0; l < 3; ++l)
291 while (isdigit (*tz))
293 if (l < 2 && *tz == ':')
296 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
298 /* There is no rule. See if there is a default rule file. */
299 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
300 tz_rules[0].offset, tz_rules[1].offset);
311 /* There is no DST. */
312 tz_rules[1].name = tz_rules[0].name;
313 tz_rules[1].offset = tz_rules[0].offset;
318 /* Figure out the standard <-> DST rules. */
319 for (whichrule = 0; whichrule < 2; ++whichrule)
321 register tz_rule *tzr = &tz_rules[whichrule];
323 /* Ignore comma to support string following the incorrect
324 specification in early POSIX.1 printings. */
327 /* Get the date of the change. */
328 if (*tz == 'J' || isdigit (*tz))
331 tzr->type = *tz == 'J' ? J1 : J0;
332 if (tzr->type == J1 && !isdigit (*++tz))
334 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
335 if (end == tz || tzr->d > 365)
337 else if (tzr->type == J1 && tzr->d == 0)
345 if (sscanf (tz, "M%hu.%hu.%hu%n",
346 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
347 tzr->m < 1 || tzr->m > 12 ||
348 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
352 else if (*tz == '\0')
354 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
356 if (tzr == &tz_rules[0])
372 if (*tz != '\0' && *tz != '/' && *tz != ',')
376 /* Get the time of day of the change. */
380 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
383 hh = 2; /* Default to 2:00 AM. */
391 for (l = 0; l < 3; ++l)
393 while (isdigit (*tz))
395 if (l < 2 && *tz == ':')
398 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
401 /* Default to 2:00 AM. */
402 tzr->secs = 2 * 60 * 60;
404 tzr->computed_for = -1;
408 __daylight = tz_rules[0].offset != tz_rules[1].offset;
409 __timezone = -tz_rules[0].offset;
410 __tzname[0] = (char *) tz_rules[0].name;
411 __tzname[1] = (char *) tz_rules[1].name;
414 /* Keep __tzname_cur_max up to date. */
415 size_t len0 = strlen (__tzname[0]);
416 size_t len1 = strlen (__tzname[1]);
417 if (len0 > __tzname_cur_max)
418 __tzname_cur_max = len0;
419 if (len1 > __tzname_cur_max)
420 __tzname_cur_max = len1;
424 /* Figure out the exact time (as a time_t) in YEAR
425 when the change described by RULE will occur and
426 put it in RULE->change, saving YEAR in RULE->computed_for. */
429 compute_change (rule, year)
435 if (year != -1 && rule->computed_for == year)
436 /* Operations on times in 2 BC will be slower. Oh well. */
439 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
441 t = ((year - 1970) * 365
442 + /* Compute the number of leapdays between 1970 and YEAR
443 (exclusive). There is a leapday every 4th year ... */
444 + ((year - 1) / 4 - 1970 / 4)
445 /* ... except every 100th year ... */
446 - ((year - 1) / 100 - 1970 / 100)
447 /* ... but still every 400th year. */
448 + ((year - 1) / 400 - 1970 / 400)) * SECSPERDAY;
455 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
456 In non-leap years, or if the day number is 59 or less, just
457 add SECSPERDAY times the day number-1 to the time of
458 January 1, midnight, to get the day. */
459 t += (rule->d - 1) * SECSPERDAY;
460 if (rule->d >= 60 && __isleap (year))
466 Just add SECSPERDAY times the day number to the time of Jan 1st. */
467 t += rule->d * SECSPERDAY;
471 /* Mm.n.d - Nth "Dth day" of month M. */
474 int d, m1, yy0, yy1, yy2, dow;
475 const unsigned short int *myday =
476 &__mon_yday[__isleap (year)][rule->m];
478 /* First add SECSPERDAY for each day in months before M. */
479 t += myday[-1] * SECSPERDAY;
481 /* Use Zeller's Congruence to get day-of-week of first day of month. */
482 m1 = (rule->m + 9) % 12 + 1;
483 yy0 = (rule->m <= 2) ? (year - 1) : year;
486 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
490 /* DOW is the day-of-week of the first day of the month. Get the
491 day-of-month (zero-origin) of the first DOW day of the month. */
495 for (i = 1; i < rule->n; ++i)
497 if (d + 7 >= (int) myday[0] - myday[-1])
502 /* D is the day-of-month (zero-origin) of the day we want. */
508 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
509 Just add the time of day and local offset from GMT, and we're done. */
511 rule->change = t - rule->offset + rule->secs;
512 rule->computed_for = year;
516 /* Figure out the correct timezone for TM and set `__tzname',
517 `__timezone', and `__daylight' accordingly. */
523 compute_change (&tz_rules[0], 1900 + tm->tm_year);
524 compute_change (&tz_rules[1], 1900 + tm->tm_year);
527 /* Reinterpret the TZ environment variable and set `tzname'. */
533 __libc_lock_lock (tzset_lock);
540 __tzname[0] = (char *) tz_rules[0].name;
541 __tzname[1] = (char *) tz_rules[1].name;
544 __libc_lock_unlock (tzset_lock);
546 weak_alias (__tzset, tzset)
548 /* Return the `struct tm' representation of *TIMER in the local timezone.
549 Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */
551 __tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
553 long int leap_correction;
558 __set_errno (EINVAL);
562 __libc_lock_lock (tzset_lock);
564 /* Update internal database according to current TZ setting.
565 POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
566 This is a good idea since this allows at least a bit more parallelism.
567 By analogy we apply the same rule to gmtime_r. */
568 tzset_internal (tp == &_tmbuf);
571 __tzfile_compute (*timer, use_localtime, &leap_correction,
572 &leap_extra_secs, tp);
575 if (! __offtime (timer, 0, tp))
579 leap_correction = 0L;
591 /* We have to distinguish between northern and southern
592 hemisphere. For the latter the daylight saving time
593 ends in the next year. */
594 if (__builtin_expect (tz_rules[0].change
595 > tz_rules[1].change, 0))
596 isdst = (*timer < tz_rules[1].change
597 || *timer >= tz_rules[0].change);
599 isdst = (*timer >= tz_rules[0].change
600 && *timer < tz_rules[1].change);
601 tp->tm_isdst = isdst;
602 tp->tm_zone = __tzname[isdst];
603 tp->tm_gmtoff = tz_rules[isdst].offset;
613 if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp))
614 tp->tm_sec += leap_extra_secs;
619 __libc_lock_unlock (tzset_lock);
625 libc_freeres_fn (free_mem)
627 while (tzstring_list != NULL)
629 struct tzstring_l *old = tzstring_list;
631 tzstring_list = tzstring_list->next;