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