Wed May 29 00:57:37 1996 David Mosberger-Tang <davidm@azstarnet.com>
[platform/upstream/glibc.git] / time / tzset.c
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
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>
26
27 /* Defined in mktime.c.  */
28 extern CONST unsigned short int __mon_yday[2][13];
29
30 #define NOID
31 #include "tzfile.h"
32
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));
38
39 char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
40 int __daylight = 0;
41 long int __timezone = 0L;
42
43 weak_alias (__tzname, tzname)
44 weak_alias (__daylight, daylight)
45 weak_alias (__timezone, timezone)
46
47
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)
51
52
53 /* This structure contains all the information about a
54    timezone given in the POSIX standard TZ envariable.  */
55 typedef struct
56   {
57     char *name;
58
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.  */
63
64     long int offset;            /* Seconds east of GMT (west if < 0).  */
65
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;
71
72 /* tz_rules[0] is standard, tz_rules[1] is daylight.  */
73 static tz_rule tz_rules[2];
74 \f
75 int __tzset_run = 0;
76
77 /* Interpret the TZ envariable.  */
78 void
79 DEFUN_VOID(__tzset)
80 {
81   register CONST char *tz;
82   register size_t l;
83   unsigned short int hh, mm, ss;
84   unsigned short int whichrule;
85
86   /* Free old storage.  */
87   if (tz_rules[0].name != NULL && *tz_rules[0].name != '\0')
88     {
89       free((PTR) tz_rules[0].name);
90       tz_rules[0].name = NULL;
91     }
92   if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
93       tz_rules[1].name != tz_rules[0].name)
94     {
95       free((PTR) tz_rules[1].name);
96       tz_rules[1].name = NULL;
97     }
98
99   /* Examine the TZ environment variable.  */
100   tz = getenv ("TZ");
101
102   if (tz != NULL)
103     {
104       /* A leading colon means "implementation defined syntax".
105          We ignore the colon and always use the same algorithm:
106          try a data file, and if none exists parse the 1003.1 syntax.  */
107       if (*tz == ':')
108         ++tz;
109
110       __tzfile_read (tz);
111       if (__use_tzfile)
112         {
113           __tzset_run = 1;
114           return;
115         }
116     }
117
118   if (tz == NULL || *tz == '\0')
119     {
120       __tzfile_read((char *) NULL);
121       if (!__use_tzfile)
122         {
123           const char UTC[] = "UTC";
124           size_t len = sizeof UTC;
125           tz_rules[0].name = (char *) malloc(len);
126           if (tz_rules[0].name == NULL)
127             return;
128           tz_rules[1].name = (char *) malloc(len);
129           if (tz_rules[1].name == NULL)
130             return;
131           memcpy ((PTR) tz_rules[0].name, UTC, len);
132           memcpy ((PTR) tz_rules[1].name, UTC, len);
133           tz_rules[0].type = tz_rules[1].type = J0;
134           tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
135           tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
136           tz_rules[0].secs = tz_rules[1].secs = 0;
137           tz_rules[0].offset = tz_rules[1].offset = 0L;
138           tz_rules[0].change = tz_rules[1].change = (time_t) -1;
139           tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
140         }
141       __tzset_run = 1;
142       return;
143     }
144
145   /* Clear out old state and reset to unnamed UTC.  */
146   memset (tz_rules, 0, sizeof tz_rules);
147   tz_rules[0].name = tz_rules[1].name = (char *) "";
148
149   /* Get the standard timezone name.  */
150   tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
151   if (tz_rules[0].name == NULL)
152     /* Don't set __tzset_run so we will try again.  */
153     return;
154
155   if (sscanf(tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
156       (l = strlen(tz_rules[0].name)) < 3)
157     {
158       free (tz_rules[0].name);
159       tz_rules[0].name = (char *) "";
160       return;
161     }
162
163   {
164     char *n = realloc ((PTR) tz_rules[0].name, l + 1);
165     if (n != NULL)
166       tz_rules[0].name = n;
167   }
168
169   tz += l;
170
171   /* Figure out the standard offset from UTC.  */
172   if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit(*tz)))
173     return;
174
175   if (*tz == '-' || *tz == '+')
176     tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
177   else
178     tz_rules[0].offset = -1L;
179   switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
180     {
181     default:
182       return;
183     case 1:
184       mm = 0;
185     case 2:
186       ss = 0;
187     case 3:
188       break;
189     }
190   tz_rules[0].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
191                          (min(hh, 23) * 60 * 60));
192
193   for (l = 0; l < 3; ++l)
194     {
195       while (isdigit(*tz))
196         ++tz;
197       if (l < 2 && *tz == ':')
198         ++tz;
199     }
200
201   /* Get the DST timezone name (if any).  */
202   if (*tz != '\0')
203     {
204       char *n = malloc (strlen(tz) + 1);
205       if (n != NULL)
206         {
207           tz_rules[1].name = n;
208           if (sscanf(tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
209               (l = strlen(tz_rules[1].name)) < 3)
210             {
211               free (n);
212               tz_rules[1].name = (char *) "";
213               goto done_names;  /* Punt on name, set up the offsets.  */
214             }
215           n = realloc ((PTR) tz_rules[1].name, l + 1);
216           if (n != NULL)
217             tz_rules[1].name = n;
218         }
219     }
220
221   tz += l;
222
223   /* Figure out the DST offset from GMT.  */
224   if (*tz == '-' || *tz == '+')
225     tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
226   else
227     tz_rules[1].offset = -1L;
228
229   switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
230     {
231     default:
232       /* Default to one hour later than standard time.  */
233       tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
234       break;
235
236     case 1:
237       mm = 0;
238     case 2:
239       ss = 0;
240     case 3:
241       tz_rules[1].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
242                              (min(hh, 23) * (60 * 60)));
243       break;
244     }
245   for (l = 0; l < 3; ++l)
246     {
247       while (isdigit (*tz))
248         ++tz;
249       if (l < 2 && *tz == ':')
250         ++tz;
251     }
252
253  done_names:
254
255   if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
256     {
257       /* There is no rule.  See if there is a default rule file.  */
258       __tzfile_default (tz_rules[0].name, tz_rules[1].name,
259                         tz_rules[0].offset, tz_rules[1].offset);
260       if (__use_tzfile)
261         {
262           __tzset_run = 1;
263           return;
264         }
265     }
266
267   /* Figure out the standard <-> DST rules.  */
268   for (whichrule = 0; whichrule < 2; ++whichrule)
269     {
270       register tz_rule *tzr = &tz_rules[whichrule];
271
272       if (*tz == ',')
273         {
274           ++tz;
275           if (*tz == '\0')
276             return;
277         }
278
279       /* Get the date of the change.  */
280       if (*tz == 'J' || isdigit (*tz))
281         {
282           char *end;
283           tzr->type = *tz == 'J' ? J1 : J0;
284           if (tzr->type == J1 && !isdigit (*++tz))
285             return;
286           tzr->d = (unsigned short int) strtoul (tz, &end, 10);
287           if (end == tz || tzr->d > 365)
288             return;
289           else if (tzr->type == J1 && tzr->d == 0)
290             return;
291           tz = end;
292         }
293       else if (*tz == 'M')
294         {
295           int n;
296           tzr->type = M;
297           if (sscanf (tz, "M%hu.%hu.%hu%n",
298                       &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
299               tzr->m < 1 || tzr->m > 12 ||
300               tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
301             return;
302           tz += n;
303         }
304       else if (*tz == '\0')
305         {
306           /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0".  */
307           tzr->type = M;
308           if (tzr == &tz_rules[0])
309             {
310               tzr->m = 4;
311               tzr->n = 1;
312               tzr->d = 0;
313             }
314           else
315             {
316               tzr->m = 10;
317               tzr->n = 5;
318               tzr->d = 0;
319             }
320         }
321       else
322         return;
323
324       if (*tz != '\0' && *tz != '/' && *tz != ',')
325         return;
326       else if (*tz == '/')
327         {
328           /* Get the time of day of the change.  */
329           ++tz;
330           if (*tz == '\0')
331             return;
332           switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
333             {
334             default:
335               hh = 2;           /* Default to 2:00 AM.  */
336             case 1:
337               mm = 0;
338             case 2:
339               ss = 0;
340             case 3:
341               break;
342             }
343           for (l = 0; l < 3; ++l)
344             {
345               while (isdigit(*tz))
346                 ++tz;
347               if (l < 2 && *tz == ':')
348                 ++tz;
349             }
350           tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
351         }
352       else
353         /* Default to 2:00 AM.  */
354         tzr->secs = 2 * 60 * 60;
355
356       tzr->computed_for = -1;
357     }
358
359   __tzset_run = 1;
360 }
361 \f
362 /* Maximum length of a timezone name.  __tz_compute keeps this up to date
363    (never decreasing it) when ! __use_tzfile.
364    tzfile.c keeps it up to date when __use_tzfile.  */
365 long int __tzname_cur_max;
366
367 long int
368 DEFUN_VOID(__tzname_max)
369 {
370   if (! __tzset_run)
371     __tzset ();
372
373   return __tzname_cur_max;
374 }
375 \f
376 /* Figure out the exact time (as a time_t) in YEAR
377    when the change described by RULE will occur and
378    put it in RULE->change, saving YEAR in RULE->computed_for.
379    Return nonzero if successful, zero on failure.  */
380 static int
381 DEFUN(compute_change, (rule, year), tz_rule *rule AND int year)
382 {
383   register time_t t;
384   int y;
385
386   if (year != -1 && rule->computed_for == year)
387     /* Operations on times in 1969 will be slower.  Oh well.  */
388     return 1;
389
390   /* First set T to January 1st, 0:00:00 GMT in YEAR.  */
391   t = 0;
392   for (y = 1970; y < year; ++y)
393     t += SECSPERDAY * (__isleap (y) ? 366 : 365);
394
395   switch (rule->type)
396     {
397     case J1:
398       /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
399          In non-leap years, or if the day number is 59 or less, just
400          add SECSPERDAY times the day number-1 to the time of
401          January 1, midnight, to get the day.  */
402       t += (rule->d - 1) * SECSPERDAY;
403       if (rule->d >= 60 && __isleap (year))
404         t += SECSPERDAY;
405       break;
406
407     case J0:
408       /* n - Day of year.
409          Just add SECSPERDAY times the day number to the time of Jan 1st.  */
410       t += rule->d * SECSPERDAY;
411       break;
412
413     case M:
414       /* Mm.n.d - Nth "Dth day" of month M.  */
415       {
416         register int i, d, m1, yy0, yy1, yy2, dow;
417         register CONST unsigned short int *myday =
418           &__mon_yday[__isleap (year)][rule->m];
419
420         /* First add SECSPERDAY for each day in months before M.  */
421         t += myday[-1] * SECSPERDAY;
422
423         /* Use Zeller's Congruence to get day-of-week of first day of month. */
424         m1 = (rule->m + 9) % 12 + 1;
425         yy0 = (rule->m <= 2) ? (year - 1) : year;
426         yy1 = yy0 / 100;
427         yy2 = yy0 % 100;
428         dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
429         if (dow < 0)
430           dow += 7;
431
432         /* DOW is the day-of-week of the first day of the month.  Get the
433            day-of-month (zero-origin) of the first DOW day of the month.  */
434         d = rule->d - dow;
435         if (d < 0)
436           d += 7;
437         for (i = 1; i < rule->n; ++i)
438           {
439             if (d + 7 >= myday[0] - myday[-1])
440               break;
441             d += 7;
442           }
443
444         /* D is the day-of-month (zero-origin) of the day we want.  */
445         t += d * SECSPERDAY;
446       }
447       break;
448     }
449
450   /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
451      Just add the time of day and local offset from GMT, and we're done.  */
452
453   rule->change = t + rule->offset + rule->secs;
454   rule->computed_for = year;
455   return 1;
456 }
457
458
459 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
460    and set `__tzname', `__timezone', and `__daylight' accordingly.
461    Return nonzero on success, zero on failure.  */
462 int
463 DEFUN(__tz_compute, (timer, tm),
464       time_t timer AND const struct tm *tm)
465 {
466   if (! __tzset_run)
467     __tzset ();
468
469   if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) ||
470       ! compute_change (&tz_rules[1], 1900 + tm->tm_year))
471     return 0;
472
473   __daylight = timer >= tz_rules[0].change && timer < tz_rules[1].change;
474   __timezone = tz_rules[__daylight ? 1 : 0].offset;
475   __tzname[0] = (char *) tz_rules[0].name;
476   __tzname[1] = (char *) tz_rules[1].name;
477
478   {
479     /* Keep __tzname_cur_max up to date.  */
480     size_t len0 = strlen (__tzname[0]);
481     size_t len1 = strlen (__tzname[1]);
482     if (len0 > __tzname_cur_max)
483       __tzname_cur_max = len0;
484     if (len1 > __tzname_cur_max)
485       __tzname_cur_max = len1;
486   }
487
488   return 1;
489 }
490
491 weak_alias (__tzset, tzset)