007997f5411628a766f2b6e04c51489b9f4e3ad5
[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     free((PTR) tz_rules[0].name);
89   if (tz_rules[1].name != NULL && *tz_rules[1].name != '\0' &&
90       tz_rules[1].name != tz_rules[0].name)
91     free((PTR) tz_rules[1].name);
92
93   /* Examine the TZ environment variable.  */
94   tz = getenv ("TZ");
95
96   if (tz != NULL)
97     {
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 == ':')
102         ++tz;
103
104       __tzfile_read (tz);
105       if (__use_tzfile)
106         {
107           __tzset_run = 1;
108           return;
109         }
110     }
111
112   if (tz == NULL || *tz == '\0')
113     {
114       __tzfile_read((char *) NULL);
115       if (!__use_tzfile)
116         {
117           const char UTC[] = "UTC";
118           size_t len = sizeof UTC;
119           tz_rules[0].name = (char *) malloc(len);
120           if (tz_rules[0].name == NULL)
121             return;
122           tz_rules[1].name = (char *) malloc(len);
123           if (tz_rules[1].name == NULL)
124             return;
125           memcpy ((PTR) tz_rules[0].name, UTC, len);
126           memcpy ((PTR) tz_rules[1].name, UTC, len);
127           tz_rules[0].type = tz_rules[1].type = J0;
128           tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
129           tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
130           tz_rules[0].secs = tz_rules[1].secs = 0;
131           tz_rules[0].offset = tz_rules[1].offset = 0L;
132           tz_rules[0].change = tz_rules[1].change = (time_t) -1;
133           tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
134         }
135       __tzset_run = 1;
136       return;
137     }
138
139   /* Clear out old state and reset to unnamed UTC.  */
140   memset (tz_rules, 0, sizeof tz_rules);
141   tz_rules[0].name = tz_rules[1].name = (char *) "";
142
143   /* Get the standard timezone name.  */
144   tz_rules[0].name = (char *) malloc (strlen (tz) + 1);
145   if (tz_rules[0].name == NULL)
146     /* Don't set __tzset_run so we will try again.  */
147     return;
148
149   if (sscanf(tz, "%[^0-9,+-]", tz_rules[0].name) != 1 ||
150       (l = strlen(tz_rules[0].name)) < 3)
151     {
152       free (tz_rules[0].name);
153       tz_rules[0].name = (char *) "";
154       return;
155     }
156
157   {
158     char *n = realloc ((PTR) tz_rules[0].name, l + 1);
159     if (n != NULL)
160       tz_rules[0].name = n;
161   }
162
163   tz += l;
164
165   /* Figure out the standard offset from UTC.  */
166   if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit(*tz)))
167     return;
168
169   if (*tz == '-' || *tz == '+')
170     tz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
171   else
172     tz_rules[0].offset = -1L;
173   switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
174     {
175     default:
176       return;
177     case 1:
178       mm = 0;
179     case 2:
180       ss = 0;
181     case 3:
182       break;
183     }
184   tz_rules[0].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
185                          (min(hh, 23) * 60 * 60));
186
187   for (l = 0; l < 3; ++l)
188     {
189       while (isdigit(*tz))
190         ++tz;
191       if (l < 2 && *tz == ':')
192         ++tz;
193     }
194
195   /* Get the DST timezone name (if any).  */
196   if (*tz != '\0')
197     {
198       char *n = malloc (strlen(tz) + 1);
199       if (n != NULL)
200         {
201           tz_rules[1].name = n;
202           if (sscanf(tz, "%[^0-9,+-]", tz_rules[1].name) != 1 ||
203               (l = strlen(tz_rules[1].name)) < 3)
204             {
205               free (n);
206               tz_rules[1].name = (char *) "";
207               goto done_names;  /* Punt on name, set up the offsets.  */
208             }
209           n = realloc ((PTR) tz_rules[1].name, l + 1);
210           if (n != NULL)
211             tz_rules[1].name = n;
212         }
213     }
214
215   tz += l;
216
217   /* Figure out the DST offset from GMT.  */
218   if (*tz == '-' || *tz == '+')
219     tz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
220   else
221     tz_rules[1].offset = -1L;
222
223   switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
224     {
225     default:
226       /* Default to one hour later than standard time.  */
227       tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
228       break;
229
230     case 1:
231       mm = 0;
232     case 2:
233       ss = 0;
234     case 3:
235       tz_rules[1].offset *= (min(ss, 59) + (min(mm, 59) * 60) +
236                              (min(hh, 23) * (60 * 60)));
237       break;
238     }
239   for (l = 0; l < 3; ++l)
240     {
241       while (isdigit (*tz))
242         ++tz;
243       if (l < 2 && *tz == ':')
244         ++tz;
245     }
246
247  done_names:
248
249   if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
250     {
251       /* There is no rule.  See if there is a default rule file.  */
252       __tzfile_default (tz_rules[0].name, tz_rules[1].name,
253                         tz_rules[0].offset, tz_rules[1].offset);
254       if (__use_tzfile)
255         {
256           __tzset_run = 1;
257           return;
258         }
259     }
260
261   /* Figure out the standard <-> DST rules.  */
262   for (whichrule = 0; whichrule < 2; ++whichrule)
263     {
264       register tz_rule *tzr = &tz_rules[whichrule];
265
266       if (*tz == ',')
267         {
268           ++tz;
269           if (*tz == '\0')
270             return;
271         }
272
273       /* Get the date of the change.  */
274       if (*tz == 'J' || isdigit (*tz))
275         {
276           char *end;
277           tzr->type = *tz == 'J' ? J1 : J0;
278           if (tzr->type == J1 && !isdigit (*++tz))
279             return;
280           tzr->d = (unsigned short int) strtoul (tz, &end, 10);
281           if (end == tz || tzr->d > 365)
282             return;
283           else if (tzr->type == J1 && tzr->d == 0)
284             return;
285           tz = end;
286         }
287       else if (*tz == 'M')
288         {
289           int n;
290           tzr->type = M;
291           if (sscanf (tz, "M%hu.%hu.%hu%n",
292                       &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
293               tzr->m < 1 || tzr->m > 12 ||
294               tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
295             return;
296           tz += n;
297         }
298       else if (*tz == '\0')
299         {
300           /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0".  */
301           tzr->type = M;
302           if (tzr == &tz_rules[0])
303             {
304               tzr->m = 4;
305               tzr->n = 1;
306               tzr->d = 0;
307             }
308           else
309             {
310               tzr->m = 10;
311               tzr->n = 5;
312               tzr->d = 0;
313             }
314         }
315       else
316         return;
317
318       if (*tz != '\0' && *tz != '/' && *tz != ',')
319         return;
320       else if (*tz == '/')
321         {
322           /* Get the time of day of the change.  */
323           ++tz;
324           if (*tz == '\0')
325             return;
326           switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
327             {
328             default:
329               hh = 2;           /* Default to 2:00 AM.  */
330             case 1:
331               mm = 0;
332             case 2:
333               ss = 0;
334             case 3:
335               break;
336             }
337           for (l = 0; l < 3; ++l)
338             {
339               while (isdigit(*tz))
340                 ++tz;
341               if (l < 2 && *tz == ':')
342                 ++tz;
343             }
344           tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
345         }
346       else
347         /* Default to 2:00 AM.  */
348         tzr->secs = 2 * 60 * 60;
349
350       tzr->computed_for = -1;
351     }
352
353   __tzset_run = 1;
354 }
355 \f
356 /* Maximum length of a timezone name.  __tz_compute keeps this up to date
357    (never decreasing it) when ! __use_tzfile.
358    tzfile.c keeps it up to date when __use_tzfile.  */
359 long int __tzname_cur_max;
360
361 long int
362 DEFUN_VOID(__tzname_max)
363 {
364   if (! __tzset_run)
365     __tzset ();
366
367   return __tzname_cur_max;
368 }
369 \f
370 /* Figure out the exact time (as a time_t) in YEAR
371    when the change described by RULE will occur and
372    put it in RULE->change, saving YEAR in RULE->computed_for.
373    Return nonzero if successful, zero on failure.  */
374 static int
375 DEFUN(compute_change, (rule, year), tz_rule *rule AND int year)
376 {
377   register time_t t;
378   int y;
379
380   if (year != -1 && rule->computed_for == year)
381     /* Operations on times in 1969 will be slower.  Oh well.  */
382     return 1;
383
384   /* First set T to January 1st, 0:00:00 GMT in YEAR.  */
385   t = 0;
386   for (y = 1970; y < year; ++y)
387     t += SECSPERDAY * (__isleap (y) ? 366 : 365);
388
389   switch (rule->type)
390     {
391     case J1:
392       /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
393          In non-leap years, or if the day number is 59 or less, just
394          add SECSPERDAY times the day number-1 to the time of
395          January 1, midnight, to get the day.  */
396       t += (rule->d - 1) * SECSPERDAY;
397       if (rule->d >= 60 && __isleap (year))
398         t += SECSPERDAY;
399       break;
400
401     case J0:
402       /* n - Day of year.
403          Just add SECSPERDAY times the day number to the time of Jan 1st.  */
404       t += rule->d * SECSPERDAY;
405       break;
406
407     case M:
408       /* Mm.n.d - Nth "Dth day" of month M.  */
409       {
410         register int i, d, m1, yy0, yy1, yy2, dow;
411         register CONST unsigned short int *myday =
412           &__mon_yday[__isleap (year)][rule->m];
413
414         /* First add SECSPERDAY for each day in months before M.  */
415         t += myday[-1] * 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 >= myday[0] - myday[-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)