Regenerated: autoconf sysdeps/generic/configure.in
[glibc/history.git] / time / tzset.c
blob2635155dd2e2b40ed9bef14b4416b751d7ef3a96
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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 <ctype.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
27 /* Defined in mktime.c. */
28 extern CONST unsigned short int __mon_yday[2][13];
30 #define NOID
31 #include "tzfile.h"
33 extern int __use_tzfile;
34 extern void EXFUN(__tzfile_read, (CONST char *file));
35 extern void EXFUN(__tzfile_default, (char *std AND char *dst AND
36 long int stdoff AND long int dstoff));
37 extern int EXFUN(__tzfile_compute, (time_t, struct tm));
39 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
40 int __daylight = 0;
41 long int __timezone = 0L;
43 weak_alias (__tzname, tzname)
44 weak_alias (__daylight, daylight)
45 weak_alias (__timezone, timezone)
48 #define min(a, b) ((a) < (b) ? (a) : (b))
49 #define max(a, b) ((a) > (b) ? (a) : (b))
50 #define sign(x) ((x) < 0 ? -1 : 1)
53 /* This structure contains all the information about a
54 timezone given in the POSIX standard TZ envariable. */
55 typedef struct
57 char *name;
59 /* When to change. */
60 enum { J0, J1, M } type; /* Interpretation of: */
61 unsigned short int m, n, d; /* Month, week, day. */
62 unsigned int secs; /* Time of day. */
64 long int offset; /* Seconds east of GMT (west if < 0). */
66 /* We cache the computed time of change for a
67 given year so we don't have to recompute it. */
68 time_t change; /* When to change to this zone. */
69 int computed_for; /* Year above is computed for. */
70 } tz_rule;
72 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
73 static tz_rule tz_rules[2];
75 int __tzset_run = 0;
77 /* Interpret the TZ envariable. */
78 void
79 DEFUN_VOID(__tzset)
81 register CONST char *tz;
82 register size_t l;
83 unsigned short int hh, mm, ss;
84 unsigned short int whichrule;
86 /* Free old storage. */
87 if (tz_rules[0].name != NULL && *tz_rules[0].name != '\0')
88 free((PTR) tz_rules[0].name);
89 if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
90 tz_rules[1].name != tz_rules[0].name)
91 free((PTR) tz_rules[1].name);
93 tz = getenv("TZ");
95 if (tz != NULL && *tz == ':')
97 __tzfile_read(tz + 1);
98 if (__use_tzfile)
100 __tzset_run = 1;
101 return;
103 else
104 tz = NULL;
107 if (tz == NULL || *tz == '\0')
109 __tzfile_read((char *) NULL);
110 if (!__use_tzfile)
112 const char UTC[] = "UTC";
113 size_t len = sizeof UTC;
114 tz_rules[0].name = (char *) malloc(len);
115 if (tz_rules[0].name == NULL)
116 return;
117 tz_rules[1].name = (char *) malloc(len);
118 if (tz_rules[1].name == NULL)
119 return;
120 memcpy ((PTR) tz_rules[0].name, UTC, len);
121 memcpy ((PTR) tz_rules[1].name, UTC, len);
122 tz_rules[0].type = tz_rules[1].type = J0;
123 tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
124 tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
125 tz_rules[0].secs = tz_rules[1].secs = 0;
126 tz_rules[0].offset = tz_rules[1].offset = 0L;
127 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
128 tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
130 __tzset_run = 1;
131 return;
134 /* Clear out old state and reset to unnamed UTC. */
135 memset (tz_rules, 0, sizeof tz_rules);
136 tz_rules[0].name = tz_rules[1].name = (char *) "";
138 /* Get the standard timezone name. */
139 tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
140 if (tz_rules[0].name == NULL)
141 /* Don't set __tzset_run so we will try again. */
142 return;
144 if (sscanf(tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
145 (l = strlen(tz_rules[0].name)) < 3)
147 free (tz_rules[0].name);
148 tz_rules[0].name = (char *) "";
149 return;
153 char *n = realloc ((PTR) tz_rules[0].name, l + 1);
154 if (n != NULL)
155 tz_rules[0].name = n;
158 tz += l;
160 /* Figure out the standard offset from UTC. */
161 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit(*tz)))
162 return;
164 if (*tz == '-' || *tz == '+')
165 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
166 else
167 tz_rules[0].offset = -1L;
168 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
170 default:
171 return;
172 case 1:
173 mm = 0;
174 case 2:
175 ss = 0;
176 case 3:
177 break;
179 tz_rules[0].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
180 (min(hh, 23) * 60 * 60));
182 for (l = 0; l < 3; ++l)
184 while (isdigit(*tz))
185 ++tz;
186 if (l < 2 && *tz == ':')
187 ++tz;
190 /* Get the DST timezone name (if any). */
191 if (*tz != '\0')
193 char *n = malloc (strlen(tz) + 1);
194 if (n != NULL)
196 tz_rules[1].name = n;
197 if (sscanf(tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
198 (l = strlen(tz_rules[1].name)) < 3)
200 free (n);
201 tz_rules[1].name = (char *) "";
202 goto done_names; /* Punt on name, set up the offsets. */
204 n = realloc ((PTR) tz_rules[1].name, l + 1);
205 if (n != NULL)
206 tz_rules[1].name = n;
210 tz += l;
212 /* Figure out the DST offset from GMT. */
213 if (*tz == '-' || *tz == '+')
214 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
215 else
216 tz_rules[1].offset = -1L;
218 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
220 default:
221 /* Default to one hour later than standard time. */
222 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
223 break;
225 case 1:
226 mm = 0;
227 case 2:
228 ss = 0;
229 case 3:
230 tz_rules[1].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
231 (min(hh, 23) * (60 * 60)));
232 break;
234 for (l = 0; l < 3; ++l)
236 while (isdigit (*tz))
237 ++tz;
238 if (l < 2 && *tz == ':')
239 ++tz;
242 done_names:
244 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
246 /* There is no rule. See if there is a default rule file. */
247 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
248 tz_rules[0].offset, tz_rules[1].offset);
249 if (__use_tzfile)
251 __tzset_run = 1;
252 return;
256 /* Figure out the standard <-> DST rules. */
257 for (whichrule = 0; whichrule < 2; ++whichrule)
259 register tz_rule *tzr = &tz_rules[whichrule];
261 if (*tz == ',')
263 ++tz;
264 if (*tz == '\0')
265 return;
268 /* Get the date of the change. */
269 if (*tz == 'J' || isdigit (*tz))
271 char *end;
272 tzr->type = *tz == 'J' ? J1 : J0;
273 if (tzr->type == J1 && !isdigit (*++tz))
274 return;
275 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
276 if (end == tz || tzr->d > 365)
277 return;
278 else if (tzr->type == J1 && tzr->d == 0)
279 return;
280 tz = end;
282 else if (*tz == 'M')
284 int n;
285 tzr->type = M;
286 if (sscanf (tz, "M%hu.%hu.%hu%n",
287 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
288 tzr->m < 1 || tzr->m > 12 ||
289 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
290 return;
291 tz += n;
293 else if (*tz == '\0')
295 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
296 tzr->type = M;
297 if (tzr == &tz_rules[0])
299 tzr->m = 4;
300 tzr->n = 1;
301 tzr->d = 0;
303 else
305 tzr->m = 10;
306 tzr->n = 5;
307 tzr->d = 0;
310 else
311 return;
313 if (*tz != '\0' && *tz != '/' && *tz != ',')
314 return;
315 else if (*tz == '/')
317 /* Get the time of day of the change. */
318 ++tz;
319 if (*tz == '\0')
320 return;
321 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
323 default:
324 hh = 2; /* Default to 2:00 AM. */
325 case 1:
326 mm = 0;
327 case 2:
328 ss = 0;
329 case 3:
330 break;
332 for (l = 0; l < 3; ++l)
334 while (isdigit(*tz))
335 ++tz;
336 if (l < 2 && *tz == ':')
337 ++tz;
339 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
341 else
342 /* Default to 2:00 AM. */
343 tzr->secs = 2 * 60 * 60;
345 tzr->computed_for = -1;
348 __tzset_run = 1;
351 /* Maximum length of a timezone name. __tz_compute keeps this up to date
352 (never decreasing it) when ! __use_tzfile.
353 tzfile.c keeps it up to date when __use_tzfile. */
354 long int __tzname_cur_max;
356 long int
357 DEFUN_VOID(__tzname_max)
359 if (! __tzset_run)
360 __tzset ();
362 return __tzname_cur_max;
365 /* Figure out the exact time (as a time_t) in YEAR
366 when the change described by RULE will occur and
367 put it in RULE->change, saving YEAR in RULE->computed_for.
368 Return nonzero if successful, zero on failure. */
369 static int
370 DEFUN(compute_change, (rule, year), tz_rule *rule AND int year)
372 register time_t t;
373 int y;
375 if (year != -1 && rule->computed_for == year)
376 /* Operations on times in 1969 will be slower. Oh well. */
377 return 1;
379 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
380 t = 0;
381 for (y = 1970; y < year; ++y)
382 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
384 switch (rule->type)
386 case J1:
387 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
388 In non-leap years, or if the day number is 59 or less, just
389 add SECSPERDAY times the day number-1 to the time of
390 January 1, midnight, to get the day. */
391 t += (rule->d - 1) * SECSPERDAY;
392 if (rule->d >= 60 && __isleap (year))
393 t += SECSPERDAY;
394 break;
396 case J0:
397 /* n - Day of year.
398 Just add SECSPERDAY times the day number to the time of Jan 1st. */
399 t += rule->d * SECSPERDAY;
400 break;
402 case M:
403 /* Mm.n.d - Nth "Dth day" of month M. */
405 register int i, d, m1, yy0, yy1, yy2, dow;
406 register CONST unsigned short int *myday =
407 &__mon_yday[__isleap (year)][rule->m];
409 /* First add SECSPERDAY for each day in months before M. */
410 t += myday[-1] * SECSPERDAY;
412 /* Use Zeller's Congruence to get day-of-week of first day of month. */
413 m1 = (rule->m + 9) % 12 + 1;
414 yy0 = (rule->m <= 2) ? (year - 1) : year;
415 yy1 = yy0 / 100;
416 yy2 = yy0 % 100;
417 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
418 if (dow < 0)
419 dow += 7;
421 /* DOW is the day-of-week of the first day of the month. Get the
422 day-of-month (zero-origin) of the first DOW day of the month. */
423 d = rule->d - dow;
424 if (d < 0)
425 d += 7;
426 for (i = 1; i < rule->n; ++i)
428 if (d + 7 >= myday[0] - myday[-1])
429 break;
430 d += 7;
433 /* D is the day-of-month (zero-origin) of the day we want. */
434 t += d * SECSPERDAY;
436 break;
439 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
440 Just add the time of day and local offset from GMT, and we're done. */
442 rule->change = t + rule->offset + rule->secs;
443 rule->computed_for = year;
444 return 1;
448 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
449 and set `__tzname', `__timezone', and `__daylight' accordingly.
450 Return nonzero on success, zero on failure. */
452 DEFUN(__tz_compute, (timer, tm),
453 time_t timer AND const struct tm *tm)
455 if (! __tzset_run)
456 __tzset ();
458 if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
459 ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
460 return 0;
462 __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
463 __timezone = tz_rules[__daylight ? 1 : 0].offset;
464 __tzname[0] = (char *) tz_rules[0].name;
465 __tzname[1] = (char *) tz_rules[1].name;
468 /* Keep __tzname_cur_max up to date. */
469 size_t len0 = strlen (__tzname[0]);
470 size_t len1 = strlen (__tzname[1]);
471 if (len0 > __tzname_cur_max)
472 __tzname_cur_max = len0;
473 if (len1 > __tzname_cur_max)
474 __tzname_cur_max = len1;
477 return 1;
480 weak_alias (__tzset, tzset)