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