2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
5 ** $FreeBSD: head/contrib/tzcode/stdtime/localtime.c 226828 2011-10-27 08:44:07Z trociny $
9 ** Leap second handling from Bradley White.
10 ** POSIX-style TZ environment variable handling from Guy Harris.
15 #include "namespace.h"
16 #include <sys/types.h>
22 #include "libc_private.h"
23 #include "un-namespace.h"
26 #include "float.h" /* for FLT_MAX and DBL_MAX */
28 #ifndef TZ_ABBR_MAX_LEN
29 #define TZ_ABBR_MAX_LEN 16
30 #endif /* !defined TZ_ABBR_MAX_LEN */
32 #ifndef TZ_ABBR_CHAR_SET
33 #define TZ_ABBR_CHAR_SET \
34 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
35 #endif /* !defined TZ_ABBR_CHAR_SET */
37 #ifndef TZ_ABBR_ERR_CHAR
38 #define TZ_ABBR_ERR_CHAR '_'
39 #endif /* !defined TZ_ABBR_ERR_CHAR */
41 #include "libc_private.h"
43 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
44 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
46 #define _RWLOCK_RDLOCK(x) \
48 if (__isthreaded) _pthread_rwlock_rdlock(x); \
51 #define _RWLOCK_WRLOCK(x) \
53 if (__isthreaded) _pthread_rwlock_wrlock(x); \
56 #define _RWLOCK_UNLOCK(x) \
58 if (__isthreaded) _pthread_rwlock_unlock(x); \
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 char wildabbr[] = WILDABBR;
86 static const char gmt[] = "UTC";
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 long tt_gmtoff; /* UTC offset in seconds */
101 int tt_isdst; /* used to set tm_isdst */
102 int tt_abbrind; /* abbreviation list index */
103 int tt_ttisstd; /* TRUE if transition is std time */
104 int tt_ttisgmt; /* TRUE if transition is UTC */
107 struct lsinfo { /* leap second information */
108 time_t ls_trans; /* transition time */
109 long ls_corr; /* correction to apply */
112 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
115 #define MY_TZNAME_MAX TZNAME_MAX
116 #endif /* defined TZNAME_MAX */
118 #define MY_TZNAME_MAX 255
119 #endif /* !defined TZNAME_MAX */
128 time_t ats[TZ_MAX_TIMES];
129 unsigned char types[TZ_MAX_TIMES];
130 struct ttinfo ttis[TZ_MAX_TYPES];
131 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
132 (2 * (MY_TZNAME_MAX + 1)))];
133 struct lsinfo lsis[TZ_MAX_LEAPS];
137 int r_type; /* type of rule--see below */
138 int r_day; /* day number of rule */
139 int r_week; /* week number of rule */
140 int r_mon; /* month number of rule */
141 long r_time; /* transition time of rule */
144 #define JULIAN_DAY 0 /* Jn - Julian day */
145 #define DAY_OF_YEAR 1 /* n - day of year */
146 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
149 ** Prototypes for static functions.
152 static long detzcode(const char * codep);
153 static time_t detzcode64(const char * codep);
154 static int differ_by_repeat(time_t t1, time_t t0);
155 static const char * getzname(const char * strp);
156 static const char * getqzname(const char * strp, const int delim);
157 static const char * getnum(const char * strp, int * nump, int min,
159 static const char * getsecs(const char * strp, long * secsp);
160 static const char * getoffset(const char * strp, long * offsetp);
161 static const char * getrule(const char * strp, struct rule * rulep);
162 static void gmtload(struct state * sp);
163 static struct tm * gmtsub(const time_t * timep, long offset,
165 static struct tm * localsub(const time_t * timep, long offset,
167 static int increment_overflow(int * number, int delta);
168 static int leaps_thru_end_of(int y);
169 static int long_increment_overflow(long * number, int delta);
170 static int long_normalize_overflow(long * tensptr,
171 int * unitsptr, int base);
172 static int normalize_overflow(int * tensptr, int * unitsptr,
174 static void settzname(void);
175 static time_t time1(struct tm * tmp,
176 struct tm * (*funcp)(const time_t *,
179 static time_t time2(struct tm *tmp,
180 struct tm * (*funcp)(const time_t *,
182 long offset, int * okayp);
183 static time_t time2sub(struct tm *tmp,
184 struct tm * (*funcp)(const time_t *,
186 long offset, int * okayp, int do_norm_secs);
187 static struct tm * timesub(const time_t * timep, long offset,
188 const struct state * sp, struct tm * tmp);
189 static int tmcomp(const struct tm * atmp,
190 const struct tm * btmp);
191 static time_t transtime(time_t janfirst, int year,
192 const struct rule * rulep, long offset);
193 static int typesequiv(const struct state * sp, int a, int b);
194 static int tzload(const char * name, struct state * sp,
196 static int tzparse(const char * name, struct state * sp,
199 static struct state lclmem;
200 static struct state gmtmem;
201 #define lclptr (&lclmem)
202 #define gmtptr (&gmtmem)
204 #ifndef TZ_STRLEN_MAX
205 #define TZ_STRLEN_MAX 255
206 #endif /* !defined TZ_STRLEN_MAX */
208 static char lcl_TZname[TZ_STRLEN_MAX + 1];
209 static int lcl_is_set;
210 static pthread_once_t gmt_once = PTHREAD_ONCE_INIT;
211 static pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
212 static pthread_once_t gmtime_once = PTHREAD_ONCE_INIT;
213 static pthread_key_t gmtime_key;
214 static int gmtime_key_error;
215 static pthread_once_t localtime_once = PTHREAD_ONCE_INIT;
216 static pthread_key_t localtime_key;
217 static int localtime_key_error;
225 ** Section 4.12.3 of X3.159-1989 requires that
226 ** Except for the strftime function, these functions [asctime,
227 ** ctime, gmtime, localtime] return values in one of two static
228 ** objects: a broken-down time structure and an array of char.
229 ** Thanks to Paul Eggert for noting this.
238 detzcode(const char * const codep)
243 result = (codep[0] & 0x80) ? ~0L : 0;
244 for (i = 0; i < 4; ++i)
245 result = (result << 8) | (codep[i] & 0xff);
250 detzcode64(const char * const codep)
255 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
256 for (i = 0; i < 8; ++i)
257 result = result * 256 + (codep[i] & 0xff);
264 struct state * sp = lclptr;
267 tzname[0] = wildabbr;
268 tzname[1] = wildabbr;
272 ** And to get the latest zone names into tzname. . .
274 for (i = 0; i < sp->typecnt; ++i) {
275 const struct ttinfo * const ttisp = &sp->ttis[sp->types[i]];
277 tzname[ttisp->tt_isdst] =
278 &sp->chars[ttisp->tt_abbrind];
281 if (!ttisp->tt_isdst)
282 timezone = -(ttisp->tt_gmtoff);
285 ** Finally, scrub the abbreviations.
286 ** First, replace bogus characters.
288 for (i = 0; i < sp->charcnt; ++i)
289 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
290 sp->chars[i] = TZ_ABBR_ERR_CHAR;
292 ** Second, truncate long abbreviations.
294 for (i = 0; i < sp->typecnt; ++i) {
295 const struct ttinfo * const ttisp = &sp->ttis[i];
296 char * cp = &sp->chars[ttisp->tt_abbrind];
298 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
299 strcmp(cp, GRANDPARENTED) != 0)
300 *(cp + TZ_ABBR_MAX_LEN) = '\0';
305 differ_by_repeat(const time_t t1, const time_t t0)
307 int_fast64_t _t0 = t0;
308 int_fast64_t _t1 = t1;
310 if (TYPE_INTEGRAL(time_t) &&
311 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
313 //turn ((int_fast64_t)(t1 - t0) == SECSPERREPEAT);
314 return _t1 - _t0 == SECSPERREPEAT;
318 tzload(const char *name, struct state * const sp, const int doextend)
327 struct tzhead tzhead;
328 char buf[2 * sizeof(struct tzhead) +
335 sp->goback = sp->goahead = FALSE;
337 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
338 if (name != NULL && issetugid() != 0)
339 if ((name[0] == ':' && name[1] == '/') ||
340 name[0] == '/' || strchr(name, '.'))
342 if (name == NULL && (name = TZDEFAULT) == NULL)
348 ** Section 4.9.1 of the C standard says that
349 ** "FILENAME_MAX expands to an integral constant expression
350 ** that is the size needed for an array of char large enough
351 ** to hold the longest file name string that the implementation
352 ** guarantees can be opened."
356 fullname = malloc(FILENAME_MAX + 1);
357 if (fullname == NULL)
362 doaccess = name[0] == '/';
364 if ((p = TZDIR) == NULL) {
368 if (strlen(p) + 1 + strlen(name) >= FILENAME_MAX) {
372 (void) strcpy(fullname, p);
373 (void) strcat(fullname, "/");
374 (void) strcat(fullname, name);
376 ** Set doaccess if '.' (as in "../") shows up in name.
378 if (strchr(name, '.') != NULL)
382 if (doaccess && access(name, R_OK) != 0) {
386 if ((fid = _open(name, O_RDONLY)) == -1) {
390 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
397 u = malloc(sizeof(*u));
400 nread = _read(fid, u->buf, sizeof u->buf);
401 if (_close(fid) < 0 || nread <= 0)
403 for (stored = 4; stored <= 8; stored *= 2) {
407 ttisstdcnt = (int) detzcode(u->tzhead.tzh_ttisstdcnt);
408 ttisgmtcnt = (int) detzcode(u->tzhead.tzh_ttisgmtcnt);
409 sp->leapcnt = (int) detzcode(u->tzhead.tzh_leapcnt);
410 sp->timecnt = (int) detzcode(u->tzhead.tzh_timecnt);
411 sp->typecnt = (int) detzcode(u->tzhead.tzh_typecnt);
412 sp->charcnt = (int) detzcode(u->tzhead.tzh_charcnt);
413 p = u->tzhead.tzh_charcnt + sizeof u->tzhead.tzh_charcnt;
414 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
415 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
416 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
417 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
418 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
419 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
421 if (nread - (p - u->buf) <
422 sp->timecnt * stored + /* ats */
423 sp->timecnt + /* types */
424 sp->typecnt * 6 + /* ttinfos */
425 sp->charcnt + /* chars */
426 sp->leapcnt * (stored + 4) + /* lsinfos */
427 ttisstdcnt + /* ttisstds */
428 ttisgmtcnt) /* ttisgmts */
430 for (i = 0; i < sp->timecnt; ++i) {
431 sp->ats[i] = (stored == 4) ?
432 detzcode(p) : detzcode64(p);
435 for (i = 0; i < sp->timecnt; ++i) {
436 sp->types[i] = (unsigned char) *p++;
437 if (sp->types[i] >= sp->typecnt)
440 for (i = 0; i < sp->typecnt; ++i) {
441 struct ttinfo * ttisp;
443 ttisp = &sp->ttis[i];
444 ttisp->tt_gmtoff = detzcode(p);
446 ttisp->tt_isdst = (unsigned char) *p++;
447 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
449 ttisp->tt_abbrind = (unsigned char) *p++;
450 if (ttisp->tt_abbrind < 0 ||
451 ttisp->tt_abbrind > sp->charcnt)
454 for (i = 0; i < sp->charcnt; ++i)
456 sp->chars[i] = '\0'; /* ensure '\0' at end */
457 for (i = 0; i < sp->leapcnt; ++i) {
458 struct lsinfo * lsisp;
460 lsisp = &sp->lsis[i];
461 lsisp->ls_trans = (stored == 4) ?
462 detzcode(p) : detzcode64(p);
464 lsisp->ls_corr = detzcode(p);
467 for (i = 0; i < sp->typecnt; ++i) {
468 struct ttinfo * ttisp;
470 ttisp = &sp->ttis[i];
472 ttisp->tt_ttisstd = FALSE;
474 ttisp->tt_ttisstd = *p++;
475 if (ttisp->tt_ttisstd != TRUE &&
476 ttisp->tt_ttisstd != FALSE)
480 for (i = 0; i < sp->typecnt; ++i) {
481 struct ttinfo * ttisp;
483 ttisp = &sp->ttis[i];
485 ttisp->tt_ttisgmt = FALSE;
487 ttisp->tt_ttisgmt = *p++;
488 if (ttisp->tt_ttisgmt != TRUE &&
489 ttisp->tt_ttisgmt != FALSE)
494 ** Out-of-sort ats should mean we're running on a
495 ** signed time_t system but using a data file with
496 ** unsigned values (or vice versa).
498 for (i = 0; i < sp->timecnt - 2; ++i)
499 if (sp->ats[i] > sp->ats[i + 1]) {
501 if (TYPE_SIGNED(time_t)) {
503 ** Ignore the end (easy).
508 ** Ignore the beginning (harder).
512 for (j = 0; j + i < sp->timecnt; ++j) {
513 sp->ats[j] = sp->ats[j + i];
514 sp->types[j] = sp->types[j + i];
521 ** If this is an old file, we're done.
523 if (u->tzhead.tzh_version[0] == '\0')
526 for (i = 0; i < nread; ++i)
529 ** If this is a narrow integer time_t system, we're done.
531 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
534 if (doextend && nread > 2 &&
535 u->buf[0] == '\n' && u->buf[nread - 1] == '\n' &&
536 sp->typecnt + 2 <= TZ_MAX_TYPES) {
540 ts = malloc(sizeof(*ts));
543 u->buf[nread - 1] = '\0';
544 result = tzparse(&u->buf[1], ts, FALSE);
545 if (result == 0 && ts->typecnt == 2 &&
546 sp->charcnt + ts->charcnt <= TZ_MAX_CHARS) {
547 for (i = 0; i < 2; ++i)
548 ts->ttis[i].tt_abbrind +=
550 for (i = 0; i < ts->charcnt; ++i)
551 sp->chars[sp->charcnt++] =
554 while (i < ts->timecnt &&
556 sp->ats[sp->timecnt - 1])
558 while (i < ts->timecnt &&
559 sp->timecnt < TZ_MAX_TIMES) {
560 sp->ats[sp->timecnt] =
562 sp->types[sp->timecnt] =
568 sp->ttis[sp->typecnt++] = ts->ttis[0];
569 sp->ttis[sp->typecnt++] = ts->ttis[1];
573 if (sp->timecnt > 1) {
574 for (i = 1; i < sp->timecnt; ++i)
575 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
576 differ_by_repeat(sp->ats[i], sp->ats[0])) {
580 for (i = sp->timecnt - 2; i >= 0; --i)
581 if (typesequiv(sp, sp->types[sp->timecnt - 1],
583 differ_by_repeat(sp->ats[sp->timecnt - 1],
596 typesequiv(const struct state * const sp, const int a, const int b)
601 a < 0 || a >= sp->typecnt ||
602 b < 0 || b >= sp->typecnt)
605 const struct ttinfo * ap = &sp->ttis[a];
606 const struct ttinfo * bp = &sp->ttis[b];
607 result = ap->tt_gmtoff == bp->tt_gmtoff &&
608 ap->tt_isdst == bp->tt_isdst &&
609 ap->tt_ttisstd == bp->tt_ttisstd &&
610 ap->tt_ttisgmt == bp->tt_ttisgmt &&
611 strcmp(&sp->chars[ap->tt_abbrind],
612 &sp->chars[bp->tt_abbrind]) == 0;
617 static const int mon_lengths[2][MONSPERYEAR] = {
618 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
619 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
622 static const int year_lengths[2] = {
623 DAYSPERNYEAR, DAYSPERLYEAR
627 ** Given a pointer into a time zone string, scan until a character that is not
628 ** a valid character in a zone name is found. Return a pointer to that
633 getzname(const char *strp)
637 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
644 ** Given a pointer into an extended time zone string, scan until the ending
645 ** delimiter of the zone name is located. Return a pointer to the delimiter.
647 ** As with getzname above, the legal character set is actually quite
648 ** restricted, with other characters producing undefined results.
649 ** We don't do any checking here; checking is done later in common-case code.
653 getqzname(const char *strp, const int delim)
657 while ((c = *strp) != '\0' && c != delim)
663 ** Given a pointer into a time zone string, extract a number from that string.
664 ** Check that the number is within a specified range; if it is not, return
666 ** Otherwise, return a pointer to the first character not part of the number.
670 getnum(const char *strp, int * const nump, const int min, const int max)
675 if (strp == NULL || !is_digit(c = *strp))
679 num = num * 10 + (c - '0');
681 return NULL; /* illegal value */
683 } while (is_digit(c));
685 return NULL; /* illegal value */
691 ** Given a pointer into a time zone string, extract a number of seconds,
692 ** in hh[:mm[:ss]] form, from the string.
693 ** If any error occurs, return NULL.
694 ** Otherwise, return a pointer to the first character not part of the number
699 getsecs(const char *strp, long * const secsp)
704 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
705 ** "M10.4.6/26", which does not conform to Posix,
706 ** but which specifies the equivalent of
707 ** ``02:00 on the first Sunday on or after 23 Oct''.
709 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
712 *secsp = num * (long) SECSPERHOUR;
715 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
718 *secsp += num * SECSPERMIN;
721 /* `SECSPERMIN' allows for leap seconds. */
722 strp = getnum(strp, &num, 0, SECSPERMIN);
732 ** Given a pointer into a time zone string, extract an offset, in
733 ** [+-]hh[:mm[:ss]] form, from the string.
734 ** If any error occurs, return NULL.
735 ** Otherwise, return a pointer to the first character not part of the time.
739 getoffset(const char *strp, long * const offsetp)
746 } else if (*strp == '+')
748 strp = getsecs(strp, offsetp);
750 return NULL; /* illegal time */
752 *offsetp = -*offsetp;
757 ** Given a pointer into a time zone string, extract a rule in the form
758 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
759 ** If a valid rule is not found, return NULL.
760 ** Otherwise, return a pointer to the first character not part of the rule.
764 getrule(const char *strp, struct rule * const rulep)
770 rulep->r_type = JULIAN_DAY;
772 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
773 } else if (*strp == 'M') {
777 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
779 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
784 strp = getnum(strp, &rulep->r_week, 1, 5);
789 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
790 } else if (is_digit(*strp)) {
794 rulep->r_type = DAY_OF_YEAR;
795 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
796 } else return NULL; /* invalid format */
804 strp = getsecs(strp, &rulep->r_time);
805 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
810 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
811 ** year, a rule, and the offset from UTC at the time that rule takes effect,
812 ** calculate the Epoch-relative time that rule takes effect.
816 transtime(const time_t janfirst, const int year,
817 const struct rule * const rulep, const long offset)
822 int d, m1, yy0, yy1, yy2, dow;
825 leapyear = isleap(year);
826 switch (rulep->r_type) {
830 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
832 ** In non-leap years, or if the day number is 59 or less, just
833 ** add SECSPERDAY times the day number-1 to the time of
834 ** January 1, midnight, to get the day.
836 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
837 if (leapyear && rulep->r_day >= 60)
844 ** Just add SECSPERDAY times the day number to the time of
845 ** January 1, midnight, to get the day.
847 value = janfirst + rulep->r_day * SECSPERDAY;
850 case MONTH_NTH_DAY_OF_WEEK:
852 ** Mm.n.d - nth "dth day" of month m.
855 for (i = 0; i < rulep->r_mon - 1; ++i)
856 value += mon_lengths[leapyear][i] * SECSPERDAY;
859 ** Use Zeller's Congruence to get day-of-week of first day of
862 m1 = (rulep->r_mon + 9) % 12 + 1;
863 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
866 dow = ((26 * m1 - 2) / 10 +
867 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
872 ** "dow" is the day-of-week of the first day of the month. Get
873 ** the day-of-month (zero-origin) of the first "dow" day of the
876 d = rulep->r_day - dow;
879 for (i = 1; i < rulep->r_week; ++i) {
880 if (d + DAYSPERWEEK >=
881 mon_lengths[leapyear][rulep->r_mon - 1])
887 ** "d" is the day-of-month (zero-origin) of the day we want.
889 value += d * SECSPERDAY;
894 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
895 ** question. To get the Epoch-relative time of the specified local
896 ** time on that day, add the transition time and the current offset
899 return value + rulep->r_time + offset;
903 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
908 tzparse(const char *name, struct state * const sp, const int lastditch)
910 const char * stdname;
911 const char * dstname;
917 unsigned char * typep;
924 stdlen = strlen(name); /* length of standard zone name */
926 if (stdlen >= sizeof sp->chars)
927 stdlen = (sizeof sp->chars) - 1;
933 name = getqzname(name, '>');
936 stdlen = name - stdname;
939 name = getzname(name);
940 stdlen = name - stdname;
943 return -1; /* was "stdoffset = 0;" */
945 name = getoffset(name, &stdoffset);
950 load_result = tzload(TZDEFRULES, sp, FALSE);
951 if (load_result != 0)
952 sp->leapcnt = 0; /* so, we're off a little */
956 name = getqzname(name, '>');
959 dstlen = name - dstname;
963 name = getzname(name);
964 dstlen = name - dstname; /* length of DST zone name */
966 if (*name != '\0' && *name != ',' && *name != ';') {
967 name = getoffset(name, &dstoffset);
970 } else dstoffset = stdoffset - SECSPERHOUR;
971 if (*name == '\0' && load_result != 0)
972 name = TZDEFRULESTRING;
973 if (*name == ',' || *name == ';') {
982 if ((name = getrule(name, &start)) == NULL)
986 if ((name = getrule(name, &end)) == NULL)
990 sp->typecnt = 2; /* standard time and DST */
992 ** Two transitions per year, from EPOCH_YEAR forward.
994 sp->ttis[0].tt_gmtoff = -dstoffset;
995 sp->ttis[0].tt_isdst = 1;
996 sp->ttis[0].tt_abbrind = stdlen + 1;
997 sp->ttis[1].tt_gmtoff = -stdoffset;
998 sp->ttis[1].tt_isdst = 0;
999 sp->ttis[1].tt_abbrind = 0;
1004 for (year = EPOCH_YEAR;
1005 sp->timecnt + 2 <= TZ_MAX_TIMES;
1009 starttime = transtime(janfirst, year, &start,
1011 endtime = transtime(janfirst, year, &end,
1013 if (starttime > endtime) {
1015 *typep++ = 1; /* DST ends */
1017 *typep++ = 0; /* DST begins */
1020 *typep++ = 0; /* DST begins */
1022 *typep++ = 1; /* DST ends */
1025 newfirst = janfirst;
1026 newfirst += year_lengths[isleap(year)] *
1028 if (newfirst <= janfirst)
1030 janfirst = newfirst;
1033 long theirstdoffset;
1034 long theirdstoffset;
1043 ** Initial values of theirstdoffset and theirdstoffset.
1046 for (i = 0; i < sp->timecnt; ++i) {
1048 if (!sp->ttis[j].tt_isdst) {
1050 -sp->ttis[j].tt_gmtoff;
1055 for (i = 0; i < sp->timecnt; ++i) {
1057 if (sp->ttis[j].tt_isdst) {
1059 -sp->ttis[j].tt_gmtoff;
1064 ** Initially we're assumed to be in standard time.
1067 theiroffset = theirstdoffset;
1069 ** Now juggle transition times and types
1070 ** tracking offsets as you do.
1072 for (i = 0; i < sp->timecnt; ++i) {
1074 sp->types[i] = sp->ttis[j].tt_isdst;
1075 if (sp->ttis[j].tt_ttisgmt) {
1076 /* No adjustment to transition time */
1079 ** If summer time is in effect, and the
1080 ** transition time was not specified as
1081 ** standard time, add the summer time
1082 ** offset to the transition time;
1083 ** otherwise, add the standard time
1084 ** offset to the transition time.
1087 ** Transitions from DST to DDST
1088 ** will effectively disappear since
1089 ** POSIX provides for only one DST
1092 if (isdst && !sp->ttis[j].tt_ttisstd) {
1093 sp->ats[i] += dstoffset -
1096 sp->ats[i] += stdoffset -
1100 theiroffset = -sp->ttis[j].tt_gmtoff;
1101 if (sp->ttis[j].tt_isdst)
1102 theirdstoffset = theiroffset;
1103 else theirstdoffset = theiroffset;
1106 ** Finally, fill in ttis.
1107 ** ttisstd and ttisgmt need not be handled.
1109 sp->ttis[0].tt_gmtoff = -stdoffset;
1110 sp->ttis[0].tt_isdst = FALSE;
1111 sp->ttis[0].tt_abbrind = 0;
1112 sp->ttis[1].tt_gmtoff = -dstoffset;
1113 sp->ttis[1].tt_isdst = TRUE;
1114 sp->ttis[1].tt_abbrind = stdlen + 1;
1119 sp->typecnt = 1; /* only standard time */
1121 sp->ttis[0].tt_gmtoff = -stdoffset;
1122 sp->ttis[0].tt_isdst = 0;
1123 sp->ttis[0].tt_abbrind = 0;
1125 sp->charcnt = stdlen + 1;
1127 sp->charcnt += dstlen + 1;
1128 if ((size_t) sp->charcnt > sizeof sp->chars)
1131 (void) strncpy(cp, stdname, stdlen);
1135 (void) strncpy(cp, dstname, dstlen);
1136 *(cp + dstlen) = '\0';
1142 gmtload(struct state * const sp)
1144 if (tzload(gmt, sp, TRUE) != 0)
1145 (void) tzparse(gmt, sp, TRUE);
1149 tzsetwall_basic(int rdlocked)
1152 _RWLOCK_RDLOCK(&lcl_rwlock);
1153 if (lcl_is_set < 0) {
1155 _RWLOCK_UNLOCK(&lcl_rwlock);
1158 _RWLOCK_UNLOCK(&lcl_rwlock);
1160 _RWLOCK_WRLOCK(&lcl_rwlock);
1163 if (tzload((char *) NULL, lclptr, TRUE) != 0)
1166 _RWLOCK_UNLOCK(&lcl_rwlock);
1169 _RWLOCK_RDLOCK(&lcl_rwlock);
1179 tzset_basic(int rdlocked)
1183 name = getenv("TZ");
1185 tzsetwall_basic(rdlocked);
1190 _RWLOCK_RDLOCK(&lcl_rwlock);
1191 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
1193 _RWLOCK_UNLOCK(&lcl_rwlock);
1196 _RWLOCK_UNLOCK(&lcl_rwlock);
1198 _RWLOCK_WRLOCK(&lcl_rwlock);
1199 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1201 (void) strcpy(lcl_TZname, name);
1203 if (*name == '\0') {
1205 ** User wants it fast rather than right.
1207 lclptr->leapcnt = 0; /* so, we're off a little */
1208 lclptr->timecnt = 0;
1209 lclptr->typecnt = 0;
1210 lclptr->ttis[0].tt_isdst = 0;
1211 lclptr->ttis[0].tt_gmtoff = 0;
1212 lclptr->ttis[0].tt_abbrind = 0;
1213 (void) strcpy(lclptr->chars, gmt);
1214 } else if (tzload(name, lclptr, TRUE) != 0)
1215 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1216 (void) gmtload(lclptr);
1218 _RWLOCK_UNLOCK(&lcl_rwlock);
1221 _RWLOCK_RDLOCK(&lcl_rwlock);
1231 ** The easy way to behave "as if no library function calls" localtime
1232 ** is to not call it--so we drop its guts into "localsub", which can be
1233 ** freely called. (And no, the PANS doesn't require the above behavior--
1234 ** but it *is* desirable.)
1236 ** The unused offset argument is for the benefit of mktime variants.
1241 localsub(const time_t * const timep, const long offset, struct tm * const tmp)
1244 const struct ttinfo * ttisp;
1247 const time_t t = *timep;
1250 if ((sp->goback && t < sp->ats[0]) ||
1251 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1255 int_fast64_t icycles;
1258 seconds = sp->ats[0] - t;
1259 else seconds = t - sp->ats[sp->timecnt - 1];
1261 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1264 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1267 seconds *= YEARSPERREPEAT;
1268 seconds *= AVGSECSPERYEAR;
1271 else newt -= seconds;
1272 if (newt < sp->ats[0] ||
1273 newt > sp->ats[sp->timecnt - 1])
1274 return NULL; /* "cannot happen" */
1275 result = localsub(&newt, offset, tmp);
1276 if (result == tmp) {
1279 newy = tmp->tm_year;
1281 newy -= icycles * YEARSPERREPEAT;
1282 else newy += icycles * YEARSPERREPEAT;
1283 tmp->tm_year = newy;
1284 if (tmp->tm_year != newy)
1289 if (sp->timecnt == 0 || t < sp->ats[0]) {
1291 while (sp->ttis[i].tt_isdst)
1292 if (++i >= sp->typecnt) {
1298 int hi = sp->timecnt;
1301 int mid = (lo + hi) >> 1;
1303 if (t < sp->ats[mid])
1307 i = (int) sp->types[lo - 1];
1309 ttisp = &sp->ttis[i];
1311 ** To get (wrong) behavior that's compatible with System V Release 2.0
1312 ** you'd replace the statement below with
1313 ** t += ttisp->tt_gmtoff;
1314 ** timesub(&t, 0L, sp, tmp);
1316 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1317 tmp->tm_isdst = ttisp->tt_isdst;
1318 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1320 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1321 #endif /* defined TM_ZONE */
1326 localtime_key_init(void)
1329 localtime_key_error = _pthread_key_create(&localtime_key, free);
1333 localtime(const time_t * const timep)
1337 if (__isthreaded != 0) {
1338 _once(&localtime_once, localtime_key_init);
1339 if (localtime_key_error != 0) {
1340 errno = localtime_key_error;
1343 p_tm = _pthread_getspecific(localtime_key);
1345 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1348 _pthread_setspecific(localtime_key, p_tm);
1350 _RWLOCK_RDLOCK(&lcl_rwlock);
1352 localsub(timep, 0L, p_tm);
1353 _RWLOCK_UNLOCK(&lcl_rwlock);
1357 localsub(timep, 0L, &tm);
1363 ** Re-entrant version of localtime.
1367 localtime_r(const time_t * const timep, struct tm *tmp)
1369 _RWLOCK_RDLOCK(&lcl_rwlock);
1371 localsub(timep, 0L, tmp);
1372 _RWLOCK_UNLOCK(&lcl_rwlock);
1383 ** gmtsub is to gmtime as localsub is to localtime.
1387 gmtsub(const time_t * const timep, const long offset, struct tm * const tmp)
1391 _once(&gmt_once, gmt_init);
1392 result = timesub(timep, offset, gmtptr, tmp);
1395 ** Could get fancy here and deliver something such as
1396 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1397 ** but this is no time for a treasure hunt.
1400 tmp->TM_ZONE = wildabbr;
1402 tmp->TM_ZONE = gmtptr->chars;
1404 #endif /* defined TM_ZONE */
1409 gmtime_key_init(void)
1412 gmtime_key_error = _pthread_key_create(&gmtime_key, free);
1416 gmtime(const time_t * const timep)
1420 if (__isthreaded != 0) {
1421 _once(&gmtime_once, gmtime_key_init);
1422 if (gmtime_key_error != 0) {
1423 errno = gmtime_key_error;
1427 * Changed to follow POSIX.1 threads standard, which
1428 * is what BSD currently has.
1430 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1431 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1435 _pthread_setspecific(gmtime_key, p_tm);
1437 gmtsub(timep, 0L, p_tm);
1441 gmtsub(timep, 0L, &tm);
1447 * Re-entrant version of gmtime.
1451 gmtime_r(const time_t * const timep, struct tm *tmp)
1453 return gmtsub(timep, 0L, tmp);
1459 offtime(const time_t * const timep, const long offset)
1461 return gmtsub(timep, offset, &tm);
1464 #endif /* defined STD_INSPIRED */
1467 ** Return the number of leap years through the end of the given year
1468 ** where, to make the math easy, the answer for year zero is defined as zero.
1472 leaps_thru_end_of(const int y)
1474 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1475 -(leaps_thru_end_of(-(y + 1)) + 1);
1479 timesub(const time_t * const timep, const long offset,
1480 const struct state * const sp, struct tm * const tmp)
1482 const struct lsinfo * lp;
1484 int idays; /* unsigned would be so 2003 */
1497 if (*timep >= lp->ls_trans) {
1498 if (*timep == lp->ls_trans) {
1499 hit = ((i == 0 && lp->ls_corr > 0) ||
1500 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1503 sp->lsis[i].ls_trans ==
1504 sp->lsis[i - 1].ls_trans + 1 &&
1505 sp->lsis[i].ls_corr ==
1506 sp->lsis[i - 1].ls_corr + 1) {
1516 tdays = *timep / SECSPERDAY;
1517 rem = *timep - tdays * SECSPERDAY;
1518 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1524 tdelta = tdays / DAYSPERLYEAR;
1526 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1529 idelta = (tdays < 0) ? -1 : 1;
1531 if (increment_overflow(&newy, idelta))
1533 leapdays = leaps_thru_end_of(newy - 1) -
1534 leaps_thru_end_of(y - 1);
1535 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1542 seconds = tdays * SECSPERDAY + 0.5;
1543 tdays = seconds / SECSPERDAY;
1544 rem += seconds - tdays * SECSPERDAY;
1547 ** Given the range, we can now fearlessly cast...
1550 rem += offset - corr;
1555 while (rem >= SECSPERDAY) {
1560 if (increment_overflow(&y, -1))
1562 idays += year_lengths[isleap(y)];
1564 while (idays >= year_lengths[isleap(y)]) {
1565 idays -= year_lengths[isleap(y)];
1566 if (increment_overflow(&y, 1))
1570 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1572 tmp->tm_yday = idays;
1574 ** The "extra" mods below avoid overflow problems.
1576 tmp->tm_wday = EPOCH_WDAY +
1577 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1578 (DAYSPERNYEAR % DAYSPERWEEK) +
1579 leaps_thru_end_of(y - 1) -
1580 leaps_thru_end_of(EPOCH_YEAR - 1) +
1582 tmp->tm_wday %= DAYSPERWEEK;
1583 if (tmp->tm_wday < 0)
1584 tmp->tm_wday += DAYSPERWEEK;
1585 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1587 tmp->tm_min = (int) (rem / SECSPERMIN);
1589 ** A positive leap second requires a special
1590 ** representation. This uses "... ??:59:60" et seq.
1592 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1593 ip = mon_lengths[isleap(y)];
1594 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1595 idays -= ip[tmp->tm_mon];
1596 tmp->tm_mday = (int) (idays + 1);
1599 tmp->TM_GMTOFF = offset;
1600 #endif /* defined TM_GMTOFF */
1605 ctime(const time_t * const timep)
1608 ** Section 4.12.3.2 of X3.159-1989 requires that
1609 ** The ctime function converts the calendar time pointed to by timer
1610 ** to local time in the form of a string. It is equivalent to
1611 ** asctime(localtime(timer))
1613 return asctime(localtime(timep));
1617 ctime_r(const time_t * const timep, char *buf)
1621 return asctime_r(localtime_r(timep, &mytm), buf);
1625 ** Adapted from code provided by Robert Elz, who writes:
1626 ** The "best" way to do mktime I think is based on an idea of Bob
1627 ** Kridle's (so its said...) from a long time ago.
1628 ** It does a binary search of the time_t space. Since time_t's are
1629 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1630 ** would still be very reasonable).
1635 #endif /* !defined WRONG */
1638 ** Simplified normalize logic courtesy Paul Eggert.
1642 increment_overflow(int *number, int delta)
1648 return (*number < number0) != (delta < 0);
1652 long_increment_overflow(long *number, int delta)
1658 return (*number < number0) != (delta < 0);
1662 normalize_overflow(int * const tensptr, int * const unitsptr, const int base)
1666 tensdelta = (*unitsptr >= 0) ?
1667 (*unitsptr / base) :
1668 (-1 - (-1 - *unitsptr) / base);
1669 *unitsptr -= tensdelta * base;
1670 return increment_overflow(tensptr, tensdelta);
1674 long_normalize_overflow(long * const tensptr, int * const unitsptr,
1679 tensdelta = (*unitsptr >= 0) ?
1680 (*unitsptr / base) :
1681 (-1 - (-1 - *unitsptr) / base);
1682 *unitsptr -= tensdelta * base;
1683 return long_increment_overflow(tensptr, tensdelta);
1687 tmcomp(const struct tm * const atmp, const struct tm * const btmp)
1691 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1692 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1693 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1694 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1695 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1696 result = atmp->tm_sec - btmp->tm_sec;
1701 time2sub(struct tm * const tmp,
1702 struct tm * (* const funcp)(const time_t *, long, struct tm *),
1703 const long offset, int * const okayp, const int do_norm_secs)
1705 const struct state * sp;
1715 struct tm yourtm, mytm;
1720 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1724 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1726 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1729 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
1732 ** Turn y into an actual year number for now.
1733 ** It is converted back to an offset from TM_YEAR_BASE later.
1735 if (long_increment_overflow(&y, TM_YEAR_BASE))
1737 while (yourtm.tm_mday <= 0) {
1738 if (long_increment_overflow(&y, -1))
1740 li = y + (1 < yourtm.tm_mon);
1741 yourtm.tm_mday += year_lengths[isleap(li)];
1743 while (yourtm.tm_mday > DAYSPERLYEAR) {
1744 li = y + (1 < yourtm.tm_mon);
1745 yourtm.tm_mday -= year_lengths[isleap(li)];
1746 if (long_increment_overflow(&y, 1))
1750 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1751 if (yourtm.tm_mday <= i)
1753 yourtm.tm_mday -= i;
1754 if (++yourtm.tm_mon >= MONSPERYEAR) {
1756 if (long_increment_overflow(&y, 1))
1760 if (long_increment_overflow(&y, -TM_YEAR_BASE))
1763 if (yourtm.tm_year != y)
1765 /* Don't go below 1900 for POLA */
1766 if (yourtm.tm_year < 0)
1768 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1770 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1772 ** We can't set tm_sec to 0, because that might push the
1773 ** time below the minimum representable time.
1774 ** Set tm_sec to 59 instead.
1775 ** This assumes that the minimum representable time is
1776 ** not in the same minute that a leap second was deleted from,
1777 ** which is a safer assumption than using 58 would be.
1779 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1781 saved_seconds = yourtm.tm_sec;
1782 yourtm.tm_sec = SECSPERMIN - 1;
1784 saved_seconds = yourtm.tm_sec;
1788 ** Do a binary search (this works whatever time_t's type is).
1790 if (!TYPE_SIGNED(time_t)) {
1793 } else if (!TYPE_INTEGRAL(time_t)) {
1794 if (sizeof(time_t) > sizeof(float))
1795 hi = (time_t) DBL_MAX;
1796 else hi = (time_t) FLT_MAX;
1800 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1805 t = lo / 2 + hi / 2;
1810 if ((*funcp)(&t, offset, &mytm) == NULL) {
1812 ** Assume that t is too extreme to be represented in
1813 ** a struct tm; arrange things so that it is less
1814 ** extreme on the next pass.
1816 dir = (t > 0) ? 1 : -1;
1817 } else dir = tmcomp(&mytm, &yourtm);
1824 } else if (t == hi) {
1837 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1840 ** Right time, wrong type.
1841 ** Hunt for right time, right type.
1842 ** It's okay to guess wrong since the guess
1845 sp = (const struct state *)
1846 ((funcp == localsub) ? lclptr : gmtptr);
1847 for (i = sp->typecnt - 1; i >= 0; --i) {
1848 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1850 for (j = sp->typecnt - 1; j >= 0; --j) {
1851 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1853 newt = t + sp->ttis[j].tt_gmtoff -
1854 sp->ttis[i].tt_gmtoff;
1855 if ((*funcp)(&newt, offset, &mytm) == NULL)
1857 if (tmcomp(&mytm, &yourtm) != 0)
1859 if (mytm.tm_isdst != yourtm.tm_isdst)
1871 newt = t + saved_seconds;
1872 if ((newt < t) != (saved_seconds < 0))
1875 if ((*funcp)(&t, offset, tmp))
1881 time2(struct tm * const tmp,
1882 struct tm * (* const funcp)(const time_t *, long, struct tm *),
1883 const long offset, int * const okayp)
1888 ** First try without normalization of seconds
1889 ** (in case tm_sec contains a value associated with a leap second).
1890 ** If that fails, try with normalization of seconds.
1892 t = time2sub(tmp, funcp, offset, okayp, FALSE);
1893 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1897 time1(struct tm *tmp,
1898 struct tm * (* const funcp)(const time_t *, long, struct tm *),
1902 const struct state * sp;
1904 int sameind, otherind;
1907 int seen[TZ_MAX_TYPES];
1908 int types[TZ_MAX_TYPES];
1916 if (tmp->tm_isdst > 1)
1918 t = time2(tmp, funcp, offset, &okay);
1920 ** PCTS code courtesy Grant Sullivan.
1924 if (tmp->tm_isdst < 0)
1925 tmp->tm_isdst = 0; /* reset to std and try again */
1927 ** We're supposed to assume that somebody took a time of one type
1928 ** and did some math on it that yielded a "struct tm" that's bad.
1929 ** We try to divine the type they started from and adjust to the
1932 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
1933 for (i = 0; i < sp->typecnt; ++i)
1936 for (i = sp->timecnt - 1; i >= 0; --i)
1937 if (!seen[sp->types[i]]) {
1938 seen[sp->types[i]] = TRUE;
1939 types[nseen++] = sp->types[i];
1941 for (sameind = 0; sameind < nseen; ++sameind) {
1942 samei = types[sameind];
1943 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1945 for (otherind = 0; otherind < nseen; ++otherind) {
1946 otheri = types[otherind];
1947 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1949 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1950 sp->ttis[samei].tt_gmtoff;
1951 tmp->tm_isdst = !tmp->tm_isdst;
1952 t = time2(tmp, funcp, offset, &okay);
1955 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1956 sp->ttis[samei].tt_gmtoff;
1957 tmp->tm_isdst = !tmp->tm_isdst;
1964 mktime(struct tm * const tmp)
1966 time_t mktime_return_value;
1967 _RWLOCK_RDLOCK(&lcl_rwlock);
1969 mktime_return_value = time1(tmp, localsub, 0L);
1970 _RWLOCK_UNLOCK(&lcl_rwlock);
1971 return(mktime_return_value);
1977 timelocal(struct tm * const tmp)
1980 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1985 timegm(struct tm * const tmp)
1989 return time1(tmp, gmtsub, 0L);
1993 timeoff(struct tm * const tmp, const long offset)
1997 return time1(tmp, gmtsub, offset);
2000 #endif /* defined STD_INSPIRED */
2003 ** XXX--is the below the right way to conditionalize??
2009 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2010 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2011 ** is not the case if we are accounting for leap seconds.
2012 ** So, we provide the following conversion routines for use
2013 ** when exchanging timestamps with POSIX conforming systems.
2017 leapcorr(time_t *timep)
2027 if (*timep >= lp->ls_trans)
2034 time2posix(time_t t)
2037 return t - leapcorr(&t);
2041 posix2time(time_t t)
2048 ** For a positive leap second hit, the result
2049 ** is not unique. For a negative leap second
2050 ** hit, the corresponding time doesn't exist,
2051 ** so we return an adjacent second.
2053 x = t + leapcorr(&t);
2054 y = x - leapcorr(&x);
2058 y = x - leapcorr(&x);
2065 y = x - leapcorr(&x);
2073 #endif /* defined STD_INSPIRED */