* elf/dl-reloc.c: Include "dynamic-link.h" at file scope.
[glibc.git] / time / tzset.c
blobd3a33dc1ceb16d019a9db528123a3a8c0ce49a44
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')
89 free((PTR) tz_rules[0].name);
90 tz_rules[0].name = NULL;
92 if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
93 tz_rules[1].name != tz_rules[0].name)
95 free((PTR) tz_rules[1].name);
96 tz_rules[1].name = NULL;
99 /* Examine the TZ environment variable. */
100 tz = getenv ("TZ");
102 /* A leading colon means "implementation defined syntax".
103 We ignore the colon and always use the same algorithm:
104 try a data file, and if none exists parse the 1003.1 syntax. */
105 if (tz && *tz == ':')
106 ++tz;
108 /* Try to read a data file. */
109 __tzfile_read (tz);
110 if (__use_tzfile)
112 __tzset_run = 1;
113 return;
116 /* No data file found. Default to UTC if nothing specified. */
118 if (tz == NULL || *tz == '\0')
120 static const char UTC[] = "UTC";
121 size_t len = sizeof UTC;
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, UTC, len);
129 memcpy ((PTR) tz_rules[1].name, UTC, 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;
137 __tzset_run = 1;
138 return;
141 /* Clear out old state and reset to unnamed UTC. */
142 memset (tz_rules, 0, sizeof tz_rules);
143 tz_rules[0].name = tz_rules[1].name = (char *) "";
145 /* Get the standard timezone name. */
146 tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
147 if (tz_rules[0].name == NULL)
148 /* Don't set __tzset_run so we will try again. */
149 return;
151 if (sscanf(tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
152 (l = strlen(tz_rules[0].name)) < 3)
154 free (tz_rules[0].name);
155 tz_rules[0].name = (char *) "";
156 return;
160 char *n = realloc ((PTR) tz_rules[0].name, l + 1);
161 if (n != NULL)
162 tz_rules[0].name = n;
165 tz += l;
167 /* Figure out the standard offset from UTC. */
168 if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit(*tz)))
169 return;
171 if (*tz == '-' || *tz == '+')
172 tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
173 else
174 tz_rules[0].offset = -1L;
175 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
177 default:
178 return;
179 case 1:
180 mm = 0;
181 case 2:
182 ss = 0;
183 case 3:
184 break;
186 tz_rules[0].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
187 (min(hh, 23) * 60 * 60));
189 for (l = 0; l < 3; ++l)
191 while (isdigit(*tz))
192 ++tz;
193 if (l < 2 && *tz == ':')
194 ++tz;
197 /* Get the DST timezone name (if any). */
198 if (*tz != '\0')
200 char *n = malloc (strlen(tz) + 1);
201 if (n != NULL)
203 tz_rules[1].name = n;
204 if (sscanf(tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
205 (l = strlen(tz_rules[1].name)) < 3)
207 free (n);
208 tz_rules[1].name = (char *) "";
209 goto done_names; /* Punt on name, set up the offsets. */
211 n = realloc ((PTR) tz_rules[1].name, l + 1);
212 if (n != NULL)
213 tz_rules[1].name = n;
217 tz += l;
219 /* Figure out the DST offset from GMT. */
220 if (*tz == '-' || *tz == '+')
221 tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
222 else
223 tz_rules[1].offset = -1L;
225 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
227 default:
228 /* Default to one hour later than standard time. */
229 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
230 break;
232 case 1:
233 mm = 0;
234 case 2:
235 ss = 0;
236 case 3:
237 tz_rules[1].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
238 (min(hh, 23) * (60 * 60)));
239 break;
241 for (l = 0; l < 3; ++l)
243 while (isdigit (*tz))
244 ++tz;
245 if (l < 2 && *tz == ':')
246 ++tz;
249 done_names:
251 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
253 /* There is no rule. See if there is a default rule file. */
254 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
255 tz_rules[0].offset, tz_rules[1].offset);
256 if (__use_tzfile)
258 __tzset_run = 1;
259 return;
263 /* Figure out the standard <-> DST rules. */
264 for (whichrule = 0; whichrule < 2; ++whichrule)
266 register tz_rule *tzr = &tz_rules[whichrule];
268 if (*tz == ',')
270 ++tz;
271 if (*tz == '\0')
272 return;
275 /* Get the date of the change. */
276 if (*tz == 'J' || isdigit (*tz))
278 char *end;
279 tzr->type = *tz == 'J' ? J1 : J0;
280 if (tzr->type == J1 && !isdigit (*++tz))
281 return;
282 tzr->d = (unsigned short int) strtoul (tz, &end, 10);
283 if (end == tz || tzr->d > 365)
284 return;
285 else if (tzr->type == J1 && tzr->d == 0)
286 return;
287 tz = end;
289 else if (*tz == 'M')
291 int n;
292 tzr->type = M;
293 if (sscanf (tz, "M%hu.%hu.%hu%n",
294 &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
295 tzr->m < 1 || tzr->m > 12 ||
296 tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
297 return;
298 tz += n;
300 else if (*tz == '\0')
302 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
303 tzr->type = M;
304 if (tzr == &tz_rules[0])
306 tzr->m = 4;
307 tzr->n = 1;
308 tzr->d = 0;
310 else
312 tzr->m = 10;
313 tzr->n = 5;
314 tzr->d = 0;
317 else
318 return;
320 if (*tz != '\0' && *tz != '/' && *tz != ',')
321 return;
322 else if (*tz == '/')
324 /* Get the time of day of the change. */
325 ++tz;
326 if (*tz == '\0')
327 return;
328 switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
330 default:
331 hh = 2; /* Default to 2:00 AM. */
332 case 1:
333 mm = 0;
334 case 2:
335 ss = 0;
336 case 3:
337 break;
339 for (l = 0; l < 3; ++l)
341 while (isdigit(*tz))
342 ++tz;
343 if (l < 2 && *tz == ':')
344 ++tz;
346 tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
348 else
349 /* Default to 2:00 AM. */
350 tzr->secs = 2 * 60 * 60;
352 tzr->computed_for = -1;
355 __tzset_run = 1;
358 /* Maximum length of a timezone name. __tz_compute keeps this up to date
359 (never decreasing it) when ! __use_tzfile.
360 tzfile.c keeps it up to date when __use_tzfile. */
361 long int __tzname_cur_max;
363 long int
364 DEFUN_VOID(__tzname_max)
366 if (! __tzset_run)
367 __tzset ();
369 return __tzname_cur_max;
372 /* Figure out the exact time (as a time_t) in YEAR
373 when the change described by RULE will occur and
374 put it in RULE->change, saving YEAR in RULE->computed_for.
375 Return nonzero if successful, zero on failure. */
376 static int
377 DEFUN(compute_change, (rule, year), tz_rule *rule AND int year)
379 register time_t t;
380 int y;
382 if (year != -1 && rule->computed_for == year)
383 /* Operations on times in 1969 will be slower. Oh well. */
384 return 1;
386 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
387 t = 0;
388 for (y = 1970; y < year; ++y)
389 t += SECSPERDAY * (__isleap (y) ? 366 : 365);
391 switch (rule->type)
393 case J1:
394 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
395 In non-leap years, or if the day number is 59 or less, just
396 add SECSPERDAY times the day number-1 to the time of
397 January 1, midnight, to get the day. */
398 t += (rule->d - 1) * SECSPERDAY;
399 if (rule->d >= 60 && __isleap (year))
400 t += SECSPERDAY;
401 break;
403 case J0:
404 /* n - Day of year.
405 Just add SECSPERDAY times the day number to the time of Jan 1st. */
406 t += rule->d * SECSPERDAY;
407 break;
409 case M:
410 /* Mm.n.d - Nth "Dth day" of month M. */
412 register int i, d, m1, yy0, yy1, yy2, dow;
413 register CONST unsigned short int *myday =
414 &__mon_yday[__isleap (year)][rule->m];
416 /* First add SECSPERDAY for each day in months before M. */
417 t += myday[-1] * 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 >= myday[0] - myday[-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)