]> git.mxchange.org Git - flightgear.git/blob - src/Time/lowleveltime.cxx
Initial revisions.
[flightgear.git] / src / Time / lowleveltime.cxx
1 /* -*- Mode: C++ -*- *****************************************************
2  * Written by various people (I"ll look up the exact credits later)
3  * Modified by Durk Talsma, July 1999 for use in FlightGear
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  **************************************************************************/
20
21 /********************************************************************
22  * This file redefines some low-level Unix-like time functions for  *
23  * use with FlightGear. Most notably, localtime() is adapted to use *
24  * a custom timezone, in order to get the 'local' time for a given  *
25  * aircraft's position, and not only for the current location of the*
26  * computer running the sim.                                        *
27  *                                                                  *
28  * Software adapted from glibc functions, by Durk Talsma. Started   *
29  * July, 17, 1999.                                                  *
30  ********************************************************************/
31
32
33 #include <time.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <errno.h>
37 //#include <libc-lock.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <limits.h>
43 #include "lowleveltime.h"
44
45
46 /* BIG FAT WARNING: NOTICE THAT I HARDCODED ENDIANNES. PLEASE CHANGE THIS */
47 #ifndef BIG_ENDIAN 
48 #define BIG_ENDIAN 4321
49 #endif
50
51 #ifndef LITTLE_ENDIAN
52 #define LITTLE_ENDIAN 1234
53 #endif
54
55 #ifndef BYTE_ORDER 
56 #define BYTE_ORDER LITTLE_ENDIAN
57 #endif
58
59
60 #ifndef BYTE_ORDER
61 #define BYTE_ORDER 
62 #endif
63
64 /* END OF BIG FAT WARNING */
65
66 //#include "tzfile.h"
67
68 #define min(a, b)       ((a) < (b) ? (a) : (b))
69 #define max(a, b)       ((a) > (b) ? (a) : (b))
70 #define sign(x)         ((x) < 0 ? -1 : 1)
71
72 struct leap
73   {
74     time_t transition;          /* Time the transition takes effect.  */
75     long int change;            /* Seconds of correction to apply.  */
76   };
77
78 /* Header for a list of buffers containing time zone strings.  */
79 struct tzstring_head
80 {
81   struct tzstring_head *next;
82   /* The buffer itself immediately follows the header.
83      The buffer contains zero or more (possibly overlapping) strings.
84      The last string is followed by 2 '\0's instead of the usual 1.  */
85 };
86
87
88 /* First in a list of buffers containing time zone strings.
89    All the buffers but the last are read-only.  */
90 static struct
91 {
92   struct tzstring_head head;
93   char data[48];
94 } tzstring_list;
95
96 /* Size of the last buffer in the list, not counting its header.  */
97 static size_t tzstring_last_buffer_size = sizeof tzstring_list.data;
98
99
100 static char *old_fgtz = NULL;
101 static int use_fgtzfile = 1;
102 static int fgdaylight;
103 static char* fgtzname[2];
104 static long int fgtimezone;
105
106
107 static size_t num_transitions;
108 static time_t *transitions = NULL;
109 static unsigned char *type_idxs = NULL;
110 static size_t num_types;
111 static struct ttinfo *types = NULL;
112 static char *zone_names = NULL;
113 static size_t num_leaps;
114 static struct leap *leaps = NULL;
115
116
117 static void fgtzset_internal (int always, const char *tz);
118 static int fgtz_compute(time_t timer, const struct tm *tm);
119 static int fgcompute_change(fgtz_rule *rule, int year);
120 static struct ttinfo *fgfind_transition (time_t timer);
121 static void fgcompute_tzname_max (size_t chars);
122 static inline int decode (const void *ptr);
123 void fgtzfile_read (const char *file);
124 static void offtime (const time_t *t, long int offset, struct tm *tp);
125 static char *tzstring (const char* string);
126
127 /* tz_rules[0] is standard, tz_rules[1] is daylight.  */
128 static fgtz_rule fgtz_rules[2];
129
130 int fgtzfile_compute (time_t timer, int use_localtime,
131                   long int *leap_correct, int *leap_hit);
132 struct ttinfo
133   {
134     long int offset;            /* Seconds east of GMT.  */
135     unsigned char isdst;        /* Used to set tm_isdst.  */
136     unsigned char idx;          /* Index into `zone_names'.  */
137     unsigned char isstd;        /* Transition times are in standard time.  */
138     unsigned char isgmt;        /* Transition times are in GMT.  */
139   };
140
141
142
143 /* How many days come before each month (0-12).  */
144 const unsigned short int mon_yday[2][13] =
145   {
146     /* Normal years.  */
147     { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
148     /* Leap years.  */
149     { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
150   };
151
152
153 /* The C Standard says that localtime and gmtime return the same pointer.  */
154 struct tm _fgtmbuf;
155
156
157 #ifndef isleap
158 /* Nonzero if YEAR is a leap year (every 4 years,
159    except every 100th isn't, and every 400th is).  */
160 # define isleap(year) \
161   ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
162 #endif
163
164
165
166
167 /* Return the `struct tm' representation of *T in local time.  */
168 struct tm * fgLocaltime (const time_t *t, const char *tzName)
169 {
170   return fgtz_convert (t, 1, &_fgtmbuf, tzName);
171 }
172
173
174 /* Return the `struct tm' representation of *TIMER in the local timezone.
175    Use local time if USE_LOCALTIME is nonzero, UTC otherwise.  */
176 struct tm * fgtz_convert (const time_t *timer, int use_localtime, struct tm *tp, const char *tzName)
177 {
178   long int leap_correction;
179   long int offsetCorr;                 // ADDED TO RESOLVE NON-ANSI FIELDS IN struct tm
180   int leap_extra_secs;
181
182   if (timer == NULL)
183     {
184       //set_errno (EINVAL);
185       return NULL;
186     }
187
188   //libc_lock_lock (tzset_lock);
189
190   /* Update internal database according to current TZ setting.
191      POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
192      This is a good idea since this allows at least a bit more parallelism.
193      By analogy we apply the same rule to gmtime_r.  */
194   fgtzset_internal (tp == &_fgtmbuf, tzName);
195
196   if (use_fgtzfile)
197     {
198       if (! fgtzfile_compute (*timer, use_localtime,
199                               &leap_correction, &leap_extra_secs))
200         tp = NULL;
201     }
202   else
203     {
204       offtime (timer, 0, tp);
205       if (! fgtz_compute (*timer, tp))
206         tp = NULL;
207       leap_correction = 0L;
208       leap_extra_secs = 0;
209     }
210
211   if (tp)
212     {
213       if (use_localtime)
214         {
215           tp->tm_isdst = fgdaylight;
216           //tp->tm_zone = fgtzname[fgdaylight];       // NON_ANSI
217           //tp->tm_gmtoff = -fgtimezone;              // NON_ANSI
218           offsetCorr = -fgtimezone;
219         }
220       else
221         {
222           tp->tm_isdst = 0;
223           //tp->tm_zone = "GMT";                     // NON_ANSI
224           //tp->tm_gmtoff = 0L;                      // NON_ANSI
225           offsetCorr = -fgtimezone;
226         }
227
228       //offtime (timer, tp->tm_gmtoff - leap_correction, tp);
229       offtime (timer, offsetCorr - leap_correction, tp);
230       tp->tm_sec += leap_extra_secs;
231     }
232
233   //libc_lock_unlock (tzset_lock);
234
235   return tp;
236 }
237
238
239
240 /* the following stuff is adapted from the tzCode package */
241
242 static size_t   longest;
243 static char *   abbr (struct tm * tmp);
244
245 void show(const char *zone, time_t t, int v)
246 {
247         struct tm *     tmp;
248
249         (void) printf("%-*s  ", (int) longest, zone);
250         if (v)
251                 (void) printf("%.24s UTC = ", asctime(gmtime(&t)));
252         tmp = fgLocaltime(&t, zone);
253         (void) printf("%.24s", asctime(tmp));
254         if (*abbr(tmp) != '\0')
255                 (void) printf(" %s", abbr(tmp));
256         if (v) {
257                 (void) printf(" isdst=%d", tmp->tm_isdst);
258 #ifdef TM_GMTOFF
259                 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
260 #endif /* defined TM_GMTOFF */
261         }
262         (void) printf("\n");
263 }
264
265 static char *abbr(struct tm *tmp)
266 {
267         register char * result;
268         static char     nada;
269
270         if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
271                 return &nada;
272         result = fgtzname[tmp->tm_isdst];
273         return (result == NULL) ? &nada : result;
274 }
275
276
277
278 /***********************************************************************/
279
280
281 /* Interpret the TZ envariable.  */
282 static void fgtzset_internal (int always, const char *tz)
283 {
284   time_t now;
285   time(&now);
286   static int is_initialized = 0;
287   //register const char *tz;
288   register size_t l;
289   char *tzbuf;
290   unsigned short int hh, mm, ss;
291   unsigned short int whichrule;
292
293   if (is_initialized && !always)
294     return;
295   is_initialized = 1;
296
297   /* Examine the TZ environment variable.  */
298   //tz = getenv ("TZ");
299   if (tz == NULL)
300     /* No user specification; use the site-wide default.  */
301     tz = TZDEFAULT;
302   else if (*tz == '\0')
303     /* User specified the empty string; use UTC explicitly.  */
304     tz = "Universal";
305
306   /* A leading colon means "implementation defined syntax".
307      We ignore the colon and always use the same algorithm:
308      try a data file, and if none exists parse the 1003.1 syntax.  */
309   if (tz && *tz == ':')
310     ++tz;
311
312   /* Check whether the value changes since the last run.  */
313   if (old_fgtz != NULL && tz != NULL && strcmp (tz, old_fgtz) == 0)
314     /* No change, simply return.  */
315     return;
316
317   fgtz_rules[0].name = NULL;
318   fgtz_rules[1].name = NULL;
319
320   /* Save the value of `tz'.  */
321   if (old_fgtz != NULL)
322     free (old_fgtz);
323   old_fgtz = tz ? strdup (tz) : NULL;
324
325   /* Try to read a data file.  */
326   fgtzfile_read (tz);
327   if (use_fgtzfile)
328     return;
329   // The default behaviour of the originale tzset_internal (int always, char* tz) 
330   // function is to set up a default timezone, in any casetz file_read() fails
331   // Currently this leads to problems, because it modidifies the system timezone
332   // and not the local aircraft timezone, contained in FlightGear. I could adapt 
333   // this in future versions of this code, but doubt whether this is what we really
334   // want. So right now, exit when timezone information reading failed. 
335   // Guess I'll change that to something like 12 * (FG_LON / 180.0)
336   // 
337   // For now, I'll leave it like this.
338   else
339   {
340     printf ("Timezone reading failed\n");
341     exit(1);
342   }
343   // this emacs "comment out" function is cool!
344  
345 //   // /* No data file found.  Default to UTC if nothing specified.  */
346 // //   printf ("1. Current local time          = %24s", asctime(localtime(&now)));
347 //   if (tz == NULL || *tz == '\0')
348 //     {
349 //       fgtz_rules[0].name = fgtz_rules[1].name = "UTC";
350 //       fgtz_rules[0].type = fgtz_rules[1].type = fgtz_rule::J0;
351 //       fgtz_rules[0].m = fgtz_rules[0].n = fgtz_rules[0].d = 0;
352 //       fgtz_rules[1].m = fgtz_rules[1].n = fgtz_rules[1].d = 0;
353 //       fgtz_rules[0].secs = fgtz_rules[1].secs = 0;
354 //       fgtz_rules[0].offset = fgtz_rules[1].offset = 0L;
355 //       fgtz_rules[0].change = fgtz_rules[1].change = (time_t) -1;
356 //       fgtz_rules[0].computed_for = fgtz_rules[1].computed_for = 0;
357 //       return;
358 //     }
359
360 //   /* Clear out old state and reset to unnamed UTC.  */
361 //   //printf ("2. Current local time          = %24s", asctime(localtime(&now)));
362 //   memset (fgtz_rules, 0, sizeof fgtz_rules);
363 //   fgtz_rules[0].name = fgtz_rules[1].name = "";
364
365 //   /* Get the standard timezone name.  */
366 //   tzbuf = malloc (strlen (tz) + 1);
367 //   if (! tzbuf)
368 //     {
369 //       /* Clear the old tz name so we will try again.  */
370 //       free (old_fgtz);
371 //       old_fgtz = NULL;
372 //       return;
373 //     }
374 //   //printf ("3. Current local time          = %24s", asctime(localtime(&now)));
375 //   if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
376 //       (l = strlen (tzbuf)) < 3)
377 //     {
378 //       free (tzbuf);
379 //       return;
380 //     }
381
382 //   fgtz_rules[0].name = tzstring (tzbuf);
383
384 //   tz += l;
385 //   //printf ("4. Current local time          = %24s", asctime(localtime(&now)));
386 //   /* Figure out the standard offset from UTC.  */
387 //   if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
388 //     {
389 //       free (tzbuf);
390 //       return;
391 //     }
392 //   //printf ("5. Current local time          = %24s", asctime(localtime(&now)));
393 //   if (*tz == '-' || *tz == '+')
394 //     fgtz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
395 //   else
396 //     fgtz_rules[0].offset = -1L;
397 //   switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
398 //     {
399 //     default:
400 //       free (tzbuf);
401 //       return;
402 //     case 1:
403 //       mm = 0;
404 //     case 2:
405 //       ss = 0;
406 //     case 3:
407 //       break;
408 //     }
409 //     //printf ("6. Current local time          = %24s", asctime(localtime(&now)));
410 //   fgtz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
411 //                       (min (hh, 23) * 60 * 60));
412
413 //   for (l = 0; l < 3; ++l)
414 //     {
415 //       while (isdigit(*tz))
416 //      ++tz;
417 //       if (l < 2 && *tz == ':')
418 //      ++tz;
419 //     }
420 //   //printf ("7. Current local time          = %24s", asctime(localtime(&now)));
421 //   /* Get the DST timezone name (if any).  */
422 //   if (*tz != '\0')
423 //     {
424 //       char *n = tzbuf + strlen (tzbuf) + 1;
425 //       if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
426 //        (l = strlen (n)) < 3)
427 //      goto done_names;        /* Punt on name, set up the offsets.  */
428 //   //printf ("7.1 Current local time          = %24s", asctime(localtime(&now)));
429 //       fgtz_rules[1].name = tzstring (n);
430
431 //       tz += l;
432
433 //       /* Figure out the DST offset from GMT.  */
434 //       if (*tz == '-' || *tz == '+')
435 //      fgtz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
436 //       else
437 //      fgtz_rules[1].offset = -1L;
438 //   //printf ("7.2 Current local time          = %24s", asctime(localtime(&now)));
439 //       switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
440 //      {
441 //      default:
442 //        /* Default to one hour later than standard time.  */
443 //        fgtz_rules[1].offset = fgtz_rules[0].offset + (60 * 60);
444 //        break;
445
446 //      case 1:
447 //        mm = 0;
448 //      case 2:
449 //        ss = 0;
450 //      case 3:
451 //        fgtz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
452 //                               (min (hh, 23) * (60 * 60)));
453 //        break;
454 //      }
455 //   //printf ("7.3 Current local time          = %24s", asctime(localtime(&now)));
456 //       for (l = 0; l < 3; ++l)
457 //      {
458 //        while (isdigit (*tz))
459 //          ++tz;
460 //        if (l < 2 && *tz == ':')
461 //          ++tz;
462 //      }
463 //   //printf ("7.4 Current local time          = %24s", asctime(localtime(&now)));
464 //       if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
465 //      {
466 //        /* There is no rule.  See if there is a default rule file.  */
467 //   //printf ("7.4.1 Current local time          = %24s", asctime(localtime(&now)));
468 //        tzfile_default (fgtz_rules[0].name, fgtz_rules[1].name,
469 //                          fgtz_rules[0].offset, fgtz_rules[1].offset);
470 //   //printf ("7.4.2 Current local time          = %24s", asctime(localtime(&now)));
471 //        if (use_fgtzfile)
472 //          {
473 //            free (old_fgtz);
474 //            old_fgtz = NULL;
475 //            free (tzbuf);
476 //            return;
477 //          }
478 //      }
479 //     }
480 //   else
481 //     {
482 //       /* There is no DST.  */
483 //       fgtz_rules[1].name = fgtz_rules[0].name;
484 //       free (tzbuf);
485 //       return;
486 //     }
487 //   //printf ("7.5 Current local time          = %24s", asctime(localtime(&now)));
488 //  done_names:
489 //   //printf ("8. Current local time          = %24s", asctime(localtime(&now)));
490 //   free (tzbuf);
491
492 //   /* Figure out the standard <-> DST rules.  */
493 //   for (whichrule = 0; whichrule < 2; ++whichrule)
494 //     {
495 //       register fgtz_rule *tzr = &fgtz_rules[whichrule];
496
497 //       /* Ignore comma to support string following the incorrect
498 //       specification in early POSIX.1 printings.  */
499 //       tz += *tz == ',';
500
501 //       /* Get the date of the change.  */
502 //       if (*tz == 'J' || isdigit (*tz))
503 //      {
504 //        char *end;
505 //        tzr->type = *tz == 'J' ? fgtz_rule::J1 : fgtz_rule::J0;
506 //        if (tzr->type == fgtz_rule::J1 && !isdigit (*++tz))
507 //          return;
508 //        tzr->d = (unsigned short int) strtoul (tz, &end, 10);
509 //        if (end == tz || tzr->d > 365)
510 //          return;
511 //        else if (tzr->type == fgtz_rule::J1 && tzr->d == 0)
512 //          return;
513 //        tz = end;
514 //      }
515 //       else if (*tz == 'M')
516 //      {
517 //        int n;
518 //        tzr->type = fgtz_rule::M;
519 //        if (sscanf (tz, "M%hu.%hu.%hu%n",
520 //                    &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
521 //            tzr->m < 1 || tzr->m > 12 ||
522 //            tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
523 //          return;
524 //        tz += n;
525 //      }
526 //       else if (*tz == '\0')
527 //      {
528 //        /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0".  */
529 //        tzr->type = fgtz_rule::M;
530 //        if (tzr == &fgtz_rules[0])
531 //          {
532 //            tzr->m = 4;
533 //            tzr->n = 1;
534 //            tzr->d = 0;
535 //          }
536 //        else
537 //          {
538 //            tzr->m = 10;
539 //            tzr->n = 5;
540 //            tzr->d = 0;
541 //          }
542 //      }
543 //       else
544 //      return;
545 //       //printf ("9. Current local time          = %24s", asctime(localtime(&now)));
546 //       if (*tz != '\0' && *tz != '/' && *tz != ',')
547 //      return;
548 //       else if (*tz == '/')
549 //      {
550 //        /* Get the time of day of the change.  */
551 //        ++tz;
552 //        if (*tz == '\0')
553 //          return;
554 //        switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
555 //          {
556 //          default:
557 //            hh = 2;           /* Default to 2:00 AM.  */
558 //          case 1:
559 //            mm = 0;
560 //          case 2:
561 //            ss = 0;
562 //          case 3:
563 //            break;
564 //          }
565 //        for (l = 0; l < 3; ++l)
566 //          {
567 //            while (isdigit (*tz))
568 //              ++tz;
569 //            if (l < 2 && *tz == ':')
570 //              ++tz;
571 //          }
572 //        tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
573 //      }
574 //       else
575 //      /* Default to 2:00 AM.  */
576 //      tzr->secs = 2 * 60 * 60;
577
578 //       tzr->computed_for = -1;
579 //     }
580 // //   printf ("10. Current local time          = %24s", asctime(localtime(&now)));
581 // 
582 }
583
584 /************************************************************************/
585
586
587 /* Figure out the correct timezone for *TIMER and TM (which must be the same)
588    and set `tzname', `timezone', and `daylight' accordingly.
589    Return nonzero on success, zero on failure.  */
590 size_t fgtzname_cur_max;
591
592 static int fgtz_compute (time_t timer, const struct tm* tm)
593   //     time_t timer;
594   // const struct tm *tm;
595 {
596   if (! fgcompute_change (&fgtz_rules[0], 1900 + tm->tm_year) ||
597       ! fgcompute_change (&fgtz_rules[1], 1900 + tm->tm_year))
598     return 0;
599
600   fgdaylight = timer >= fgtz_rules[0].change && timer < fgtz_rules[1].change;
601   fgtimezone = -fgtz_rules[fgdaylight].offset;
602   fgtzname[0] = (char *) fgtz_rules[0].name;
603   fgtzname[1] = (char *) fgtz_rules[1].name;
604
605   {
606     /* Keep tzname_cur_max up to date.  */
607     size_t len0 = strlen (fgtzname[0]);
608     size_t len1 = strlen (fgtzname[1]);
609     if (len0 > fgtzname_cur_max)
610       fgtzname_cur_max = len0;
611     if (len1 > fgtzname_cur_max)
612       fgtzname_cur_max = len1;
613   }
614
615   return 1;
616 }
617
618 /**********************************************************************/
619
620 /* Figure out the exact time (as a time_t) in YEAR
621    when the change described by RULE will occur and
622    put it in RULE->change, saving YEAR in RULE->computed_for.
623    Return nonzero if successful, zero on failure.  */
624 static int fgcompute_change (fgtz_rule *rule, int year)
625   //     tz_rule *rule;
626   // int year;
627 {
628   register time_t t;
629   int y;
630
631   if (year != -1 && rule->computed_for == year)
632     /* Operations on times in 1969 will be slower.  Oh well.  */
633     return 1;
634
635   /* First set T to January 1st, 0:00:00 GMT in YEAR.  */
636   t = 0;
637   for (y = 1970; y < year; ++y)
638     t += SECSPERDAY * (isleap (y) ? 366 : 365);
639
640   switch (rule->type)
641     {
642     case fgtz_rule::J1:
643       /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
644          In non-leap years, or if the day number is 59 or less, just
645          add SECSPERDAY times the day number-1 to the time of
646          January 1, midnight, to get the day.  */
647       t += (rule->d - 1) * SECSPERDAY;
648       if (rule->d >= 60 && isleap (year))
649         t += SECSPERDAY;
650       break;
651
652     case fgtz_rule::J0:
653       /* n - Day of year.
654          Just add SECSPERDAY times the day number to the time of Jan 1st.  */
655       t += rule->d * SECSPERDAY;
656       break;
657
658     case fgtz_rule::M:
659       /* Mm.n.d - Nth "Dth day" of month M.  */
660       {
661         register int i, d, m1, yy0, yy1, yy2, dow;
662         register const unsigned short int *myday =
663           &mon_yday[isleap (year)][rule->m];
664
665         /* First add SECSPERDAY for each day in months before M.  */
666         t += myday[-1] * SECSPERDAY;
667
668         /* Use Zeller's Congruence to get day-of-week of first day of month. */
669         m1 = (rule->m + 9) % 12 + 1;
670         yy0 = (rule->m <= 2) ? (year - 1) : year;
671         yy1 = yy0 / 100;
672         yy2 = yy0 % 100;
673         dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
674         if (dow < 0)
675           dow += 7;
676
677         /* DOW is the day-of-week of the first day of the month.  Get the
678            day-of-month (zero-origin) of the first DOW day of the month.  */
679         d = rule->d - dow;
680         if (d < 0)
681           d += 7;
682         for (i = 1; i < rule->n; ++i)
683           {
684             if (d + 7 >= myday[0] - myday[-1])
685               break;
686             d += 7;
687           }
688
689         /* D is the day-of-month (zero-origin) of the day we want.  */
690         t += d * SECSPERDAY;
691       }
692       break;
693     }
694
695   /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
696      Just add the time of day and local offset from GMT, and we're done.  */
697
698   rule->change = t - rule->offset + rule->secs;
699   rule->computed_for = year;
700   return 1;
701 }
702
703 /*************************************************************************/
704
705 int fgtzfile_compute (time_t timer, int use_localtime,
706                   long int *leap_correct, int *leap_hit)
707 {
708   register size_t i;
709
710   if (use_localtime)
711     {
712       struct ttinfo *info = fgfind_transition (timer);
713       fgdaylight = info->isdst;
714       fgtimezone = -info->offset;
715       for (i = 0;
716            i < num_types && i < sizeof (fgtzname) / sizeof (fgtzname[0]);
717            ++i)
718         fgtzname[types[i].isdst] = &zone_names[types[i].idx];
719       if (info->isdst < sizeof (fgtzname) / sizeof (fgtzname[0]))
720         fgtzname[info->isdst] = &zone_names[info->idx];
721     }
722
723   *leap_correct = 0L;
724   *leap_hit = 0;
725
726   /* Find the last leap second correction transition time before TIMER.  */
727   i = num_leaps;
728   do
729     if (i-- == 0)
730       return 1;
731   while (timer < leaps[i].transition);
732
733   /* Apply its correction.  */
734   *leap_correct = leaps[i].change;
735
736   if (timer == leaps[i].transition && /* Exactly at the transition time.  */
737       ((i == 0 && leaps[i].change > 0) ||
738        leaps[i].change > leaps[i - 1].change))
739     {
740       *leap_hit = 1;
741       while (i > 0 &&
742              leaps[i].transition == leaps[i - 1].transition + 1 &&
743              leaps[i].change == leaps[i - 1].change + 1)
744         {
745           ++*leap_hit;
746           --i;
747         }
748     }
749
750   return 1;
751 }
752
753 /**************************************************************************/
754
755 static struct ttinfo * fgfind_transition (time_t timer)
756 {
757   size_t i;
758
759   if (num_transitions == 0 || timer < transitions[0])
760     {
761       /* TIMER is before any transition (or there are no transitions).
762          Choose the first non-DST type
763          (or the first if they're all DST types).  */
764       i = 0;
765       while (i < num_types && types[i].isdst)
766         ++i;
767       if (i == num_types)
768         i = 0;
769     }
770   else
771     {
772       /* Find the first transition after TIMER, and
773          then pick the type of the transition before it.  */
774       for (i = 1; i < num_transitions; ++i)
775         if (timer < transitions[i])
776           break;
777       i = type_idxs[i - 1];
778     }
779
780   return &types[i];
781 }
782
783
784 /**************************************************************************/
785 void fgtzfile_read (const char *file)
786 {
787   static const char default_tzdir[] = TZDIR;
788   size_t num_isstd, num_isgmt;
789   register FILE *f;
790   struct tzhead tzhead;
791   size_t chars;
792   register size_t i;
793   struct ttinfo *info;
794
795   use_fgtzfile = 0;
796
797   if (transitions != NULL)
798     free ((void *) transitions);
799   transitions = NULL;
800   if (type_idxs != NULL)
801     free ((void *) type_idxs);
802   type_idxs = NULL;
803   if (types != NULL)
804     free ((void *) types);
805   types = NULL;
806   if (zone_names != NULL)
807     free ((void *) zone_names);
808   zone_names = NULL;
809   if (leaps != NULL)
810     free ((void *) leaps);
811   leaps = NULL;
812
813   if (file == NULL)
814     /* No user specification; use the site-wide default.  */
815     file = TZDEFAULT;
816   else if (*file == '\0')
817     /* User specified the empty string; use UTC with no leap seconds.  */
818     return;
819   else
820     {
821       /* We must not allow to read an arbitrary file in a setuid
822          program.  So we fail for any file which is not in the
823          directory hierachy starting at TZDIR
824          and which is not the system wide default TZDEFAULT.  */
825       //if (libc_enable_secure
826       //  && ((*file == '/'
827       //       && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
828       //       && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
829       //      || strstr (file, "../") != NULL))
830         /* This test is certainly a bit too restrictive but it should
831            catch all critical cases.  */
832       //return;
833     }
834
835 //   if (*file != '/') // if a relative path is used, append what file points to
836 //                     // to the path indicated by TZDIR.
837 //     {
838 //       const char *tzdir;
839 //       unsigned int len, tzdir_len;
840 //       char *_new;
841
842 //       tzdir = getenv ("TZDIR");
843 //       if (tzdir == NULL || *tzdir == '\0')
844 //      {
845 //        tzdir = default_tzdir;
846 //        tzdir_len = sizeof (default_tzdir) - 1;
847 //      }
848 //       else
849 //      tzdir_len = strlen (tzdir);
850 //       len = strlen (file) + 1;
851 //       _new = (char *) alloca (tzdir_len + 1 + len);
852 //       memcpy (_new, tzdir, tzdir_len);
853 //       _new[tzdir_len] = '/';
854 //       memcpy (&_new[tzdir_len + 1], file, len);
855 //       file = _new;
856 //     }
857
858   f = fopen (file, "rb");
859   if (f == NULL)
860     return;
861
862   if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
863     goto lose;
864
865   num_transitions = (size_t) decode (tzhead.tzh_timecnt);
866   num_types = (size_t) decode (tzhead.tzh_typecnt);
867   chars = (size_t) decode (tzhead.tzh_charcnt);
868   num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
869   num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
870   num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
871
872   if (num_transitions > 0)
873     {
874       transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
875       if (transitions == NULL)
876         goto lose;
877       type_idxs = (unsigned char *) malloc (num_transitions);
878       if (type_idxs == NULL)
879         goto lose;
880     }
881   if (num_types > 0)
882     {
883       types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
884       if (types == NULL)
885         goto lose;
886     }
887   if (chars > 0)
888     {
889       zone_names = (char *) malloc (chars);
890       if (zone_names == NULL)
891         goto lose;
892     }
893   if (num_leaps > 0)
894     {
895       leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
896       if (leaps == NULL)
897         goto lose;
898     }
899
900   if (sizeof (time_t) < 4)
901       abort ();
902
903   if (fread(transitions, 4, num_transitions, f) != num_transitions ||
904       fread(type_idxs, 1, num_transitions, f) != num_transitions)
905     goto lose;
906
907   /* Check for bogus indices in the data file, so we can hereafter
908      safely use type_idxs[T] as indices into `types' and never crash.  */
909   for (i = 0; i < num_transitions; ++i)
910     if (type_idxs[i] >= num_types)
911       goto lose;
912
913   if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
914     {
915       /* Decode the transition times, stored as 4-byte integers in
916          network (big-endian) byte order.  We work from the end of
917          the array so as not to clobber the next element to be
918          processed when sizeof (time_t) > 4.  */
919       i = num_transitions;
920       while (i-- > 0)
921         transitions[i] = decode ((char *) transitions + i*4);
922     }
923
924   for (i = 0; i < num_types; ++i)
925     {
926       unsigned char x[4];
927       if (fread (x, 1, 4, f) != 4 ||
928           fread (&types[i].isdst, 1, 1, f) != 1 ||
929           fread (&types[i].idx, 1, 1, f) != 1)
930         goto lose;
931       if (types[i].idx >= chars) /* Bogus index in data file.  */
932         goto lose;
933       types[i].offset = (long int) decode (x);
934     }
935
936   if (fread (zone_names, 1, chars, f) != chars)
937     goto lose;
938
939   for (i = 0; i < num_leaps; ++i)
940     {
941       unsigned char x[4];
942       if (fread (x, 1, sizeof (x), f) != sizeof (x))
943         goto lose;
944       leaps[i].transition = (time_t) decode (x);
945       if (fread (x, 1, sizeof (x), f) != sizeof (x))
946         goto lose;
947       leaps[i].change = (long int) decode (x);
948     }
949
950   for (i = 0; i < num_isstd; ++i)
951     {
952       int c = getc (f);
953       if (c == EOF)
954         goto lose;
955       types[i].isstd = c != 0;
956     }
957   while (i < num_types)
958     types[i++].isstd = 0;
959
960   for (i = 0; i < num_isgmt; ++i)
961     {
962       int c = getc (f);
963       if (c == EOF)
964         goto lose;
965       types[i].isgmt = c != 0;
966     }
967   while (i < num_types)
968     types[i++].isgmt = 0;
969
970   fclose (f);
971
972   info = fgfind_transition (0);
973   for (i = 0; i < num_types && i < sizeof (fgtzname) / sizeof (fgtzname[0]);
974        ++i)
975     fgtzname[types[i].isdst] = tzstring (&zone_names[types[i].idx]);
976   if (info->isdst < sizeof (fgtzname) / sizeof (fgtzname[0]))
977     fgtzname[info->isdst] = tzstring (&zone_names[info->idx]);
978
979   fgcompute_tzname_max (chars);
980
981   use_fgtzfile = 1;
982   return;
983
984  lose:;
985   fclose(f);
986 }
987
988 /****************************************************************************/
989 static void fgcompute_tzname_max (size_t chars)
990 {
991   extern size_t tzname_cur_max; /* Defined in tzset.c. */
992
993   const char *p;
994
995   p = zone_names;
996   do
997     {
998       const char *start = p;
999       while (*p != '\0')
1000         ++p;
1001       if ((size_t) (p - start) > fgtzname_cur_max)
1002         fgtzname_cur_max = p - start;
1003     } while (++p < &zone_names[chars]);
1004 }
1005
1006 /**************************************************************************/
1007
1008 //#include <endian.h>
1009
1010 /* Decode the four bytes at PTR as a signed integer in network byte order.  */
1011 static inline int decode (const void *ptr)
1012 {
1013   if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
1014     return *(const int *) ptr;
1015   else
1016     {
1017       const unsigned char *p = (unsigned char *)ptr;
1018       int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
1019
1020       result = (result << 8) | *p++;
1021       result = (result << 8) | *p++;
1022       result = (result << 8) | *p++;
1023       result = (result << 8) | *p++;
1024
1025       return result;
1026     }
1027 }
1028
1029
1030
1031 #define SECS_PER_HOUR   (60 * 60)
1032 #define SECS_PER_DAY    (SECS_PER_HOUR * 24)
1033
1034 /* Compute the `struct tm' representation of *T,
1035    offset OFFSET seconds east of UTC,
1036    and store year, yday, mon, mday, wday, hour, min, sec into *TP.  */
1037
1038 void offtime (const time_t *t, long int offset, struct tm *tp)
1039   //    const time_t *t;
1040   // long int offset;
1041   // struct tm *tp;
1042 {
1043   register long int days, rem, y;
1044   register const unsigned short int *ip;
1045
1046   days = *t / SECS_PER_DAY;
1047   rem = *t % SECS_PER_DAY;
1048   rem += offset;
1049   while (rem < 0)
1050     {
1051       rem += SECS_PER_DAY;
1052       --days;
1053     }
1054   while (rem >= SECS_PER_DAY)
1055     {
1056       rem -= SECS_PER_DAY;
1057       ++days;
1058     }
1059   tp->tm_hour = rem / SECS_PER_HOUR;
1060   rem %= SECS_PER_HOUR;
1061   tp->tm_min = rem / 60;
1062   tp->tm_sec = rem % 60;
1063   /* January 1, 1970 was a Thursday.  */
1064   tp->tm_wday = (4 + days) % 7;
1065   if (tp->tm_wday < 0)
1066     tp->tm_wday += 7;
1067   y = 1970;
1068
1069 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1070
1071   while (days < 0 || days >= (isleap (y) ? 366 : 365))
1072     {
1073       /* Guess a corrected year, assuming 365 days per year.  */
1074       long int yg = y + days / 365 - (days % 365 < 0);
1075
1076       /* Adjust DAYS and Y to match the guessed year.  */
1077       days -= ((yg - y) * 365
1078                + LEAPS_THRU_END_OF (yg - 1)
1079                - LEAPS_THRU_END_OF (y - 1));
1080       y = yg;
1081     }
1082   tp->tm_year = y - 1900;
1083   tp->tm_yday = days;
1084   ip = mon_yday[isleap(y)];
1085   for (y = 11; days < ip[y]; --y)
1086     continue;
1087   days -= ip[y];
1088   tp->tm_mon = y;
1089   tp->tm_mday = days + 1;
1090 }
1091
1092 /* Allocate a time zone string with given contents.
1093    The string will never be moved or deallocated.
1094    However, its contents may be shared with other such strings.  */
1095 char *tzstring (const char* string)
1096   //const char *string;
1097 {
1098   struct tzstring_head *h = &tzstring_list.head;
1099   size_t needed;
1100   char *p;
1101
1102   /* Look through time zone string list for a duplicate of this one.  */
1103   for (h = &tzstring_list.head;  ;  h = h->next)
1104     {
1105       for (p = (char *) (h + 1);  p[0] | p[1];  ++p)
1106         if (strcmp (p, string) == 0)
1107           return p;
1108       if (! h->next)
1109         break;
1110     }
1111
1112   /* No duplicate was found.  Copy to the end of this buffer if there's room;
1113      otherwise, append a large-enough new buffer to the list and use it.  */
1114   ++p;
1115   needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string.  */
1116
1117   if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed)
1118     {
1119       size_t buffer_size = tzstring_last_buffer_size;
1120       while ((buffer_size *= 2) < needed)
1121         continue;
1122       if (! (h = h->next = (struct tzstring_head *)malloc (sizeof *h + buffer_size)))
1123         return NULL;
1124       h->next = NULL;
1125       tzstring_last_buffer_size = buffer_size;
1126       p = (char *) (h + 1);
1127     }
1128
1129   return strncpy (p, string, needed);
1130 }