use double-quotes instead of angle-brackets for non-system include files (bug #4058)
[asterisk/asterisk.git] / stdtime / localtime.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Multi-timezone Localtime code
5  * 
6  * Copyright (C) 2003, Mark Spencer
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  *
11  * Most of this code is in the public domain, so clarified as of
12  * June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
13  *
14  * All modifications to this code to abstract timezones away from
15  * the environment are by Tilghman Lesher, <tlesher@vcch.com>, with
16  * the copyright assigned to Digium.
17  */
18
19 /*
20  * Asterisk defines
21  *
22  * Don't mess with these unless you're really sure you know what you're doing.
23  */
24 #ifndef _THREAD_SAFE
25 #define _THREAD_SAFE
26 #endif
27 #define TZ_STRLEN_MAX   255
28 /* #define DEBUG */
29 #include "asterisk/lock.h"
30 #include "asterisk/localtime.h"
31
32
33 #ifndef lint
34 #ifndef NOID
35 static const char       elsieid[] = "@(#)localtime.c    7.57";
36 #endif /* !defined NOID */
37 #endif /* !defined lint */
38
39 /*
40 ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
41 ** POSIX-style TZ environment variable handling from Guy Harris
42 ** (guy@auspex.com).
43 */
44
45 /*LINTLIBRARY*/
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include "private.h"
50 #include "tzfile.h"
51 #include <fcntl.h>
52 #ifdef DEBUG
53 #include <stdio.h>
54 #endif
55
56 /*
57 ** SunOS 4.1.1 headers lack O_BINARY.
58 */
59
60 #ifdef O_BINARY
61 #define OPEN_MODE       (O_RDONLY | O_BINARY)
62 #endif /* defined O_BINARY */
63 #ifndef O_BINARY
64 #define OPEN_MODE       O_RDONLY
65 #endif /* !defined O_BINARY */
66
67 #ifndef WILDABBR
68 /*
69 ** Someone might make incorrect use of a time zone abbreviation:
70 **      1.      They might reference tzname[0] before calling ast_tzset (explicitly
71 **              or implicitly).
72 **      2.      They might reference tzname[1] before calling ast_tzset (explicitly
73 **              or implicitly).
74 **      3.      They might reference tzname[1] after setting to a time zone
75 **              in which Daylight Saving Time is never observed.
76 **      4.      They might reference tzname[0] after setting to a time zone
77 **              in which Standard Time is never observed.
78 **      5.      They might reference tm.TM_ZONE after calling offtime.
79 ** What's best to do in the above cases is open to debate;
80 ** for now, we just set things up so that in any of the five cases
81 ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
82 ** string "tzname[0] used before set", and similarly for the other cases.
83 ** And another:  initialize tzname[0] to "ERA", with an explanation in the
84 ** manual page of what this "time zone abbreviation" means (doing this so
85 ** that tzname[0] has the "normal" length of three characters).
86 */
87 #define WILDABBR        "   "
88 #endif /* !defined WILDABBR */
89
90 static char             wildabbr[] = "WILDABBR";
91
92 /* FreeBSD defines 'zone' in 'struct tm' as non-const, so don't declare this
93    string as const. */
94 static char             gmt[] = "GMT";
95
96 struct ttinfo {                         /* time type information */
97         long            tt_gmtoff;      /* GMT offset in seconds */
98         int             tt_isdst;       /* used to set tm_isdst */
99         int             tt_abbrind;     /* abbreviation list index */
100         int             tt_ttisstd;     /* TRUE if transition is std time */
101         int             tt_ttisgmt;     /* TRUE if transition is GMT */
102 };
103
104 struct lsinfo {                         /* leap second information */
105         time_t          ls_trans;       /* transition time */
106         long            ls_corr;        /* correction to apply */
107 };
108
109 #define BIGGEST(a, b)   (((a) > (b)) ? (a) : (b))
110
111 #ifdef TZNAME_MAX
112 #define MY_TZNAME_MAX   TZNAME_MAX
113 #endif /* defined TZNAME_MAX */
114 #ifndef TZNAME_MAX
115 #define MY_TZNAME_MAX   255
116 #endif /* !defined TZNAME_MAX */
117
118 #ifdef SOLARIS
119 #undef TM_ZONE
120 #undef TM_GMTOFF 
121 #endif
122
123
124 struct state {
125         char    name[TZ_STRLEN_MAX + 1];
126         int             leapcnt;
127         int             timecnt;
128         int             typecnt;
129         int             charcnt;
130         time_t          ats[TZ_MAX_TIMES];
131         unsigned char   types[TZ_MAX_TIMES];
132         struct ttinfo   ttis[TZ_MAX_TYPES];
133         char            chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
134                                 (2 * (MY_TZNAME_MAX + 1)))];
135         struct lsinfo   lsis[TZ_MAX_LEAPS];
136         struct state    *next;
137 };
138
139 struct rule {
140         int             r_type;         /* type of rule--see below */
141         int             r_day;          /* day number of rule */
142         int             r_week;         /* week number of rule */
143         int             r_mon;          /* month number of rule */
144         long            r_time;         /* transition time of rule */
145 };
146
147 #define JULIAN_DAY              0       /* Jn - Julian day */
148 #define DAY_OF_YEAR             1       /* n - day of year */
149 #define MONTH_NTH_DAY_OF_WEEK   2       /* Mm.n.d - month, week, day of week */
150
151 /*
152 ** Prototypes for static functions.
153 */
154
155 static long             detzcode P((const char * codep));
156 static const char *     getnum P((const char * strp, int * nump, int min,
157                                 int max));
158 static const char *     getsecs P((const char * strp, long * secsp));
159 static const char *     getoffset P((const char * strp, long * offsetp));
160 static const char *     getrule P((const char * strp, struct rule * rulep));
161 static void             gmtload P((struct state * sp));
162 static void             gmtsub P((const time_t * timep, long offset,
163                                 struct tm * tmp, const char * zone));
164 static void             localsub P((const time_t * timep, long offset,
165                                 struct tm * tmp, const char * zone));
166 static int              increment_overflow P((int * number, int delta));
167 static int              normalize_overflow P((int * tensptr, int * unitsptr,
168                                 int base));
169 static time_t           time1 P((struct tm * tmp,
170                                 void(*funcp) P((const time_t *,
171                                 long, struct tm *, const char*)),
172                                 long offset, const char * zone));
173 static time_t           time2 P((struct tm *tmp,
174                                 void(*funcp) P((const time_t *,
175                                 long, struct tm*, const char*)),
176                                 long offset, int * okayp, const char * zone));
177 static void             timesub P((const time_t * timep, long offset,
178                                 const struct state * sp, struct tm * tmp));
179 static int              tmcomp P((const struct tm * atmp,
180                                 const struct tm * btmp));
181 static time_t           transtime P((time_t janfirst, int year,
182                                 const struct rule * rulep, long offset));
183 static int              tzload P((const char * name, struct state * sp));
184 static int              tzparse P((const char * name, struct state * sp,
185                                 int lastditch));
186
187 static struct state *   lclptr      = NULL;
188 static struct state *   last_lclptr = NULL;
189 static struct state *   gmtptr      = NULL;
190
191 #ifndef TZ_STRLEN_MAX
192 #define TZ_STRLEN_MAX 255
193 #endif /* !defined TZ_STRLEN_MAX */
194
195 static int              gmt_is_set;
196 #ifdef  _THREAD_SAFE
197 AST_MUTEX_DEFINE_STATIC(lcl_mutex);
198 AST_MUTEX_DEFINE_STATIC(tzset_mutex);
199 AST_MUTEX_DEFINE_STATIC(tzsetwall_mutex);
200 AST_MUTEX_DEFINE_STATIC(gmt_mutex);
201 #endif
202
203 /*
204 ** Section 4.12.3 of X3.159-1989 requires that
205 **      Except for the strftime function, these functions [asctime,
206 **      ctime, gmtime, localtime] return values in one of two static
207 **      objects: a broken-down time structure and an array of char.
208 ** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
209 */
210
211 static long
212 detzcode(codep)
213 const char * const      codep;
214 {
215         register long   result;
216         register int    i;
217
218         result = (codep[0] & 0x80) ? ~0L : 0L;
219         for (i = 0; i < 4; ++i)
220                 result = (result << 8) | (codep[i] & 0xff);
221         return result;
222 }
223
224 static int
225 tzload(name, sp)
226 register const char *           name;
227 register struct state * const   sp;
228 {
229         register const char *   p;
230         register int            i;
231         register int            fid;
232
233 #ifdef DEBUG
234         fprintf(stderr,"tzload called with name=%s, sp=%d\n", name, sp);
235 #endif
236         if (name == NULL && (name = TZDEFAULT) == NULL)
237                 return -1;
238         {
239                 register int    doaccess;
240                 struct stat     stab;
241                 /*
242                 ** Section 4.9.1 of the C standard says that
243                 ** "FILENAME_MAX expands to an integral constant expression
244                 ** that is the size needed for an array of char large enough
245                 ** to hold the longest file name string that the implementation
246                 ** guarantees can be opened."
247                 */
248                 char            fullname[FILENAME_MAX + 1] = "";
249
250                 if (name[0] == ':')
251                         ++name;
252                 doaccess = name[0] == '/';
253                 if (!doaccess) {
254                         if ((p = TZDIR) == NULL)
255                                 return -1;
256                         if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
257                                 return -1;
258                         (void) strncpy(fullname, p, sizeof(fullname) - 1);
259                         (void) strncat(fullname, "/", sizeof(fullname) - strlen(fullname) - 1);
260                         (void) strncat(fullname, name, sizeof(fullname) - strlen(fullname) - 1);
261                         /*
262                         ** Set doaccess if '.' (as in "../") shows up in name.
263                         */
264                         if (strchr(name, '.') != NULL)
265                                 doaccess = TRUE;
266                         name = fullname;
267                 }
268                 if (doaccess && access(name, R_OK) != 0)
269                         return -1;
270                 if ((fid = open(name, OPEN_MODE)) == -1)
271                         return -1;
272                 if ((fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
273                         close(fid);
274                         return -1;
275                 }
276         }
277         {
278                 struct tzhead * tzhp;
279                 char            buf[sizeof *sp + sizeof *tzhp];
280                 int             ttisstdcnt;
281                 int             ttisgmtcnt;
282
283                 i = read(fid, buf, sizeof buf);
284                 if (close(fid) != 0)
285                         return -1;
286                 p = buf;
287                 p += (sizeof tzhp->tzh_magic) + (sizeof tzhp->tzh_reserved);
288                 ttisstdcnt = (int) detzcode(p);
289                 p += 4;
290                 ttisgmtcnt = (int) detzcode(p);
291                 p += 4;
292                 sp->leapcnt = (int) detzcode(p);
293                 p += 4;
294                 sp->timecnt = (int) detzcode(p);
295                 p += 4;
296                 sp->typecnt = (int) detzcode(p);
297                 p += 4;
298                 sp->charcnt = (int) detzcode(p);
299                 p += 4;
300                 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
301                         sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
302                         sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
303                         sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
304                         (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
305                         (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
306                                 return -1;
307                 if (i - (p - buf) < sp->timecnt * 4 +   /* ats */
308                         sp->timecnt +                   /* types */
309                         sp->typecnt * (4 + 2) +         /* ttinfos */
310                         sp->charcnt +                   /* chars */
311                         sp->leapcnt * (4 + 4) +         /* lsinfos */
312                         ttisstdcnt +                    /* ttisstds */
313                         ttisgmtcnt)                     /* ttisgmts */
314                                 return -1;
315                 for (i = 0; i < sp->timecnt; ++i) {
316                         sp->ats[i] = detzcode(p);
317                         p += 4;
318                 }
319                 for (i = 0; i < sp->timecnt; ++i) {
320                         sp->types[i] = (unsigned char) *p++;
321                         if (sp->types[i] >= sp->typecnt)
322                                 return -1;
323                 }
324                 for (i = 0; i < sp->typecnt; ++i) {
325                         register struct ttinfo *        ttisp;
326
327                         ttisp = &sp->ttis[i];
328                         ttisp->tt_gmtoff = detzcode(p);
329                         p += 4;
330                         ttisp->tt_isdst = (unsigned char) *p++;
331                         if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
332                                 return -1;
333                         ttisp->tt_abbrind = (unsigned char) *p++;
334                         if (ttisp->tt_abbrind < 0 ||
335                                 ttisp->tt_abbrind > sp->charcnt)
336                                         return -1;
337                 }
338                 for (i = 0; i < sp->charcnt; ++i)
339                         sp->chars[i] = *p++;
340                 sp->chars[i] = '\0';    /* ensure '\0' at end */
341                 for (i = 0; i < sp->leapcnt; ++i) {
342                         register struct lsinfo *        lsisp;
343
344                         lsisp = &sp->lsis[i];
345                         lsisp->ls_trans = detzcode(p);
346                         p += 4;
347                         lsisp->ls_corr = detzcode(p);
348                         p += 4;
349                 }
350                 for (i = 0; i < sp->typecnt; ++i) {
351                         register struct ttinfo *        ttisp;
352
353                         ttisp = &sp->ttis[i];
354                         if (ttisstdcnt == 0)
355                                 ttisp->tt_ttisstd = FALSE;
356                         else {
357                                 ttisp->tt_ttisstd = *p++;
358                                 if (ttisp->tt_ttisstd != TRUE &&
359                                         ttisp->tt_ttisstd != FALSE)
360                                                 return -1;
361                         }
362                 }
363                 for (i = 0; i < sp->typecnt; ++i) {
364                         register struct ttinfo *        ttisp;
365
366                         ttisp = &sp->ttis[i];
367                         if (ttisgmtcnt == 0)
368                                 ttisp->tt_ttisgmt = FALSE;
369                         else {
370                                 ttisp->tt_ttisgmt = *p++;
371                                 if (ttisp->tt_ttisgmt != TRUE &&
372                                         ttisp->tt_ttisgmt != FALSE)
373                                                 return -1;
374                         }
375                 }
376         }
377         return 0;
378 }
379
380 static const int        mon_lengths[2][MONSPERYEAR] = {
381         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
382         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
383 };
384
385 static const int        year_lengths[2] = {
386         DAYSPERNYEAR, DAYSPERLYEAR
387 };
388
389 /*
390 ** Given a pointer into a time zone string, extract a number from that string.
391 ** Check that the number is within a specified range; if it is not, return
392 ** NULL.
393 ** Otherwise, return a pointer to the first character not part of the number.
394 */
395
396 static const char *
397 getnum(strp, nump, min, max)
398 register const char *   strp;
399 int * const             nump;
400 const int               min;
401 const int               max;
402 {
403         register char   c;
404         register int    num;
405
406         if (strp == NULL || !is_digit(c = *strp))
407                 return NULL;
408         num = 0;
409         do {
410                 num = num * 10 + (c - '0');
411                 if (num > max)
412                         return NULL;    /* illegal value */
413                 c = *++strp;
414         } while (is_digit(c));
415         if (num < min)
416                 return NULL;            /* illegal value */
417         *nump = num;
418         return strp;
419 }
420
421 /*
422 ** Given a pointer into a time zone string, extract a number of seconds,
423 ** in hh[:mm[:ss]] form, from the string.
424 ** If any error occurs, return NULL.
425 ** Otherwise, return a pointer to the first character not part of the number
426 ** of seconds.
427 */
428
429 static const char *
430 getsecs(strp, secsp)
431 register const char *   strp;
432 long * const            secsp;
433 {
434         int     num;
435
436         /*
437         ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
438         ** "M10.4.6/26", which does not conform to Posix,
439         ** but which specifies the equivalent of
440         ** ``02:00 on the first Sunday on or after 23 Oct''.
441         */
442         strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
443         if (strp == NULL)
444                 return NULL;
445         *secsp = num * (long) SECSPERHOUR;
446         if (*strp == ':') {
447                 ++strp;
448                 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
449                 if (strp == NULL)
450                         return NULL;
451                 *secsp += num * SECSPERMIN;
452                 if (*strp == ':') {
453                         ++strp;
454                         /* `SECSPERMIN' allows for leap seconds.  */
455                         strp = getnum(strp, &num, 0, SECSPERMIN);
456                         if (strp == NULL)
457                                 return NULL;
458                         *secsp += num;
459                 }
460         }
461         return strp;
462 }
463
464 /*
465 ** Given a pointer into a time zone string, extract an offset, in
466 ** [+-]hh[:mm[:ss]] form, from the string.
467 ** If any error occurs, return NULL.
468 ** Otherwise, return a pointer to the first character not part of the time.
469 */
470
471 static const char *
472 getoffset(strp, offsetp)
473 register const char *   strp;
474 long * const            offsetp;
475 {
476         register int    neg = 0;
477
478         if (*strp == '-') {
479                 neg = 1;
480                 ++strp;
481         } else if (*strp == '+')
482                 ++strp;
483         strp = getsecs(strp, offsetp);
484         if (strp == NULL)
485                 return NULL;            /* illegal time */
486         if (neg)
487                 *offsetp = -*offsetp;
488         return strp;
489 }
490
491 /*
492 ** Given a pointer into a time zone string, extract a rule in the form
493 ** date[/time].  See POSIX section 8 for the format of "date" and "time".
494 ** If a valid rule is not found, return NULL.
495 ** Otherwise, return a pointer to the first character not part of the rule.
496 */
497
498 static const char *
499 getrule(strp, rulep)
500 const char *                    strp;
501 register struct rule * const    rulep;
502 {
503         if (*strp == 'J') {
504                 /*
505                 ** Julian day.
506                 */
507                 rulep->r_type = JULIAN_DAY;
508                 ++strp;
509                 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
510         } else if (*strp == 'M') {
511                 /*
512                 ** Month, week, day.
513                 */
514                 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
515                 ++strp;
516                 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
517                 if (strp == NULL)
518                         return NULL;
519                 if (*strp++ != '.')
520                         return NULL;
521                 strp = getnum(strp, &rulep->r_week, 1, 5);
522                 if (strp == NULL)
523                         return NULL;
524                 if (*strp++ != '.')
525                         return NULL;
526                 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
527         } else if (is_digit(*strp)) {
528                 /*
529                 ** Day of year.
530                 */
531                 rulep->r_type = DAY_OF_YEAR;
532                 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
533         } else  return NULL;            /* invalid format */
534         if (strp == NULL)
535                 return NULL;
536         if (*strp == '/') {
537                 /*
538                 ** Time specified.
539                 */
540                 ++strp;
541                 strp = getsecs(strp, &rulep->r_time);
542         } else  rulep->r_time = 2 * SECSPERHOUR;        /* default = 2:00:00 */
543         return strp;
544 }
545
546 /*
547 ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
548 ** year, a rule, and the offset from GMT at the time that rule takes effect,
549 ** calculate the Epoch-relative time that rule takes effect.
550 */
551
552 static time_t
553 transtime(janfirst, year, rulep, offset)
554 const time_t                            janfirst;
555 const int                               year;
556 register const struct rule * const      rulep;
557 const long                              offset;
558 {
559         register int    leapyear;
560         register time_t value = 0;
561         register int    i;
562         int             d, m1, yy0, yy1, yy2, dow;
563
564         leapyear = isleap(year);
565         switch (rulep->r_type) {
566
567         case JULIAN_DAY:
568                 /*
569                 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
570                 ** years.
571                 ** In non-leap years, or if the day number is 59 or less, just
572                 ** add SECSPERDAY times the day number-1 to the time of
573                 ** January 1, midnight, to get the day.
574                 */
575                 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
576                 if (leapyear && rulep->r_day >= 60)
577                         value += SECSPERDAY;
578                 break;
579
580         case DAY_OF_YEAR:
581                 /*
582                 ** n - day of year.
583                 ** Just add SECSPERDAY times the day number to the time of
584                 ** January 1, midnight, to get the day.
585                 */
586                 value = janfirst + rulep->r_day * SECSPERDAY;
587                 break;
588
589         case MONTH_NTH_DAY_OF_WEEK:
590                 /*
591                 ** Mm.n.d - nth "dth day" of month m.
592                 */
593                 value = janfirst;
594                 for (i = 0; i < rulep->r_mon - 1; ++i)
595                         value += mon_lengths[leapyear][i] * SECSPERDAY;
596
597                 /*
598                 ** Use Zeller's Congruence to get day-of-week of first day of
599                 ** month.
600                 */
601                 m1 = (rulep->r_mon + 9) % 12 + 1;
602                 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
603                 yy1 = yy0 / 100;
604                 yy2 = yy0 % 100;
605                 dow = ((26 * m1 - 2) / 10 +
606                         1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
607                 if (dow < 0)
608                         dow += DAYSPERWEEK;
609
610                 /*
611                 ** "dow" is the day-of-week of the first day of the month.  Get
612                 ** the day-of-month (zero-origin) of the first "dow" day of the
613                 ** month.
614                 */
615                 d = rulep->r_day - dow;
616                 if (d < 0)
617                         d += DAYSPERWEEK;
618                 for (i = 1; i < rulep->r_week; ++i) {
619                         if (d + DAYSPERWEEK >=
620                                 mon_lengths[leapyear][rulep->r_mon - 1])
621                                         break;
622                         d += DAYSPERWEEK;
623                 }
624
625                 /*
626                 ** "d" is the day-of-month (zero-origin) of the day we want.
627                 */
628                 value += d * SECSPERDAY;
629                 break;
630         }
631
632         /*
633         ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
634         ** question.  To get the Epoch-relative time of the specified local
635         ** time on that day, add the transition time and the current offset
636         ** from GMT.
637         */
638         return value + rulep->r_time + offset;
639 }
640
641 /*
642 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
643 ** appropriate.
644 */
645
646 static int
647 tzparse(name, sp, lastditch)
648 const char *                    name;
649 register struct state * const   sp;
650 const int                       lastditch;
651 {
652         const char *                    stdname;
653         const char *                    dstname = NULL;
654         size_t                          stdlen = 0;
655         size_t                          dstlen = 0;
656         long                            stdoffset = 0L;
657         long                            dstoffset = 0L;
658         register time_t *               atp;
659         register unsigned char *        typep;
660         register char *                 cp;
661         register int                    load_result;
662
663         stdname = name;
664 #ifdef DEBUG
665         fprintf(stderr, "tzparse(): loading default rules\n");
666 #endif
667         load_result = tzload(TZDEFRULES, sp);
668         if (load_result != 0)
669                 sp->leapcnt = 0;                /* so, we're off a little */
670         if (*name != '\0') {
671                 if (*name != '\0' && *name != ',' && *name != ';') {
672                         name = getoffset(name, &dstoffset);
673                         if (name == NULL)
674                                 return -1;
675                 } else  dstoffset = stdoffset - SECSPERHOUR;
676                 if (*name == ',' || *name == ';') {
677                         struct rule     start;
678                         struct rule     end;
679                         register int    year;
680                         register time_t janfirst;
681                         time_t          starttime;
682                         time_t          endtime;
683
684                         ++name;
685                         if ((name = getrule(name, &start)) == NULL)
686                                 return -1;
687                         if (*name++ != ',')
688                                 return -1;
689                         if ((name = getrule(name, &end)) == NULL)
690                                 return -1;
691                         if (*name != '\0')
692                                 return -1;
693                         sp->typecnt = 2;        /* standard time and DST */
694                         /*
695                         ** Two transitions per year, from EPOCH_YEAR to 2037.
696                         */
697                         sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
698                         if (sp->timecnt > TZ_MAX_TIMES)
699                                 return -1;
700                         sp->ttis[0].tt_gmtoff = -dstoffset;
701                         sp->ttis[0].tt_isdst = 1;
702                         sp->ttis[0].tt_abbrind = stdlen + 1;
703                         sp->ttis[1].tt_gmtoff = -stdoffset;
704                         sp->ttis[1].tt_isdst = 0;
705                         sp->ttis[1].tt_abbrind = 0;
706                         atp = sp->ats;
707                         typep = sp->types;
708                         janfirst = 0;
709                         for (year = EPOCH_YEAR; year <= 2037; ++year) {
710                                 starttime = transtime(janfirst, year, &start,
711                                         stdoffset);
712                                 endtime = transtime(janfirst, year, &end,
713                                         dstoffset);
714                                 if (starttime > endtime) {
715                                         *atp++ = endtime;
716                                         *typep++ = 1;   /* DST ends */
717                                         *atp++ = starttime;
718                                         *typep++ = 0;   /* DST begins */
719                                 } else {
720                                         *atp++ = starttime;
721                                         *typep++ = 0;   /* DST begins */
722                                         *atp++ = endtime;
723                                         *typep++ = 1;   /* DST ends */
724                                 }
725                                 janfirst += year_lengths[isleap(year)] *
726                                         SECSPERDAY;
727                         }
728                 } else {
729                         register long   theirstdoffset;
730                         register long   theirdstoffset;
731                         register long   theiroffset;
732                         register int    isdst;
733                         register int    i;
734                         register int    j;
735
736                         if (*name != '\0')
737                                 return -1;
738                         if (load_result != 0)
739                                 return -1;
740                         /*
741                         ** Initial values of theirstdoffset and theirdstoffset.
742                         */
743                         theirstdoffset = 0;
744                         for (i = 0; i < sp->timecnt; ++i) {
745                                 j = sp->types[i];
746                                 if (!sp->ttis[j].tt_isdst) {
747                                         theirstdoffset =
748                                                 -sp->ttis[j].tt_gmtoff;
749                                         break;
750                                 }
751                         }
752                         theirdstoffset = 0;
753                         for (i = 0; i < sp->timecnt; ++i) {
754                                 j = sp->types[i];
755                                 if (sp->ttis[j].tt_isdst) {
756                                         theirdstoffset =
757                                                 -sp->ttis[j].tt_gmtoff;
758                                         break;
759                                 }
760                         }
761                         /*
762                         ** Initially we're assumed to be in standard time.
763                         */
764                         isdst = FALSE;
765                         theiroffset = theirstdoffset;
766                         /*
767                         ** Now juggle transition times and types
768                         ** tracking offsets as you do.
769                         */
770                         for (i = 0; i < sp->timecnt; ++i) {
771                                 j = sp->types[i];
772                                 sp->types[i] = sp->ttis[j].tt_isdst;
773                                 if (sp->ttis[j].tt_ttisgmt) {
774                                         /* No adjustment to transition time */
775                                 } else {
776                                         /*
777                                         ** If summer time is in effect, and the
778                                         ** transition time was not specified as
779                                         ** standard time, add the summer time
780                                         ** offset to the transition time;
781                                         ** otherwise, add the standard time
782                                         ** offset to the transition time.
783                                         */
784                                         /*
785                                         ** Transitions from DST to DDST
786                                         ** will effectively disappear since
787                                         ** POSIX provides for only one DST
788                                         ** offset.
789                                         */
790                                         if (isdst && !sp->ttis[j].tt_ttisstd) {
791                                                 sp->ats[i] += dstoffset -
792                                                         theirdstoffset;
793                                         } else {
794                                                 sp->ats[i] += stdoffset -
795                                                         theirstdoffset;
796                                         }
797                                 }
798                                 theiroffset = -sp->ttis[j].tt_gmtoff;
799                                 if (sp->ttis[j].tt_isdst)
800                                         theirdstoffset = theiroffset;
801                                 else    theirstdoffset = theiroffset;
802                         }
803                         /*
804                         ** Finally, fill in ttis.
805                         ** ttisstd and ttisgmt need not be handled.
806                         */
807                         sp->ttis[0].tt_gmtoff = -stdoffset;
808                         sp->ttis[0].tt_isdst = FALSE;
809                         sp->ttis[0].tt_abbrind = 0;
810                         sp->ttis[1].tt_gmtoff = -dstoffset;
811                         sp->ttis[1].tt_isdst = TRUE;
812                         sp->ttis[1].tt_abbrind = stdlen + 1;
813                 }
814         } else {
815                 dstlen = 0;
816                 sp->typecnt = 1;                /* only standard time */
817                 sp->timecnt = 0;
818                 sp->ttis[0].tt_gmtoff = -stdoffset;
819                 sp->ttis[0].tt_isdst = 0;
820                 sp->ttis[0].tt_abbrind = 0;
821         }
822         sp->charcnt = stdlen + 1;
823         if (dstlen != 0)
824                 sp->charcnt += dstlen + 1;
825         if (sp->charcnt > sizeof sp->chars)
826                 return -1;
827         cp = sp->chars;
828         (void) strncpy(cp, stdname, stdlen);
829         cp += stdlen;
830         *cp++ = '\0';
831         if (dstlen != 0) {
832                 (void) strncpy(cp, dstname, dstlen);
833                 *(cp + dstlen) = '\0';
834         }
835         return 0;
836 }
837
838 static void
839 gmtload(sp)
840 struct state * const    sp;
841 {
842         if (tzload(gmt, sp) != 0)
843                 (void) tzparse(gmt, sp, TRUE);
844 }
845
846 /*
847 ** A non-static declaration of ast_tzsetwall in a system header file
848 ** may cause a warning about this upcoming static declaration...
849 */
850 static
851 #ifdef  _THREAD_SAFE
852 int
853 ast_tzsetwall_basic P((void))
854 #else
855 int
856 ast_tzsetwall P((void))
857 #endif
858 {
859         struct state *cur_state = lclptr;
860
861         /* Find the appropriate structure, if already parsed */
862         while (cur_state != NULL) {
863                 if (cur_state->name[0] == '\0')
864                         break;
865                 cur_state = cur_state->next;
866         }
867         if (cur_state != NULL)
868                 return 0;
869         cur_state = malloc(sizeof(struct state));
870         if (cur_state == NULL) {
871                 return -1;
872         }
873         memset(cur_state,0,sizeof(struct state));
874         if (tzload((char *) NULL, cur_state) != 0)
875 #ifdef DEBUG
876         {
877                 fprintf(stderr, "ast_tzsetwall: calling gmtload()\n");
878 #endif
879                 gmtload(cur_state);
880 #ifdef DEBUG
881         }
882 #endif
883
884         if (last_lclptr)
885                 last_lclptr->next = cur_state;
886         else
887                 lclptr = cur_state;
888         last_lclptr = cur_state;
889         return 0;
890 }
891
892 #ifdef  _THREAD_SAFE
893 int
894 ast_tzsetwall P((void))
895 {
896         ast_mutex_lock(&tzsetwall_mutex);
897         ast_tzsetwall_basic();
898         ast_mutex_unlock(&tzsetwall_mutex);
899         return 0;
900 }
901 #endif
902
903 #ifdef  _THREAD_SAFE
904 static int
905 ast_tzset_basic P((const char *name))
906 #else
907 int
908 ast_tzset P((const char *name))
909 #endif
910 {
911         struct state *cur_state = lclptr;
912
913         /* Not set at all */
914         if (name == NULL) {
915                 return ast_tzsetwall();
916         }
917
918         /* Find the appropriate structure, if already parsed */
919         while (cur_state != NULL) {
920                 if (!strcmp(cur_state->name,name))
921                         break;
922                 cur_state = cur_state->next;
923         }
924         if (cur_state != NULL)
925                 return 0;
926
927         cur_state = malloc(sizeof(struct state));
928         if (cur_state == NULL) {
929                 return -1;
930         }
931         memset(cur_state,0,sizeof(*cur_state));
932
933         /* Name is set, but set to the empty string == no adjustments */
934         if (name[0] == '\0') {
935                 /*
936                 ** User wants it fast rather than right.
937                 */
938                 cur_state->leapcnt = 0;         /* so, we're off a little */
939                 cur_state->timecnt = 0;
940                 cur_state->ttis[0].tt_gmtoff = 0;
941                 cur_state->ttis[0].tt_abbrind = 0;
942                 (void) strncpy(cur_state->chars, gmt, sizeof(cur_state->chars) - 1);
943         } else if (tzload(name, cur_state) != 0) {
944                 if (name[0] == ':') {
945                         (void) gmtload(cur_state);
946                 } else if (tzparse(name, cur_state, FALSE) != 0) {
947                         /* If not found, load localtime */
948                         if (tzload("/etc/localtime", cur_state) != 0)
949                                 /* Last ditch, get GMT */
950                                 (void) gmtload(cur_state);
951                 }
952         }
953         strncpy(cur_state->name, name, sizeof(cur_state->name) - 1);
954         if (last_lclptr)
955                 last_lclptr->next = cur_state;
956         else
957                 lclptr = cur_state;
958         last_lclptr = cur_state;
959         return 0;
960 }
961
962 #ifdef  _THREAD_SAFE
963 void
964 ast_tzset P((const char *name))
965 {
966         ast_mutex_lock(&tzset_mutex);
967         ast_tzset_basic(name);
968         ast_mutex_unlock(&tzset_mutex);
969 }
970 #endif
971
972 /*
973 ** The easy way to behave "as if no library function calls" localtime
974 ** is to not call it--so we drop its guts into "localsub", which can be
975 ** freely called.  (And no, the PANS doesn't require the above behavior--
976 ** but it *is* desirable.)
977 **
978 ** The unused offset argument is for the benefit of mktime variants.
979 */
980
981 /*ARGSUSED*/
982 static void
983 localsub(timep, offset, tmp, zone)
984 const time_t * const    timep;
985 const long              offset;
986 struct tm * const       tmp;
987 const char * const      zone;
988 {
989         register struct state *         sp;
990         register const struct ttinfo *  ttisp;
991         register int                    i;
992         const time_t                    t = *timep;
993
994         sp = lclptr;
995         /* Find the right zone record */
996         if (zone == NULL)
997                 sp = NULL;
998         else
999                 while (sp != NULL) {
1000                         if (!strcmp(sp->name,zone))
1001                                 break;
1002                         sp = sp->next;
1003                 }
1004
1005         if (sp == NULL) {
1006                 ast_tzsetwall();
1007                 sp = lclptr;
1008                 /* Find the default zone record */
1009                 while (sp != NULL) {
1010                         if (sp->name[0] == '\0')
1011                                 break;
1012                         sp = sp->next;
1013                 }
1014         }
1015
1016         /* Last ditch effort, use GMT */
1017         if (sp == NULL) {
1018                 gmtsub(timep, offset, tmp, zone);
1019                 return;
1020         }
1021         if (sp->timecnt == 0 || t < sp->ats[0]) {
1022                 i = 0;
1023                 while (sp->ttis[i].tt_isdst)
1024                         if (++i >= sp->typecnt) {
1025                                 i = 0;
1026                                 break;
1027                         }
1028         } else {
1029                 for (i = 1; i < sp->timecnt; ++i)
1030                         if (t < sp->ats[i])
1031                                 break;
1032                 i = sp->types[i - 1];
1033         }
1034         ttisp = &sp->ttis[i];
1035         /*
1036         ** To get (wrong) behavior that's compatible with System V Release 2.0
1037         ** you'd replace the statement below with
1038         **      t += ttisp->tt_gmtoff;
1039         **      timesub(&t, 0L, sp, tmp);
1040         */
1041         timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1042         tmp->tm_isdst = ttisp->tt_isdst;
1043         tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1044 #ifdef TM_ZONE
1045         tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1046 #endif /* defined TM_ZONE */
1047 }
1048
1049 struct tm *
1050 ast_localtime(timep, p_tm, zone)
1051 const time_t * const    timep;
1052 struct tm *p_tm;
1053 const char * const      zone;
1054 {
1055 #ifdef _THREAD_SAFE
1056         ast_mutex_lock(&lcl_mutex);
1057 #endif
1058         ast_tzset(zone);
1059         localsub(timep, 0L, p_tm, zone);
1060 #ifdef _THREAD_SAFE
1061         ast_mutex_unlock(&lcl_mutex);
1062 #endif
1063         return(p_tm);
1064 }
1065
1066 /*
1067 ** gmtsub is to gmtime as localsub is to localtime.
1068 */
1069
1070 static void
1071 gmtsub(timep, offset, tmp, zone)
1072 const time_t * const    timep;
1073 const long              offset;
1074 struct tm * const       tmp;
1075 const char * const      zone;
1076 {
1077 #ifdef  _THREAD_SAFE
1078         ast_mutex_lock(&gmt_mutex);
1079 #endif
1080         if (!gmt_is_set) {
1081                 gmt_is_set = TRUE;
1082                 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1083                 if (gmtptr != NULL)
1084                         gmtload(gmtptr);
1085         }
1086         ast_mutex_unlock(&gmt_mutex);
1087         timesub(timep, offset, gmtptr, tmp);
1088 #ifdef TM_ZONE
1089         /*
1090         ** Could get fancy here and deliver something such as
1091         ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1092         ** but this is no time for a treasure hunt.
1093         */
1094         if (offset != 0)
1095                 tmp->TM_ZONE = wildabbr;
1096         else {
1097                 if (gmtptr == NULL)
1098                         tmp->TM_ZONE = gmt;
1099                 else    tmp->TM_ZONE = gmtptr->chars;
1100         }
1101 #endif /* defined TM_ZONE */
1102 }
1103
1104 static void
1105 timesub(timep, offset, sp, tmp)
1106 const time_t * const                    timep;
1107 const long                              offset;
1108 register const struct state * const     sp;
1109 register struct tm * const              tmp;
1110 {
1111         register const struct lsinfo *  lp;
1112         register long                   days;
1113         register long                   rem;
1114         register int                    y;
1115         register int                    yleap;
1116         register const int *            ip;
1117         register long                   corr;
1118         register int                    hit;
1119         register int                    i;
1120
1121         corr = 0;
1122         hit = 0;
1123         i = (sp == NULL) ? 0 : sp->leapcnt;
1124         while (--i >= 0) {
1125                 lp = &sp->lsis[i];
1126                 if (*timep >= lp->ls_trans) {
1127                         if (*timep == lp->ls_trans) {
1128                                 hit = ((i == 0 && lp->ls_corr > 0) ||
1129                                         lp->ls_corr > sp->lsis[i - 1].ls_corr);
1130                                 if (hit)
1131                                         while (i > 0 &&
1132                                                 sp->lsis[i].ls_trans ==
1133                                                 sp->lsis[i - 1].ls_trans + 1 &&
1134                                                 sp->lsis[i].ls_corr ==
1135                                                 sp->lsis[i - 1].ls_corr + 1) {
1136                                                         ++hit;
1137                                                         --i;
1138                                         }
1139                         }
1140                         corr = lp->ls_corr;
1141                         break;
1142                 }
1143         }
1144         days = *timep / SECSPERDAY;
1145         rem = *timep % SECSPERDAY;
1146 #ifdef mc68k
1147         if (*timep == 0x80000000) {
1148                 /*
1149                 ** A 3B1 muffs the division on the most negative number.
1150                 */
1151                 days = -24855;
1152                 rem = -11648;
1153         }
1154 #endif /* defined mc68k */
1155         rem += (offset - corr);
1156         while (rem < 0) {
1157                 rem += SECSPERDAY;
1158                 --days;
1159         }
1160         while (rem >= SECSPERDAY) {
1161                 rem -= SECSPERDAY;
1162                 ++days;
1163         }
1164         tmp->tm_hour = (int) (rem / SECSPERHOUR);
1165         rem = rem % SECSPERHOUR;
1166         tmp->tm_min = (int) (rem / SECSPERMIN);
1167         /*
1168         ** A positive leap second requires a special
1169         ** representation.  This uses "... ??:59:60" et seq.
1170         */
1171         tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1172         tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1173         if (tmp->tm_wday < 0)
1174                 tmp->tm_wday += DAYSPERWEEK;
1175         y = EPOCH_YEAR;
1176 #define LEAPS_THRU_END_OF(y)    ((y) / 4 - (y) / 100 + (y) / 400)
1177         while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1178                 register int    newy;
1179
1180                 newy = y + days / DAYSPERNYEAR;
1181                 if (days < 0)
1182                         --newy;
1183                 days -= (newy - y) * DAYSPERNYEAR +
1184                         LEAPS_THRU_END_OF(newy - 1) -
1185                         LEAPS_THRU_END_OF(y - 1);
1186                 y = newy;
1187         }
1188         tmp->tm_year = y - TM_YEAR_BASE;
1189         tmp->tm_yday = (int) days;
1190         ip = mon_lengths[yleap];
1191         for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1192                 days = days - (long) ip[tmp->tm_mon];
1193         tmp->tm_mday = (int) (days + 1);
1194         tmp->tm_isdst = 0;
1195 #ifdef TM_GMTOFF
1196         tmp->TM_GMTOFF = offset;
1197 #endif /* defined TM_GMTOFF */
1198 }
1199
1200 char *
1201 ast_ctime(timep)
1202 const time_t * const    timep;
1203 {
1204 /*
1205 ** Section 4.12.3.2 of X3.159-1989 requires that
1206 **      The ctime funciton converts the calendar time pointed to by timer
1207 **      to local time in the form of a string.  It is equivalent to
1208 **              asctime(localtime(timer))
1209 */
1210         return asctime(localtime(timep));
1211 }
1212
1213 char *
1214 ast_ctime_r(timep, buf)
1215 const time_t * const    timep;
1216 char *buf;
1217 {
1218         struct tm tm;
1219 #ifdef SOLARIS
1220         return asctime_r(localtime_r(timep, &tm), buf, 256);
1221 #else
1222         return asctime_r(localtime_r(timep, &tm), buf);
1223 #endif
1224 }
1225
1226 /*
1227 ** Adapted from code provided by Robert Elz, who writes:
1228 **      The "best" way to do mktime I think is based on an idea of Bob
1229 **      Kridle's (so its said...) from a long time ago.
1230 **      [kridle@xinet.com as of 1996-01-16.]
1231 **      It does a binary search of the time_t space.  Since time_t's are
1232 **      just 32 bits, its a max of 32 iterations (even at 64 bits it
1233 **      would still be very reasonable).
1234 */
1235
1236 #ifndef WRONG
1237 #define WRONG   (-1)
1238 #endif /* !defined WRONG */
1239
1240 /*
1241 ** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1242 */
1243
1244 static int
1245 increment_overflow(number, delta)
1246 int *   number;
1247 int     delta;
1248 {
1249         int     number0;
1250
1251         number0 = *number;
1252         *number += delta;
1253         return (*number < number0) != (delta < 0);
1254 }
1255
1256 static int
1257 normalize_overflow(tensptr, unitsptr, base)
1258 int * const     tensptr;
1259 int * const     unitsptr;
1260 const int       base;
1261 {
1262         register int    tensdelta;
1263
1264         tensdelta = (*unitsptr >= 0) ?
1265                 (*unitsptr / base) :
1266                 (-1 - (-1 - *unitsptr) / base);
1267         *unitsptr -= tensdelta * base;
1268         return increment_overflow(tensptr, tensdelta);
1269 }
1270
1271 static int
1272 tmcomp(atmp, btmp)
1273 register const struct tm * const atmp;
1274 register const struct tm * const btmp;
1275 {
1276         register int    result;
1277
1278         if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1279                 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1280                 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1281                 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1282                 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1283                         result = atmp->tm_sec - btmp->tm_sec;
1284         return result;
1285 }
1286
1287 static time_t
1288 time2(tmp, funcp, offset, okayp, zone)
1289 struct tm * const       tmp;
1290 void (* const           funcp) P((const time_t*, long, struct tm*, const char*));
1291 const long              offset;
1292 int * const             okayp;
1293 const char * const      zone;
1294 {
1295         register const struct state *   sp;
1296         register int                    dir;
1297         register int                    bits;
1298         register int                    i, j ;
1299         register int                    saved_seconds;
1300         time_t                          newt;
1301         time_t                          t;
1302         struct tm                       yourtm, mytm;
1303
1304         *okayp = FALSE;
1305         yourtm = *tmp;
1306         if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1307                 return WRONG;
1308         if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1309                 return WRONG;
1310         if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1311                 return WRONG;
1312         /*
1313         ** Turn yourtm.tm_year into an actual year number for now.
1314         ** It is converted back to an offset from TM_YEAR_BASE later.
1315         */
1316         if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1317                 return WRONG;
1318         while (yourtm.tm_mday <= 0) {
1319                 if (increment_overflow(&yourtm.tm_year, -1))
1320                         return WRONG;
1321                 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1322                 yourtm.tm_mday += year_lengths[isleap(i)];
1323         }
1324         while (yourtm.tm_mday > DAYSPERLYEAR) {
1325                 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1326                 yourtm.tm_mday -= year_lengths[isleap(i)];
1327                 if (increment_overflow(&yourtm.tm_year, 1))
1328                         return WRONG;
1329         }
1330         for ( ; ; ) {
1331                 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1332                 if (yourtm.tm_mday <= i)
1333                         break;
1334                 yourtm.tm_mday -= i;
1335                 if (++yourtm.tm_mon >= MONSPERYEAR) {
1336                         yourtm.tm_mon = 0;
1337                         if (increment_overflow(&yourtm.tm_year, 1))
1338                                 return WRONG;
1339                 }
1340         }
1341         if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1342                 return WRONG;
1343         if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1344                 /*
1345                 ** We can't set tm_sec to 0, because that might push the
1346                 ** time below the minimum representable time.
1347                 ** Set tm_sec to 59 instead.
1348                 ** This assumes that the minimum representable time is
1349                 ** not in the same minute that a leap second was deleted from,
1350                 ** which is a safer assumption than using 58 would be.
1351                 */
1352                 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1353                         return WRONG;
1354                 saved_seconds = yourtm.tm_sec;
1355                 yourtm.tm_sec = SECSPERMIN - 1;
1356         } else {
1357                 saved_seconds = yourtm.tm_sec;
1358                 yourtm.tm_sec = 0;
1359         }
1360         /*
1361         ** Divide the search space in half
1362         ** (this works whether time_t is signed or unsigned).
1363         */
1364         bits = TYPE_BIT(time_t) - 1;
1365         /*
1366         ** If time_t is signed, then 0 is just above the median,
1367         ** assuming two's complement arithmetic.
1368         ** If time_t is unsigned, then (1 << bits) is just above the median.
1369         */
1370         t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1371         for ( ; ; ) {
1372                 (*funcp)(&t, offset, &mytm, zone);
1373                 dir = tmcomp(&mytm, &yourtm);
1374                 if (dir != 0) {
1375                         if (bits-- < 0)
1376                                 return WRONG;
1377                         if (bits < 0)
1378                                 --t; /* may be needed if new t is minimal */
1379                         else if (dir > 0)
1380                                 t -= ((time_t) 1) << bits;
1381                         else    t += ((time_t) 1) << bits;
1382                         continue;
1383                 }
1384                 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1385                         break;
1386                 /*
1387                 ** Right time, wrong type.
1388                 ** Hunt for right time, right type.
1389                 ** It's okay to guess wrong since the guess
1390                 ** gets checked.
1391                 */
1392                 /*
1393                 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1394                 */
1395                 sp = (const struct state *)
1396                         (((void *) funcp == (void *) localsub) ?
1397                         lclptr : gmtptr);
1398                 if (sp == NULL)
1399                         return WRONG;
1400                 for (i = sp->typecnt - 1; i >= 0; --i) {
1401                         if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1402                                 continue;
1403                         for (j = sp->typecnt - 1; j >= 0; --j) {
1404                                 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1405                                         continue;
1406                                 newt = t + sp->ttis[j].tt_gmtoff -
1407                                         sp->ttis[i].tt_gmtoff;
1408                                 (*funcp)(&newt, offset, &mytm, zone);
1409                                 if (tmcomp(&mytm, &yourtm) != 0)
1410                                         continue;
1411                                 if (mytm.tm_isdst != yourtm.tm_isdst)
1412                                         continue;
1413                                 /*
1414                                 ** We have a match.
1415                                 */
1416                                 t = newt;
1417                                 goto label;
1418                         }
1419                 }
1420                 return WRONG;
1421         }
1422 label:
1423         newt = t + saved_seconds;
1424         if ((newt < t) != (saved_seconds < 0))
1425                 return WRONG;
1426         t = newt;
1427         (*funcp)(&t, offset, tmp, zone);
1428         *okayp = TRUE;
1429         return t;
1430 }
1431
1432 static time_t
1433 time1(tmp, funcp, offset, zone)
1434 struct tm * const       tmp;
1435 void (* const           funcp) P((const time_t *, long, struct tm *, const char*));
1436 const long              offset;
1437 const char * const      zone;
1438 {
1439         register time_t                 t;
1440         register const struct state *   sp;
1441         register int                    samei, otheri;
1442         int                             okay;
1443
1444         if (tmp->tm_isdst > 1)
1445                 tmp->tm_isdst = 1;
1446         t = time2(tmp, funcp, offset, &okay, zone);
1447 #ifdef PCTS
1448         /*
1449         ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1450         */
1451         if (okay)
1452                 return t;
1453         if (tmp->tm_isdst < 0)
1454                 tmp->tm_isdst = 0;      /* reset to std and try again */
1455 #endif /* defined PCTS */
1456 #ifndef PCTS
1457         if (okay || tmp->tm_isdst < 0)
1458                 return t;
1459 #endif /* !defined PCTS */
1460         /*
1461         ** We're supposed to assume that somebody took a time of one type
1462         ** and did some math on it that yielded a "struct tm" that's bad.
1463         ** We try to divine the type they started from and adjust to the
1464         ** type they need.
1465         */
1466         /*
1467         ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1468         */
1469         sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1470                 lclptr : gmtptr);
1471         if (sp == NULL)
1472                 return WRONG;
1473         for (samei = sp->typecnt - 1; samei >= 0; --samei) {
1474                 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1475                         continue;
1476                 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
1477                         if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1478                                 continue;
1479                         tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1480                                         sp->ttis[samei].tt_gmtoff;
1481                         tmp->tm_isdst = !tmp->tm_isdst;
1482                         t = time2(tmp, funcp, offset, &okay, zone);
1483                         if (okay)
1484                                 return t;
1485                         tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1486                                         sp->ttis[samei].tt_gmtoff;
1487                         tmp->tm_isdst = !tmp->tm_isdst;
1488                 }
1489         }
1490         return WRONG;
1491 }
1492
1493 time_t
1494 ast_mktime(tmp,zone)
1495 struct tm * const       tmp;
1496 const char * const      zone;
1497 {
1498         time_t mktime_return_value;
1499 #ifdef  _THREAD_SAFE
1500         ast_mutex_lock(&lcl_mutex);
1501 #endif
1502         ast_tzset(zone);
1503         mktime_return_value = time1(tmp, localsub, 0L, zone);
1504 #ifdef  _THREAD_SAFE
1505         ast_mutex_unlock(&lcl_mutex);
1506 #endif
1507         return(mktime_return_value);
1508 }
1509