2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
7 ** Leap second handling from Bradley White.
8 ** POSIX-style TZ environment variable handling from Guy Harris.
13 #define LOCALTIME_IMPLEMENTATION
21 static pthread_mutex_t locallock = PTHREAD_MUTEX_INITIALIZER;
22 static int lock(void) { return pthread_mutex_lock(&locallock); }
23 static void unlock(void) { pthread_mutex_unlock(&locallock); }
25 static int lock(void) { return 0; }
26 static void unlock(void) { }
29 /* NETBSD_INSPIRED_EXTERN functions are exported to callers if
30 NETBSD_INSPIRED is defined, and are private otherwise. */
32 # define NETBSD_INSPIRED_EXTERN
34 # define NETBSD_INSPIRED_EXTERN static
37 #ifndef TZ_ABBR_MAX_LEN
38 #define TZ_ABBR_MAX_LEN 16
39 #endif /* !defined TZ_ABBR_MAX_LEN */
41 #ifndef TZ_ABBR_CHAR_SET
42 #define TZ_ABBR_CHAR_SET \
43 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
44 #endif /* !defined TZ_ABBR_CHAR_SET */
46 #ifndef TZ_ABBR_ERR_CHAR
47 #define TZ_ABBR_ERR_CHAR '_'
48 #endif /* !defined TZ_ABBR_ERR_CHAR */
51 ** SunOS 4.1.1 headers lack O_BINARY.
55 #define OPEN_MODE (O_RDONLY | O_BINARY)
56 #endif /* defined O_BINARY */
58 #define OPEN_MODE O_RDONLY
59 #endif /* !defined O_BINARY */
63 ** Someone might make incorrect use of a time zone abbreviation:
64 ** 1. They might reference tzname[0] before calling tzset (explicitly
66 ** 2. They might reference tzname[1] before calling tzset (explicitly
68 ** 3. They might reference tzname[1] after setting to a time zone
69 ** in which Daylight Saving Time is never observed.
70 ** 4. They might reference tzname[0] after setting to a time zone
71 ** in which Standard Time is never observed.
72 ** 5. They might reference tm.TM_ZONE after calling offtime.
73 ** What's best to do in the above cases is open to debate;
74 ** for now, we just set things up so that in any of the five cases
75 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
76 ** string "tzname[0] used before set", and similarly for the other cases.
77 ** And another: initialize tzname[0] to "ERA", with an explanation in the
78 ** manual page of what this "time zone abbreviation" means (doing this so
79 ** that tzname[0] has the "normal" length of three characters).
82 #endif /* !defined WILDABBR */
84 static const char wildabbr[] = WILDABBR;
86 static const char gmt[] = "GMT";
89 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
90 ** We default to US rules as of 1999-08-17.
91 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
92 ** implementation dependent; for historical reasons, US rules are a
95 #ifndef TZDEFRULESTRING
96 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
97 #endif /* !defined TZDEFDST */
99 struct ttinfo { /* time type information */
100 int_fast32_t tt_gmtoff; /* UT offset in seconds */
101 bool tt_isdst; /* used to set tm_isdst */
102 int tt_abbrind; /* abbreviation list index */
103 bool tt_ttisstd; /* transition is std time */
104 bool tt_ttisgmt; /* transition is UT */
107 struct lsinfo { /* leap second information */
108 time_t ls_trans; /* transition time */
109 int_fast64_t ls_corr; /* correction to apply */
112 #define SMALLEST(a, b) (((a) < (b)) ? (a) : (b))
113 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
116 #define MY_TZNAME_MAX TZNAME_MAX
117 #endif /* defined TZNAME_MAX */
119 #define MY_TZNAME_MAX 255
120 #endif /* !defined TZNAME_MAX */
129 time_t ats[TZ_MAX_TIMES];
130 unsigned char types[TZ_MAX_TIMES];
131 struct ttinfo ttis[TZ_MAX_TYPES];
132 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
133 (2 * (MY_TZNAME_MAX + 1)))];
134 struct lsinfo lsis[TZ_MAX_LEAPS];
135 int defaulttype; /* for early times or if no transitions */
139 JULIAN_DAY, /* Jn = Julian day */
140 DAY_OF_YEAR, /* n = day of year */
141 MONTH_NTH_DAY_OF_WEEK /* Mm.n.d = month, week, day of week */
145 enum r_type r_type; /* type of rule */
146 int r_day; /* day number of rule */
147 int r_week; /* week number of rule */
148 int r_mon; /* month number of rule */
149 int_fast32_t r_time; /* transition time of rule */
152 static struct tm *gmtsub(struct state const *, time_t const *, int_fast32_t,
154 static bool increment_overflow(int *, int);
155 static bool increment_overflow_time(time_t *, int_fast32_t);
156 static bool normalize_overflow32(int_fast32_t *, int *, int);
157 static struct tm *timesub(time_t const *, int_fast32_t, struct state const *,
159 static bool typesequiv(struct state const *, int, int);
160 static bool tzparse(char const *, struct state *, bool);
163 static struct state * lclptr;
164 static struct state * gmtptr;
165 #endif /* defined ALL_STATE */
168 static struct state lclmem;
169 static struct state gmtmem;
170 #define lclptr (&lclmem)
171 #define gmtptr (&gmtmem)
172 #endif /* State Farm */
174 #ifndef TZ_STRLEN_MAX
175 #define TZ_STRLEN_MAX 255
176 #endif /* !defined TZ_STRLEN_MAX */
178 static char lcl_TZname[TZ_STRLEN_MAX + 1];
179 static int lcl_is_set;
187 ** Section 4.12.3 of X3.159-1989 requires that
188 ** Except for the strftime function, these functions [asctime,
189 ** ctime, gmtime, localtime] return values in one of two static
190 ** objects: a broken-down time structure and an array of char.
191 ** Thanks to Paul Eggert for noting this.
199 #endif /* defined USG_COMPAT */
203 #endif /* defined ALTZONE */
205 /* Initialize *S to a value based on GMTOFF, ISDST, and ABBRIND. */
207 init_ttinfo(struct ttinfo *s, int_fast32_t gmtoff, bool isdst, int abbrind)
209 s->tt_gmtoff = gmtoff;
211 s->tt_abbrind = abbrind;
212 s->tt_ttisstd = false;
213 s->tt_ttisgmt = false;
217 detzcode(const char *const codep)
219 register int_fast32_t result;
221 int_fast32_t one = 1;
222 int_fast32_t halfmaxval = one << (32 - 2);
223 int_fast32_t maxval = halfmaxval - 1 + halfmaxval;
224 int_fast32_t minval = -1 - maxval;
226 result = codep[0] & 0x7f;
227 for (i = 1; i < 4; ++i)
228 result = (result << 8) | (codep[i] & 0xff);
230 if (codep[0] & 0x80) {
231 /* Do two's-complement negation even on non-two's-complement machines.
232 If the result would be minval - 1, return minval. */
233 result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0;
240 detzcode64(const char *const codep)
242 register uint_fast64_t result;
244 int_fast64_t one = 1;
245 int_fast64_t halfmaxval = one << (64 - 2);
246 int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
247 int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
249 result = codep[0] & 0x7f;
250 for (i = 1; i < 8; ++i)
251 result = (result << 8) | (codep[i] & 0xff);
253 if (codep[0] & 0x80) {
254 /* Do two's-complement negation even on non-two's-complement machines.
255 If the result would be minval - 1, return minval. */
256 result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
263 update_tzname_etc(struct state const *sp, struct ttinfo const *ttisp)
265 tzname[ttisp->tt_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
267 if (!ttisp->tt_isdst)
268 timezone = - ttisp->tt_gmtoff;
272 altzone = - ttisp->tt_gmtoff;
279 register struct state * const sp = lclptr;
282 tzname[0] = tzname[1] = (char *) wildabbr;
286 #endif /* defined USG_COMPAT */
289 #endif /* defined ALTZONE */
291 tzname[0] = tzname[1] = (char *) gmt;
295 ** And to get the latest zone names into tzname. . .
297 for (i = 0; i < sp->typecnt; ++i) {
298 register const struct ttinfo * const ttisp = &sp->ttis[i];
299 update_tzname_etc(sp, ttisp);
301 for (i = 0; i < sp->timecnt; ++i) {
302 register const struct ttinfo * const ttisp =
305 update_tzname_etc(sp, ttisp);
309 #endif /* defined USG_COMPAT */
314 scrub_abbrs(struct state *sp)
318 ** First, replace bogus characters.
320 for (i = 0; i < sp->charcnt; ++i)
321 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
322 sp->chars[i] = TZ_ABBR_ERR_CHAR;
324 ** Second, truncate long abbreviations.
326 for (i = 0; i < sp->typecnt; ++i) {
327 register const struct ttinfo * const ttisp = &sp->ttis[i];
328 register char * cp = &sp->chars[ttisp->tt_abbrind];
330 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
331 strcmp(cp, GRANDPARENTED) != 0)
332 *(cp + TZ_ABBR_MAX_LEN) = '\0';
337 differ_by_repeat(const time_t t1, const time_t t0)
339 if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
341 return t1 - t0 == SECSPERREPEAT;
344 /* Input buffer for data read from a compiled tz file. */
346 /* The first part of the buffer, interpreted as a header. */
347 struct tzhead tzhead;
349 /* The entire buffer. */
350 char buf[2 * sizeof(struct tzhead) + 2 * sizeof (struct state)
354 /* Local storage needed for 'tzloadbody'. */
355 union local_storage {
356 /* The file name to be opened. */
357 char fullname[FILENAME_MAX + 1];
359 /* The results of analyzing the file's contents after it is opened. */
361 /* The input buffer. */
362 union input_buffer u;
364 /* A temporary state used for parsing a TZ string in the file. */
369 /* Load tz data from the file named NAME into *SP. Read extended
370 format if DOEXTEND. Use *LSP for temporary storage. Return 0 on
371 success, an errno value on failure. */
373 tzloadbody(char const *name, struct state *sp, bool doextend,
374 union local_storage *lsp)
379 register ssize_t nread;
380 register bool doaccess;
381 register char *fullname = lsp->fullname;
382 register union input_buffer *up = &lsp->u.u;
383 register int tzheadsize = sizeof (struct tzhead);
385 sp->goback = sp->goahead = false;
395 doaccess = name[0] == '/';
397 char const *p = TZDIR;
400 if (sizeof lsp->fullname - 1 <= strlen(p) + strlen(name))
403 strcat(fullname, "/");
404 strcat(fullname, name);
405 /* Set doaccess if '.' (as in "../") shows up in name. */
406 if (strchr(name, '.'))
410 if (doaccess && access(name, R_OK) != 0)
412 fid = open(name, OPEN_MODE);
416 nread = read(fid, up->buf, sizeof up->buf);
417 if (nread < tzheadsize) {
418 int err = nread < 0 ? errno : EINVAL;
424 for (stored = 4; stored <= 8; stored *= 2) {
425 int_fast32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
426 int_fast32_t ttisgmtcnt = detzcode(up->tzhead.tzh_ttisgmtcnt);
427 int_fast32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
428 int_fast32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
429 int_fast32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
430 int_fast32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
431 char const *p = up->buf + tzheadsize;
432 if (! (0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
433 && 0 < typecnt && typecnt < TZ_MAX_TYPES
434 && 0 <= timecnt && timecnt < TZ_MAX_TIMES
435 && 0 <= charcnt && charcnt < TZ_MAX_CHARS
436 && (ttisstdcnt == typecnt || ttisstdcnt == 0)
437 && (ttisgmtcnt == typecnt || ttisgmtcnt == 0)))
440 < (tzheadsize /* struct tzhead */
441 + timecnt * stored /* ats */
442 + timecnt /* types */
443 + typecnt * 6 /* ttinfos */
444 + charcnt /* chars */
445 + leapcnt * (stored + 4) /* lsinfos */
446 + ttisstdcnt /* ttisstds */
447 + ttisgmtcnt)) /* ttisgmts */
449 sp->leapcnt = leapcnt;
450 sp->timecnt = timecnt;
451 sp->typecnt = typecnt;
452 sp->charcnt = charcnt;
454 /* Read transitions, discarding those out of time_t range.
455 But pretend the last transition before time_t_min
456 occurred at time_t_min. */
458 for (i = 0; i < sp->timecnt; ++i) {
460 = stored == 4 ? detzcode(p) : detzcode64(p);
461 sp->types[i] = at <= time_t_max;
464 = ((TYPE_SIGNED(time_t) ? at < time_t_min : at < 0)
466 if (timecnt && attime <= sp->ats[timecnt - 1]) {
467 if (attime < sp->ats[timecnt - 1])
469 sp->types[i - 1] = 0;
472 sp->ats[timecnt++] = attime;
478 for (i = 0; i < sp->timecnt; ++i) {
479 unsigned char typ = *p++;
480 if (sp->typecnt <= typ)
483 sp->types[timecnt++] = typ;
485 sp->timecnt = timecnt;
486 for (i = 0; i < sp->typecnt; ++i) {
487 register struct ttinfo * ttisp;
488 unsigned char isdst, abbrind;
490 ttisp = &sp->ttis[i];
491 ttisp->tt_gmtoff = detzcode(p);
496 ttisp->tt_isdst = isdst;
498 if (! (abbrind < sp->charcnt))
500 ttisp->tt_abbrind = abbrind;
502 for (i = 0; i < sp->charcnt; ++i)
504 sp->chars[i] = '\0'; /* ensure '\0' at end */
506 /* Read leap seconds, discarding those out of time_t range. */
508 for (i = 0; i < sp->leapcnt; ++i) {
509 int_fast64_t tr = stored == 4 ? detzcode(p) : detzcode64(p);
510 int_fast32_t corr = detzcode(p + stored);
512 if (tr <= time_t_max) {
514 = ((TYPE_SIGNED(time_t) ? tr < time_t_min : tr < 0)
516 if (leapcnt && trans <= sp->lsis[leapcnt - 1].ls_trans) {
517 if (trans < sp->lsis[leapcnt - 1].ls_trans)
521 sp->lsis[leapcnt].ls_trans = trans;
522 sp->lsis[leapcnt].ls_corr = corr;
526 sp->leapcnt = leapcnt;
528 for (i = 0; i < sp->typecnt; ++i) {
529 register struct ttinfo * ttisp;
531 ttisp = &sp->ttis[i];
533 ttisp->tt_ttisstd = false;
535 if (*p != true && *p != false)
537 ttisp->tt_ttisstd = *p++;
540 for (i = 0; i < sp->typecnt; ++i) {
541 register struct ttinfo * ttisp;
543 ttisp = &sp->ttis[i];
545 ttisp->tt_ttisgmt = false;
547 if (*p != true && *p != false)
549 ttisp->tt_ttisgmt = *p++;
553 ** If this is an old file, we're done.
555 if (up->tzhead.tzh_version[0] == '\0')
557 nread -= p - up->buf;
558 memmove(up->buf, p, nread);
560 if (doextend && nread > 2 &&
561 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
562 sp->typecnt + 2 <= TZ_MAX_TYPES) {
563 struct state *ts = &lsp->u.st;
565 up->buf[nread - 1] = '\0';
566 if (tzparse(&up->buf[1], ts, false)
567 && ts->typecnt == 2) {
569 /* Attempt to reuse existing abbreviations.
570 Without this, America/Anchorage would stop
571 working after 2037 when TZ_MAX_CHARS is 50, as
572 sp->charcnt equals 42 (for LMT CAT CAWT CAPT AHST
573 AHDT YST AKDT AKST) and ts->charcnt equals 10
574 (for AKST AKDT). Reusing means sp->charcnt can
575 stay 42 in this example. */
577 int charcnt = sp->charcnt;
578 for (i = 0; i < 2; i++) {
579 char *tsabbr = ts->chars + ts->ttis[i].tt_abbrind;
581 for (j = 0; j < charcnt; j++)
582 if (strcmp(sp->chars + j, tsabbr) == 0) {
583 ts->ttis[i].tt_abbrind = j;
587 if (! (j < charcnt)) {
588 int tsabbrlen = strlen(tsabbr);
589 if (j + tsabbrlen < TZ_MAX_CHARS) {
590 strcpy(sp->chars + j, tsabbr);
591 charcnt = j + tsabbrlen + 1;
592 ts->ttis[i].tt_abbrind = j;
598 sp->charcnt = charcnt;
599 for (i = 0; i < ts->timecnt; i++)
600 if (sp->ats[sp->timecnt - 1] < ts->ats[i])
602 while (i < ts->timecnt
603 && sp->timecnt < TZ_MAX_TIMES) {
604 sp->ats[sp->timecnt] = ts->ats[i];
605 sp->types[sp->timecnt] = (sp->typecnt
610 sp->ttis[sp->typecnt++] = ts->ttis[0];
611 sp->ttis[sp->typecnt++] = ts->ttis[1];
615 if (sp->timecnt > 1) {
616 for (i = 1; i < sp->timecnt; ++i)
617 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
618 differ_by_repeat(sp->ats[i], sp->ats[0])) {
622 for (i = sp->timecnt - 2; i >= 0; --i)
623 if (typesequiv(sp, sp->types[sp->timecnt - 1],
625 differ_by_repeat(sp->ats[sp->timecnt - 1],
632 ** If type 0 is is unused in transitions,
633 ** it's the type to use for early times.
635 for (i = 0; i < sp->timecnt; ++i)
636 if (sp->types[i] == 0)
638 i = i < sp->timecnt ? -1 : 0;
641 ** if there are transition times
642 ** and the first transition is to a daylight time
643 ** find the standard type less than and closest to
644 ** the type of the first transition.
646 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
649 if (!sp->ttis[i].tt_isdst)
653 ** If no result yet, find the first standard type.
654 ** If there is none, punt to type zero.
658 while (sp->ttis[i].tt_isdst)
659 if (++i >= sp->typecnt) {
668 /* Load tz data from the file named NAME into *SP. Read extended
669 format if DOEXTEND. Return 0 on success, an errno value on failure. */
671 tzload(char const *name, struct state *sp, bool doextend)
674 union local_storage *lsp = malloc(sizeof *lsp);
678 int err = tzloadbody(name, sp, doextend, lsp);
683 union local_storage ls;
684 return tzloadbody(name, sp, doextend, &ls);
689 typesequiv(const struct state *sp, int a, int b)
691 register bool result;
694 a < 0 || a >= sp->typecnt ||
695 b < 0 || b >= sp->typecnt)
698 register const struct ttinfo * ap = &sp->ttis[a];
699 register const struct ttinfo * bp = &sp->ttis[b];
700 result = ap->tt_gmtoff == bp->tt_gmtoff &&
701 ap->tt_isdst == bp->tt_isdst &&
702 ap->tt_ttisstd == bp->tt_ttisstd &&
703 ap->tt_ttisgmt == bp->tt_ttisgmt &&
704 strcmp(&sp->chars[ap->tt_abbrind],
705 &sp->chars[bp->tt_abbrind]) == 0;
710 static const int mon_lengths[2][MONSPERYEAR] = {
711 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
712 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
715 static const int year_lengths[2] = {
716 DAYSPERNYEAR, DAYSPERLYEAR
720 ** Given a pointer into a time zone string, scan until a character that is not
721 ** a valid character in a zone name is found. Return a pointer to that
725 static const char * ATTRIBUTE_PURE
726 getzname(register const char *strp)
730 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
737 ** Given a pointer into an extended time zone string, scan until the ending
738 ** delimiter of the zone name is located. Return a pointer to the delimiter.
740 ** As with getzname above, the legal character set is actually quite
741 ** restricted, with other characters producing undefined results.
742 ** We don't do any checking here; checking is done later in common-case code.
745 static const char * ATTRIBUTE_PURE
746 getqzname(register const char *strp, const int delim)
750 while ((c = *strp) != '\0' && c != delim)
756 ** Given a pointer into a time zone string, extract a number from that string.
757 ** Check that the number is within a specified range; if it is not, return
759 ** Otherwise, return a pointer to the first character not part of the number.
763 getnum(register const char *strp, int *const nump, const int min, const int max)
768 if (strp == NULL || !is_digit(c = *strp))
772 num = num * 10 + (c - '0');
774 return NULL; /* illegal value */
776 } while (is_digit(c));
778 return NULL; /* illegal value */
784 ** Given a pointer into a time zone string, extract a number of seconds,
785 ** in hh[:mm[:ss]] form, from the string.
786 ** If any error occurs, return NULL.
787 ** Otherwise, return a pointer to the first character not part of the number
792 getsecs(register const char *strp, int_fast32_t *const secsp)
797 ** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
798 ** "M10.4.6/26", which does not conform to Posix,
799 ** but which specifies the equivalent of
800 ** "02:00 on the first Sunday on or after 23 Oct".
802 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
805 *secsp = num * (int_fast32_t) SECSPERHOUR;
808 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
811 *secsp += num * SECSPERMIN;
814 /* 'SECSPERMIN' allows for leap seconds. */
815 strp = getnum(strp, &num, 0, SECSPERMIN);
825 ** Given a pointer into a time zone string, extract an offset, in
826 ** [+-]hh[:mm[:ss]] form, from the string.
827 ** If any error occurs, return NULL.
828 ** Otherwise, return a pointer to the first character not part of the time.
832 getoffset(register const char *strp, int_fast32_t *const offsetp)
834 register bool neg = false;
839 } else if (*strp == '+')
841 strp = getsecs(strp, offsetp);
843 return NULL; /* illegal time */
845 *offsetp = -*offsetp;
850 ** Given a pointer into a time zone string, extract a rule in the form
851 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
852 ** If a valid rule is not found, return NULL.
853 ** Otherwise, return a pointer to the first character not part of the rule.
857 getrule(const char *strp, register struct rule *const rulep)
863 rulep->r_type = JULIAN_DAY;
865 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
866 } else if (*strp == 'M') {
870 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
872 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
877 strp = getnum(strp, &rulep->r_week, 1, 5);
882 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
883 } else if (is_digit(*strp)) {
887 rulep->r_type = DAY_OF_YEAR;
888 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
889 } else return NULL; /* invalid format */
897 strp = getoffset(strp, &rulep->r_time);
898 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
903 ** Given a year, a rule, and the offset from UT at the time that rule takes
904 ** effect, calculate the year-relative time that rule takes effect.
907 static int_fast32_t ATTRIBUTE_PURE
908 transtime(const int year, register const struct rule *const rulep,
909 const int_fast32_t offset)
911 register bool leapyear;
912 register int_fast32_t value;
914 int d, m1, yy0, yy1, yy2, dow;
917 leapyear = isleap(year);
918 switch (rulep->r_type) {
922 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
924 ** In non-leap years, or if the day number is 59 or less, just
925 ** add SECSPERDAY times the day number-1 to the time of
926 ** January 1, midnight, to get the day.
928 value = (rulep->r_day - 1) * SECSPERDAY;
929 if (leapyear && rulep->r_day >= 60)
936 ** Just add SECSPERDAY times the day number to the time of
937 ** January 1, midnight, to get the day.
939 value = rulep->r_day * SECSPERDAY;
942 case MONTH_NTH_DAY_OF_WEEK:
944 ** Mm.n.d - nth "dth day" of month m.
948 ** Use Zeller's Congruence to get day-of-week of first day of
951 m1 = (rulep->r_mon + 9) % 12 + 1;
952 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
955 dow = ((26 * m1 - 2) / 10 +
956 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
961 ** "dow" is the day-of-week of the first day of the month. Get
962 ** the day-of-month (zero-origin) of the first "dow" day of the
965 d = rulep->r_day - dow;
968 for (i = 1; i < rulep->r_week; ++i) {
969 if (d + DAYSPERWEEK >=
970 mon_lengths[leapyear][rulep->r_mon - 1])
976 ** "d" is the day-of-month (zero-origin) of the day we want.
978 value = d * SECSPERDAY;
979 for (i = 0; i < rulep->r_mon - 1; ++i)
980 value += mon_lengths[leapyear][i] * SECSPERDAY;
985 ** "value" is the year-relative time of 00:00:00 UT on the day in
986 ** question. To get the year-relative time of the specified local
987 ** time on that day, add the transition time and the current offset
990 return value + rulep->r_time + offset;
994 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
999 tzparse(const char *name, struct state *sp, bool lastditch)
1001 const char * stdname;
1002 const char * dstname;
1006 int_fast32_t stdoffset;
1007 int_fast32_t dstoffset;
1009 register bool load_ok;
1013 stdlen = sizeof gmt - 1;
1020 name = getqzname(name, '>');
1023 stdlen = name - stdname;
1026 name = getzname(name);
1027 stdlen = name - stdname;
1031 name = getoffset(name, &stdoffset);
1035 charcnt = stdlen + 1;
1036 if (sizeof sp->chars < charcnt)
1038 load_ok = tzload(TZDEFRULES, sp, false) == 0;
1040 sp->leapcnt = 0; /* so, we're off a little */
1041 if (*name != '\0') {
1044 name = getqzname(name, '>');
1047 dstlen = name - dstname;
1051 name = getzname(name);
1052 dstlen = name - dstname; /* length of DST zone name */
1056 charcnt += dstlen + 1;
1057 if (sizeof sp->chars < charcnt)
1059 if (*name != '\0' && *name != ',' && *name != ';') {
1060 name = getoffset(name, &dstoffset);
1063 } else dstoffset = stdoffset - SECSPERHOUR;
1064 if (*name == '\0' && !load_ok)
1065 name = TZDEFRULESTRING;
1066 if (*name == ',' || *name == ';') {
1070 register int yearlim;
1071 register int timecnt;
1075 if ((name = getrule(name, &start)) == NULL)
1079 if ((name = getrule(name, &end)) == NULL)
1083 sp->typecnt = 2; /* standard time and DST */
1085 ** Two transitions per year, from EPOCH_YEAR forward.
1087 init_ttinfo(&sp->ttis[0], -dstoffset, true, stdlen + 1);
1088 init_ttinfo(&sp->ttis[1], -stdoffset, false, 0);
1089 sp->defaulttype = 0;
1092 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1093 for (year = EPOCH_YEAR; year < yearlim; year++) {
1095 starttime = transtime(year, &start, stdoffset),
1096 endtime = transtime(year, &end, dstoffset);
1098 yearsecs = (year_lengths[isleap(year)]
1100 bool reversed = endtime < starttime;
1102 int_fast32_t swap = starttime;
1103 starttime = endtime;
1107 || (starttime < endtime
1108 && (endtime - starttime
1110 + (stdoffset - dstoffset))))) {
1111 if (TZ_MAX_TIMES - 2 < timecnt)
1113 yearlim = year + YEARSPERREPEAT + 1;
1114 sp->ats[timecnt] = janfirst;
1115 if (increment_overflow_time
1116 (&sp->ats[timecnt], starttime))
1118 sp->types[timecnt++] = reversed;
1119 sp->ats[timecnt] = janfirst;
1120 if (increment_overflow_time
1121 (&sp->ats[timecnt], endtime))
1123 sp->types[timecnt++] = !reversed;
1125 if (increment_overflow_time(&janfirst, yearsecs))
1128 sp->timecnt = timecnt;
1130 sp->typecnt = 1; /* Perpetual DST. */
1132 register int_fast32_t theirstdoffset;
1133 register int_fast32_t theirdstoffset;
1134 register int_fast32_t theiroffset;
1135 register bool isdst;
1142 ** Initial values of theirstdoffset and theirdstoffset.
1145 for (i = 0; i < sp->timecnt; ++i) {
1147 if (!sp->ttis[j].tt_isdst) {
1149 -sp->ttis[j].tt_gmtoff;
1154 for (i = 0; i < sp->timecnt; ++i) {
1156 if (sp->ttis[j].tt_isdst) {
1158 -sp->ttis[j].tt_gmtoff;
1163 ** Initially we're assumed to be in standard time.
1166 theiroffset = theirstdoffset;
1168 ** Now juggle transition times and types
1169 ** tracking offsets as you do.
1171 for (i = 0; i < sp->timecnt; ++i) {
1173 sp->types[i] = sp->ttis[j].tt_isdst;
1174 if (sp->ttis[j].tt_ttisgmt) {
1175 /* No adjustment to transition time */
1178 ** If summer time is in effect, and the
1179 ** transition time was not specified as
1180 ** standard time, add the summer time
1181 ** offset to the transition time;
1182 ** otherwise, add the standard time
1183 ** offset to the transition time.
1186 ** Transitions from DST to DDST
1187 ** will effectively disappear since
1188 ** POSIX provides for only one DST
1191 if (isdst && !sp->ttis[j].tt_ttisstd) {
1192 sp->ats[i] += dstoffset -
1195 sp->ats[i] += stdoffset -
1199 theiroffset = -sp->ttis[j].tt_gmtoff;
1200 if (sp->ttis[j].tt_isdst)
1201 theirdstoffset = theiroffset;
1202 else theirstdoffset = theiroffset;
1205 ** Finally, fill in ttis.
1207 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1208 init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1210 sp->defaulttype = 0;
1214 sp->typecnt = 1; /* only standard time */
1216 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1217 sp->defaulttype = 0;
1219 sp->charcnt = charcnt;
1221 memcpy(cp, stdname, stdlen);
1225 memcpy(cp, dstname, dstlen);
1226 *(cp + dstlen) = '\0';
1232 gmtload(struct state *const sp)
1234 if (tzload(gmt, sp, true) != 0)
1235 tzparse(gmt, sp, true);
1238 /* Initialize *SP to a value appropriate for the TZ setting NAME.
1239 Return 0 on success, an errno value on failure. */
1241 zoneinit(struct state *sp, char const *name)
1243 if (name && ! name[0]) {
1245 ** User wants it fast rather than right.
1247 sp->leapcnt = 0; /* so, we're off a little */
1251 sp->goback = sp->goahead = false;
1252 init_ttinfo(&sp->ttis[0], 0, false, 0);
1253 strcpy(sp->chars, gmt);
1254 sp->defaulttype = 0;
1257 int err = tzload(name, sp, true);
1258 if (err != 0 && name && name[0] != ':' && tzparse(name, sp, false))
1267 tzsetlcl(char const *name)
1269 struct state *sp = lclptr;
1270 int lcl = name ? strlen(name) < sizeof lcl_TZname : -1;
1273 : 0 < lcl_is_set && strcmp(lcl_TZname, name) == 0)
1277 lclptr = sp = malloc(sizeof *lclptr);
1278 #endif /* defined ALL_STATE */
1280 if (zoneinit(sp, name) != 0)
1283 strcpy(lcl_TZname, name);
1301 tzset_unlocked(void)
1303 tzsetlcl(getenv("TZ"));
1318 static bool gmt_is_set;
1323 gmtptr = malloc(sizeof *gmtptr);
1335 tzalloc(char const *name)
1337 timezone_t sp = malloc(sizeof *sp);
1339 int err = zoneinit(sp, name);
1350 tzfree(timezone_t sp)
1356 ** NetBSD 6.1.4 has ctime_rz, but omit it because POSIX says ctime and
1357 ** ctime_r are obsolescent and have potential security problems that
1358 ** ctime_rz would share. Callers can instead use localtime_rz + strftime.
1360 ** NetBSD 6.1.4 has tzgetname, but omit it because it doesn't work
1361 ** in zones with three or more time zone abbreviations.
1362 ** Callers can instead use localtime_rz + strftime.
1368 ** The easy way to behave "as if no library function calls" localtime
1369 ** is to not call it, so we drop its guts into "localsub", which can be
1370 ** freely called. (And no, the PANS doesn't require the above behavior,
1371 ** but it *is* desirable.)
1373 ** If successful and SETNAME is nonzero,
1374 ** set the applicable parts of tzname, timezone and altzone;
1375 ** however, it's OK to omit this step if the time zone is POSIX-compatible,
1376 ** since in that case tzset should have already done this step correctly.
1377 ** SETNAME's type is intfast32_t for compatibility with gmtsub,
1378 ** but it is actually a boolean and its value should be 0 or 1.
1383 localsub(struct state const *sp, time_t const *timep, int_fast32_t setname,
1384 struct tm *const tmp)
1386 register const struct ttinfo * ttisp;
1388 register struct tm * result;
1389 const time_t t = *timep;
1392 /* Don't bother to set tzname etc.; tzset has already done it. */
1393 return gmtsub(gmtptr, timep, 0, tmp);
1395 if ((sp->goback && t < sp->ats[0]) ||
1396 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1398 register time_t seconds;
1399 register time_t years;
1402 seconds = sp->ats[0] - t;
1403 else seconds = t - sp->ats[sp->timecnt - 1];
1405 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1406 seconds = years * AVGSECSPERYEAR;
1409 else newt -= seconds;
1410 if (newt < sp->ats[0] ||
1411 newt > sp->ats[sp->timecnt - 1])
1412 return NULL; /* "cannot happen" */
1413 result = localsub(sp, &newt, setname, tmp);
1415 register int_fast64_t newy;
1417 newy = result->tm_year;
1421 if (! (INT_MIN <= newy && newy <= INT_MAX))
1423 result->tm_year = newy;
1427 if (sp->timecnt == 0 || t < sp->ats[0]) {
1428 i = sp->defaulttype;
1430 register int lo = 1;
1431 register int hi = sp->timecnt;
1434 register int mid = (lo + hi) >> 1;
1436 if (t < sp->ats[mid])
1440 i = (int) sp->types[lo - 1];
1442 ttisp = &sp->ttis[i];
1444 ** To get (wrong) behavior that's compatible with System V Release 2.0
1445 ** you'd replace the statement below with
1446 ** t += ttisp->tt_gmtoff;
1447 ** timesub(&t, 0L, sp, tmp);
1449 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1451 result->tm_isdst = ttisp->tt_isdst;
1453 result->TM_ZONE = (char *) &sp->chars[ttisp->tt_abbrind];
1454 #endif /* defined TM_ZONE */
1456 update_tzname_etc(sp, ttisp);
1464 localtime_rz(struct state *sp, time_t const *timep, struct tm *tmp)
1466 return localsub(sp, timep, 0, tmp);
1472 localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
1479 if (setname || !lcl_is_set)
1481 tmp = localsub(lclptr, timep, setname, tmp);
1487 localtime(const time_t *timep)
1489 return localtime_tzset(timep, &tm, true);
1493 localtime_r(const time_t *timep, struct tm *tmp)
1495 return localtime_tzset(timep, tmp, false);
1499 ** gmtsub is to gmtime as localsub is to localtime.
1503 gmtsub(struct state const *sp, time_t const *timep, int_fast32_t offset,
1506 register struct tm * result;
1508 result = timesub(timep, offset, gmtptr, tmp);
1511 ** Could get fancy here and deliver something such as
1512 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
1513 ** but this is no time for a treasure hunt.
1515 tmp->TM_ZONE = ((char *)
1516 (offset ? wildabbr : gmtptr ? gmtptr->chars : gmt));
1517 #endif /* defined TM_ZONE */
1522 * Re-entrant version of gmtime.
1526 gmtime_r(const time_t *timep, struct tm *tmp)
1529 return gmtsub(gmtptr, timep, 0, tmp);
1533 gmtime(const time_t *timep)
1535 return gmtime_r(timep, &tm);
1541 offtime(const time_t *timep, long offset)
1544 return gmtsub(gmtptr, timep, offset, &tm);
1547 #endif /* defined STD_INSPIRED */
1550 ** Return the number of leap years through the end of the given year
1551 ** where, to make the math easy, the answer for year zero is defined as zero.
1554 static int ATTRIBUTE_PURE
1555 leaps_thru_end_of(register const int y)
1557 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1558 -(leaps_thru_end_of(-(y + 1)) + 1);
1562 timesub(const time_t *timep, int_fast32_t offset,
1563 const struct state *sp, struct tm *tmp)
1565 register const struct lsinfo * lp;
1566 register time_t tdays;
1567 register int idays; /* unsigned would be so 2003 */
1568 register int_fast64_t rem;
1570 register const int * ip;
1571 register int_fast64_t corr;
1577 i = (sp == NULL) ? 0 : sp->leapcnt;
1580 if (*timep >= lp->ls_trans) {
1581 if (*timep == lp->ls_trans) {
1582 hit = ((i == 0 && lp->ls_corr > 0) ||
1583 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1586 sp->lsis[i].ls_trans ==
1587 sp->lsis[i - 1].ls_trans + 1 &&
1588 sp->lsis[i].ls_corr ==
1589 sp->lsis[i - 1].ls_corr + 1) {
1599 tdays = *timep / SECSPERDAY;
1600 rem = *timep % SECSPERDAY;
1601 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1603 register time_t tdelta;
1604 register int idelta;
1605 register int leapdays;
1607 tdelta = tdays / DAYSPERLYEAR;
1608 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1609 && tdelta <= INT_MAX))
1613 idelta = (tdays < 0) ? -1 : 1;
1615 if (increment_overflow(&newy, idelta))
1617 leapdays = leaps_thru_end_of(newy - 1) -
1618 leaps_thru_end_of(y - 1);
1619 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1624 ** Given the range, we can now fearlessly cast...
1627 rem += offset - corr;
1632 while (rem >= SECSPERDAY) {
1637 if (increment_overflow(&y, -1))
1639 idays += year_lengths[isleap(y)];
1641 while (idays >= year_lengths[isleap(y)]) {
1642 idays -= year_lengths[isleap(y)];
1643 if (increment_overflow(&y, 1))
1647 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1649 tmp->tm_yday = idays;
1651 ** The "extra" mods below avoid overflow problems.
1653 tmp->tm_wday = EPOCH_WDAY +
1654 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1655 (DAYSPERNYEAR % DAYSPERWEEK) +
1656 leaps_thru_end_of(y - 1) -
1657 leaps_thru_end_of(EPOCH_YEAR - 1) +
1659 tmp->tm_wday %= DAYSPERWEEK;
1660 if (tmp->tm_wday < 0)
1661 tmp->tm_wday += DAYSPERWEEK;
1662 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1664 tmp->tm_min = (int) (rem / SECSPERMIN);
1666 ** A positive leap second requires a special
1667 ** representation. This uses "... ??:59:60" et seq.
1669 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1670 ip = mon_lengths[isleap(y)];
1671 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1672 idays -= ip[tmp->tm_mon];
1673 tmp->tm_mday = (int) (idays + 1);
1676 tmp->TM_GMTOFF = offset;
1677 #endif /* defined TM_GMTOFF */
1686 ctime(const time_t *timep)
1689 ** Section 4.12.3.2 of X3.159-1989 requires that
1690 ** The ctime function converts the calendar time pointed to by timer
1691 ** to local time in the form of a string. It is equivalent to
1692 ** asctime(localtime(timer))
1694 struct tm *tmp = localtime(timep);
1695 return tmp ? asctime(tmp) : NULL;
1699 ctime_r(const time_t *timep, char *buf)
1702 struct tm *tmp = localtime_r(timep, &mytm);
1703 return tmp ? asctime_r(tmp, buf) : NULL;
1707 ** Adapted from code provided by Robert Elz, who writes:
1708 ** The "best" way to do mktime I think is based on an idea of Bob
1709 ** Kridle's (so its said...) from a long time ago.
1710 ** It does a binary search of the time_t space. Since time_t's are
1711 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1712 ** would still be very reasonable).
1717 #endif /* !defined WRONG */
1720 ** Normalize logic courtesy Paul Eggert.
1724 increment_overflow(int *ip, int j)
1726 register int const i = *ip;
1729 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1730 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1731 ** If i < 0 there can only be overflow if i + j < INT_MIN
1732 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1734 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1741 increment_overflow32(int_fast32_t *const lp, int const m)
1743 register int_fast32_t const l = *lp;
1745 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1752 increment_overflow_time(time_t *tp, int_fast32_t j)
1756 ** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
1757 ** except that it does the right thing even if *tp + j would overflow.
1760 ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
1761 : *tp <= time_t_max - j))
1768 normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
1770 register int tensdelta;
1772 tensdelta = (*unitsptr >= 0) ?
1773 (*unitsptr / base) :
1774 (-1 - (-1 - *unitsptr) / base);
1775 *unitsptr -= tensdelta * base;
1776 return increment_overflow(tensptr, tensdelta);
1780 normalize_overflow32(int_fast32_t *tensptr, int *unitsptr, int base)
1782 register int tensdelta;
1784 tensdelta = (*unitsptr >= 0) ?
1785 (*unitsptr / base) :
1786 (-1 - (-1 - *unitsptr) / base);
1787 *unitsptr -= tensdelta * base;
1788 return increment_overflow32(tensptr, tensdelta);
1792 tmcomp(register const struct tm *const atmp,
1793 register const struct tm *const btmp)
1795 register int result;
1797 if (atmp->tm_year != btmp->tm_year)
1798 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1799 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1800 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1801 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1802 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1803 result = atmp->tm_sec - btmp->tm_sec;
1808 time2sub(struct tm *const tmp,
1809 struct tm *(*funcp)(struct state const *, time_t const *,
1810 int_fast32_t, struct tm *),
1811 struct state const *sp,
1812 const int_fast32_t offset,
1818 register int saved_seconds;
1819 register int_fast32_t li;
1825 struct tm yourtm, mytm;
1830 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1834 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1836 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1839 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
1842 ** Turn y into an actual year number for now.
1843 ** It is converted back to an offset from TM_YEAR_BASE later.
1845 if (increment_overflow32(&y, TM_YEAR_BASE))
1847 while (yourtm.tm_mday <= 0) {
1848 if (increment_overflow32(&y, -1))
1850 li = y + (1 < yourtm.tm_mon);
1851 yourtm.tm_mday += year_lengths[isleap(li)];
1853 while (yourtm.tm_mday > DAYSPERLYEAR) {
1854 li = y + (1 < yourtm.tm_mon);
1855 yourtm.tm_mday -= year_lengths[isleap(li)];
1856 if (increment_overflow32(&y, 1))
1860 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1861 if (yourtm.tm_mday <= i)
1863 yourtm.tm_mday -= i;
1864 if (++yourtm.tm_mon >= MONSPERYEAR) {
1866 if (increment_overflow32(&y, 1))
1870 if (increment_overflow32(&y, -TM_YEAR_BASE))
1872 if (! (INT_MIN <= y && y <= INT_MAX))
1875 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1877 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1879 ** We can't set tm_sec to 0, because that might push the
1880 ** time below the minimum representable time.
1881 ** Set tm_sec to 59 instead.
1882 ** This assumes that the minimum representable time is
1883 ** not in the same minute that a leap second was deleted from,
1884 ** which is a safer assumption than using 58 would be.
1886 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1888 saved_seconds = yourtm.tm_sec;
1889 yourtm.tm_sec = SECSPERMIN - 1;
1891 saved_seconds = yourtm.tm_sec;
1895 ** Do a binary search (this works whatever time_t's type is).
1900 t = lo / 2 + hi / 2;
1905 if (! funcp(sp, &t, offset, &mytm)) {
1907 ** Assume that t is too extreme to be represented in
1908 ** a struct tm; arrange things so that it is less
1909 ** extreme on the next pass.
1911 dir = (t > 0) ? 1 : -1;
1912 } else dir = tmcomp(&mytm, &yourtm);
1915 if (t == time_t_max)
1919 } else if (t == hi) {
1920 if (t == time_t_min)
1932 #if defined TM_GMTOFF && ! UNINIT_TRAP
1933 if (mytm.TM_GMTOFF != yourtm.TM_GMTOFF
1934 && (yourtm.TM_GMTOFF < 0
1935 ? (-SECSPERDAY <= yourtm.TM_GMTOFF
1936 && (mytm.TM_GMTOFF <=
1937 (SMALLEST (INT_FAST32_MAX, LONG_MAX)
1938 + yourtm.TM_GMTOFF)))
1939 : (yourtm.TM_GMTOFF <= SECSPERDAY
1940 && ((BIGGEST (INT_FAST32_MIN, LONG_MIN)
1942 <= mytm.TM_GMTOFF)))) {
1943 /* MYTM matches YOURTM except with the wrong UTC offset.
1944 YOURTM.TM_GMTOFF is plausible, so try it instead.
1945 It's OK if YOURTM.TM_GMTOFF contains uninitialized data,
1946 since the guess gets checked. */
1948 int_fast32_t diff = mytm.TM_GMTOFF - yourtm.TM_GMTOFF;
1949 if (!increment_overflow_time(&altt, diff)) {
1951 if (funcp(sp, &altt, offset, &alttm)
1952 && alttm.tm_isdst == mytm.tm_isdst
1953 && alttm.TM_GMTOFF == yourtm.TM_GMTOFF
1954 && tmcomp(&alttm, &yourtm) == 0) {
1961 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1964 ** Right time, wrong type.
1965 ** Hunt for right time, right type.
1966 ** It's okay to guess wrong since the guess
1971 for (i = sp->typecnt - 1; i >= 0; --i) {
1972 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1974 for (j = sp->typecnt - 1; j >= 0; --j) {
1975 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1977 newt = t + sp->ttis[j].tt_gmtoff -
1978 sp->ttis[i].tt_gmtoff;
1979 if (! funcp(sp, &newt, offset, &mytm))
1981 if (tmcomp(&mytm, &yourtm) != 0)
1983 if (mytm.tm_isdst != yourtm.tm_isdst)
1995 newt = t + saved_seconds;
1996 if ((newt < t) != (saved_seconds < 0))
1999 if (funcp(sp, &t, offset, tmp))
2005 time2(struct tm * const tmp,
2006 struct tm *(*funcp)(struct state const *, time_t const *,
2007 int_fast32_t, struct tm *),
2008 struct state const *sp,
2009 const int_fast32_t offset,
2015 ** First try without normalization of seconds
2016 ** (in case tm_sec contains a value associated with a leap second).
2017 ** If that fails, try with normalization of seconds.
2019 t = time2sub(tmp, funcp, sp, offset, okayp, false);
2020 return *okayp ? t : time2sub(tmp, funcp, sp, offset, okayp, true);
2024 time1(struct tm *const tmp,
2025 struct tm *(*funcp) (struct state const *, time_t const *,
2026 int_fast32_t, struct tm *),
2027 struct state const *sp,
2028 const int_fast32_t offset)
2031 register int samei, otheri;
2032 register int sameind, otherind;
2035 char seen[TZ_MAX_TYPES];
2036 unsigned char types[TZ_MAX_TYPES];
2043 if (tmp->tm_isdst > 1)
2045 t = time2(tmp, funcp, sp, offset, &okay);
2048 if (tmp->tm_isdst < 0)
2051 ** POSIX Conformance Test Suite code courtesy Grant Sullivan.
2053 tmp->tm_isdst = 0; /* reset to std and try again */
2056 #endif /* !defined PCTS */
2058 ** We're supposed to assume that somebody took a time of one type
2059 ** and did some math on it that yielded a "struct tm" that's bad.
2060 ** We try to divine the type they started from and adjust to the
2065 for (i = 0; i < sp->typecnt; ++i)
2068 for (i = sp->timecnt - 1; i >= 0; --i)
2069 if (!seen[sp->types[i]]) {
2070 seen[sp->types[i]] = true;
2071 types[nseen++] = sp->types[i];
2073 for (sameind = 0; sameind < nseen; ++sameind) {
2074 samei = types[sameind];
2075 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2077 for (otherind = 0; otherind < nseen; ++otherind) {
2078 otheri = types[otherind];
2079 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2081 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2082 sp->ttis[samei].tt_gmtoff;
2083 tmp->tm_isdst = !tmp->tm_isdst;
2084 t = time2(tmp, funcp, sp, offset, &okay);
2087 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2088 sp->ttis[samei].tt_gmtoff;
2089 tmp->tm_isdst = !tmp->tm_isdst;
2096 mktime_tzname(struct state *sp, struct tm *tmp, bool setname)
2099 return time1(tmp, localsub, sp, setname);
2102 return time1(tmp, gmtsub, gmtptr, 0);
2109 mktime_z(struct state *sp, struct tm *tmp)
2111 return mktime_tzname(sp, tmp, false);
2117 mktime(struct tm *tmp)
2126 t = mktime_tzname(lclptr, tmp, true);
2134 timelocal(struct tm *tmp)
2137 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2142 timegm(struct tm *tmp)
2144 return timeoff(tmp, 0);
2148 timeoff(struct tm *tmp, long offset)
2153 return time1(tmp, gmtsub, gmtptr, offset);
2156 #endif /* defined STD_INSPIRED */
2159 ** XXX--is the below the right way to conditionalize??
2165 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2166 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2167 ** is not the case if we are accounting for leap seconds.
2168 ** So, we provide the following conversion routines for use
2169 ** when exchanging timestamps with POSIX conforming systems.
2173 leapcorr(struct state const *sp, time_t t)
2175 register struct lsinfo const * lp;
2181 if (t >= lp->ls_trans)
2187 NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2188 time2posix_z(struct state *sp, time_t t)
2190 return t - leapcorr(sp, t);
2194 time2posix(time_t t)
2204 t = time2posix_z(lclptr, t);
2209 NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2210 posix2time_z(struct state *sp, time_t t)
2215 ** For a positive leap second hit, the result
2216 ** is not unique. For a negative leap second
2217 ** hit, the corresponding time doesn't exist,
2218 ** so we return an adjacent second.
2220 x = t + leapcorr(sp, t);
2221 y = x - leapcorr(sp, x);
2225 y = x - leapcorr(sp, x);
2231 y = x - leapcorr(sp, x);
2239 posix2time(time_t t)
2249 t = posix2time_z(lclptr, t);
2254 #endif /* defined STD_INSPIRED */
2258 /* Convert from the underlying system's time_t to the ersatz time_tz,
2259 which is called 'time_t' in this file. */
2264 time_t r = sys_time(0);