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