1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
26 /* Defined in mktime.c. */
27 extern const unsigned short int __mon_yday
[2][13];
32 extern int __use_tzfile
;
33 extern void __tzfile_read
__P ((const char *file
));
34 extern void __tzfile_default
__P ((char *std
, char *dst
,
35 long int stdoff
, long int dstoff
));
36 extern int __tz_compute
__P ((time_t timer
, const struct tm
*tm
));
38 char *__tzname
[2] = { (char *) "GMT", (char *) "GMT" };
40 long int __timezone
= 0L;
42 weak_alias (__tzname
, tzname
)
43 weak_alias (__daylight
, daylight
)
44 weak_alias (__timezone
, timezone
)
47 #define min(a, b) ((a) < (b) ? (a) : (b))
48 #define max(a, b) ((a) > (b) ? (a) : (b))
49 #define sign(x) ((x) < 0 ? -1 : 1)
52 /* This structure contains all the information about a
53 timezone given in the POSIX standard TZ envariable. */
59 enum { J0
, J1
, M
} type
; /* Interpretation of: */
60 unsigned short int m
, n
, d
; /* Month, week, day. */
61 unsigned int secs
; /* Time of day. */
63 long int offset
; /* Seconds east of GMT (west if < 0). */
65 /* We cache the computed time of change for a
66 given year so we don't have to recompute it. */
67 time_t change
; /* When to change to this zone. */
68 int computed_for
; /* Year above is computed for. */
71 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
72 static tz_rule tz_rules
[2];
75 static int compute_change
__P ((tz_rule
*rule
, int year
));
77 static char *old_tz
= NULL
;
79 /* Interpret the TZ envariable. */
80 void __tzset_internal
__P ((int always
));
82 __tzset_internal (always
)
85 static int is_initialized
= 0;
86 register const char *tz
;
88 unsigned short int hh
, mm
, ss
;
89 unsigned short int whichrule
;
91 if (is_initialized
&& !always
)
95 /* Examine the TZ environment variable. */
98 /* A leading colon means "implementation defined syntax".
99 We ignore the colon and always use the same algorithm:
100 try a data file, and if none exists parse the 1003.1 syntax. */
101 if (tz
&& *tz
== ':')
104 /* Check whether the value changes since the last run. */
105 if (old_tz
!= NULL
&& tz
!= NULL
&& strcmp (tz
, old_tz
) == 0)
106 /* No change, simply return. */
109 /* Free old storage. */
110 if (tz_rules
[0].name
!= NULL
&& *tz_rules
[0].name
!= '\0')
111 free((void *) tz_rules
[0].name
);
112 if (tz_rules
[1].name
!= NULL
&& *tz_rules
[1].name
!= '\0' &&
113 tz_rules
[1].name
!= tz_rules
[0].name
)
114 free((void *) tz_rules
[1].name
);
115 tz_rules
[0].name
= NULL
;
116 tz_rules
[1].name
= NULL
;
118 /* Save the value of `tz'. */
121 old_tz
= tz
? __strdup (tz
) : NULL
;
123 /* Try to read a data file. */
128 /* No data file found. Default to UTC if nothing specified. */
130 if (tz
== NULL
|| *tz
== '\0')
132 static const char UTC
[] = "UTC";
133 size_t len
= sizeof UTC
;
134 tz_rules
[0].name
= (char *) malloc (len
);
135 if (tz_rules
[0].name
== NULL
)
137 tz_rules
[1].name
= (char *) malloc (len
);
138 if (tz_rules
[1].name
== NULL
)
140 memcpy ((void *) tz_rules
[0].name
, UTC
, len
);
141 memcpy ((void *) tz_rules
[1].name
, UTC
, len
);
142 tz_rules
[0].type
= tz_rules
[1].type
= J0
;
143 tz_rules
[0].m
= tz_rules
[0].n
= tz_rules
[0].d
= 0;
144 tz_rules
[1].m
= tz_rules
[1].n
= tz_rules
[1].d
= 0;
145 tz_rules
[0].secs
= tz_rules
[1].secs
= 0;
146 tz_rules
[0].offset
= tz_rules
[1].offset
= 0L;
147 tz_rules
[0].change
= tz_rules
[1].change
= (time_t) -1;
148 tz_rules
[0].computed_for
= tz_rules
[1].computed_for
= 0;
152 /* Clear out old state and reset to unnamed UTC. */
153 memset (tz_rules
, 0, sizeof tz_rules
);
154 tz_rules
[0].name
= tz_rules
[1].name
= (char *) "";
156 /* Get the standard timezone name. */
157 tz_rules
[0].name
= (char *) malloc (strlen (tz
) + 1);
158 if (tz_rules
[0].name
== NULL
)
160 /* Clear the old tz name so we will try again. */
166 if (sscanf(tz
, "%[^0-9,+-]", tz_rules
[0].name
) != 1 ||
167 (l
= strlen(tz_rules
[0].name
)) < 3)
169 free (tz_rules
[0].name
);
170 tz_rules
[0].name
= (char *) "";
175 char *n
= realloc ((void *) tz_rules
[0].name
, l
+ 1);
177 tz_rules
[0].name
= n
;
182 /* Figure out the standard offset from UTC. */
183 if (*tz
== '\0' || (*tz
!= '+' && *tz
!= '-' && !isdigit(*tz
)))
186 if (*tz
== '-' || *tz
== '+')
187 tz_rules
[0].offset
= *tz
++ == '-' ? 1L : -1L;
189 tz_rules
[0].offset
= -1L;
190 switch (sscanf (tz
, "%hu:%hu:%hu", &hh
, &mm
, &ss
))
201 tz_rules
[0].offset
*= (min (ss
, 59) + (min (mm
, 59) * 60) +
202 (min (hh
, 23) * 60 * 60));
204 for (l
= 0; l
< 3; ++l
)
208 if (l
< 2 && *tz
== ':')
212 /* Get the DST timezone name (if any). */
215 char *n
= malloc (strlen (tz
) + 1);
218 tz_rules
[1].name
= n
;
219 if (sscanf (tz
, "%[^0-9,+-]", tz_rules
[1].name
) != 1 ||
220 (l
= strlen (tz_rules
[1].name
)) < 3)
223 tz_rules
[1].name
= (char *) "";
224 goto done_names
; /* Punt on name, set up the offsets. */
226 n
= realloc ((void *) tz_rules
[1].name
, l
+ 1);
228 tz_rules
[1].name
= n
;
233 /* Figure out the DST offset from GMT. */
234 if (*tz
== '-' || *tz
== '+')
235 tz_rules
[1].offset
= *tz
++ == '-' ? 1L : -1L;
237 tz_rules
[1].offset
= -1L;
239 switch (sscanf (tz
, "%hu:%hu:%hu", &hh
, &mm
, &ss
))
242 /* Default to one hour later than standard time. */
243 tz_rules
[1].offset
= tz_rules
[0].offset
+ (60 * 60);
251 tz_rules
[1].offset
*= (min (ss
, 59) + (min (mm
, 59) * 60) +
252 (min (hh
, 23) * (60 * 60)));
255 for (l
= 0; l
< 3; ++l
)
257 while (isdigit (*tz
))
259 if (l
< 2 && *tz
== ':')
264 /* There is no DST. */
265 tz_rules
[1].name
= tz_rules
[0].name
;
269 if (*tz
== '\0' || (tz
[0] == ',' && tz
[1] == '\0'))
271 /* There is no rule. See if there is a default rule file. */
272 __tzfile_default (tz_rules
[0].name
, tz_rules
[1].name
,
273 tz_rules
[0].offset
, tz_rules
[1].offset
);
282 /* Figure out the standard <-> DST rules. */
283 for (whichrule
= 0; whichrule
< 2; ++whichrule
)
285 register tz_rule
*tzr
= &tz_rules
[whichrule
];
294 /* Get the date of the change. */
295 if (*tz
== 'J' || isdigit (*tz
))
298 tzr
->type
= *tz
== 'J' ? J1
: J0
;
299 if (tzr
->type
== J1
&& !isdigit (*++tz
))
301 tzr
->d
= (unsigned short int) strtoul (tz
, &end
, 10);
302 if (end
== tz
|| tzr
->d
> 365)
304 else if (tzr
->type
== J1
&& tzr
->d
== 0)
312 if (sscanf (tz
, "M%hu.%hu.%hu%n",
313 &tzr
->m
, &tzr
->n
, &tzr
->d
, &n
) != 3 ||
314 tzr
->m
< 1 || tzr
->m
> 12 ||
315 tzr
->n
< 1 || tzr
->n
> 5 || tzr
->d
> 6)
319 else if (*tz
== '\0')
321 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
323 if (tzr
== &tz_rules
[0])
339 if (*tz
!= '\0' && *tz
!= '/' && *tz
!= ',')
343 /* Get the time of day of the change. */
347 switch (sscanf (tz
, "%hu:%hu:%hu", &hh
, &mm
, &ss
))
350 hh
= 2; /* Default to 2:00 AM. */
358 for (l
= 0; l
< 3; ++l
)
360 while (isdigit (*tz
))
362 if (l
< 2 && *tz
== ':')
365 tzr
->secs
= (hh
* 60 * 60) + (mm
* 60) + ss
;
368 /* Default to 2:00 AM. */
369 tzr
->secs
= 2 * 60 * 60;
371 tzr
->computed_for
= -1;
375 /* Maximum length of a timezone name. __tz_compute keeps this up to date
376 (never decreasing it) when ! __use_tzfile.
377 tzfile.c keeps it up to date when __use_tzfile. */
378 size_t __tzname_cur_max
;
383 __tzset_internal (0);
385 return __tzname_cur_max
;
388 /* Figure out the exact time (as a time_t) in YEAR
389 when the change described by RULE will occur and
390 put it in RULE->change, saving YEAR in RULE->computed_for.
391 Return nonzero if successful, zero on failure. */
393 compute_change (rule
, year
)
400 if (year
!= -1 && rule
->computed_for
== year
)
401 /* Operations on times in 1969 will be slower. Oh well. */
404 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
406 for (y
= 1970; y
< year
; ++y
)
407 t
+= SECSPERDAY
* (__isleap (y
) ? 366 : 365);
412 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
413 In non-leap years, or if the day number is 59 or less, just
414 add SECSPERDAY times the day number-1 to the time of
415 January 1, midnight, to get the day. */
416 t
+= (rule
->d
- 1) * SECSPERDAY
;
417 if (rule
->d
>= 60 && __isleap (year
))
423 Just add SECSPERDAY times the day number to the time of Jan 1st. */
424 t
+= rule
->d
* SECSPERDAY
;
428 /* Mm.n.d - Nth "Dth day" of month M. */
430 register int i
, d
, m1
, yy0
, yy1
, yy2
, dow
;
431 register const unsigned short int *myday
=
432 &__mon_yday
[__isleap (year
)][rule
->m
];
434 /* First add SECSPERDAY for each day in months before M. */
435 t
+= myday
[-1] * SECSPERDAY
;
437 /* Use Zeller's Congruence to get day-of-week of first day of month. */
438 m1
= (rule
->m
+ 9) % 12 + 1;
439 yy0
= (rule
->m
<= 2) ? (year
- 1) : year
;
442 dow
= ((26 * m1
- 2) / 10 + 1 + yy2
+ yy2
/ 4 + yy1
/ 4 - 2 * yy1
) % 7;
446 /* DOW is the day-of-week of the first day of the month. Get the
447 day-of-month (zero-origin) of the first DOW day of the month. */
451 for (i
= 1; i
< rule
->n
; ++i
)
453 if (d
+ 7 >= myday
[0] - myday
[-1])
458 /* D is the day-of-month (zero-origin) of the day we want. */
464 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
465 Just add the time of day and local offset from GMT, and we're done. */
467 rule
->change
= t
- rule
->offset
+ rule
->secs
;
468 rule
->computed_for
= year
;
473 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
474 and set `__tzname', `__timezone', and `__daylight' accordingly.
475 Return nonzero on success, zero on failure. */
477 __tz_compute (timer
, tm
)
481 __tzset_internal (0);
483 if (! compute_change (&tz_rules
[0], 1900 + tm
->tm_year
) ||
484 ! compute_change (&tz_rules
[1], 1900 + tm
->tm_year
))
487 __daylight
= timer
>= tz_rules
[0].change
&& timer
< tz_rules
[1].change
;
488 __timezone
= tz_rules
[__daylight
? 1 : 0].offset
;
489 __tzname
[0] = (char *) tz_rules
[0].name
;
490 __tzname
[1] = (char *) tz_rules
[1].name
;
493 /* Keep __tzname_cur_max up to date. */
494 size_t len0
= strlen (__tzname
[0]);
495 size_t len1
= strlen (__tzname
[1]);
496 if (len0
> __tzname_cur_max
)
497 __tzname_cur_max
= len0
;
498 if (len1
> __tzname_cur_max
)
499 __tzname_cur_max
= len1
;
505 #include <libc-lock.h>
507 /* This locks all the state variables in tzfile.c and this file. */
508 __libc_lock_define (, __tzset_lock
)
510 /* Reinterpret the TZ environment variable and set `tzname'. */
516 __libc_lock_lock (__tzset_lock
);
518 __tzset_internal (1);
523 __tzname
[0] = (char *) tz_rules
[0].name
;
524 __tzname
[1] = (char *) tz_rules
[1].name
;
527 __libc_lock_unlock (__tzset_lock
);
529 weak_alias (__tzset
, tzset
)