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. */
20 #include <localeinfo.h>
28 /* Defined in mktime.c. */
29 extern CONST
unsigned short int __mon_lengths
[2][12];
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
45 weak_alias (__tzname
, tzname
)
46 weak_alias (__daylight
, daylight
)
47 weak_alias (__timezone
, timezone
)
50 char *__tzname
[2] = { (char *) "GMT", (char *) "GMT" };
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. */
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. */
79 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
80 static tz_rule tz_rules
[2];
84 /* Interpret the TZ envariable. */
88 register CONST
char *tz
;
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
);
102 if (tz
!= NULL
&& *tz
== ':')
104 __tzfile_read(tz
+ 1);
114 if (tz
== NULL
|| *tz
== '\0')
116 if (tz
== NULL
|| *tz
== '\0')
118 __tzfile_read((char *) NULL
);
121 size_t len
= strlen(_time_info
->ut0
) + 1;
122 tz_rules
[0].name
= (char *) malloc(len
);
123 if (tz_rules
[0].name
== NULL
)
125 tz_rules
[1].name
= (char *) malloc(len
);
126 if (tz_rules
[1].name
== NULL
)
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;
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. */
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 *) "";
161 char *n
= realloc ((PTR
) tz_rules
[0].name
, l
+ 1);
163 tz_rules
[0].name
= n
;
168 /* Figure out the standard offset from GMT. */
169 if (*tz
== '\0' || (*tz
!= '+' && *tz
!= '-' && !isdigit(*tz
)))
172 if (*tz
== '-' || *tz
== '+')
173 tz_rules
[0].offset
= *tz
++ == '-' ? 1L : -1L;
175 tz_rules
[0].offset
= -1L;
176 switch (sscanf (tz
, "%hu:%hu:%hu", &hh
, &mm
, &ss
))
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
)
194 if (l
< 2 && *tz
== ':')
198 /* Get the DST timezone name (if any). */
201 char *n
= malloc (strlen(tz
) + 1);
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)
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);
214 tz_rules
[1].name
= n
;
220 /* Figure out the DST offset from GMT. */
221 if (*tz
== '-' || *tz
== '+')
222 tz_rules
[1].offset
= *tz
++ == '-' ? 1L : -1L;
224 tz_rules
[1].offset
= -1L;
226 switch (sscanf (tz
, "%hu:%hu:%hu", &hh
, &mm
, &ss
))
229 /* Default to one hour later than standard time. */
230 tz_rules
[1].offset
= tz_rules
[0].offset
+ (60 * 60);
238 tz_rules
[1].offset
*= (min(ss
, 59) + (min(mm
, 59) * 60) +
239 (min(hh
, 12) * (60 * 60)));
242 for (l
= 0; l
< 3; ++l
)
244 while (isdigit (*tz
))
246 if (l
< 2 && *tz
== ':')
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
);
264 /* Figure out the standard <-> DST rules. */
265 for (whichrule
= 0; whichrule
< 2; ++whichrule
)
267 register tz_rule
*tzr
= &tz_rules
[whichrule
];
276 /* Get the date of the change. */
277 if (*tz
== 'J' || isdigit (*tz
))
280 tzr
->type
= *tz
== 'J' ? J1
: J0
;
281 if (tzr
->type
== J1
&& !isdigit (*++tz
))
283 tzr
->d
= (unsigned short int) strtoul (tz
, &end
, 10);
284 if (end
== tz
|| tzr
->d
> 365)
286 else if (tzr
->type
== J1
&& tzr
->d
== 0)
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)
301 else if (*tz
== '\0')
303 /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */
305 if (tzr
== &tz_rules
[0])
321 if (*tz
!= '\0' && *tz
!= '/' && *tz
!= ',')
325 /* Get the time of day of the change. */
329 switch (sscanf (tz
, "%hu:%hu:%hu", &hh
, &mm
, &ss
))
332 hh
= 2; /* Default to 2:00 AM. */
340 for (l
= 0; l
< 3; ++l
)
344 if (l
< 2 && *tz
== ':')
347 tzr
->secs
= (hh
* 60 * 60) + (mm
* 60) + ss
;
350 /* Default to 2:00 AM. */
351 tzr
->secs
= 2 * 60 * 60;
353 tzr
->computed_for
= -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
;
365 DEFUN_VOID(__tzname_max
)
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. */
378 DEFUN(compute_change
, (rule
, year
), tz_rule
*rule AND
int year
)
383 if (year
!= -1 && rule
->computed_for
== year
)
384 /* Operations on times in 1969 will be slower. Oh well. */
387 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
389 for (y
= 1970; y
< year
; ++y
)
390 t
+= SECSPERDAY
* (__isleap (y
) ? 366 : 365);
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
))
406 Just add SECSPERDAY times the day number to the time of Jan 1st. */
407 t
+= rule
->d
* SECSPERDAY
;
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
;
424 dow
= ((26 * m1
- 2) / 10 + 1 + yy2
+ yy2
/ 4 + yy1
/ 4 - 2 * yy1
) % 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. */
433 for (i
= 1; i
< rule
->n
; ++i
)
435 if (d
+ 7 >= __mon_lengths
[__isleap (year
)][rule
->m
- 1])
440 /* D is the day-of-month (zero-origin) of the day we want. */
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
;
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
)
465 if (! compute_change (&tz_rules
[0], 1900 + tm
->tm_year
) ||
466 ! compute_change (&tz_rules
[1], 1900 + tm
->tm_year
))
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
;
487 weak_alias (__tzset
, tzset
)