1 /* Copyright (C) 1991, 1992 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 #include <localeinfo.h>
29 #define __tzname tzname
30 #define __daylight daylight
31 #define __timezone timezone
34 extern int __use_tzfile;
35 extern void EXFUN(__tzfile_read, (CONST char *file));
36 extern void EXFUN(__tzfile_default, (char *std AND char *dst AND
37 long int stdoff AND long int dstoff));
38 extern int EXFUN(__tzfile_compute, (time_t, struct tm));
40 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
42 long int __timezone = 0L;
45 #define min(a, b) ((a) < (b) ? (a) : (b))
46 #define max(a, b) ((a) > (b) ? (a) : (b))
47 #define sign(x) ((x) < 0 ? -1 : 1)
50 /* This structure contains all the information about a
51 timezone given in the POSIX standard TZ envariable. */
57 enum { J0, J1, M } type; /* Interpretation of: */
58 unsigned short int m, n, d; /* Month, week, day. */
59 unsigned int secs:17; /* Time of day. */
61 long int offset; /* Seconds east of GMT (west if < 0). */
63 /* We cache the computed time of change for a
64 given year so we don't have to recompute it. */
65 time_t change; /* When to change to this zone. */
66 int computed_for; /* Year above is computed for. */
69 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
70 static tz_rule tz_rules[2];
72 static int tzset_run = 0;
74 /* Interpret the TZ envariable. */
78 register CONST char *tz;
80 unsigned short int hh, mm, ss;
81 unsigned short int whichrule;
83 /* Free old storage. */
84 if (tz_rules[0].name != NULL && *tz_rules[0].name != '\0')
85 free((PTR) tz_rules[0].name);
86 if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
87 tz_rules[1].name != tz_rules[0].name)
88 free((PTR) tz_rules[1].name);
92 if (tz != NULL && *tz == ':')
94 __tzfile_read(tz + 1);
104 if (tz == NULL || *tz == '\0')
106 if (tz == NULL || *tz == '\0')
108 __tzfile_read((char *) NULL);
111 size_t len = strlen(_time_info->ut0) + 1;
112 tz_rules[0].name = (char *) malloc(len);
113 if (tz_rules[0].name == NULL)
115 tz_rules[1].name = (char *) malloc(len);
116 if (tz_rules[1].name == NULL)
118 memcpy((PTR) tz_rules[0].name, _time_info->ut0, len);
119 memcpy((PTR) tz_rules[1].name, _time_info->ut0, len);
120 tz_rules[0].type = tz_rules[1].type = J0;
121 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
122 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
123 tz_rules[0].secs = tz_rules[1].secs = 0;
124 tz_rules[0].offset = tz_rules[1].offset = 0L;
125 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
126 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
132 /* Get the standard timezone name. */
133 tz_rules[0].name = (char *) malloc(strlen(tz) + 1);
134 if (tz_rules[0].name == NULL)
137 if (sscanf(tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
138 (l = strlen(tz_rules[0].name)) < 3)
141 tz_rules[0].name = (char *) realloc((PTR) tz_rules[0].name, l + 1);
142 if (tz_rules[0].name == NULL)
147 /* Figure out the standard offset from GMT. */
148 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit(*tz)))
151 if (*tz == '-' || *tz == '+')
152 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
154 tz_rules[0].offset = -1L;
155 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
166 tz_rules[0].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
167 (min(hh, 12) * 60 * 60));
169 for (l = 0; l < 3; ++l)
173 if (l < 2 && *tz == ':')
177 /* Get the DST timezone name (if any). */
179 tz_rules[1].name = (char *) "";
182 tz_rules[1].name = (char *) malloc(strlen(tz) + 1);
183 if (tz_rules[1].name == NULL)
185 if (sscanf(tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
186 (l = strlen(tz_rules[1].name)) < 3)
188 tz_rules[1].name = (char *) realloc((PTR) tz_rules[1].name, l + 1);
189 if (tz_rules[1].name == NULL)
195 /* Figure out the DST offset from GMT. */
196 if (*tz == '-' || *tz == '+')
197 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
199 tz_rules[1].offset = -1L;
201 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
204 /* Default to one hour later than standard time. */
205 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
213 tz_rules[1].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
214 (min(hh, 12) * (60 * 60)));
217 for (l = 0; l < 3; ++l)
221 if (l < 2 && *tz == ':')
225 /* If no standard or DST offset was given, default to GMT
226 for standard and one hour later than standard for DST. */
227 if (*tz_rules[0].name == '\0')
228 tz_rules[0].offset = 0L;
229 if (*tz_rules[1].name == '\0')
230 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
232 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
234 /* There is no rule. See if there is a default rule file. */
235 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
236 tz_rules[0].offset, tz_rules[1].offset);
241 /* Figure out the standard <-> DST rules. */
242 for (whichrule = 0; whichrule < 2; ++whichrule)
244 register tz_rule *tzr = &tz_rules[whichrule];
246 if (*tz != '\0' && *tz == ',')
253 /* Get the date of the change. */
254 if (*tz == 'J' || isdigit(*tz))
257 tzr->type = *tz == 'J' ? J1 : J0;
258 if (tzr->type == J1 && !isdigit(*++tz))
260 tzr->n = (unsigned short int) strtoul(tz, &end, 10);
261 if (end == tz || tzr->n > 365)
263 else if (tzr->type == J1 && tzr->n == 0)
265 if (tzr->type == J1 && tzr->n == 60)
266 /* Can't do February 29. */
274 if (sscanf (tz, "M%hu.%hu.%hu%n",
275 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
276 tzr->m < 1 || tzr->m > 12 ||
277 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
281 else if (*tz == '\0')
283 /* United States Federal Law, the equivalent of "M3.1.0,M8.5.0". */
285 if (tzr == &tz_rules[0])
301 if (*tz != '\0' && *tz != '/' && *tz != ',')
305 /* Get the time of day of the change. */
309 switch (sscanf(tz, "%hu:%hu:%hu", &hh, &mm, &ss))
320 for (l = 0; l < 3; ++l)
324 if (l < 2 && *tz == ':')
327 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
330 /* Default to 2:00 AM. */
331 tzr->secs = 2 * 60 * 60;
333 tzr->computed_for = -1;
339 /* Figure out the exact time (as a time_t) in YEAR
340 when the change described by RULE will occur and
341 put it in RULE->change, saving YEAR in RULE->computed_for.
342 Return nonzero if successful, zero on failure. */
344 DEFUN(compute_change, (rule, year), tz_rule *rule AND int year)
346 register unsigned short int m = rule->m, n = rule->n, d = rule->d;
350 if (year != -1 && rule->computed_for == year)
351 /* Operations on times in 1969 will be slower. Oh well. */
354 memset((PTR) &tbuf, 0, sizeof(tbuf));
359 /* Defined in _offtime.c. */
360 extern CONST unsigned short int __mon_lengths[2][12];
361 unsigned short int ml = __mon_lengths[__isleap(tbuf.tm_year)][m - 1];
366 tbuf.tm_mday = max((n - 1) * 7, 1);
367 tbuf.tm_sec = rule->secs;
369 if (t == (time_t) -1)
371 if (tbuf.tm_wday != d)
373 if (d > tbuf.tm_wday)
376 tbuf.tm_mday += tbuf.tm_wday - d;
379 tbuf.tm_mday -= tbuf.tm_wday - d;
380 if (tbuf.tm_mday < 1)
383 if (tbuf.tm_mday > ml) tbuf.tm_mday -= 7;
385 if (t == (time_t) -1)
392 if (rule->type == J1)
395 tbuf.tm_sec = rule->secs;
397 if (t == (time_t) -1)
402 rule->computed_for = year;
407 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
408 and set `__tzname', `__timezone', and `__daylight' accordingly.
409 Return nonzero on success, zero on failure. */
411 DEFUN(__tz_compute, (timer, tm),
412 time_t timer AND struct tm tm)
418 return __tzfile_compute(timer, tm);
420 if (!compute_change(&tz_rules[0], tm.tm_year) ||
421 !compute_change(&tz_rules[1], tm.tm_year))
424 __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
425 __timezone = tz_rules[__daylight ? 1 : 0].offset;
426 __tzname[0] = (char *) tz_rules[0].name;
427 __tzname[1] = (char *) tz_rules[1].name;
434 DEFUN_VOID (__tzname_max)
436 size_t len0 = strlen (__tzname[0]), len1 = strlen (__tzname[1]);
437 return len0 > len1 ? len0 : len1;