]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tzcode/localtime.c
Mechanically convert if_sume(4) to IfAPI
[FreeBSD/FreeBSD.git] / contrib / tzcode / localtime.c
1 /* Convert timestamp from time_t to struct tm.  */
2
3 /*
4 ** This file is in the public domain, so clarified as of
5 ** 1996-06-05 by Arthur David Olson.
6 */
7
8 /*
9 ** Leap second handling from Bradley White.
10 ** POSIX-style TZ environment variable handling from Guy Harris.
11 */
12
13 /*LINTLIBRARY*/
14
15 #define LOCALTIME_IMPLEMENTATION
16 #include "namespace.h"
17 #ifdef DETECT_TZ_CHANGES
18 #ifndef DETECT_TZ_CHANGES_INTERVAL
19 #define DETECT_TZ_CHANGES_INTERVAL 61
20 #endif
21 #include <sys/stat.h>
22 #endif
23 #include <fcntl.h>
24 #if THREAD_SAFE
25 #include <pthread.h>
26 #endif
27 #include "private.h"
28 #include "un-namespace.h"
29
30 #include "tzfile.h"
31
32 #include "libc_private.h"
33
34 #if defined THREAD_SAFE && THREAD_SAFE
35 static pthread_mutex_t locallock = PTHREAD_MUTEX_INITIALIZER;
36 static int lock(void) {
37         if (__isthreaded)
38                 return _pthread_mutex_lock(&locallock);
39         return 0;
40 }
41 static void unlock(void) {
42         if (__isthreaded)
43                 _pthread_mutex_unlock(&locallock);
44 }
45 #else
46 static int lock(void) { return 0; }
47 static void unlock(void) { }
48 #endif
49
50 #ifndef TZ_ABBR_MAX_LEN
51 # define TZ_ABBR_MAX_LEN 16
52 #endif /* !defined TZ_ABBR_MAX_LEN */
53
54 #ifndef TZ_ABBR_CHAR_SET
55 # define TZ_ABBR_CHAR_SET \
56         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
57 #endif /* !defined TZ_ABBR_CHAR_SET */
58
59 #ifndef TZ_ABBR_ERR_CHAR
60 # define TZ_ABBR_ERR_CHAR '_'
61 #endif /* !defined TZ_ABBR_ERR_CHAR */
62
63 /*
64 ** Support non-POSIX platforms that distinguish between text and binary files.
65 */
66
67 #ifndef O_BINARY
68 # define O_BINARY 0
69 #endif
70
71 #ifndef WILDABBR
72 /*
73 ** Someone might make incorrect use of a time zone abbreviation:
74 **      1.      They might reference tzname[0] before calling tzset (explicitly
75 **              or implicitly).
76 **      2.      They might reference tzname[1] before calling tzset (explicitly
77 **              or implicitly).
78 **      3.      They might reference tzname[1] after setting to a time zone
79 **              in which Daylight Saving Time is never observed.
80 **      4.      They might reference tzname[0] after setting to a time zone
81 **              in which Standard Time is never observed.
82 **      5.      They might reference tm.TM_ZONE after calling offtime.
83 ** What's best to do in the above cases is open to debate;
84 ** for now, we just set things up so that in any of the five cases
85 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
86 ** string "tzname[0] used before set", and similarly for the other cases.
87 ** And another: initialize tzname[0] to "ERA", with an explanation in the
88 ** manual page of what this "time zone abbreviation" means (doing this so
89 ** that tzname[0] has the "normal" length of three characters).
90 */
91 # define WILDABBR "   "
92 #endif /* !defined WILDABBR */
93
94 static const char       wildabbr[] = WILDABBR;
95
96 static char const etc_utc[] = "Etc/UTC";
97 static char const *utc = etc_utc + sizeof "Etc/" - 1;
98
99 /*
100 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
101 ** Default to US rules as of 2017-05-07.
102 ** POSIX does not specify the default DST rules;
103 ** for historical reasons, US rules are a common default.
104 */
105 #ifndef TZDEFRULESTRING
106 # define TZDEFRULESTRING ",M3.2.0,M11.1.0"
107 #endif
108
109 struct ttinfo {                         /* time type information */
110         int_fast32_t    tt_utoff;       /* UT offset in seconds */
111         bool            tt_isdst;       /* used to set tm_isdst */
112         int             tt_desigidx;    /* abbreviation list index */
113         bool            tt_ttisstd;     /* transition is std time */
114         bool            tt_ttisut;      /* transition is UT */
115 };
116
117 struct lsinfo {                         /* leap second information */
118         time_t          ls_trans;       /* transition time */
119         int_fast32_t    ls_corr;        /* correction to apply */
120 };
121
122 /* This abbreviation means local time is unspecified.  */
123 static char const UNSPEC[] = "-00";
124
125 /* How many extra bytes are needed at the end of struct state's chars array.
126    This needs to be at least 1 for null termination in case the input
127    data isn't properly terminated, and it also needs to be big enough
128    for ttunspecified to work without crashing.  */
129 enum { CHARS_EXTRA = max(sizeof UNSPEC, 2) - 1 };
130
131 #ifdef TZNAME_MAX
132 # define MY_TZNAME_MAX TZNAME_MAX
133 #endif /* defined TZNAME_MAX */
134 #ifndef TZNAME_MAX
135 # define MY_TZNAME_MAX 255
136 #endif /* !defined TZNAME_MAX */
137
138 struct state {
139         int             leapcnt;
140         int             timecnt;
141         int             typecnt;
142         int             charcnt;
143         bool            goback;
144         bool            goahead;
145         time_t          ats[TZ_MAX_TIMES];
146         unsigned char   types[TZ_MAX_TIMES];
147         struct ttinfo   ttis[TZ_MAX_TYPES];
148         char chars[max(max(TZ_MAX_CHARS + CHARS_EXTRA, sizeof "UTC"),
149                        2 * (MY_TZNAME_MAX + 1))];
150         struct lsinfo   lsis[TZ_MAX_LEAPS];
151
152         /* The time type to use for early times or if no transitions.
153            It is always zero for recent tzdb releases.
154            It might be nonzero for data from tzdb 2018e or earlier.  */
155         int defaulttype;
156 };
157
158 enum r_type {
159   JULIAN_DAY,           /* Jn = Julian day */
160   DAY_OF_YEAR,          /* n = day of year */
161   MONTH_NTH_DAY_OF_WEEK /* Mm.n.d = month, week, day of week */
162 };
163
164 struct rule {
165         enum r_type     r_type;         /* type of rule */
166         int             r_day;          /* day number of rule */
167         int             r_week;         /* week number of rule */
168         int             r_mon;          /* month number of rule */
169         int_fast32_t    r_time;         /* transition time of rule */
170 };
171
172 static struct tm *gmtsub(struct state const *, time_t const *, int_fast32_t,
173                          struct tm *);
174 static bool increment_overflow(int *, int);
175 static bool increment_overflow_time(time_t *, int_fast32_t);
176 static int_fast32_t leapcorr(struct state const *, time_t);
177 static bool normalize_overflow32(int_fast32_t *, int *, int);
178 static struct tm *timesub(time_t const *, int_fast32_t, struct state const *,
179                           struct tm *);
180 static bool typesequiv(struct state const *, int, int);
181 static bool tzparse(char const *, struct state *, struct state *);
182
183 #ifdef ALL_STATE
184 static struct state *   lclptr;
185 static struct state *   gmtptr;
186 #endif /* defined ALL_STATE */
187
188 #ifndef ALL_STATE
189 static struct state     lclmem;
190 static struct state     gmtmem;
191 static struct state *const lclptr = &lclmem;
192 static struct state *const gmtptr = &gmtmem;
193 #endif /* State Farm */
194
195 #ifndef TZ_STRLEN_MAX
196 # define TZ_STRLEN_MAX 255
197 #endif /* !defined TZ_STRLEN_MAX */
198
199 static char             lcl_TZname[TZ_STRLEN_MAX + 1];
200 static int              lcl_is_set;
201
202 static pthread_once_t   gmt_once = PTHREAD_ONCE_INIT;
203 static pthread_once_t   gmtime_once = PTHREAD_ONCE_INIT;
204 static pthread_key_t    gmtime_key;
205 static int              gmtime_key_error;
206 static pthread_once_t   localtime_once = PTHREAD_ONCE_INIT;
207 static pthread_key_t    localtime_key;
208 static int              localtime_key_error;
209
210 /*
211 ** Section 4.12.3 of X3.159-1989 requires that
212 **      Except for the strftime function, these functions [asctime,
213 **      ctime, gmtime, localtime] return values in one of two static
214 **      objects: a broken-down time structure and an array of char.
215 ** Thanks to Paul Eggert for noting this.
216 */
217
218 static struct tm        tm;
219
220 #if 2 <= HAVE_TZNAME + TZ_TIME_T
221 char *                  tzname[2] = {
222         (char *) wildabbr,
223         (char *) wildabbr
224 };
225 #endif
226 #if 2 <= USG_COMPAT + TZ_TIME_T
227 long                    timezone;
228 int                     daylight;
229 #endif
230 #if 2 <= ALTZONE + TZ_TIME_T
231 long                    altzone;
232 #endif
233
234 /* Initialize *S to a value based on UTOFF, ISDST, and DESIGIDX.  */
235 static void
236 init_ttinfo(struct ttinfo *s, int_fast32_t utoff, bool isdst, int desigidx)
237 {
238   s->tt_utoff = utoff;
239   s->tt_isdst = isdst;
240   s->tt_desigidx = desigidx;
241   s->tt_ttisstd = false;
242   s->tt_ttisut = false;
243 }
244
245 /* Return true if SP's time type I does not specify local time.  */
246 static bool
247 ttunspecified(struct state const *sp, int i)
248 {
249   char const *abbr = &sp->chars[sp->ttis[i].tt_desigidx];
250   /* memcmp is likely faster than strcmp, and is safe due to CHARS_EXTRA.  */
251   return memcmp(abbr, UNSPEC, sizeof UNSPEC) == 0;
252 }
253
254 static int_fast32_t
255 detzcode(const char *const codep)
256 {
257         register int_fast32_t   result;
258         register int            i;
259         int_fast32_t one = 1;
260         int_fast32_t halfmaxval = one << (32 - 2);
261         int_fast32_t maxval = halfmaxval - 1 + halfmaxval;
262         int_fast32_t minval = -1 - maxval;
263
264         result = codep[0] & 0x7f;
265         for (i = 1; i < 4; ++i)
266                 result = (result << 8) | (codep[i] & 0xff);
267
268         if (codep[0] & 0x80) {
269           /* Do two's-complement negation even on non-two's-complement machines.
270              If the result would be minval - 1, return minval.  */
271           result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0;
272           result += minval;
273         }
274         return result;
275 }
276
277 static int_fast64_t
278 detzcode64(const char *const codep)
279 {
280         register int_fast64_t result;
281         register int    i;
282         int_fast64_t one = 1;
283         int_fast64_t halfmaxval = one << (64 - 2);
284         int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
285         int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
286
287         result = codep[0] & 0x7f;
288         for (i = 1; i < 8; ++i)
289                 result = (result << 8) | (codep[i] & 0xff);
290
291         if (codep[0] & 0x80) {
292           /* Do two's-complement negation even on non-two's-complement machines.
293              If the result would be minval - 1, return minval.  */
294           result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
295           result += minval;
296         }
297         return result;
298 }
299
300 static void
301 update_tzname_etc(struct state const *sp, struct ttinfo const *ttisp)
302 {
303 #if HAVE_TZNAME
304   tzname[ttisp->tt_isdst] = (char *) &sp->chars[ttisp->tt_desigidx];
305 #endif
306 #if USG_COMPAT
307   if (!ttisp->tt_isdst)
308     timezone = - ttisp->tt_utoff;
309 #endif
310 #if ALTZONE
311   if (ttisp->tt_isdst)
312     altzone = - ttisp->tt_utoff;
313 #endif
314 }
315
316 /* If STDDST_MASK indicates that SP's TYPE provides useful info,
317    update tzname, timezone, and/or altzone and return STDDST_MASK,
318    diminished by the provided info if it is a specified local time.
319    Otherwise, return STDDST_MASK.  See settzname for STDDST_MASK.  */
320 static int
321 may_update_tzname_etc(int stddst_mask, struct state *sp, int type)
322 {
323   struct ttinfo *ttisp = &sp->ttis[type];
324   int this_bit = 1 << ttisp->tt_isdst;
325   if (stddst_mask & this_bit) {
326     update_tzname_etc(sp, ttisp);
327     if (!ttunspecified(sp, type))
328       return stddst_mask & ~this_bit;
329   }
330   return stddst_mask;
331 }
332
333 static void
334 settzname(void)
335 {
336         register struct state * const   sp = lclptr;
337         register int                    i;
338
339         /* If STDDST_MASK & 1 we need info about a standard time.
340            If STDDST_MASK & 2 we need info about a daylight saving time.
341            When STDDST_MASK becomes zero we can stop looking.  */
342         int stddst_mask = 0;
343
344 #if HAVE_TZNAME
345         tzname[0] = tzname[1] = (char *) (sp ? wildabbr : utc);
346         stddst_mask = 3;
347 #endif
348 #if USG_COMPAT
349         timezone = 0;
350         stddst_mask = 3;
351 #endif
352 #if ALTZONE
353         altzone = 0;
354         stddst_mask |= 2;
355 #endif
356         /*
357         ** And to get the latest time zone abbreviations into tzname. . .
358         */
359         if (sp) {
360           for (i = sp->timecnt - 1; stddst_mask && 0 <= i; i--)
361             stddst_mask = may_update_tzname_etc(stddst_mask, sp, sp->types[i]);
362           for (i = sp->typecnt - 1; stddst_mask && 0 <= i; i--)
363             stddst_mask = may_update_tzname_etc(stddst_mask, sp, i);
364         }
365 #if USG_COMPAT
366         daylight = stddst_mask >> 1 ^ 1;
367 #endif
368 }
369
370 static void
371 scrub_abbrs(struct state *sp)
372 {
373         int i;
374         /*
375         ** First, replace bogus characters.
376         */
377         for (i = 0; i < sp->charcnt; ++i)
378                 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
379                         sp->chars[i] = TZ_ABBR_ERR_CHAR;
380         /*
381         ** Second, truncate long abbreviations.
382         */
383         for (i = 0; i < sp->typecnt; ++i) {
384                 register const struct ttinfo * const    ttisp = &sp->ttis[i];
385                 char *cp = &sp->chars[ttisp->tt_desigidx];
386
387                 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
388                         strcmp(cp, GRANDPARENTED) != 0)
389                                 *(cp + TZ_ABBR_MAX_LEN) = '\0';
390         }
391 }
392
393 #ifdef DETECT_TZ_CHANGES
394 /*
395  * Determine if there's a change in the timezone since the last time we checked.
396  * Returns: -1 on error
397  *           0 if the timezone has not changed
398  *           1 if the timezone has changed
399  */
400 static int
401 change_in_tz(const char *name)
402 {
403         static char old_name[PATH_MAX];
404         static struct stat old_sb;
405         struct stat sb;
406         int error;
407
408         error = stat(name, &sb);
409         if (error != 0)
410                 return -1;
411
412         if (strcmp(name, old_name) != 0) {
413                 strlcpy(old_name, name, sizeof(old_name));
414                 old_sb = sb;
415                 return 1;
416         }
417
418         if (sb.st_dev != old_sb.st_dev ||
419             sb.st_ino != old_sb.st_ino ||
420             sb.st_ctime != old_sb.st_ctime ||
421             sb.st_mtime != old_sb.st_mtime) {
422                 old_sb = sb;
423                 return 1;
424         }
425
426         return 0;
427 }
428 #else /* !DETECT_TZ_CHANGES */
429 #define change_in_tz(X) 1
430 #endif /* !DETECT_TZ_CHANGES */
431
432 /* Input buffer for data read from a compiled tz file.  */
433 union input_buffer {
434   /* The first part of the buffer, interpreted as a header.  */
435   struct tzhead tzhead;
436
437   /* The entire buffer.  */
438   char buf[2 * sizeof(struct tzhead) + 2 * sizeof(struct state)
439            + 4 * TZ_MAX_TIMES];
440 };
441
442 /* TZDIR with a trailing '/' rather than a trailing '\0'.  */
443 static char const tzdirslash[sizeof TZDIR] = TZDIR "/";
444
445 /* Local storage needed for 'tzloadbody'.  */
446 union local_storage {
447   /* The results of analyzing the file's contents after it is opened.  */
448   struct file_analysis {
449     /* The input buffer.  */
450     union input_buffer u;
451
452     /* A temporary state used for parsing a TZ string in the file.  */
453     struct state st;
454   } u;
455
456   /* The file name to be opened.  */
457   char fullname[max(sizeof(struct file_analysis), sizeof tzdirslash + 1024)];
458 };
459
460 /* Load tz data from the file named NAME into *SP.  Read extended
461    format if DOEXTEND.  Use *LSP for temporary storage.  Return 0 on
462    success, an errno value on failure.  */
463 static int
464 tzloadbody(char const *name, struct state *sp, bool doextend,
465            union local_storage *lsp)
466 {
467         register int                    i;
468         register int                    fid;
469         register int                    stored;
470         register ssize_t                nread;
471         register union input_buffer *up = &lsp->u.u;
472         register int tzheadsize = sizeof(struct tzhead);
473
474         sp->goback = sp->goahead = false;
475
476         if (! name) {
477                 name = TZDEFAULT;
478                 if (! name)
479                   return EINVAL;
480         }
481
482         if (name[0] == ':')
483                 ++name;
484         if (name[0] != '/') {
485                 if (sizeof lsp->fullname - sizeof tzdirslash <= strlen(name))
486                   return ENAMETOOLONG;
487
488                 /* Create a string "TZDIR/NAME".  Using sprintf here
489                    would pull in stdio (and would fail if the
490                    resulting string length exceeded INT_MAX!).  */
491                 memcpy(lsp->fullname, tzdirslash, sizeof tzdirslash);
492                 strcpy(lsp->fullname + sizeof tzdirslash, name);
493
494                 name = lsp->fullname;
495         }
496         if (doextend) {
497                 /*
498                  * Detect if the timezone file has changed.  Check
499                  * 'doextend' to ignore TZDEFRULES; the change_in_tz()
500                  * function can only keep state for a single file.
501                  */
502                 int ret = change_in_tz(name);
503                 if (ret <= 0) {
504                         /*
505                          * Returns an errno value if there was an error,
506                          * and 0 if the timezone had not changed.
507                          */
508                         return errno;
509                 }
510         }
511         fid = _open(name, O_RDONLY | O_BINARY);
512         if (fid < 0)
513           return errno;
514
515         nread = _read(fid, up->buf, sizeof up->buf);
516         if (nread < tzheadsize) {
517           int err = nread < 0 ? errno : EINVAL;
518           _close(fid);
519           return err;
520         }
521         if (_close(fid) < 0)
522           return errno;
523         for (stored = 4; stored <= 8; stored *= 2) {
524             char version = up->tzhead.tzh_version[0];
525             bool skip_datablock = stored == 4 && version;
526             int_fast32_t datablock_size;
527             int_fast32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
528             int_fast32_t ttisutcnt = detzcode(up->tzhead.tzh_ttisutcnt);
529             int_fast64_t prevtr = -1;
530             int_fast32_t prevcorr;
531             int_fast32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
532             int_fast32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
533             int_fast32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
534             int_fast32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
535             char const *p = up->buf + tzheadsize;
536             /* Although tzfile(5) currently requires typecnt to be nonzero,
537                support future formats that may allow zero typecnt
538                in files that have a TZ string and no transitions.  */
539             if (! (0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
540                    && 0 <= typecnt && typecnt < TZ_MAX_TYPES
541                    && 0 <= timecnt && timecnt < TZ_MAX_TIMES
542                    && 0 <= charcnt && charcnt < TZ_MAX_CHARS
543                    && 0 <= ttisstdcnt && ttisstdcnt < TZ_MAX_TYPES
544                    && 0 <= ttisutcnt && ttisutcnt < TZ_MAX_TYPES))
545               return EINVAL;
546             datablock_size
547                     = (timecnt * stored         /* ats */
548                        + timecnt                /* types */
549                        + typecnt * 6            /* ttinfos */
550                        + charcnt                /* chars */
551                        + leapcnt * (stored + 4) /* lsinfos */
552                        + ttisstdcnt             /* ttisstds */
553                        + ttisutcnt);            /* ttisuts */
554             if (nread < tzheadsize + datablock_size)
555               return EINVAL;
556             if (skip_datablock)
557                 p += datablock_size;
558             else {
559                 if (! ((ttisstdcnt == typecnt || ttisstdcnt == 0)
560                        && (ttisutcnt == typecnt || ttisutcnt == 0)))
561                   return EINVAL;
562
563                 sp->leapcnt = leapcnt;
564                 sp->timecnt = timecnt;
565                 sp->typecnt = typecnt;
566                 sp->charcnt = charcnt;
567
568                 /* Read transitions, discarding those out of time_t range.
569                    But pretend the last transition before TIME_T_MIN
570                    occurred at TIME_T_MIN.  */
571                 timecnt = 0;
572                 for (i = 0; i < sp->timecnt; ++i) {
573                         int_fast64_t at
574                           = stored == 4 ? detzcode(p) : detzcode64(p);
575                         sp->types[i] = at <= TIME_T_MAX;
576                         if (sp->types[i]) {
577                           time_t attime
578                             = ((TYPE_SIGNED(time_t) ? at < TIME_T_MIN : at < 0)
579                                ? TIME_T_MIN : at);
580                           if (timecnt && attime <= sp->ats[timecnt - 1]) {
581                             if (attime < sp->ats[timecnt - 1])
582                               return EINVAL;
583                             sp->types[i - 1] = 0;
584                             timecnt--;
585                           }
586                           sp->ats[timecnt++] = attime;
587                         }
588                         p += stored;
589                 }
590
591                 timecnt = 0;
592                 for (i = 0; i < sp->timecnt; ++i) {
593                         unsigned char typ = *p++;
594                         if (sp->typecnt <= typ)
595                           return EINVAL;
596                         if (sp->types[i])
597                                 sp->types[timecnt++] = typ;
598                 }
599                 sp->timecnt = timecnt;
600                 for (i = 0; i < sp->typecnt; ++i) {
601                         register struct ttinfo *        ttisp;
602                         unsigned char isdst, desigidx;
603
604                         ttisp = &sp->ttis[i];
605                         ttisp->tt_utoff = detzcode(p);
606                         p += 4;
607                         isdst = *p++;
608                         if (! (isdst < 2))
609                           return EINVAL;
610                         ttisp->tt_isdst = isdst;
611                         desigidx = *p++;
612                         if (! (desigidx < sp->charcnt))
613                           return EINVAL;
614                         ttisp->tt_desigidx = desigidx;
615                 }
616                 for (i = 0; i < sp->charcnt; ++i)
617                         sp->chars[i] = *p++;
618                 /* Ensure '\0'-terminated, and make it safe to call
619                    ttunspecified later.  */
620                 memset(&sp->chars[i], 0, CHARS_EXTRA);
621
622                 /* Read leap seconds, discarding those out of time_t range.  */
623                 leapcnt = 0;
624                 for (i = 0; i < sp->leapcnt; ++i) {
625                   int_fast64_t tr = stored == 4 ? detzcode(p) : detzcode64(p);
626                   int_fast32_t corr = detzcode(p + stored);
627                   p += stored + 4;
628
629                   /* Leap seconds cannot occur before the Epoch,
630                      or out of order.  */
631                   if (tr <= prevtr)
632                     return EINVAL;
633
634                   /* To avoid other botches in this code, each leap second's
635                      correction must differ from the previous one's by 1
636                      second or less, except that the first correction can be
637                      any value; these requirements are more generous than
638                      RFC 8536, to allow future RFC extensions.  */
639                   if (! (i == 0
640                          || (prevcorr < corr
641                              ? corr == prevcorr + 1
642                              : (corr == prevcorr
643                                 || corr == prevcorr - 1))))
644                     return EINVAL;
645                   prevtr = tr;
646                   prevcorr = corr;
647
648                   if (tr <= TIME_T_MAX) {
649                     sp->lsis[leapcnt].ls_trans = tr;
650                     sp->lsis[leapcnt].ls_corr = corr;
651                     leapcnt++;
652                   }
653                 }
654                 sp->leapcnt = leapcnt;
655
656                 for (i = 0; i < sp->typecnt; ++i) {
657                         register struct ttinfo *        ttisp;
658
659                         ttisp = &sp->ttis[i];
660                         if (ttisstdcnt == 0)
661                                 ttisp->tt_ttisstd = false;
662                         else {
663                                 if (*p != true && *p != false)
664                                   return EINVAL;
665                                 ttisp->tt_ttisstd = *p++;
666                         }
667                 }
668                 for (i = 0; i < sp->typecnt; ++i) {
669                         register struct ttinfo *        ttisp;
670
671                         ttisp = &sp->ttis[i];
672                         if (ttisutcnt == 0)
673                                 ttisp->tt_ttisut = false;
674                         else {
675                                 if (*p != true && *p != false)
676                                                 return EINVAL;
677                                 ttisp->tt_ttisut = *p++;
678                         }
679                 }
680             }
681
682             nread -= p - up->buf;
683             memmove(up->buf, p, nread);
684
685             /* If this is an old file, we're done.  */
686             if (!version)
687               break;
688         }
689         if (doextend && nread > 2 &&
690                 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
691                 sp->typecnt + 2 <= TZ_MAX_TYPES) {
692                         struct state    *ts = &lsp->u.st;
693
694                         up->buf[nread - 1] = '\0';
695                         if (tzparse(&up->buf[1], ts, sp)) {
696
697                           /* Attempt to reuse existing abbreviations.
698                              Without this, America/Anchorage would be right on
699                              the edge after 2037 when TZ_MAX_CHARS is 50, as
700                              sp->charcnt equals 40 (for LMT AST AWT APT AHST
701                              AHDT YST AKDT AKST) and ts->charcnt equals 10
702                              (for AKST AKDT).  Reusing means sp->charcnt can
703                              stay 40 in this example.  */
704                           int gotabbr = 0;
705                           int charcnt = sp->charcnt;
706                           for (i = 0; i < ts->typecnt; i++) {
707                             char *tsabbr = ts->chars + ts->ttis[i].tt_desigidx;
708                             int j;
709                             for (j = 0; j < charcnt; j++)
710                               if (strcmp(sp->chars + j, tsabbr) == 0) {
711                                 ts->ttis[i].tt_desigidx = j;
712                                 gotabbr++;
713                                 break;
714                               }
715                             if (! (j < charcnt)) {
716                               int tsabbrlen = strlen(tsabbr);
717                               if (j + tsabbrlen < TZ_MAX_CHARS) {
718                                 strcpy(sp->chars + j, tsabbr);
719                                 charcnt = j + tsabbrlen + 1;
720                                 ts->ttis[i].tt_desigidx = j;
721                                 gotabbr++;
722                               }
723                             }
724                           }
725                           if (gotabbr == ts->typecnt) {
726                             sp->charcnt = charcnt;
727
728                             /* Ignore any trailing, no-op transitions generated
729                                by zic as they don't help here and can run afoul
730                                of bugs in zic 2016j or earlier.  */
731                             while (1 < sp->timecnt
732                                    && (sp->types[sp->timecnt - 1]
733                                        == sp->types[sp->timecnt - 2]))
734                               sp->timecnt--;
735
736                             for (i = 0;
737                                  i < ts->timecnt && sp->timecnt < TZ_MAX_TIMES;
738                                  i++) {
739                               time_t t = ts->ats[i];
740                               if (increment_overflow_time(&t, leapcorr(sp, t))
741                                   || (0 < sp->timecnt
742                                       && t <= sp->ats[sp->timecnt - 1]))
743                                 continue;
744                               sp->ats[sp->timecnt] = t;
745                               sp->types[sp->timecnt] = (sp->typecnt
746                                                         + ts->types[i]);
747                               sp->timecnt++;
748                             }
749                             for (i = 0; i < ts->typecnt; i++)
750                               sp->ttis[sp->typecnt++] = ts->ttis[i];
751                           }
752                         }
753         }
754         if (sp->typecnt == 0)
755           return EINVAL;
756         if (sp->timecnt > 1) {
757             if (sp->ats[0] <= TIME_T_MAX - SECSPERREPEAT) {
758                 time_t repeatat = sp->ats[0] + SECSPERREPEAT;
759                 int repeattype = sp->types[0];
760                 for (i = 1; i < sp->timecnt; ++i)
761                   if (sp->ats[i] == repeatat
762                       && typesequiv(sp, sp->types[i], repeattype)) {
763                                         sp->goback = true;
764                                         break;
765                   }
766             }
767             if (TIME_T_MIN + SECSPERREPEAT <= sp->ats[sp->timecnt - 1]) {
768                 time_t repeatat = sp->ats[sp->timecnt - 1] - SECSPERREPEAT;
769                 int repeattype = sp->types[sp->timecnt - 1];
770                 for (i = sp->timecnt - 2; i >= 0; --i)
771                   if (sp->ats[i] == repeatat
772                       && typesequiv(sp, sp->types[i], repeattype)) {
773                                         sp->goahead = true;
774                                         break;
775                   }
776             }
777         }
778
779         /* Infer sp->defaulttype from the data.  Although this default
780            type is always zero for data from recent tzdb releases,
781            things are trickier for data from tzdb 2018e or earlier.
782
783            The first set of heuristics work around bugs in 32-bit data
784            generated by tzdb 2013c or earlier.  The workaround is for
785            zones like Australia/Macquarie where timestamps before the
786            first transition have a time type that is not the earliest
787            standard-time type.  See:
788            https://mm.icann.org/pipermail/tz/2013-May/019368.html */
789         /*
790         ** If type 0 does not specify local time, or is unused in transitions,
791         ** it's the type to use for early times.
792         */
793         for (i = 0; i < sp->timecnt; ++i)
794                 if (sp->types[i] == 0)
795                         break;
796         i = i < sp->timecnt && ! ttunspecified(sp, 0) ? -1 : 0;
797         /*
798         ** Absent the above,
799         ** if there are transition times
800         ** and the first transition is to a daylight time
801         ** find the standard type less than and closest to
802         ** the type of the first transition.
803         */
804         if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
805                 i = sp->types[0];
806                 while (--i >= 0)
807                         if (!sp->ttis[i].tt_isdst)
808                                 break;
809         }
810         /* The next heuristics are for data generated by tzdb 2018e or
811            earlier, for zones like EST5EDT where the first transition
812            is to DST.  */
813         /*
814         ** If no result yet, find the first standard type.
815         ** If there is none, punt to type zero.
816         */
817         if (i < 0) {
818                 i = 0;
819                 while (sp->ttis[i].tt_isdst)
820                         if (++i >= sp->typecnt) {
821                                 i = 0;
822                                 break;
823                         }
824         }
825         /* A simple 'sp->defaulttype = 0;' would suffice here if we
826            didn't have to worry about 2018e-or-earlier data.  Even
827            simpler would be to remove the defaulttype member and just
828            use 0 in its place.  */
829         sp->defaulttype = i;
830
831         return 0;
832 }
833
834 /* Load tz data from the file named NAME into *SP.  Read extended
835    format if DOEXTEND.  Return 0 on success, an errno value on failure.  */
836 static int
837 tzload(char const *name, struct state *sp, bool doextend)
838 {
839 #ifdef ALL_STATE
840   union local_storage *lsp = malloc(sizeof *lsp);
841   if (!lsp) {
842     return HAVE_MALLOC_ERRNO ? errno : ENOMEM;
843   } else {
844     int err = tzloadbody(name, sp, doextend, lsp);
845     free(lsp);
846     return err;
847   }
848 #else
849   union local_storage ls;
850   return tzloadbody(name, sp, doextend, &ls);
851 #endif
852 }
853
854 static bool
855 typesequiv(const struct state *sp, int a, int b)
856 {
857         register bool result;
858
859         if (sp == NULL ||
860                 a < 0 || a >= sp->typecnt ||
861                 b < 0 || b >= sp->typecnt)
862                         result = false;
863         else {
864                 /* Compare the relevant members of *AP and *BP.
865                    Ignore tt_ttisstd and tt_ttisut, as they are
866                    irrelevant now and counting them could cause
867                    sp->goahead to mistakenly remain false.  */
868                 register const struct ttinfo *  ap = &sp->ttis[a];
869                 register const struct ttinfo *  bp = &sp->ttis[b];
870                 result = (ap->tt_utoff == bp->tt_utoff
871                           && ap->tt_isdst == bp->tt_isdst
872                           && (strcmp(&sp->chars[ap->tt_desigidx],
873                                      &sp->chars[bp->tt_desigidx])
874                               == 0));
875         }
876         return result;
877 }
878
879 static const int        mon_lengths[2][MONSPERYEAR] = {
880         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
881         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
882 };
883
884 static const int        year_lengths[2] = {
885         DAYSPERNYEAR, DAYSPERLYEAR
886 };
887
888 /* Is C an ASCII digit?  */
889 static bool
890 is_digit(char c)
891 {
892   return '0' <= c && c <= '9';
893 }
894
895 /*
896 ** Given a pointer into a timezone string, scan until a character that is not
897 ** a valid character in a time zone abbreviation is found.
898 ** Return a pointer to that character.
899 */
900
901 static ATTRIBUTE_REPRODUCIBLE const char *
902 getzname(register const char *strp)
903 {
904         register char   c;
905
906         while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
907                 c != '+')
908                         ++strp;
909         return strp;
910 }
911
912 /*
913 ** Given a pointer into an extended timezone string, scan until the ending
914 ** delimiter of the time zone abbreviation is located.
915 ** Return a pointer to the delimiter.
916 **
917 ** As with getzname above, the legal character set is actually quite
918 ** restricted, with other characters producing undefined results.
919 ** We don't do any checking here; checking is done later in common-case code.
920 */
921
922 static ATTRIBUTE_REPRODUCIBLE const char *
923 getqzname(register const char *strp, const int delim)
924 {
925         register int    c;
926
927         while ((c = *strp) != '\0' && c != delim)
928                 ++strp;
929         return strp;
930 }
931
932 /*
933 ** Given a pointer into a timezone string, extract a number from that string.
934 ** Check that the number is within a specified range; if it is not, return
935 ** NULL.
936 ** Otherwise, return a pointer to the first character not part of the number.
937 */
938
939 static const char *
940 getnum(register const char *strp, int *const nump, const int min, const int max)
941 {
942         register char   c;
943         register int    num;
944
945         if (strp == NULL || !is_digit(c = *strp))
946                 return NULL;
947         num = 0;
948         do {
949                 num = num * 10 + (c - '0');
950                 if (num > max)
951                         return NULL;    /* illegal value */
952                 c = *++strp;
953         } while (is_digit(c));
954         if (num < min)
955                 return NULL;            /* illegal value */
956         *nump = num;
957         return strp;
958 }
959
960 /*
961 ** Given a pointer into a timezone string, extract a number of seconds,
962 ** in hh[:mm[:ss]] form, from the string.
963 ** If any error occurs, return NULL.
964 ** Otherwise, return a pointer to the first character not part of the number
965 ** of seconds.
966 */
967
968 static const char *
969 getsecs(register const char *strp, int_fast32_t *const secsp)
970 {
971         int     num;
972         int_fast32_t secsperhour = SECSPERHOUR;
973
974         /*
975         ** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
976         ** "M10.4.6/26", which does not conform to Posix,
977         ** but which specifies the equivalent of
978         ** "02:00 on the first Sunday on or after 23 Oct".
979         */
980         strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
981         if (strp == NULL)
982                 return NULL;
983         *secsp = num * secsperhour;
984         if (*strp == ':') {
985                 ++strp;
986                 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
987                 if (strp == NULL)
988                         return NULL;
989                 *secsp += num * SECSPERMIN;
990                 if (*strp == ':') {
991                         ++strp;
992                         /* 'SECSPERMIN' allows for leap seconds.  */
993                         strp = getnum(strp, &num, 0, SECSPERMIN);
994                         if (strp == NULL)
995                                 return NULL;
996                         *secsp += num;
997                 }
998         }
999         return strp;
1000 }
1001
1002 /*
1003 ** Given a pointer into a timezone string, extract an offset, in
1004 ** [+-]hh[:mm[:ss]] form, from the string.
1005 ** If any error occurs, return NULL.
1006 ** Otherwise, return a pointer to the first character not part of the time.
1007 */
1008
1009 static const char *
1010 getoffset(register const char *strp, int_fast32_t *const offsetp)
1011 {
1012         register bool neg = false;
1013
1014         if (*strp == '-') {
1015                 neg = true;
1016                 ++strp;
1017         } else if (*strp == '+')
1018                 ++strp;
1019         strp = getsecs(strp, offsetp);
1020         if (strp == NULL)
1021                 return NULL;            /* illegal time */
1022         if (neg)
1023                 *offsetp = -*offsetp;
1024         return strp;
1025 }
1026
1027 /*
1028 ** Given a pointer into a timezone string, extract a rule in the form
1029 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
1030 ** If a valid rule is not found, return NULL.
1031 ** Otherwise, return a pointer to the first character not part of the rule.
1032 */
1033
1034 static const char *
1035 getrule(const char *strp, register struct rule *const rulep)
1036 {
1037         if (*strp == 'J') {
1038                 /*
1039                 ** Julian day.
1040                 */
1041                 rulep->r_type = JULIAN_DAY;
1042                 ++strp;
1043                 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
1044         } else if (*strp == 'M') {
1045                 /*
1046                 ** Month, week, day.
1047                 */
1048                 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
1049                 ++strp;
1050                 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
1051                 if (strp == NULL)
1052                         return NULL;
1053                 if (*strp++ != '.')
1054                         return NULL;
1055                 strp = getnum(strp, &rulep->r_week, 1, 5);
1056                 if (strp == NULL)
1057                         return NULL;
1058                 if (*strp++ != '.')
1059                         return NULL;
1060                 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
1061         } else if (is_digit(*strp)) {
1062                 /*
1063                 ** Day of year.
1064                 */
1065                 rulep->r_type = DAY_OF_YEAR;
1066                 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
1067         } else  return NULL;            /* invalid format */
1068         if (strp == NULL)
1069                 return NULL;
1070         if (*strp == '/') {
1071                 /*
1072                 ** Time specified.
1073                 */
1074                 ++strp;
1075                 strp = getoffset(strp, &rulep->r_time);
1076         } else  rulep->r_time = 2 * SECSPERHOUR;        /* default = 2:00:00 */
1077         return strp;
1078 }
1079
1080 /*
1081 ** Given a year, a rule, and the offset from UT at the time that rule takes
1082 ** effect, calculate the year-relative time that rule takes effect.
1083 */
1084
1085 static int_fast32_t
1086 transtime(const int year, register const struct rule *const rulep,
1087           const int_fast32_t offset)
1088 {
1089         register bool   leapyear;
1090         register int_fast32_t value;
1091         register int    i;
1092         int             d, m1, yy0, yy1, yy2, dow;
1093
1094         leapyear = isleap(year);
1095         switch (rulep->r_type) {
1096
1097         case JULIAN_DAY:
1098                 /*
1099                 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
1100                 ** years.
1101                 ** In non-leap years, or if the day number is 59 or less, just
1102                 ** add SECSPERDAY times the day number-1 to the time of
1103                 ** January 1, midnight, to get the day.
1104                 */
1105                 value = (rulep->r_day - 1) * SECSPERDAY;
1106                 if (leapyear && rulep->r_day >= 60)
1107                         value += SECSPERDAY;
1108                 break;
1109
1110         case DAY_OF_YEAR:
1111                 /*
1112                 ** n - day of year.
1113                 ** Just add SECSPERDAY times the day number to the time of
1114                 ** January 1, midnight, to get the day.
1115                 */
1116                 value = rulep->r_day * SECSPERDAY;
1117                 break;
1118
1119         case MONTH_NTH_DAY_OF_WEEK:
1120                 /*
1121                 ** Mm.n.d - nth "dth day" of month m.
1122                 */
1123
1124                 /*
1125                 ** Use Zeller's Congruence to get day-of-week of first day of
1126                 ** month.
1127                 */
1128                 m1 = (rulep->r_mon + 9) % 12 + 1;
1129                 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
1130                 yy1 = yy0 / 100;
1131                 yy2 = yy0 % 100;
1132                 dow = ((26 * m1 - 2) / 10 +
1133                         1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
1134                 if (dow < 0)
1135                         dow += DAYSPERWEEK;
1136
1137                 /*
1138                 ** "dow" is the day-of-week of the first day of the month. Get
1139                 ** the day-of-month (zero-origin) of the first "dow" day of the
1140                 ** month.
1141                 */
1142                 d = rulep->r_day - dow;
1143                 if (d < 0)
1144                         d += DAYSPERWEEK;
1145                 for (i = 1; i < rulep->r_week; ++i) {
1146                         if (d + DAYSPERWEEK >=
1147                                 mon_lengths[leapyear][rulep->r_mon - 1])
1148                                         break;
1149                         d += DAYSPERWEEK;
1150                 }
1151
1152                 /*
1153                 ** "d" is the day-of-month (zero-origin) of the day we want.
1154                 */
1155                 value = d * SECSPERDAY;
1156                 for (i = 0; i < rulep->r_mon - 1; ++i)
1157                         value += mon_lengths[leapyear][i] * SECSPERDAY;
1158                 break;
1159
1160         default: unreachable();
1161         }
1162
1163         /*
1164         ** "value" is the year-relative time of 00:00:00 UT on the day in
1165         ** question. To get the year-relative time of the specified local
1166         ** time on that day, add the transition time and the current offset
1167         ** from UT.
1168         */
1169         return value + rulep->r_time + offset;
1170 }
1171
1172 /*
1173 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
1174 ** appropriate.
1175 */
1176
1177 static bool
1178 tzparse(const char *name, struct state *sp, struct state *basep)
1179 {
1180         const char *                    stdname;
1181         const char *                    dstname;
1182         int_fast32_t                    stdoffset;
1183         int_fast32_t                    dstoffset;
1184         register char *                 cp;
1185         register bool                   load_ok;
1186         ptrdiff_t stdlen, dstlen, charcnt;
1187         time_t atlo = TIME_T_MIN, leaplo = TIME_T_MIN;
1188
1189         stdname = name;
1190         if (*name == '<') {
1191           name++;
1192           stdname = name;
1193           name = getqzname(name, '>');
1194           if (*name != '>')
1195             return false;
1196           stdlen = name - stdname;
1197           name++;
1198         } else {
1199           name = getzname(name);
1200           stdlen = name - stdname;
1201         }
1202         if (!stdlen)
1203           return false;
1204         name = getoffset(name, &stdoffset);
1205         if (name == NULL)
1206           return false;
1207         charcnt = stdlen + 1;
1208         if (sizeof sp->chars < charcnt)
1209           return false;
1210         if (basep) {
1211           if (0 < basep->timecnt)
1212             atlo = basep->ats[basep->timecnt - 1];
1213           load_ok = false;
1214           sp->leapcnt = basep->leapcnt;
1215           memcpy(sp->lsis, basep->lsis, sp->leapcnt * sizeof *sp->lsis);
1216         } else {
1217           load_ok = tzload(TZDEFRULES, sp, false) == 0;
1218           if (!load_ok)
1219             sp->leapcnt = 0;    /* So, we're off a little.  */
1220         }
1221         if (0 < sp->leapcnt)
1222           leaplo = sp->lsis[sp->leapcnt - 1].ls_trans;
1223         if (*name != '\0') {
1224                 if (*name == '<') {
1225                         dstname = ++name;
1226                         name = getqzname(name, '>');
1227                         if (*name != '>')
1228                           return false;
1229                         dstlen = name - dstname;
1230                         name++;
1231                 } else {
1232                         dstname = name;
1233                         name = getzname(name);
1234                         dstlen = name - dstname; /* length of DST abbr. */
1235                 }
1236                 if (!dstlen)
1237                   return false;
1238                 charcnt += dstlen + 1;
1239                 if (sizeof sp->chars < charcnt)
1240                   return false;
1241                 if (*name != '\0' && *name != ',' && *name != ';') {
1242                         name = getoffset(name, &dstoffset);
1243                         if (name == NULL)
1244                           return false;
1245                 } else  dstoffset = stdoffset - SECSPERHOUR;
1246                 if (*name == '\0' && !load_ok)
1247                         name = TZDEFRULESTRING;
1248                 if (*name == ',' || *name == ';') {
1249                         struct rule     start;
1250                         struct rule     end;
1251                         register int    year;
1252                         register int    timecnt;
1253                         time_t          janfirst;
1254                         int_fast32_t janoffset = 0;
1255                         int yearbeg, yearlim;
1256
1257                         ++name;
1258                         if ((name = getrule(name, &start)) == NULL)
1259                           return false;
1260                         if (*name++ != ',')
1261                           return false;
1262                         if ((name = getrule(name, &end)) == NULL)
1263                           return false;
1264                         if (*name != '\0')
1265                           return false;
1266                         sp->typecnt = 2;        /* standard time and DST */
1267                         /*
1268                         ** Two transitions per year, from EPOCH_YEAR forward.
1269                         */
1270                         init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1271                         init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1272                         sp->defaulttype = 0;
1273                         timecnt = 0;
1274                         janfirst = 0;
1275                         yearbeg = EPOCH_YEAR;
1276
1277                         do {
1278                           int_fast32_t yearsecs
1279                             = year_lengths[isleap(yearbeg - 1)] * SECSPERDAY;
1280                           yearbeg--;
1281                           if (increment_overflow_time(&janfirst, -yearsecs)) {
1282                             janoffset = -yearsecs;
1283                             break;
1284                           }
1285                         } while (atlo < janfirst
1286                                  && EPOCH_YEAR - YEARSPERREPEAT / 2 < yearbeg);
1287
1288                         while (true) {
1289                           int_fast32_t yearsecs
1290                             = year_lengths[isleap(yearbeg)] * SECSPERDAY;
1291                           int yearbeg1 = yearbeg;
1292                           time_t janfirst1 = janfirst;
1293                           if (increment_overflow_time(&janfirst1, yearsecs)
1294                               || increment_overflow(&yearbeg1, 1)
1295                               || atlo <= janfirst1)
1296                             break;
1297                           yearbeg = yearbeg1;
1298                           janfirst = janfirst1;
1299                         }
1300
1301                         yearlim = yearbeg;
1302                         if (increment_overflow(&yearlim, YEARSPERREPEAT + 1))
1303                           yearlim = INT_MAX;
1304                         for (year = yearbeg; year < yearlim; year++) {
1305                                 int_fast32_t
1306                                   starttime = transtime(year, &start, stdoffset),
1307                                   endtime = transtime(year, &end, dstoffset);
1308                                 int_fast32_t
1309                                   yearsecs = (year_lengths[isleap(year)]
1310                                               * SECSPERDAY);
1311                                 bool reversed = endtime < starttime;
1312                                 if (reversed) {
1313                                         int_fast32_t swap = starttime;
1314                                         starttime = endtime;
1315                                         endtime = swap;
1316                                 }
1317                                 if (reversed
1318                                     || (starttime < endtime
1319                                         && endtime - starttime < yearsecs)) {
1320                                         if (TZ_MAX_TIMES - 2 < timecnt)
1321                                                 break;
1322                                         sp->ats[timecnt] = janfirst;
1323                                         if (! increment_overflow_time
1324                                             (&sp->ats[timecnt],
1325                                              janoffset + starttime)
1326                                             && atlo <= sp->ats[timecnt])
1327                                           sp->types[timecnt++] = !reversed;
1328                                         sp->ats[timecnt] = janfirst;
1329                                         if (! increment_overflow_time
1330                                             (&sp->ats[timecnt],
1331                                              janoffset + endtime)
1332                                             && atlo <= sp->ats[timecnt]) {
1333                                           sp->types[timecnt++] = reversed;
1334                                         }
1335                                 }
1336                                 if (endtime < leaplo) {
1337                                   yearlim = year;
1338                                   if (increment_overflow(&yearlim,
1339                                                          YEARSPERREPEAT + 1))
1340                                     yearlim = INT_MAX;
1341                                 }
1342                                 if (increment_overflow_time
1343                                     (&janfirst, janoffset + yearsecs))
1344                                         break;
1345                                 janoffset = 0;
1346                         }
1347                         sp->timecnt = timecnt;
1348                         if (! timecnt) {
1349                                 sp->ttis[0] = sp->ttis[1];
1350                                 sp->typecnt = 1;        /* Perpetual DST.  */
1351                         } else if (YEARSPERREPEAT < year - yearbeg)
1352                                 sp->goback = sp->goahead = true;
1353                 } else {
1354                         register int_fast32_t   theirstdoffset;
1355                         register int_fast32_t   theirdstoffset;
1356                         register int_fast32_t   theiroffset;
1357                         register bool           isdst;
1358                         register int            i;
1359                         register int            j;
1360
1361                         if (*name != '\0')
1362                           return false;
1363                         /*
1364                         ** Initial values of theirstdoffset and theirdstoffset.
1365                         */
1366                         theirstdoffset = 0;
1367                         for (i = 0; i < sp->timecnt; ++i) {
1368                                 j = sp->types[i];
1369                                 if (!sp->ttis[j].tt_isdst) {
1370                                         theirstdoffset =
1371                                                 - sp->ttis[j].tt_utoff;
1372                                         break;
1373                                 }
1374                         }
1375                         theirdstoffset = 0;
1376                         for (i = 0; i < sp->timecnt; ++i) {
1377                                 j = sp->types[i];
1378                                 if (sp->ttis[j].tt_isdst) {
1379                                         theirdstoffset =
1380                                                 - sp->ttis[j].tt_utoff;
1381                                         break;
1382                                 }
1383                         }
1384                         /*
1385                         ** Initially we're assumed to be in standard time.
1386                         */
1387                         isdst = false;
1388                         /*
1389                         ** Now juggle transition times and types
1390                         ** tracking offsets as you do.
1391                         */
1392                         for (i = 0; i < sp->timecnt; ++i) {
1393                                 j = sp->types[i];
1394                                 sp->types[i] = sp->ttis[j].tt_isdst;
1395                                 if (sp->ttis[j].tt_ttisut) {
1396                                         /* No adjustment to transition time */
1397                                 } else {
1398                                         /*
1399                                         ** If daylight saving time is in
1400                                         ** effect, and the transition time was
1401                                         ** not specified as standard time, add
1402                                         ** the daylight saving time offset to
1403                                         ** the transition time; otherwise, add
1404                                         ** the standard time offset to the
1405                                         ** transition time.
1406                                         */
1407                                         /*
1408                                         ** Transitions from DST to DDST
1409                                         ** will effectively disappear since
1410                                         ** POSIX provides for only one DST
1411                                         ** offset.
1412                                         */
1413                                         if (isdst && !sp->ttis[j].tt_ttisstd) {
1414                                                 sp->ats[i] += dstoffset -
1415                                                         theirdstoffset;
1416                                         } else {
1417                                                 sp->ats[i] += stdoffset -
1418                                                         theirstdoffset;
1419                                         }
1420                                 }
1421                                 theiroffset = -sp->ttis[j].tt_utoff;
1422                                 if (sp->ttis[j].tt_isdst)
1423                                         theirdstoffset = theiroffset;
1424                                 else    theirstdoffset = theiroffset;
1425                         }
1426                         /*
1427                         ** Finally, fill in ttis.
1428                         */
1429                         init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1430                         init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1431                         sp->typecnt = 2;
1432                         sp->defaulttype = 0;
1433                 }
1434         } else {
1435                 dstlen = 0;
1436                 sp->typecnt = 1;                /* only standard time */
1437                 sp->timecnt = 0;
1438                 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1439                 sp->defaulttype = 0;
1440         }
1441         sp->charcnt = charcnt;
1442         cp = sp->chars;
1443         memcpy(cp, stdname, stdlen);
1444         cp += stdlen;
1445         *cp++ = '\0';
1446         if (dstlen != 0) {
1447                 memcpy(cp, dstname, dstlen);
1448                 *(cp + dstlen) = '\0';
1449         }
1450         return true;
1451 }
1452
1453 static void
1454 gmtload(struct state *const sp)
1455 {
1456         if (tzload(etc_utc, sp, true) != 0)
1457           tzparse("UTC0", sp, NULL);
1458 }
1459
1460 #ifdef DETECT_TZ_CHANGES
1461 static int
1462 recheck_tzdata()
1463 {
1464         static time_t last_checked;
1465         struct timespec now;
1466         time_t current_time;
1467         int error;
1468
1469         /*
1470          * We want to recheck the timezone file every 61 sec.
1471          */
1472         error = clock_gettime(CLOCK_MONOTONIC, &now);
1473         if (error < 0) {
1474                 /* XXX: Can we somehow report this? */
1475                 return 0;
1476         }
1477
1478         current_time = now.tv_sec;
1479         if ((current_time - last_checked > DETECT_TZ_CHANGES_INTERVAL) ||
1480             (last_checked > current_time)) {
1481                 last_checked = current_time;
1482                 return 1;
1483         }
1484
1485         return 0;
1486 }
1487 #else /* !DETECT_TZ_CHANGES */
1488 #define recheck_tzdata()        0
1489 #endif /* !DETECT_TZ_CHANGES */
1490
1491 /* Initialize *SP to a value appropriate for the TZ setting NAME.
1492    Return 0 on success, an errno value on failure.  */
1493 static int
1494 zoneinit(struct state *sp, char const *name)
1495 {
1496   if (name && ! name[0]) {
1497     /*
1498     ** User wants it fast rather than right.
1499     */
1500     sp->leapcnt = 0;            /* so, we're off a little */
1501     sp->timecnt = 0;
1502     sp->typecnt = 0;
1503     sp->charcnt = 0;
1504     sp->goback = sp->goahead = false;
1505     init_ttinfo(&sp->ttis[0], 0, false, 0);
1506     strcpy(sp->chars, utc);
1507     sp->defaulttype = 0;
1508     return 0;
1509   } else {
1510     int err = tzload(name, sp, true);
1511     if (err != 0 && name && name[0] != ':' && tzparse(name, sp, NULL))
1512       err = 0;
1513     if (err == 0)
1514       scrub_abbrs(sp);
1515     return err;
1516   }
1517 }
1518
1519 static void
1520 tzset_unlocked(void)
1521 {
1522   char const *name = getenv("TZ");
1523   struct state *sp = lclptr;
1524   int lcl = name ? strlen(name) < sizeof lcl_TZname : -1;
1525   if (lcl < 0
1526       ? lcl_is_set < 0
1527       : 0 < lcl_is_set && strcmp(lcl_TZname, name) == 0)
1528     if (recheck_tzdata() == 0)
1529       return;
1530 #ifdef ALL_STATE
1531   if (! sp)
1532     lclptr = sp = malloc(sizeof *lclptr);
1533 #endif /* defined ALL_STATE */
1534   if (sp) {
1535     if (zoneinit(sp, name) != 0)
1536       zoneinit(sp, "");
1537     if (0 < lcl)
1538       strcpy(lcl_TZname, name);
1539   }
1540   settzname();
1541   lcl_is_set = lcl;
1542 }
1543
1544 void
1545 tzset(void)
1546 {
1547   if (lock() != 0)
1548     return;
1549   tzset_unlocked();
1550   unlock();
1551 }
1552
1553 static void
1554 gmtcheck(void)
1555 {
1556   static bool gmt_is_set;
1557   if (lock() != 0)
1558     return;
1559   if (! gmt_is_set) {
1560 #ifdef ALL_STATE
1561     gmtptr = malloc(sizeof *gmtptr);
1562 #endif
1563     if (gmtptr)
1564       gmtload(gmtptr);
1565     gmt_is_set = true;
1566   }
1567   unlock();
1568 }
1569
1570 #if NETBSD_INSPIRED
1571
1572 timezone_t
1573 tzalloc(char const *name)
1574 {
1575   timezone_t sp = malloc(sizeof *sp);
1576   if (sp) {
1577     int err = zoneinit(sp, name);
1578     if (err != 0) {
1579       free(sp);
1580       errno = err;
1581       return NULL;
1582     }
1583   } else if (!HAVE_MALLOC_ERRNO)
1584     errno = ENOMEM;
1585   return sp;
1586 }
1587
1588 void
1589 tzfree(timezone_t sp)
1590 {
1591   free(sp);
1592 }
1593
1594 /*
1595 ** NetBSD 6.1.4 has ctime_rz, but omit it because POSIX says ctime and
1596 ** ctime_r are obsolescent and have potential security problems that
1597 ** ctime_rz would share.  Callers can instead use localtime_rz + strftime.
1598 **
1599 ** NetBSD 6.1.4 has tzgetname, but omit it because it doesn't work
1600 ** in zones with three or more time zone abbreviations.
1601 ** Callers can instead use localtime_rz + strftime.
1602 */
1603
1604 #endif
1605
1606 /*
1607 ** The easy way to behave "as if no library function calls" localtime
1608 ** is to not call it, so we drop its guts into "localsub", which can be
1609 ** freely called. (And no, the PANS doesn't require the above behavior,
1610 ** but it *is* desirable.)
1611 **
1612 ** If successful and SETNAME is nonzero,
1613 ** set the applicable parts of tzname, timezone and altzone;
1614 ** however, it's OK to omit this step if the timezone is POSIX-compatible,
1615 ** since in that case tzset should have already done this step correctly.
1616 ** SETNAME's type is int_fast32_t for compatibility with gmtsub,
1617 ** but it is actually a boolean and its value should be 0 or 1.
1618 */
1619
1620 /*ARGSUSED*/
1621 static struct tm *
1622 localsub(struct state const *sp, time_t const *timep, int_fast32_t setname,
1623          struct tm *const tmp)
1624 {
1625         register const struct ttinfo *  ttisp;
1626         register int                    i;
1627         register struct tm *            result;
1628         const time_t                    t = *timep;
1629
1630         if (sp == NULL) {
1631           /* Don't bother to set tzname etc.; tzset has already done it.  */
1632           return gmtsub(gmtptr, timep, 0, tmp);
1633         }
1634         if ((sp->goback && t < sp->ats[0]) ||
1635                 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1636                         time_t newt;
1637                         register time_t         seconds;
1638                         register time_t         years;
1639
1640                         if (t < sp->ats[0])
1641                                 seconds = sp->ats[0] - t;
1642                         else    seconds = t - sp->ats[sp->timecnt - 1];
1643                         --seconds;
1644
1645                         /* Beware integer overflow, as SECONDS might
1646                            be close to the maximum time_t.  */
1647                         years = seconds / SECSPERREPEAT * YEARSPERREPEAT;
1648                         seconds = years * AVGSECSPERYEAR;
1649                         years += YEARSPERREPEAT;
1650                         if (t < sp->ats[0])
1651                           newt = t + seconds + SECSPERREPEAT;
1652                         else
1653                           newt = t - seconds - SECSPERREPEAT;
1654
1655                         if (newt < sp->ats[0] ||
1656                                 newt > sp->ats[sp->timecnt - 1])
1657                                         return NULL;    /* "cannot happen" */
1658                         result = localsub(sp, &newt, setname, tmp);
1659                         if (result) {
1660 #if defined ckd_add && defined ckd_sub
1661                                 if (t < sp->ats[0]
1662                                     ? ckd_sub(&result->tm_year,
1663                                               result->tm_year, years)
1664                                     : ckd_add(&result->tm_year,
1665                                               result->tm_year, years))
1666                                   return NULL;
1667 #else
1668                                 register int_fast64_t newy;
1669
1670                                 newy = result->tm_year;
1671                                 if (t < sp->ats[0])
1672                                         newy -= years;
1673                                 else    newy += years;
1674                                 if (! (INT_MIN <= newy && newy <= INT_MAX))
1675                                         return NULL;
1676                                 result->tm_year = newy;
1677 #endif
1678                         }
1679                         return result;
1680         }
1681         if (sp->timecnt == 0 || t < sp->ats[0]) {
1682                 i = sp->defaulttype;
1683         } else {
1684                 register int    lo = 1;
1685                 register int    hi = sp->timecnt;
1686
1687                 while (lo < hi) {
1688                         register int    mid = (lo + hi) >> 1;
1689
1690                         if (t < sp->ats[mid])
1691                                 hi = mid;
1692                         else    lo = mid + 1;
1693                 }
1694                 i = sp->types[lo - 1];
1695         }
1696         ttisp = &sp->ttis[i];
1697         /*
1698         ** To get (wrong) behavior that's compatible with System V Release 2.0
1699         ** you'd replace the statement below with
1700         **      t += ttisp->tt_utoff;
1701         **      timesub(&t, 0L, sp, tmp);
1702         */
1703         result = timesub(&t, ttisp->tt_utoff, sp, tmp);
1704         if (result) {
1705           result->tm_isdst = ttisp->tt_isdst;
1706 #ifdef TM_ZONE
1707           result->TM_ZONE = (char *) &sp->chars[ttisp->tt_desigidx];
1708 #endif /* defined TM_ZONE */
1709           if (setname)
1710             update_tzname_etc(sp, ttisp);
1711         }
1712         return result;
1713 }
1714
1715 #if NETBSD_INSPIRED
1716
1717 struct tm *
1718 localtime_rz(struct state *sp, time_t const *timep, struct tm *tmp)
1719 {
1720   return localsub(sp, timep, 0, tmp);
1721 }
1722
1723 #endif
1724
1725 static struct tm *
1726 localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
1727 {
1728   int err = lock();
1729   if (err) {
1730     errno = err;
1731     return NULL;
1732   }
1733 #ifndef DETECT_TZ_CHANGES
1734   if (setname || !lcl_is_set)
1735 #endif
1736     tzset_unlocked();
1737   tmp = localsub(lclptr, timep, setname, tmp);
1738   unlock();
1739   return tmp;
1740 }
1741
1742 static void
1743 localtime_key_init(void)
1744 {
1745
1746         localtime_key_error = _pthread_key_create(&localtime_key, free);
1747 }
1748
1749 struct tm *
1750 localtime(const time_t *timep)
1751 {
1752         struct tm *p_tm = &tm;
1753
1754         if (__isthreaded != 0) {
1755                 _pthread_once(&localtime_once, localtime_key_init);
1756                 if (localtime_key_error != 0) {
1757                         errno = localtime_key_error;
1758                         return (NULL);
1759                 }
1760                 if ((p_tm = _pthread_getspecific(localtime_key)) == NULL) {
1761                         if ((p_tm = malloc(sizeof(*p_tm))) == NULL) {
1762                                 return (NULL);
1763                         }
1764                         if (_pthread_setspecific(localtime_key, p_tm) != 0) {
1765                                 free(p_tm);
1766                                 return (NULL);
1767                         }
1768                 }
1769         }
1770         return localtime_tzset(timep, p_tm, true);
1771 }
1772
1773 struct tm *
1774 localtime_r(const time_t *timep, struct tm *tmp)
1775 {
1776   return localtime_tzset(timep, tmp, false);
1777 }
1778
1779 /*
1780 ** gmtsub is to gmtime as localsub is to localtime.
1781 */
1782
1783 static struct tm *
1784 gmtsub(ATTRIBUTE_MAYBE_UNUSED struct state const *sp, time_t const *timep,
1785        int_fast32_t offset, struct tm *tmp)
1786 {
1787         register struct tm *    result;
1788
1789         result = timesub(timep, offset, gmtptr, tmp);
1790 #ifdef TM_ZONE
1791         /*
1792         ** Could get fancy here and deliver something such as
1793         ** "+xx" or "-xx" if offset is non-zero,
1794         ** but this is no time for a treasure hunt.
1795         */
1796         tmp->TM_ZONE = ((char *)
1797                         (offset ? wildabbr : gmtptr ? gmtptr->chars : utc));
1798 #endif /* defined TM_ZONE */
1799         return result;
1800 }
1801
1802 /*
1803 * Re-entrant version of gmtime.
1804 */
1805
1806 struct tm *
1807 gmtime_r(const time_t *timep, struct tm *tmp)
1808 {
1809         _once(&gmt_once, gmtcheck);
1810         return gmtsub(gmtptr, timep, 0, tmp);
1811 }
1812
1813 static void
1814 gmtime_key_init(void)
1815 {
1816
1817         gmtime_key_error = _pthread_key_create(&gmtime_key, free);
1818 }
1819
1820 struct tm *
1821 gmtime(const time_t *timep)
1822 {
1823         struct tm *p_tm = &tm;
1824
1825         if (__isthreaded != 0) {
1826                 _pthread_once(&gmtime_once, gmtime_key_init);
1827                 if (gmtime_key_error != 0) {
1828                         errno = gmtime_key_error;
1829                         return (NULL);
1830                 }
1831                 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1832                         if ((p_tm = malloc(sizeof(*p_tm))) == NULL) {
1833                                 return (NULL);
1834                         }
1835                         if (_pthread_setspecific(gmtime_key, p_tm) != 0) {
1836                                 free(p_tm);
1837                                 return (NULL);
1838                         }
1839                 }
1840         }
1841         return gmtime_r(timep, p_tm);
1842 }
1843
1844 #ifdef STD_INSPIRED
1845
1846 struct tm *
1847 offtime(const time_t *timep, long offset)
1848 {
1849   _once(&gmt_once, gmtcheck);
1850   return gmtsub(gmtptr, timep, offset, &tm);
1851 }
1852
1853 #endif /* defined STD_INSPIRED */
1854
1855 /*
1856 ** Return the number of leap years through the end of the given year
1857 ** where, to make the math easy, the answer for year zero is defined as zero.
1858 */
1859
1860 static time_t
1861 leaps_thru_end_of_nonneg(time_t y)
1862 {
1863   return y / 4 - y / 100 + y / 400;
1864 }
1865
1866 static time_t
1867 leaps_thru_end_of(time_t y)
1868 {
1869   return (y < 0
1870           ? -1 - leaps_thru_end_of_nonneg(-1 - y)
1871           : leaps_thru_end_of_nonneg(y));
1872 }
1873
1874 static struct tm *
1875 timesub(const time_t *timep, int_fast32_t offset,
1876         const struct state *sp, struct tm *tmp)
1877 {
1878         register const struct lsinfo *  lp;
1879         register time_t                 tdays;
1880         register const int *            ip;
1881         register int_fast32_t           corr;
1882         register int                    i;
1883         int_fast32_t idays, rem, dayoff, dayrem;
1884         time_t y;
1885
1886         /* If less than SECSPERMIN, the number of seconds since the
1887            most recent positive leap second; otherwise, do not add 1
1888            to localtime tm_sec because of leap seconds.  */
1889         time_t secs_since_posleap = SECSPERMIN;
1890
1891         corr = 0;
1892         i = (sp == NULL) ? 0 : sp->leapcnt;
1893         while (--i >= 0) {
1894                 lp = &sp->lsis[i];
1895                 if (*timep >= lp->ls_trans) {
1896                         corr = lp->ls_corr;
1897                         if ((i == 0 ? 0 : lp[-1].ls_corr) < corr)
1898                           secs_since_posleap = *timep - lp->ls_trans;
1899                         break;
1900                 }
1901         }
1902
1903         /* Calculate the year, avoiding integer overflow even if
1904            time_t is unsigned.  */
1905         tdays = *timep / SECSPERDAY;
1906         rem = *timep % SECSPERDAY;
1907         rem += offset % SECSPERDAY - corr % SECSPERDAY + 3 * SECSPERDAY;
1908         dayoff = offset / SECSPERDAY - corr / SECSPERDAY + rem / SECSPERDAY - 3;
1909         rem %= SECSPERDAY;
1910         /* y = (EPOCH_YEAR
1911                 + floor((tdays + dayoff) / DAYSPERREPEAT) * YEARSPERREPEAT),
1912            sans overflow.  But calculate against 1570 (EPOCH_YEAR -
1913            YEARSPERREPEAT) instead of against 1970 so that things work
1914            for localtime values before 1970 when time_t is unsigned.  */
1915         dayrem = tdays % DAYSPERREPEAT;
1916         dayrem += dayoff % DAYSPERREPEAT;
1917         y = (EPOCH_YEAR - YEARSPERREPEAT
1918              + ((1 + dayoff / DAYSPERREPEAT + dayrem / DAYSPERREPEAT
1919                  - ((dayrem % DAYSPERREPEAT) < 0)
1920                  + tdays / DAYSPERREPEAT)
1921                 * YEARSPERREPEAT));
1922         /* idays = (tdays + dayoff) mod DAYSPERREPEAT, sans overflow.  */
1923         idays = tdays % DAYSPERREPEAT;
1924         idays += dayoff % DAYSPERREPEAT + 2 * DAYSPERREPEAT;
1925         idays %= DAYSPERREPEAT;
1926         /* Increase Y and decrease IDAYS until IDAYS is in range for Y.  */
1927         while (year_lengths[isleap(y)] <= idays) {
1928                 int tdelta = idays / DAYSPERLYEAR;
1929                 int_fast32_t ydelta = tdelta + !tdelta;
1930                 time_t newy = y + ydelta;
1931                 register int    leapdays;
1932                 leapdays = leaps_thru_end_of(newy - 1) -
1933                         leaps_thru_end_of(y - 1);
1934                 idays -= ydelta * DAYSPERNYEAR;
1935                 idays -= leapdays;
1936                 y = newy;
1937         }
1938
1939 #ifdef ckd_add
1940         if (ckd_add(&tmp->tm_year, y, -TM_YEAR_BASE)) {
1941           errno = EOVERFLOW;
1942           return NULL;
1943         }
1944 #else
1945         if (!TYPE_SIGNED(time_t) && y < TM_YEAR_BASE) {
1946           int signed_y = y;
1947           tmp->tm_year = signed_y - TM_YEAR_BASE;
1948         } else if ((!TYPE_SIGNED(time_t) || INT_MIN + TM_YEAR_BASE <= y)
1949                    && y - TM_YEAR_BASE <= INT_MAX)
1950           tmp->tm_year = y - TM_YEAR_BASE;
1951         else {
1952           errno = EOVERFLOW;
1953           return NULL;
1954         }
1955 #endif
1956         tmp->tm_yday = idays;
1957         /*
1958         ** The "extra" mods below avoid overflow problems.
1959         */
1960         tmp->tm_wday = (TM_WDAY_BASE
1961                         + ((tmp->tm_year % DAYSPERWEEK)
1962                            * (DAYSPERNYEAR % DAYSPERWEEK))
1963                         + leaps_thru_end_of(y - 1)
1964                         - leaps_thru_end_of(TM_YEAR_BASE - 1)
1965                         + idays);
1966         tmp->tm_wday %= DAYSPERWEEK;
1967         if (tmp->tm_wday < 0)
1968                 tmp->tm_wday += DAYSPERWEEK;
1969         tmp->tm_hour = rem / SECSPERHOUR;
1970         rem %= SECSPERHOUR;
1971         tmp->tm_min = rem / SECSPERMIN;
1972         tmp->tm_sec = rem % SECSPERMIN;
1973
1974         /* Use "... ??:??:60" at the end of the localtime minute containing
1975            the second just before the positive leap second.  */
1976         tmp->tm_sec += secs_since_posleap <= tmp->tm_sec;
1977
1978         ip = mon_lengths[isleap(y)];
1979         for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1980                 idays -= ip[tmp->tm_mon];
1981         tmp->tm_mday = idays + 1;
1982         tmp->tm_isdst = 0;
1983 #ifdef TM_GMTOFF
1984         tmp->TM_GMTOFF = offset;
1985 #endif /* defined TM_GMTOFF */
1986         return tmp;
1987 }
1988
1989 char *
1990 ctime(const time_t *timep)
1991 {
1992 /*
1993 ** Section 4.12.3.2 of X3.159-1989 requires that
1994 **      The ctime function converts the calendar time pointed to by timer
1995 **      to local time in the form of a string. It is equivalent to
1996 **              asctime(localtime(timer))
1997 */
1998   struct tm *tmp = localtime(timep);
1999   return tmp ? asctime(tmp) : NULL;
2000 }
2001
2002 char *
2003 ctime_r(const time_t *timep, char *buf)
2004 {
2005   struct tm mytm;
2006   struct tm *tmp = localtime_r(timep, &mytm);
2007   return tmp ? asctime_r(tmp, buf) : NULL;
2008 }
2009
2010 /*
2011 ** Adapted from code provided by Robert Elz, who writes:
2012 **      The "best" way to do mktime I think is based on an idea of Bob
2013 **      Kridle's (so its said...) from a long time ago.
2014 **      It does a binary search of the time_t space. Since time_t's are
2015 **      just 32 bits, its a max of 32 iterations (even at 64 bits it
2016 **      would still be very reasonable).
2017 */
2018
2019 #ifndef WRONG
2020 # define WRONG (-1)
2021 #endif /* !defined WRONG */
2022
2023 /*
2024 ** Normalize logic courtesy Paul Eggert.
2025 */
2026
2027 static bool
2028 increment_overflow(int *ip, int j)
2029 {
2030 #ifdef ckd_add
2031         return ckd_add(ip, *ip, j);
2032 #else
2033         register int const      i = *ip;
2034
2035         /*
2036         ** If i >= 0 there can only be overflow if i + j > INT_MAX
2037         ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
2038         ** If i < 0 there can only be overflow if i + j < INT_MIN
2039         ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
2040         */
2041         if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
2042                 return true;
2043         *ip += j;
2044         return false;
2045 #endif
2046 }
2047
2048 static bool
2049 increment_overflow32(int_fast32_t *const lp, int const m)
2050 {
2051 #ifdef ckd_add
2052         return ckd_add(lp, *lp, m);
2053 #else
2054         register int_fast32_t const     l = *lp;
2055
2056         if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
2057                 return true;
2058         *lp += m;
2059         return false;
2060 #endif
2061 }
2062
2063 static bool
2064 increment_overflow_time(time_t *tp, int_fast32_t j)
2065 {
2066 #ifdef ckd_add
2067         return ckd_add(tp, *tp, j);
2068 #else
2069         /*
2070         ** This is like
2071         ** 'if (! (TIME_T_MIN <= *tp + j && *tp + j <= TIME_T_MAX)) ...',
2072         ** except that it does the right thing even if *tp + j would overflow.
2073         */
2074         if (! (j < 0
2075                ? (TYPE_SIGNED(time_t) ? TIME_T_MIN - j <= *tp : -1 - j < *tp)
2076                : *tp <= TIME_T_MAX - j))
2077                 return true;
2078         *tp += j;
2079         return false;
2080 #endif
2081 }
2082
2083 static bool
2084 normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
2085 {
2086         register int    tensdelta;
2087
2088         tensdelta = (*unitsptr >= 0) ?
2089                 (*unitsptr / base) :
2090                 (-1 - (-1 - *unitsptr) / base);
2091         *unitsptr -= tensdelta * base;
2092         return increment_overflow(tensptr, tensdelta);
2093 }
2094
2095 static bool
2096 normalize_overflow32(int_fast32_t *tensptr, int *unitsptr, int base)
2097 {
2098         register int    tensdelta;
2099
2100         tensdelta = (*unitsptr >= 0) ?
2101                 (*unitsptr / base) :
2102                 (-1 - (-1 - *unitsptr) / base);
2103         *unitsptr -= tensdelta * base;
2104         return increment_overflow32(tensptr, tensdelta);
2105 }
2106
2107 static int
2108 tmcomp(register const struct tm *const atmp,
2109        register const struct tm *const btmp)
2110 {
2111         register int    result;
2112
2113         if (atmp->tm_year != btmp->tm_year)
2114                 return atmp->tm_year < btmp->tm_year ? -1 : 1;
2115         if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
2116                 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
2117                 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
2118                 (result = (atmp->tm_min - btmp->tm_min)) == 0)
2119                         result = atmp->tm_sec - btmp->tm_sec;
2120         return result;
2121 }
2122
2123 /* Copy to *DEST from *SRC.  Copy only the members needed for mktime,
2124    as other members might not be initialized.  */
2125 static void
2126 mktmcpy(struct tm *dest, struct tm const *src)
2127 {
2128   dest->tm_sec = src->tm_sec;
2129   dest->tm_min = src->tm_min;
2130   dest->tm_hour = src->tm_hour;
2131   dest->tm_mday = src->tm_mday;
2132   dest->tm_mon = src->tm_mon;
2133   dest->tm_year = src->tm_year;
2134   dest->tm_isdst = src->tm_isdst;
2135 #if defined TM_GMTOFF && ! UNINIT_TRAP
2136   dest->TM_GMTOFF = src->TM_GMTOFF;
2137 #endif
2138 }
2139
2140 static time_t
2141 time2sub(struct tm *const tmp,
2142          struct tm *(*funcp)(struct state const *, time_t const *,
2143                              int_fast32_t, struct tm *),
2144          struct state const *sp,
2145          const int_fast32_t offset,
2146          bool *okayp,
2147          bool do_norm_secs)
2148 {
2149         register int                    dir;
2150         register int                    i, j;
2151         register int                    saved_seconds;
2152         register int_fast32_t           li;
2153         register time_t                 lo;
2154         register time_t                 hi;
2155         int_fast32_t                    y;
2156         time_t                          newt;
2157         time_t                          t;
2158         struct tm                       yourtm, mytm;
2159
2160         *okayp = false;
2161         mktmcpy(&yourtm, tmp);
2162
2163         if (do_norm_secs) {
2164                 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
2165                         SECSPERMIN))
2166                                 return WRONG;
2167         }
2168         if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
2169                 return WRONG;
2170         if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
2171                 return WRONG;
2172         y = yourtm.tm_year;
2173         if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
2174                 return WRONG;
2175         /*
2176         ** Turn y into an actual year number for now.
2177         ** It is converted back to an offset from TM_YEAR_BASE later.
2178         */
2179         if (increment_overflow32(&y, TM_YEAR_BASE))
2180                 return WRONG;
2181         while (yourtm.tm_mday <= 0) {
2182                 if (increment_overflow32(&y, -1))
2183                         return WRONG;
2184                 li = y + (1 < yourtm.tm_mon);
2185                 yourtm.tm_mday += year_lengths[isleap(li)];
2186         }
2187         while (yourtm.tm_mday > DAYSPERLYEAR) {
2188                 li = y + (1 < yourtm.tm_mon);
2189                 yourtm.tm_mday -= year_lengths[isleap(li)];
2190                 if (increment_overflow32(&y, 1))
2191                         return WRONG;
2192         }
2193         for ( ; ; ) {
2194                 i = mon_lengths[isleap(y)][yourtm.tm_mon];
2195                 if (yourtm.tm_mday <= i)
2196                         break;
2197                 yourtm.tm_mday -= i;
2198                 if (++yourtm.tm_mon >= MONSPERYEAR) {
2199                         yourtm.tm_mon = 0;
2200                         if (increment_overflow32(&y, 1))
2201                                 return WRONG;
2202                 }
2203         }
2204 #ifdef ckd_add
2205         if (ckd_add(&yourtm.tm_year, y, -TM_YEAR_BASE))
2206           return WRONG;
2207 #else
2208         if (increment_overflow32(&y, -TM_YEAR_BASE))
2209                 return WRONG;
2210         if (! (INT_MIN <= y && y <= INT_MAX))
2211                 return WRONG;
2212         yourtm.tm_year = y;
2213 #endif
2214         if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
2215                 saved_seconds = 0;
2216         else if (yourtm.tm_year < EPOCH_YEAR - TM_YEAR_BASE) {
2217                 /*
2218                 ** We can't set tm_sec to 0, because that might push the
2219                 ** time below the minimum representable time.
2220                 ** Set tm_sec to 59 instead.
2221                 ** This assumes that the minimum representable time is
2222                 ** not in the same minute that a leap second was deleted from,
2223                 ** which is a safer assumption than using 58 would be.
2224                 */
2225                 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
2226                         return WRONG;
2227                 saved_seconds = yourtm.tm_sec;
2228                 yourtm.tm_sec = SECSPERMIN - 1;
2229         } else {
2230                 saved_seconds = yourtm.tm_sec;
2231                 yourtm.tm_sec = 0;
2232         }
2233         /*
2234         ** Do a binary search (this works whatever time_t's type is).
2235         */
2236         lo = TIME_T_MIN;
2237         hi = TIME_T_MAX;
2238         for ( ; ; ) {
2239                 t = lo / 2 + hi / 2;
2240                 if (t < lo)
2241                         t = lo;
2242                 else if (t > hi)
2243                         t = hi;
2244                 if (! funcp(sp, &t, offset, &mytm)) {
2245                         /*
2246                         ** Assume that t is too extreme to be represented in
2247                         ** a struct tm; arrange things so that it is less
2248                         ** extreme on the next pass.
2249                         */
2250                         dir = (t > 0) ? 1 : -1;
2251                 } else  dir = tmcomp(&mytm, &yourtm);
2252                 if (dir != 0) {
2253                         if (t == lo) {
2254                                 if (t == TIME_T_MAX)
2255                                         return WRONG;
2256                                 ++t;
2257                                 ++lo;
2258                         } else if (t == hi) {
2259                                 if (t == TIME_T_MIN)
2260                                         return WRONG;
2261                                 --t;
2262                                 --hi;
2263                         }
2264                         if (lo > hi)
2265                                 return WRONG;
2266                         if (dir > 0)
2267                                 hi = t;
2268                         else    lo = t;
2269                         continue;
2270                 }
2271 #if defined TM_GMTOFF && ! UNINIT_TRAP
2272                 if (mytm.TM_GMTOFF != yourtm.TM_GMTOFF
2273                     && (yourtm.TM_GMTOFF < 0
2274                         ? (-SECSPERDAY <= yourtm.TM_GMTOFF
2275                            && (mytm.TM_GMTOFF <=
2276                                (min(INT_FAST32_MAX, LONG_MAX)
2277                                 + yourtm.TM_GMTOFF)))
2278                         : (yourtm.TM_GMTOFF <= SECSPERDAY
2279                            && ((max(INT_FAST32_MIN, LONG_MIN)
2280                                 + yourtm.TM_GMTOFF)
2281                                <= mytm.TM_GMTOFF)))) {
2282                   /* MYTM matches YOURTM except with the wrong UT offset.
2283                      YOURTM.TM_GMTOFF is plausible, so try it instead.
2284                      It's OK if YOURTM.TM_GMTOFF contains uninitialized data,
2285                      since the guess gets checked.  */
2286                   time_t altt = t;
2287                   int_fast32_t diff = mytm.TM_GMTOFF - yourtm.TM_GMTOFF;
2288                   if (!increment_overflow_time(&altt, diff)) {
2289                     struct tm alttm;
2290                     if (funcp(sp, &altt, offset, &alttm)
2291                         && alttm.tm_isdst == mytm.tm_isdst
2292                         && alttm.TM_GMTOFF == yourtm.TM_GMTOFF
2293                         && tmcomp(&alttm, &yourtm) == 0) {
2294                       t = altt;
2295                       mytm = alttm;
2296                     }
2297                   }
2298                 }
2299 #endif
2300                 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
2301                         break;
2302                 /*
2303                 ** Right time, wrong type.
2304                 ** Hunt for right time, right type.
2305                 ** It's okay to guess wrong since the guess
2306                 ** gets checked.
2307                 */
2308                 if (sp == NULL)
2309                         return WRONG;
2310                 for (i = sp->typecnt - 1; i >= 0; --i) {
2311                         if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
2312                                 continue;
2313                         for (j = sp->typecnt - 1; j >= 0; --j) {
2314                                 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
2315                                         continue;
2316                                 if (ttunspecified(sp, j))
2317                                   continue;
2318                                 newt = (t + sp->ttis[j].tt_utoff
2319                                         - sp->ttis[i].tt_utoff);
2320                                 if (! funcp(sp, &newt, offset, &mytm))
2321                                         continue;
2322                                 if (tmcomp(&mytm, &yourtm) != 0)
2323                                         continue;
2324                                 if (mytm.tm_isdst != yourtm.tm_isdst)
2325                                         continue;
2326                                 /*
2327                                 ** We have a match.
2328                                 */
2329                                 t = newt;
2330                                 goto label;
2331                         }
2332                 }
2333                 return WRONG;
2334         }
2335 label:
2336         newt = t + saved_seconds;
2337         if ((newt < t) != (saved_seconds < 0))
2338                 return WRONG;
2339         t = newt;
2340         if (funcp(sp, &t, offset, tmp))
2341                 *okayp = true;
2342         return t;
2343 }
2344
2345 static time_t
2346 time2(struct tm * const tmp,
2347       struct tm *(*funcp)(struct state const *, time_t const *,
2348                           int_fast32_t, struct tm *),
2349       struct state const *sp,
2350       const int_fast32_t offset,
2351       bool *okayp)
2352 {
2353         time_t  t;
2354
2355         /*
2356         ** First try without normalization of seconds
2357         ** (in case tm_sec contains a value associated with a leap second).
2358         ** If that fails, try with normalization of seconds.
2359         */
2360         t = time2sub(tmp, funcp, sp, offset, okayp, false);
2361         return *okayp ? t : time2sub(tmp, funcp, sp, offset, okayp, true);
2362 }
2363
2364 static time_t
2365 time1(struct tm *const tmp,
2366       struct tm *(*funcp)(struct state const *, time_t const *,
2367                           int_fast32_t, struct tm *),
2368       struct state const *sp,
2369       const int_fast32_t offset)
2370 {
2371         register time_t                 t;
2372         register int                    samei, otheri;
2373         register int                    sameind, otherind;
2374         register int                    i;
2375         register int                    nseen;
2376         char                            seen[TZ_MAX_TYPES];
2377         unsigned char                   types[TZ_MAX_TYPES];
2378         bool                            okay;
2379
2380         if (tmp == NULL) {
2381                 errno = EINVAL;
2382                 return WRONG;
2383         }
2384
2385         if (tmp->tm_isdst > 1)
2386                 tmp->tm_isdst = 1;
2387         t = time2(tmp, funcp, sp, offset, &okay);
2388         if (okay)
2389                 return t;
2390         if (tmp->tm_isdst < 0)
2391 #ifdef PCTS
2392                 /*
2393                 ** POSIX Conformance Test Suite code courtesy Grant Sullivan.
2394                 */
2395                 tmp->tm_isdst = 0;      /* reset to std and try again */
2396 #else
2397                 return t;
2398 #endif /* !defined PCTS */
2399         /*
2400         ** We're supposed to assume that somebody took a time of one type
2401         ** and did some math on it that yielded a "struct tm" that's bad.
2402         ** We try to divine the type they started from and adjust to the
2403         ** type they need.
2404         */
2405         if (sp == NULL)
2406                 return WRONG;
2407         for (i = 0; i < sp->typecnt; ++i)
2408                 seen[i] = false;
2409         nseen = 0;
2410         for (i = sp->timecnt - 1; i >= 0; --i)
2411                 if (!seen[sp->types[i]] && !ttunspecified(sp, sp->types[i])) {
2412                         seen[sp->types[i]] = true;
2413                         types[nseen++] = sp->types[i];
2414                 }
2415         for (sameind = 0; sameind < nseen; ++sameind) {
2416                 samei = types[sameind];
2417                 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2418                         continue;
2419                 for (otherind = 0; otherind < nseen; ++otherind) {
2420                         otheri = types[otherind];
2421                         if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2422                                 continue;
2423                         tmp->tm_sec += (sp->ttis[otheri].tt_utoff
2424                                         - sp->ttis[samei].tt_utoff);
2425                         tmp->tm_isdst = !tmp->tm_isdst;
2426                         t = time2(tmp, funcp, sp, offset, &okay);
2427                         if (okay)
2428                                 return t;
2429                         tmp->tm_sec -= (sp->ttis[otheri].tt_utoff
2430                                         - sp->ttis[samei].tt_utoff);
2431                         tmp->tm_isdst = !tmp->tm_isdst;
2432                 }
2433         }
2434         return WRONG;
2435 }
2436
2437 static time_t
2438 mktime_tzname(struct state *sp, struct tm *tmp, bool setname)
2439 {
2440   if (sp)
2441     return time1(tmp, localsub, sp, setname);
2442   else {
2443     _once(&gmt_once, gmtcheck);
2444     return time1(tmp, gmtsub, gmtptr, 0);
2445   }
2446 }
2447
2448 #if NETBSD_INSPIRED
2449
2450 time_t
2451 mktime_z(struct state *sp, struct tm *tmp)
2452 {
2453   return mktime_tzname(sp, tmp, false);
2454 }
2455
2456 #endif
2457
2458 time_t
2459 mktime(struct tm *tmp)
2460 {
2461   time_t t;
2462   int err = lock();
2463   if (err) {
2464     errno = err;
2465     return -1;
2466   }
2467   tzset_unlocked();
2468   t = mktime_tzname(lclptr, tmp, true);
2469   unlock();
2470   return t;
2471 }
2472
2473 #ifdef STD_INSPIRED
2474 time_t
2475 timelocal(struct tm *tmp)
2476 {
2477         if (tmp != NULL)
2478                 tmp->tm_isdst = -1;     /* in case it wasn't initialized */
2479         return mktime(tmp);
2480 }
2481 #else
2482 static
2483 #endif
2484 time_t
2485 timeoff(struct tm *tmp, long offset)
2486 {
2487   if (tmp)
2488     tmp->tm_isdst = 0;
2489   _once(&gmt_once, gmtcheck);
2490   return time1(tmp, gmtsub, gmtptr, offset);
2491 }
2492
2493 time_t
2494 timegm(struct tm *tmp)
2495 {
2496   time_t t;
2497   struct tm tmcpy;
2498   mktmcpy(&tmcpy, tmp);
2499   tmcpy.tm_wday = -1;
2500   t = timeoff(&tmcpy, 0);
2501   if (0 <= tmcpy.tm_wday)
2502     *tmp = tmcpy;
2503   return t;
2504 }
2505
2506 static int_fast32_t
2507 leapcorr(struct state const *sp, time_t t)
2508 {
2509         register struct lsinfo const *  lp;
2510         register int                    i;
2511
2512         i = sp->leapcnt;
2513         while (--i >= 0) {
2514                 lp = &sp->lsis[i];
2515                 if (t >= lp->ls_trans)
2516                         return lp->ls_corr;
2517         }
2518         return 0;
2519 }
2520
2521 /*
2522 ** XXX--is the below the right way to conditionalize??
2523 */
2524
2525 #ifdef STD_INSPIRED
2526
2527 /* NETBSD_INSPIRED_EXTERN functions are exported to callers if
2528    NETBSD_INSPIRED is defined, and are private otherwise.  */
2529 # if NETBSD_INSPIRED
2530 #  define NETBSD_INSPIRED_EXTERN
2531 # else
2532 #  define NETBSD_INSPIRED_EXTERN static
2533 # endif
2534
2535 /*
2536 ** IEEE Std 1003.1 (POSIX) says that 536457599
2537 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2538 ** is not the case if we are accounting for leap seconds.
2539 ** So, we provide the following conversion routines for use
2540 ** when exchanging timestamps with POSIX conforming systems.
2541 */
2542
2543 NETBSD_INSPIRED_EXTERN time_t
2544 time2posix_z(struct state *sp, time_t t)
2545 {
2546   return t - leapcorr(sp, t);
2547 }
2548
2549 time_t
2550 time2posix(time_t t)
2551 {
2552   int err = lock();
2553   if (err) {
2554     errno = err;
2555     return -1;
2556   }
2557 #ifndef DETECT_TZ_CHANGES
2558   if (!lcl_is_set)
2559 #endif
2560     tzset_unlocked();
2561   if (lclptr)
2562     t = time2posix_z(lclptr, t);
2563   unlock();
2564   return t;
2565 }
2566
2567 NETBSD_INSPIRED_EXTERN time_t
2568 posix2time_z(struct state *sp, time_t t)
2569 {
2570         time_t  x;
2571         time_t  y;
2572         /*
2573         ** For a positive leap second hit, the result
2574         ** is not unique. For a negative leap second
2575         ** hit, the corresponding time doesn't exist,
2576         ** so we return an adjacent second.
2577         */
2578         x = t + leapcorr(sp, t);
2579         y = x - leapcorr(sp, x);
2580         if (y < t) {
2581                 do {
2582                         x++;
2583                         y = x - leapcorr(sp, x);
2584                 } while (y < t);
2585                 x -= y != t;
2586         } else if (y > t) {
2587                 do {
2588                         --x;
2589                         y = x - leapcorr(sp, x);
2590                 } while (y > t);
2591                 x += y != t;
2592         }
2593         return x;
2594 }
2595
2596 time_t
2597 posix2time(time_t t)
2598 {
2599   int err = lock();
2600   if (err) {
2601     errno = err;
2602     return -1;
2603   }
2604 #ifndef DETECT_TZ_CHANGES
2605   if (!lcl_is_set)
2606 #endif
2607     tzset_unlocked();
2608   if (lclptr)
2609     t = posix2time_z(lclptr, t);
2610   unlock();
2611   return t;
2612 }
2613
2614 #endif /* defined STD_INSPIRED */
2615
2616 #if TZ_TIME_T
2617
2618 # if !USG_COMPAT
2619 #  define daylight 0
2620 #  define timezone 0
2621 # endif
2622 # if !ALTZONE
2623 #  define altzone 0
2624 # endif
2625
2626 /* Convert from the underlying system's time_t to the ersatz time_tz,
2627    which is called 'time_t' in this file.  Typically, this merely
2628    converts the time's integer width.  On some platforms, the system
2629    time is local time not UT, or uses some epoch other than the POSIX
2630    epoch.
2631
2632    Although this code appears to define a function named 'time' that
2633    returns time_t, the macros in private.h cause this code to actually
2634    define a function named 'tz_time' that returns tz_time_t.  The call
2635    to sys_time invokes the underlying system's 'time' function.  */
2636
2637 time_t
2638 time(time_t *p)
2639 {
2640   time_t r = sys_time(0);
2641   if (r != (time_t) -1) {
2642     int_fast32_t offset = EPOCH_LOCAL ? (daylight ? timezone : altzone) : 0;
2643     if (increment_overflow32(&offset, -EPOCH_OFFSET)
2644         || increment_overflow_time(&r, offset)) {
2645       errno = EOVERFLOW;
2646       r = -1;
2647     }
2648   }
2649   if (p)
2650     *p = r;
2651   return r;
2652 }
2653
2654 #endif