initial import
[glibc.git] / time / tzset.c
blobe4d5209e88883cd3d41fa8f46e36a11568f49045
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 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. */
19 #include <ansidecl.h>
20 #include <localeinfo.h>
21 #include <ctype.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_lengths[2][12];
31 #define NOID
32 #include "tzfile.h"
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 #ifndef HAVE_WEAK_SYMBOLS
41 #define __tzname tzname
42 #define __daylight daylight
43 #define __timezone timezone
44 #else
45 weak_alias (__tzname, tzname)
46 weak_alias (__daylight, daylight)
47 weak_alias (__timezone, timezone)
48 #endif
50 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
51 int __daylight = 0;
52 long int __timezone = 0L;
55 #define min(a, b) ((a) < (b) ? (a) : (b))
56 #define max(a, b) ((a) > (b) ? (a) : (b))
57 #define sign(x) ((x) < 0 ? -1 : 1)
60 /* This structure contains all the information about a
61 timezone given in the POSIX standard TZ envariable. */
62 typedef struct
64 char *name;
66 /* When to change. */
67 enum { J0, J1, M } type; /* Interpretation of: */
68 unsigned short int m, n, d; /* Month, week, day. */
69 unsigned int secs; /* Time of day. */
71 long int offset; /* Seconds east of GMT (west if < 0). */
73 /* We cache the computed time of change for a
74 given year so we don't have to recompute it. */
75 time_t change; /* When to change to this zone. */
76 int computed_for; /* Year above is computed for. */
77 } tz_rule;
79 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
80 static tz_rule tz_rules[2];
82 int __tzset_run = 0;
84 /* Interpret the TZ envariable. */
85 void
86 DEFUN_VOID(__tzset)
88 register CONST char *tz;
89 register size_t l;
90 unsigned short int hh, mm, ss;
91 unsigned short int whichrule;
93 /* Free old storage. */
94 if (tz_rules[0].name != NULL && *tz_rules[0].name != '\0')
95 free((PTR) tz_rules[0].name);
96 if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
97 tz_rules[1].name != tz_rules[0].name)
98 free((PTR) tz_rules[1].name);
100 tz = getenv("TZ");
102 if (tz != NULL && *tz == ':')
104 __tzfile_read(tz + 1);
105 if (__use_tzfile)
107 __tzset_run = 1;
108 return;
110 else
111 tz = NULL;
114 if (tz == NULL || *tz == '\0')
115 tz = _time_info->tz;
116 if (tz == NULL || *tz == '\0')
118 __tzfile_read((char *) NULL);
119 if (!__use_tzfile)
121 size_t len = strlen(_time_info->ut0) + 1;
122 tz_rules[0].name = (char *) malloc(len);
123 if (tz_rules[0].name == NULL)
124 return;
125 tz_rules[1].name = (char *) malloc(len);
126 if (tz_rules[1].name == NULL)
127 return;
128 memcpy((PTR) tz_rules[0].name, _time_info->ut0, len);
129 memcpy((PTR) tz_rules[1].name, _time_info->ut0, len);
130 tz_rules[0].type = tz_rules[1].type = J0;
131 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
132 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
133 tz_rules[0].secs = tz_rules[1].secs = 0;
134 tz_rules[0].offset = tz_rules[1].offset = 0L;
135 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
136 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
138 __tzset_run = 1;
139 return;
142 /* Clear out old state and reset to unnamed GMT. */
143 memset (tz_rules, 0, sizeof tz_rules);
144 tz_rules[0].name = tz_rules[1].name = (char *) "";
146 /* Get the standard timezone name. */
147 tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
148 if (tz_rules[0].name == NULL)
149 /* Don't set __tzset_run so we will try again. */
150 return;
152 if (sscanf(tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
153 (l = strlen(tz_rules[0].name)) < 3)
155 free (tz_rules[0].name);
156 tz_rules[0].name = (char *) "";
157 return;
161 char *n = realloc ((PTR) tz_rules[0].name, l + 1);
162 if (n != NULL)
163 tz_rules[0].name = n;
166 tz += l;
168 /* Figure out the standard offset from GMT. */
169 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit(*tz)))
170 return;
172 if (*tz == '-' || *tz == '+')
173 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
174 else
175 tz_rules[0].offset = -1L;
176 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
178 default:
179 return;
180 case 1:
181 mm = 0;
182 case 2:
183 ss = 0;
184 case 3:
185 break;
187 tz_rules[0].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
188 (min(hh, 12) * 60 * 60));
190 for (l = 0; l < 3; ++l)
192 while (isdigit(*tz))
193 ++tz;
194 if (l < 2 && *tz == ':')
195 ++tz;
198 /* Get the DST timezone name (if any). */
199 if (*tz != '\0')
201 char *n = malloc (strlen(tz) + 1);
202 if (n != NULL)
204 tz_rules[1].name = n;
205 if (sscanf(tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
206 (l = strlen(tz_rules[1].name)) < 3)
208 free (n);
209 tz_rules[1].name = (char *) "";
210 goto done_names; /* Punt on name, set up the offsets. */
212 n = realloc ((PTR) tz_rules[1].name, l + 1);
213 if (n != NULL)
214 tz_rules[1].name = n;
218 tz += l;
220 /* Figure out the DST offset from GMT. */
221 if (*tz == '-' || *tz == '+')
222 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
223 else
224 tz_rules[1].offset = -1L;
226 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
228 default:
229 /* Default to one hour later than standard time. */
230 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
231 break;
233 case 1:
234 mm = 0;
235 case 2:
236 ss = 0;
237 case 3:
238 tz_rules[1].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
239 (min(hh, 12) * (60 * 60)));
240 break;
242 for (l = 0; l < 3; ++l)
244 while (isdigit (*tz))
245 ++tz;
246 if (l < 2 && *tz == ':')
247 ++tz;
250 done_names:
252 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
254 /* There is no rule. See if there is a default rule file. */
255 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
256 tz_rules[0].offset, tz_rules[1].offset);
257 if (__use_tzfile)
259 __tzset_run = 1;
260 return;
264 /* Figure out the standard <-> DST rules. */
265 for (whichrule = 0; whichrule < 2; ++whichrule)
267 register tz_rule *tzr = &tz_rules[whichrule];
269 if (*tz == ',')
271 ++tz;
272 if (*tz == '\0')
273 return;
276 /* Get the date of the change. */
277 if (*tz == 'J' || isdigit (*tz))
279 char *end;
280 tzr->type = *tz == 'J' ? J1 : J0;
281 if (tzr->type == J1 && !isdigit (*++tz))
282 return;
283 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
284 if (end == tz || tzr->d > 365)
285 return;
286 else if (tzr->type == J1 && tzr->d == 0)
287 return;
288 tz = end;
290 else if (*tz == 'M')
292 int n;
293 tzr->type = M;
294 if (sscanf (tz, "M%hu.%hu.%hu%n",
295 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
296 tzr->m < 1 || tzr->m > 12 ||
297 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
298 return;
299 tz += n;
301 else if (*tz == '\0')
303 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
304 tzr->type = M;
305 if (tzr == &tz_rules[0])
307 tzr->m = 4;
308 tzr->n = 1;
309 tzr->d = 0;
311 else
313 tzr->m = 10;
314 tzr->n = 5;
315 tzr->d = 0;
318 else
319 return;
321 if (*tz != '\0' && *tz != '/' && *tz != ',')
322 return;
323 else if (*tz == '/')
325 /* Get the time of day of the change. */
326 ++tz;
327 if (*tz == '\0')
328 return;
329 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
331 default:
332 hh = 2; /* Default to 2:00 AM. */
333 case 1:
334 mm = 0;
335 case 2:
336 ss = 0;
337 case 3:
338 break;
340 for (l = 0; l < 3; ++l)
342 while (isdigit(*tz))
343 ++tz;
344 if (l < 2 && *tz == ':')
345 ++tz;
347 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
349 else
350 /* Default to 2:00 AM. */
351 tzr->secs = 2 * 60 * 60;
353 tzr->computed_for = -1;
356 __tzset_run = 1;
359 /* Maximum length of a timezone name. __tz_compute keeps this up to date
360 (never decreasing it) when ! __use_tzfile.
361 tzfile.c keeps it up to date when __use_tzfile. */
362 long int __tzname_cur_max;
364 long int
365 DEFUN_VOID(__tzname_max)
367 if (! __tzset_run)
368 __tzset ();
370 return __tzname_cur_max;
373 /* Figure out the exact time (as a time_t) in YEAR
374 when the change described by RULE will occur and
375 put it in RULE->change, saving YEAR in RULE->computed_for.
376 Return nonzero if successful, zero on failure. */
377 static int
378 DEFUN(compute_change, (rule, year), tz_rule *rule AND int year)
380 register time_t t;
381 int y;
383 if (year != -1 && rule->computed_for == year)
384 /* Operations on times in 1969 will be slower. Oh well. */
385 return 1;
387 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
388 t = 0;
389 for (y = 1970; y < year; ++y)
390 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
392 switch (rule->type)
394 case J1:
395 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
396 In non-leap years, or if the day number is 59 or less, just
397 add SECSPERDAY times the day number-1 to the time of
398 January 1, midnight, to get the day. */
399 t += (rule->d - 1) * SECSPERDAY;
400 if (rule->d >= 60 && __isleap (year))
401 t += SECSPERDAY;
402 break;
404 case J0:
405 /* n - Day of year.
406 Just add SECSPERDAY times the day number to the time of Jan 1st. */
407 t += rule->d * SECSPERDAY;
408 break;
410 case M:
411 /* Mm.n.d - Nth "Dth day" of month M. */
413 register int i, d, m1, yy0, yy1, yy2, dow;
415 /* First add SECSPERDAY for each day in months before M. */
416 for (i = 0; i < rule->m - 1; ++i)
417 t += __mon_lengths[__isleap (year)][i] * SECSPERDAY;
419 /* Use Zeller's Congruence to get day-of-week of first day of month. */
420 m1 = (rule->m + 9) % 12 + 1;
421 yy0 = (rule->m <= 2) ? (year - 1) : year;
422 yy1 = yy0 / 100;
423 yy2 = yy0 % 100;
424 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
425 if (dow < 0)
426 dow += 7;
428 /* DOW is the day-of-week of the first day of the month. Get the
429 day-of-month (zero-origin) of the first DOW day of the month. */
430 d = rule->d - dow;
431 if (d < 0)
432 d += 7;
433 for (i = 1; i < rule->n; ++i)
435 if (d + 7 >= __mon_lengths[__isleap (year)][rule->m - 1])
436 break;
437 d += 7;
440 /* D is the day-of-month (zero-origin) of the day we want. */
441 t += d * SECSPERDAY;
443 break;
446 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
447 Just add the time of day and local offset from GMT, and we're done. */
449 rule->change = t + rule->offset + rule->secs;
450 rule->computed_for = year;
451 return 1;
455 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
456 and set `__tzname', `__timezone', and `__daylight' accordingly.
457 Return nonzero on success, zero on failure. */
459 DEFUN(__tz_compute, (timer, tm),
460 time_t timer AND const struct tm *tm)
462 if (! __tzset_run)
463 __tzset ();
465 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
466 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
467 return 0;
469 __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
470 __timezone = tz_rules[__daylight ? 1 : 0].offset;
471 __tzname[0] = (char *) tz_rules[0].name;
472 __tzname[1] = (char *) tz_rules[1].name;
475 /* Keep __tzname_cur_max up to date. */
476 size_t len0 = strlen (__tzname[0]);
477 size_t len1 = strlen (__tzname[1]);
478 if (len0 > __tzname_cur_max)
479 __tzname_cur_max = len0;
480 if (len1 > __tzname_cur_max)
481 __tzname_cur_max = len1;
484 return 1;
487 weak_alias (__tzset, tzset)