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