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
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.
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.
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.
19 **************************************************************************/
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. *
28 * Software adapted from glibc functions, by Durk Talsma. Started *
30 ********************************************************************/
37 //#include <libc-lock.h>
43 #include "lowleveltime.h"
46 /* BIG FAT WARNING: NOTICE THAT I HARDCODED ENDIANNES. PLEASE CHANGE THIS */
48 #define BIG_ENDIAN 4321
52 #define LITTLE_ENDIAN 1234
56 #define BYTE_ORDER LITTLE_ENDIAN
64 /* END OF BIG FAT WARNING */
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)
74 time_t transition; /* Time the transition takes effect. */
75 long int change; /* Seconds of correction to apply. */
78 /* Header for a list of buffers containing time zone strings. */
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. */
88 /* First in a list of buffers containing time zone strings.
89 All the buffers but the last are read-only. */
92 struct tzstring_head head;
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;
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;
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;
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);
127 /* tz_rules[0] is standard, tz_rules[1] is daylight. */
128 static fgtz_rule fgtz_rules[2];
130 int fgtzfile_compute (time_t timer, int use_localtime,
131 long int *leap_correct, int *leap_hit);
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. */
143 /* How many days come before each month (0-12). */
144 const unsigned short int mon_yday[2][13] =
147 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
149 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
153 /* The C Standard says that localtime and gmtime return the same pointer. */
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))
167 /* Return the `struct tm' representation of *T in local time. */
168 struct tm * fgLocaltime (const time_t *t, const char *tzName)
170 return fgtz_convert (t, 1, &_fgtmbuf, tzName);
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)
178 long int leap_correction;
179 long int offsetCorr; // ADDED TO RESOLVE NON-ANSI FIELDS IN struct tm
184 //set_errno (EINVAL);
188 //libc_lock_lock (tzset_lock);
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);
198 if (! fgtzfile_compute (*timer, use_localtime,
199 &leap_correction, &leap_extra_secs))
204 offtime (timer, 0, tp);
205 if (! fgtz_compute (*timer, tp))
207 leap_correction = 0L;
215 tp->tm_isdst = fgdaylight;
216 //tp->tm_zone = fgtzname[fgdaylight]; // NON_ANSI
217 //tp->tm_gmtoff = -fgtimezone; // NON_ANSI
218 offsetCorr = -fgtimezone;
223 //tp->tm_zone = "GMT"; // NON_ANSI
224 //tp->tm_gmtoff = 0L; // NON_ANSI
225 offsetCorr = -fgtimezone;
228 //offtime (timer, tp->tm_gmtoff - leap_correction, tp);
229 offtime (timer, offsetCorr - leap_correction, tp);
230 tp->tm_sec += leap_extra_secs;
233 //libc_lock_unlock (tzset_lock);
240 /* the following stuff is adapted from the tzCode package */
242 static size_t longest;
243 static char * abbr (struct tm * tmp);
245 void show(const char *zone, time_t t, int v)
249 (void) printf("%-*s ", (int) longest, zone);
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));
257 (void) printf(" isdst=%d", tmp->tm_isdst);
259 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
260 #endif /* defined TM_GMTOFF */
265 static char *abbr(struct tm *tmp)
267 register char * result;
270 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
272 result = fgtzname[tmp->tm_isdst];
273 return (result == NULL) ? &nada : result;
278 /***********************************************************************/
281 /* Interpret the TZ envariable. */
282 static void fgtzset_internal (int always, const char *tz)
286 static int is_initialized = 0;
287 //register const char *tz;
290 unsigned short int hh, mm, ss;
291 unsigned short int whichrule;
293 if (is_initialized && !always)
297 /* Examine the TZ environment variable. */
298 //tz = getenv ("TZ");
300 /* No user specification; use the site-wide default. */
302 else if (*tz == '\0')
303 /* User specified the empty string; use UTC explicitly. */
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 == ':')
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. */
317 fgtz_rules[0].name = NULL;
318 fgtz_rules[1].name = NULL;
320 /* Save the value of `tz'. */
321 if (old_fgtz != NULL)
323 old_fgtz = tz ? strdup (tz) : NULL;
325 /* Try to read a data file. */
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)
337 // For now, I'll leave it like this.
340 printf ("Timezone reading failed\n");
343 // this emacs "comment out" function is cool!
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')
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;
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 = "";
365 // /* Get the standard timezone name. */
366 // tzbuf = malloc (strlen (tz) + 1);
369 // /* Clear the old tz name so we will try again. */
374 // //printf ("3. Current local time = %24s", asctime(localtime(&now)));
375 // if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
376 // (l = strlen (tzbuf)) < 3)
382 // fgtz_rules[0].name = tzstring (tzbuf);
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)))
392 // //printf ("5. Current local time = %24s", asctime(localtime(&now)));
393 // if (*tz == '-' || *tz == '+')
394 // fgtz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
396 // fgtz_rules[0].offset = -1L;
397 // switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
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));
413 // for (l = 0; l < 3; ++l)
415 // while (isdigit(*tz))
417 // if (l < 2 && *tz == ':')
420 // //printf ("7. Current local time = %24s", asctime(localtime(&now)));
421 // /* Get the DST timezone name (if any). */
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);
433 // /* Figure out the DST offset from GMT. */
434 // if (*tz == '-' || *tz == '+')
435 // fgtz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
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))
442 // /* Default to one hour later than standard time. */
443 // fgtz_rules[1].offset = fgtz_rules[0].offset + (60 * 60);
451 // fgtz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
452 // (min (hh, 23) * (60 * 60)));
455 // //printf ("7.3 Current local time = %24s", asctime(localtime(&now)));
456 // for (l = 0; l < 3; ++l)
458 // while (isdigit (*tz))
460 // if (l < 2 && *tz == ':')
463 // //printf ("7.4 Current local time = %24s", asctime(localtime(&now)));
464 // if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
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)));
482 // /* There is no DST. */
483 // fgtz_rules[1].name = fgtz_rules[0].name;
487 // //printf ("7.5 Current local time = %24s", asctime(localtime(&now)));
489 // //printf ("8. Current local time = %24s", asctime(localtime(&now)));
492 // /* Figure out the standard <-> DST rules. */
493 // for (whichrule = 0; whichrule < 2; ++whichrule)
495 // register fgtz_rule *tzr = &fgtz_rules[whichrule];
497 // /* Ignore comma to support string following the incorrect
498 // specification in early POSIX.1 printings. */
501 // /* Get the date of the change. */
502 // if (*tz == 'J' || isdigit (*tz))
505 // tzr->type = *tz == 'J' ? fgtz_rule::J1 : fgtz_rule::J0;
506 // if (tzr->type == fgtz_rule::J1 && !isdigit (*++tz))
508 // tzr->d = (unsigned short int) strtoul (tz, &end, 10);
509 // if (end == tz || tzr->d > 365)
511 // else if (tzr->type == fgtz_rule::J1 && tzr->d == 0)
515 // else if (*tz == 'M')
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)
526 // else if (*tz == '\0')
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])
545 // //printf ("9. Current local time = %24s", asctime(localtime(&now)));
546 // if (*tz != '\0' && *tz != '/' && *tz != ',')
548 // else if (*tz == '/')
550 // /* Get the time of day of the change. */
554 // switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
557 // hh = 2; /* Default to 2:00 AM. */
565 // for (l = 0; l < 3; ++l)
567 // while (isdigit (*tz))
569 // if (l < 2 && *tz == ':')
572 // tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
575 // /* Default to 2:00 AM. */
576 // tzr->secs = 2 * 60 * 60;
578 // tzr->computed_for = -1;
580 // // printf ("10. Current local time = %24s", asctime(localtime(&now)));
584 /************************************************************************/
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;
592 static int fgtz_compute (time_t timer, const struct tm* tm)
594 // const struct tm *tm;
596 if (! fgcompute_change (&fgtz_rules[0], 1900 + tm->tm_year) ||
597 ! fgcompute_change (&fgtz_rules[1], 1900 + tm->tm_year))
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;
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;
618 /**********************************************************************/
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)
631 if (year != -1 && rule->computed_for == year)
632 /* Operations on times in 1969 will be slower. Oh well. */
635 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
637 for (y = 1970; y < year; ++y)
638 t += SECSPERDAY * (isleap (y) ? 366 : 365);
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))
654 Just add SECSPERDAY times the day number to the time of Jan 1st. */
655 t += rule->d * SECSPERDAY;
659 /* Mm.n.d - Nth "Dth day" of month M. */
661 register int i, d, m1, yy0, yy1, yy2, dow;
662 register const unsigned short int *myday =
663 &mon_yday[isleap (year)][rule->m];
665 /* First add SECSPERDAY for each day in months before M. */
666 t += myday[-1] * SECSPERDAY;
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;
673 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
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. */
682 for (i = 1; i < rule->n; ++i)
684 if (d + 7 >= myday[0] - myday[-1])
689 /* D is the day-of-month (zero-origin) of the day we want. */
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. */
698 rule->change = t - rule->offset + rule->secs;
699 rule->computed_for = year;
703 /*************************************************************************/
705 int fgtzfile_compute (time_t timer, int use_localtime,
706 long int *leap_correct, int *leap_hit)
712 struct ttinfo *info = fgfind_transition (timer);
713 fgdaylight = info->isdst;
714 fgtimezone = -info->offset;
716 i < num_types && i < sizeof (fgtzname) / sizeof (fgtzname[0]);
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];
726 /* Find the last leap second correction transition time before TIMER. */
731 while (timer < leaps[i].transition);
733 /* Apply its correction. */
734 *leap_correct = leaps[i].change;
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))
742 leaps[i].transition == leaps[i - 1].transition + 1 &&
743 leaps[i].change == leaps[i - 1].change + 1)
753 /**************************************************************************/
755 static struct ttinfo * fgfind_transition (time_t timer)
759 if (num_transitions == 0 || timer < transitions[0])
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). */
765 while (i < num_types && types[i].isdst)
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])
777 i = type_idxs[i - 1];
784 /**************************************************************************/
785 void fgtzfile_read (const char *file)
787 static const char default_tzdir[] = TZDIR;
788 size_t num_isstd, num_isgmt;
790 struct tzhead tzhead;
797 if (transitions != NULL)
798 free ((void *) transitions);
800 if (type_idxs != NULL)
801 free ((void *) type_idxs);
804 free ((void *) types);
806 if (zone_names != NULL)
807 free ((void *) zone_names);
810 free ((void *) leaps);
814 /* No user specification; use the site-wide default. */
816 else if (*file == '\0')
817 /* User specified the empty string; use UTC with no leap seconds. */
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
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. */
835 // if (*file != '/') // if a relative path is used, append what file points to
836 // // to the path indicated by TZDIR.
838 // const char *tzdir;
839 // unsigned int len, tzdir_len;
842 // tzdir = getenv ("TZDIR");
843 // if (tzdir == NULL || *tzdir == '\0')
845 // tzdir = default_tzdir;
846 // tzdir_len = sizeof (default_tzdir) - 1;
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);
858 f = fopen (file, "rb");
862 if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
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);
872 if (num_transitions > 0)
874 transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
875 if (transitions == NULL)
877 type_idxs = (unsigned char *) malloc (num_transitions);
878 if (type_idxs == NULL)
883 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
889 zone_names = (char *) malloc (chars);
890 if (zone_names == NULL)
895 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
900 if (sizeof (time_t) < 4)
903 if (fread(transitions, 4, num_transitions, f) != num_transitions ||
904 fread(type_idxs, 1, num_transitions, f) != num_transitions)
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)
913 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
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. */
921 transitions[i] = decode ((char *) transitions + i*4);
924 for (i = 0; i < num_types; ++i)
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)
931 if (types[i].idx >= chars) /* Bogus index in data file. */
933 types[i].offset = (long int) decode (x);
936 if (fread (zone_names, 1, chars, f) != chars)
939 for (i = 0; i < num_leaps; ++i)
942 if (fread (x, 1, sizeof (x), f) != sizeof (x))
944 leaps[i].transition = (time_t) decode (x);
945 if (fread (x, 1, sizeof (x), f) != sizeof (x))
947 leaps[i].change = (long int) decode (x);
950 for (i = 0; i < num_isstd; ++i)
955 types[i].isstd = c != 0;
957 while (i < num_types)
958 types[i++].isstd = 0;
960 for (i = 0; i < num_isgmt; ++i)
965 types[i].isgmt = c != 0;
967 while (i < num_types)
968 types[i++].isgmt = 0;
972 info = fgfind_transition (0);
973 for (i = 0; i < num_types && i < sizeof (fgtzname) / sizeof (fgtzname[0]);
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]);
979 fgcompute_tzname_max (chars);
988 /****************************************************************************/
989 static void fgcompute_tzname_max (size_t chars)
991 extern size_t tzname_cur_max; /* Defined in tzset.c. */
998 const char *start = p;
1001 if ((size_t) (p - start) > fgtzname_cur_max)
1002 fgtzname_cur_max = p - start;
1003 } while (++p < &zone_names[chars]);
1006 /**************************************************************************/
1008 //#include <endian.h>
1010 /* Decode the four bytes at PTR as a signed integer in network byte order. */
1011 static inline int decode (const void *ptr)
1013 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
1014 return *(const int *) ptr;
1017 const unsigned char *p = (unsigned char *)ptr;
1018 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
1020 result = (result << 8) | *p++;
1021 result = (result << 8) | *p++;
1022 result = (result << 8) | *p++;
1023 result = (result << 8) | *p++;
1031 #define SECS_PER_HOUR (60 * 60)
1032 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
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. */
1038 void offtime (const time_t *t, long int offset, struct tm *tp)
1043 register long int days, rem, y;
1044 register const unsigned short int *ip;
1046 days = *t / SECS_PER_DAY;
1047 rem = *t % SECS_PER_DAY;
1051 rem += SECS_PER_DAY;
1054 while (rem >= SECS_PER_DAY)
1056 rem -= SECS_PER_DAY;
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)
1069 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1071 while (days < 0 || days >= (isleap (y) ? 366 : 365))
1073 /* Guess a corrected year, assuming 365 days per year. */
1074 long int yg = y + days / 365 - (days % 365 < 0);
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));
1082 tp->tm_year = y - 1900;
1084 ip = mon_yday[isleap(y)];
1085 for (y = 11; days < ip[y]; --y)
1089 tp->tm_mday = days + 1;
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;
1098 struct tzstring_head *h = &tzstring_list.head;
1102 /* Look through time zone string list for a duplicate of this one. */
1103 for (h = &tzstring_list.head; ; h = h->next)
1105 for (p = (char *) (h + 1); p[0] | p[1]; ++p)
1106 if (strcmp (p, string) == 0)
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. */
1115 needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string. */
1117 if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed)
1119 size_t buffer_size = tzstring_last_buffer_size;
1120 while ((buffer_size *= 2) < needed)
1122 if (! (h = h->next = (struct tzstring_head *)malloc (sizeof *h + buffer_size)))
1125 tzstring_last_buffer_size = buffer_size;
1126 p = (char *) (h + 1);
1129 return strncpy (p, string, needed);