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); \
62 ** SunOS 4.1.1 headers lack O_BINARY.
66 #define OPEN_MODE (O_RDONLY | O_BINARY)
67 #endif /* defined O_BINARY */
69 #define OPEN_MODE O_RDONLY
70 #endif /* !defined O_BINARY */
74 ** Someone might make incorrect use of a time zone abbreviation:
75 ** 1. They might reference tzname[0] before calling tzset (explicitly
77 ** 2. They might reference tzname[1] before calling tzset (explicitly
79 ** 3. They might reference tzname[1] after setting to a time zone
80 ** in which Daylight Saving Time is never observed.
81 ** 4. They might reference tzname[0] after setting to a time zone
82 ** in which Standard Time is never observed.
83 ** 5. They might reference tm.TM_ZONE after calling offtime.
84 ** What's best to do in the above cases is open to debate;
85 ** for now, we just set things up so that in any of the five cases
86 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
87 ** string "tzname[0] used before set", and similarly for the other cases.
88 ** And another: initialize tzname[0] to "ERA", with an explanation in the
89 ** manual page of what this "time zone abbreviation" means (doing this so
90 ** that tzname[0] has the "normal" length of three characters).
93 #endif /* !defined WILDABBR */
95 static char wildabbr[] = WILDABBR;
98 * In June 2004 it was decided UTC was a more appropriate default time
102 static const char gmt[] = "UTC";
105 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
106 ** We default to US rules as of 1999-08-17.
107 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
108 ** implementation dependent; for historical reasons, US rules are a
111 #ifndef TZDEFRULESTRING
112 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
113 #endif /* !defined TZDEFDST */
115 struct ttinfo { /* time type information */
116 long tt_gmtoff; /* UTC offset in seconds */
117 int tt_isdst; /* used to set tm_isdst */
118 int tt_abbrind; /* abbreviation list index */
119 int tt_ttisstd; /* TRUE if transition is std time */
120 int tt_ttisgmt; /* TRUE if transition is UTC */
123 struct lsinfo { /* leap second information */
124 time_t ls_trans; /* transition time */
125 long ls_corr; /* correction to apply */
128 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
131 #define MY_TZNAME_MAX TZNAME_MAX
132 #endif /* defined TZNAME_MAX */
134 #define MY_TZNAME_MAX 255
135 #endif /* !defined TZNAME_MAX */
144 time_t ats[TZ_MAX_TIMES];
145 unsigned char types[TZ_MAX_TIMES];
146 struct ttinfo ttis[TZ_MAX_TYPES];
147 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
148 (2 * (MY_TZNAME_MAX + 1)))];
149 struct lsinfo lsis[TZ_MAX_LEAPS];
153 int r_type; /* type of rule--see below */
154 int r_day; /* day number of rule */
155 int r_week; /* week number of rule */
156 int r_mon; /* month number of rule */
157 long r_time; /* transition time of rule */
160 #define JULIAN_DAY 0 /* Jn - Julian day */
161 #define DAY_OF_YEAR 1 /* n - day of year */
162 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
165 ** Prototypes for static functions.
168 static long detzcode(const char * codep);
169 static time_t detzcode64(const char * codep);
170 static int differ_by_repeat(time_t t1, time_t t0);
171 static const char * getzname(const char * strp);
172 static const char * getqzname(const char * strp, const int delim);
173 static const char * getnum(const char * strp, int * nump, int min,
175 static const char * getsecs(const char * strp, long * secsp);
176 static const char * getoffset(const char * strp, long * offsetp);
177 static const char * getrule(const char * strp, struct rule * rulep);
178 static void gmtload(struct state * sp);
179 static struct tm * gmtsub(const time_t * timep, long offset,
181 static struct tm * localsub(const time_t * timep, long offset,
183 static int increment_overflow(int * number, int delta);
184 static int leaps_thru_end_of(int y);
185 static int long_increment_overflow(long * number, int delta);
186 static int long_normalize_overflow(long * tensptr,
187 int * unitsptr, int base);
188 static int normalize_overflow(int * tensptr, int * unitsptr,
190 static void settzname(void);
191 static time_t time1(struct tm * tmp,
192 struct tm * (*funcp)(const time_t *,
195 static time_t time2(struct tm *tmp,
196 struct tm * (*funcp)(const time_t *,
198 long offset, int * okayp);
199 static time_t time2sub(struct tm *tmp,
200 struct tm * (*funcp)(const time_t *,
202 long offset, int * okayp, int do_norm_secs);
203 static struct tm * timesub(const time_t * timep, long offset,
204 const struct state * sp, struct tm * tmp);
205 static int tmcomp(const struct tm * atmp,
206 const struct tm * btmp);
207 static time_t transtime(time_t janfirst, int year,
208 const struct rule * rulep, long offset);
209 static int typesequiv(const struct state * sp, int a, int b);
210 static int tzload(const char * name, struct state * sp,
212 static int tzparse(const char * name, struct state * sp,
216 static struct state * lclptr;
217 static struct state * gmtptr;
218 #endif /* defined ALL_STATE */
221 static struct state lclmem;
222 static struct state gmtmem;
223 #define lclptr (&lclmem)
224 #define gmtptr (&gmtmem)
225 #endif /* State Farm */
227 #ifndef TZ_STRLEN_MAX
228 #define TZ_STRLEN_MAX 255
229 #endif /* !defined TZ_STRLEN_MAX */
231 static char lcl_TZname[TZ_STRLEN_MAX + 1];
232 static int lcl_is_set;
233 static pthread_once_t gmt_once = PTHREAD_ONCE_INIT;
234 static pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
235 static pthread_once_t gmtime_once = PTHREAD_ONCE_INIT;
236 static pthread_key_t gmtime_key;
237 static int gmtime_key_error;
238 static pthread_once_t localtime_once = PTHREAD_ONCE_INIT;
239 static pthread_key_t localtime_key;
240 static int localtime_key_error;
248 ** Section 4.12.3 of X3.159-1989 requires that
249 ** Except for the strftime function, these functions [asctime,
250 ** ctime, gmtime, localtime] return values in one of two static
251 ** objects: a broken-down time structure and an array of char.
252 ** Thanks to Paul Eggert for noting this.
262 #endif /* defined ALTZONE */
266 const char * const codep;
271 result = (codep[0] & 0x80) ? ~0L : 0;
272 for (i = 0; i < 4; ++i)
273 result = (result << 8) | (codep[i] & 0xff);
279 const char * const codep;
281 register time_t result;
284 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
285 for (i = 0; i < 8; ++i)
286 result = result * 256 + (codep[i] & 0xff);
293 struct state * sp = lclptr;
296 tzname[0] = wildabbr;
297 tzname[1] = wildabbr;
302 #endif /* defined ALTZONE */
305 tzname[0] = tzname[1] = gmt;
308 #endif /* defined ALL_STATE */
310 ** And to get the latest zone names into tzname. . .
312 for (i = 0; i < sp->typecnt; ++i) {
313 const struct ttinfo * const ttisp = &sp->ttis[sp->types[i]];
315 tzname[ttisp->tt_isdst] =
316 &sp->chars[ttisp->tt_abbrind];
319 if (!ttisp->tt_isdst)
320 timezone = -(ttisp->tt_gmtoff);
323 altzone = -(ttisp->tt_gmtoff);
324 #endif /* defined ALTZONE */
327 ** Finally, scrub the abbreviations.
328 ** First, replace bogus characters.
330 for (i = 0; i < sp->charcnt; ++i)
331 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
332 sp->chars[i] = TZ_ABBR_ERR_CHAR;
334 ** Second, truncate long abbreviations.
336 for (i = 0; i < sp->typecnt; ++i) {
337 register const struct ttinfo * const ttisp = &sp->ttis[i];
338 register char * cp = &sp->chars[ttisp->tt_abbrind];
340 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
341 strcmp(cp, GRANDPARENTED) != 0)
342 *(cp + TZ_ABBR_MAX_LEN) = '\0';
347 differ_by_repeat(t1, t0)
351 int_fast64_t _t0 = t0;
352 int_fast64_t _t1 = t1;
354 if (TYPE_INTEGRAL(time_t) &&
355 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
357 //turn ((int_fast64_t)(t1 - t0) == SECSPERREPEAT);
358 return _t1 - _t0 == SECSPERREPEAT;
362 tzload(name, sp, doextend)
364 struct state * const sp;
365 register const int doextend;
374 struct tzhead tzhead;
375 char buf[2 * sizeof(struct tzhead) +
382 sp->goback = sp->goahead = FALSE;
384 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
385 if (name != NULL && issetugid() != 0)
386 if ((name[0] == ':' && name[1] == '/') ||
387 name[0] == '/' || strchr(name, '.'))
389 if (name == NULL && (name = TZDEFAULT) == NULL)
395 ** Section 4.9.1 of the C standard says that
396 ** "FILENAME_MAX expands to an integral constant expression
397 ** that is the size needed for an array of char large enough
398 ** to hold the longest file name string that the implementation
399 ** guarantees can be opened."
403 fullname = malloc(FILENAME_MAX + 1);
404 if (fullname == NULL)
409 doaccess = name[0] == '/';
411 if ((p = TZDIR) == NULL) {
415 if (strlen(p) + 1 + strlen(name) >= FILENAME_MAX) {
419 (void) strcpy(fullname, p);
420 (void) strcat(fullname, "/");
421 (void) strcat(fullname, name);
423 ** Set doaccess if '.' (as in "../") shows up in name.
425 if (strchr(name, '.') != NULL)
429 if (doaccess && access(name, R_OK) != 0) {
433 if ((fid = _open(name, OPEN_MODE)) == -1) {
437 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
444 u = malloc(sizeof(*u));
447 nread = _read(fid, u->buf, sizeof u->buf);
448 if (_close(fid) < 0 || nread <= 0)
450 for (stored = 4; stored <= 8; stored *= 2) {
454 ttisstdcnt = (int) detzcode(u->tzhead.tzh_ttisstdcnt);
455 ttisgmtcnt = (int) detzcode(u->tzhead.tzh_ttisgmtcnt);
456 sp->leapcnt = (int) detzcode(u->tzhead.tzh_leapcnt);
457 sp->timecnt = (int) detzcode(u->tzhead.tzh_timecnt);
458 sp->typecnt = (int) detzcode(u->tzhead.tzh_typecnt);
459 sp->charcnt = (int) detzcode(u->tzhead.tzh_charcnt);
460 p = u->tzhead.tzh_charcnt + sizeof u->tzhead.tzh_charcnt;
461 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
462 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
463 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
464 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
465 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
466 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
468 if (nread - (p - u->buf) <
469 sp->timecnt * stored + /* ats */
470 sp->timecnt + /* types */
471 sp->typecnt * 6 + /* ttinfos */
472 sp->charcnt + /* chars */
473 sp->leapcnt * (stored + 4) + /* lsinfos */
474 ttisstdcnt + /* ttisstds */
475 ttisgmtcnt) /* ttisgmts */
477 for (i = 0; i < sp->timecnt; ++i) {
478 sp->ats[i] = (stored == 4) ?
479 detzcode(p) : detzcode64(p);
482 for (i = 0; i < sp->timecnt; ++i) {
483 sp->types[i] = (unsigned char) *p++;
484 if (sp->types[i] >= sp->typecnt)
487 for (i = 0; i < sp->typecnt; ++i) {
488 struct ttinfo * ttisp;
490 ttisp = &sp->ttis[i];
491 ttisp->tt_gmtoff = detzcode(p);
493 ttisp->tt_isdst = (unsigned char) *p++;
494 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
496 ttisp->tt_abbrind = (unsigned char) *p++;
497 if (ttisp->tt_abbrind < 0 ||
498 ttisp->tt_abbrind > sp->charcnt)
501 for (i = 0; i < sp->charcnt; ++i)
503 sp->chars[i] = '\0'; /* ensure '\0' at end */
504 for (i = 0; i < sp->leapcnt; ++i) {
505 struct lsinfo * lsisp;
507 lsisp = &sp->lsis[i];
508 lsisp->ls_trans = (stored == 4) ?
509 detzcode(p) : detzcode64(p);
511 lsisp->ls_corr = detzcode(p);
514 for (i = 0; i < sp->typecnt; ++i) {
515 struct ttinfo * ttisp;
517 ttisp = &sp->ttis[i];
519 ttisp->tt_ttisstd = FALSE;
521 ttisp->tt_ttisstd = *p++;
522 if (ttisp->tt_ttisstd != TRUE &&
523 ttisp->tt_ttisstd != FALSE)
527 for (i = 0; i < sp->typecnt; ++i) {
528 struct ttinfo * ttisp;
530 ttisp = &sp->ttis[i];
532 ttisp->tt_ttisgmt = FALSE;
534 ttisp->tt_ttisgmt = *p++;
535 if (ttisp->tt_ttisgmt != TRUE &&
536 ttisp->tt_ttisgmt != FALSE)
541 ** Out-of-sort ats should mean we're running on a
542 ** signed time_t system but using a data file with
543 ** unsigned values (or vice versa).
545 for (i = 0; i < sp->timecnt - 2; ++i)
546 if (sp->ats[i] > sp->ats[i + 1]) {
548 if (TYPE_SIGNED(time_t)) {
550 ** Ignore the end (easy).
555 ** Ignore the beginning (harder).
559 for (j = 0; j + i < sp->timecnt; ++j) {
560 sp->ats[j] = sp->ats[j + i];
561 sp->types[j] = sp->types[j + i];
568 ** If this is an old file, we're done.
570 if (u->tzhead.tzh_version[0] == '\0')
573 for (i = 0; i < nread; ++i)
576 ** If this is a narrow integer time_t system, we're done.
578 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
581 if (doextend && nread > 2 &&
582 u->buf[0] == '\n' && u->buf[nread - 1] == '\n' &&
583 sp->typecnt + 2 <= TZ_MAX_TYPES) {
587 ts = malloc(sizeof(*ts));
590 u->buf[nread - 1] = '\0';
591 result = tzparse(&u->buf[1], ts, FALSE);
592 if (result == 0 && ts->typecnt == 2 &&
593 sp->charcnt + ts->charcnt <= TZ_MAX_CHARS) {
594 for (i = 0; i < 2; ++i)
595 ts->ttis[i].tt_abbrind +=
597 for (i = 0; i < ts->charcnt; ++i)
598 sp->chars[sp->charcnt++] =
601 while (i < ts->timecnt &&
603 sp->ats[sp->timecnt - 1])
605 while (i < ts->timecnt &&
606 sp->timecnt < TZ_MAX_TIMES) {
607 sp->ats[sp->timecnt] =
609 sp->types[sp->timecnt] =
615 sp->ttis[sp->typecnt++] = ts->ttis[0];
616 sp->ttis[sp->typecnt++] = ts->ttis[1];
620 if (sp->timecnt > 1) {
621 for (i = 1; i < sp->timecnt; ++i)
622 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
623 differ_by_repeat(sp->ats[i], sp->ats[0])) {
627 for (i = sp->timecnt - 2; i >= 0; --i)
628 if (typesequiv(sp, sp->types[sp->timecnt - 1],
630 differ_by_repeat(sp->ats[sp->timecnt - 1],
644 const struct state * const sp;
651 a < 0 || a >= sp->typecnt ||
652 b < 0 || b >= sp->typecnt)
655 register const struct ttinfo * ap = &sp->ttis[a];
656 register const struct ttinfo * bp = &sp->ttis[b];
657 result = ap->tt_gmtoff == bp->tt_gmtoff &&
658 ap->tt_isdst == bp->tt_isdst &&
659 ap->tt_ttisstd == bp->tt_ttisstd &&
660 ap->tt_ttisgmt == bp->tt_ttisgmt &&
661 strcmp(&sp->chars[ap->tt_abbrind],
662 &sp->chars[bp->tt_abbrind]) == 0;
667 static const int mon_lengths[2][MONSPERYEAR] = {
668 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
669 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
672 static const int year_lengths[2] = {
673 DAYSPERNYEAR, DAYSPERLYEAR
677 ** Given a pointer into a time zone string, scan until a character that is not
678 ** a valid character in a zone name is found. Return a pointer to that
688 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
695 ** Given a pointer into an extended time zone string, scan until the ending
696 ** delimiter of the zone name is located. Return a pointer to the delimiter.
698 ** As with getzname above, the legal character set is actually quite
699 ** restricted, with other characters producing undefined results.
700 ** We don't do any checking here; checking is done later in common-case code.
704 getqzname(register const char *strp, const int delim)
708 while ((c = *strp) != '\0' && c != delim)
714 ** Given a pointer into a time zone string, extract a number from that string.
715 ** Check that the number is within a specified range; if it is not, return
717 ** Otherwise, return a pointer to the first character not part of the number.
721 getnum(strp, nump, min, max)
730 if (strp == NULL || !is_digit(c = *strp))
734 num = num * 10 + (c - '0');
736 return NULL; /* illegal value */
738 } while (is_digit(c));
740 return NULL; /* illegal value */
746 ** Given a pointer into a time zone string, extract a number of seconds,
747 ** in hh[:mm[:ss]] form, from the string.
748 ** If any error occurs, return NULL.
749 ** Otherwise, return a pointer to the first character not part of the number
761 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
762 ** "M10.4.6/26", which does not conform to Posix,
763 ** but which specifies the equivalent of
764 ** ``02:00 on the first Sunday on or after 23 Oct''.
766 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
769 *secsp = num * (long) SECSPERHOUR;
772 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
775 *secsp += num * SECSPERMIN;
778 /* `SECSPERMIN' allows for leap seconds. */
779 strp = getnum(strp, &num, 0, SECSPERMIN);
789 ** Given a pointer into a time zone string, extract an offset, in
790 ** [+-]hh[:mm[:ss]] form, from the string.
791 ** If any error occurs, return NULL.
792 ** Otherwise, return a pointer to the first character not part of the time.
796 getoffset(strp, offsetp)
798 long * const offsetp;
805 } else if (*strp == '+')
807 strp = getsecs(strp, offsetp);
809 return NULL; /* illegal time */
811 *offsetp = -*offsetp;
816 ** Given a pointer into a time zone string, extract a rule in the form
817 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
818 ** If a valid rule is not found, return NULL.
819 ** Otherwise, return a pointer to the first character not part of the rule.
825 struct rule * const rulep;
831 rulep->r_type = JULIAN_DAY;
833 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
834 } else if (*strp == 'M') {
838 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
840 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
845 strp = getnum(strp, &rulep->r_week, 1, 5);
850 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
851 } else if (is_digit(*strp)) {
855 rulep->r_type = DAY_OF_YEAR;
856 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
857 } else return NULL; /* invalid format */
865 strp = getsecs(strp, &rulep->r_time);
866 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
871 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
872 ** year, a rule, and the offset from UTC at the time that rule takes effect,
873 ** calculate the Epoch-relative time that rule takes effect.
877 transtime(janfirst, year, rulep, offset)
878 const time_t janfirst;
880 const struct rule * const rulep;
886 int d, m1, yy0, yy1, yy2, dow;
889 leapyear = isleap(year);
890 switch (rulep->r_type) {
894 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
896 ** In non-leap years, or if the day number is 59 or less, just
897 ** add SECSPERDAY times the day number-1 to the time of
898 ** January 1, midnight, to get the day.
900 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
901 if (leapyear && rulep->r_day >= 60)
908 ** Just add SECSPERDAY times the day number to the time of
909 ** January 1, midnight, to get the day.
911 value = janfirst + rulep->r_day * SECSPERDAY;
914 case MONTH_NTH_DAY_OF_WEEK:
916 ** Mm.n.d - nth "dth day" of month m.
919 for (i = 0; i < rulep->r_mon - 1; ++i)
920 value += mon_lengths[leapyear][i] * SECSPERDAY;
923 ** Use Zeller's Congruence to get day-of-week of first day of
926 m1 = (rulep->r_mon + 9) % 12 + 1;
927 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
930 dow = ((26 * m1 - 2) / 10 +
931 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
936 ** "dow" is the day-of-week of the first day of the month. Get
937 ** the day-of-month (zero-origin) of the first "dow" day of the
940 d = rulep->r_day - dow;
943 for (i = 1; i < rulep->r_week; ++i) {
944 if (d + DAYSPERWEEK >=
945 mon_lengths[leapyear][rulep->r_mon - 1])
951 ** "d" is the day-of-month (zero-origin) of the day we want.
953 value += d * SECSPERDAY;
958 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
959 ** question. To get the Epoch-relative time of the specified local
960 ** time on that day, add the transition time and the current offset
963 return value + rulep->r_time + offset;
967 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
972 tzparse(name, sp, lastditch)
974 struct state * const sp;
977 const char * stdname;
978 const char * dstname;
984 unsigned char * typep;
991 stdlen = strlen(name); /* length of standard zone name */
993 if (stdlen >= sizeof sp->chars)
994 stdlen = (sizeof sp->chars) - 1;
1000 name = getqzname(name, '>');
1003 stdlen = name - stdname;
1006 name = getzname(name);
1007 stdlen = name - stdname;
1010 return -1; /* was "stdoffset = 0;" */
1012 name = getoffset(name, &stdoffset);
1017 load_result = tzload(TZDEFRULES, sp, FALSE);
1018 if (load_result != 0)
1019 sp->leapcnt = 0; /* so, we're off a little */
1020 if (*name != '\0') {
1023 name = getqzname(name, '>');
1026 dstlen = name - dstname;
1030 name = getzname(name);
1031 dstlen = name - dstname; /* length of DST zone name */
1033 if (*name != '\0' && *name != ',' && *name != ';') {
1034 name = getoffset(name, &dstoffset);
1037 } else dstoffset = stdoffset - SECSPERHOUR;
1038 if (*name == '\0' && load_result != 0)
1039 name = TZDEFRULESTRING;
1040 if (*name == ',' || *name == ';') {
1049 if ((name = getrule(name, &start)) == NULL)
1053 if ((name = getrule(name, &end)) == NULL)
1057 sp->typecnt = 2; /* standard time and DST */
1059 ** Two transitions per year, from EPOCH_YEAR forward.
1061 sp->ttis[0].tt_gmtoff = -dstoffset;
1062 sp->ttis[0].tt_isdst = 1;
1063 sp->ttis[0].tt_abbrind = stdlen + 1;
1064 sp->ttis[1].tt_gmtoff = -stdoffset;
1065 sp->ttis[1].tt_isdst = 0;
1066 sp->ttis[1].tt_abbrind = 0;
1071 for (year = EPOCH_YEAR;
1072 sp->timecnt + 2 <= TZ_MAX_TIMES;
1076 starttime = transtime(janfirst, year, &start,
1078 endtime = transtime(janfirst, year, &end,
1080 if (starttime > endtime) {
1082 *typep++ = 1; /* DST ends */
1084 *typep++ = 0; /* DST begins */
1087 *typep++ = 0; /* DST begins */
1089 *typep++ = 1; /* DST ends */
1092 newfirst = janfirst;
1093 newfirst += year_lengths[isleap(year)] *
1095 if (newfirst <= janfirst)
1097 janfirst = newfirst;
1100 long theirstdoffset;
1101 long theirdstoffset;
1110 ** Initial values of theirstdoffset and theirdstoffset.
1113 for (i = 0; i < sp->timecnt; ++i) {
1115 if (!sp->ttis[j].tt_isdst) {
1117 -sp->ttis[j].tt_gmtoff;
1122 for (i = 0; i < sp->timecnt; ++i) {
1124 if (sp->ttis[j].tt_isdst) {
1126 -sp->ttis[j].tt_gmtoff;
1131 ** Initially we're assumed to be in standard time.
1134 theiroffset = theirstdoffset;
1136 ** Now juggle transition times and types
1137 ** tracking offsets as you do.
1139 for (i = 0; i < sp->timecnt; ++i) {
1141 sp->types[i] = sp->ttis[j].tt_isdst;
1142 if (sp->ttis[j].tt_ttisgmt) {
1143 /* No adjustment to transition time */
1146 ** If summer time is in effect, and the
1147 ** transition time was not specified as
1148 ** standard time, add the summer time
1149 ** offset to the transition time;
1150 ** otherwise, add the standard time
1151 ** offset to the transition time.
1154 ** Transitions from DST to DDST
1155 ** will effectively disappear since
1156 ** POSIX provides for only one DST
1159 if (isdst && !sp->ttis[j].tt_ttisstd) {
1160 sp->ats[i] += dstoffset -
1163 sp->ats[i] += stdoffset -
1167 theiroffset = -sp->ttis[j].tt_gmtoff;
1168 if (sp->ttis[j].tt_isdst)
1169 theirdstoffset = theiroffset;
1170 else theirstdoffset = theiroffset;
1173 ** Finally, fill in ttis.
1174 ** ttisstd and ttisgmt need not be handled.
1176 sp->ttis[0].tt_gmtoff = -stdoffset;
1177 sp->ttis[0].tt_isdst = FALSE;
1178 sp->ttis[0].tt_abbrind = 0;
1179 sp->ttis[1].tt_gmtoff = -dstoffset;
1180 sp->ttis[1].tt_isdst = TRUE;
1181 sp->ttis[1].tt_abbrind = stdlen + 1;
1186 sp->typecnt = 1; /* only standard time */
1188 sp->ttis[0].tt_gmtoff = -stdoffset;
1189 sp->ttis[0].tt_isdst = 0;
1190 sp->ttis[0].tt_abbrind = 0;
1192 sp->charcnt = stdlen + 1;
1194 sp->charcnt += dstlen + 1;
1195 if ((size_t) sp->charcnt > sizeof sp->chars)
1198 (void) strncpy(cp, stdname, stdlen);
1202 (void) strncpy(cp, dstname, dstlen);
1203 *(cp + dstlen) = '\0';
1210 struct state * const sp;
1212 if (tzload(gmt, sp, TRUE) != 0)
1213 (void) tzparse(gmt, sp, TRUE);
1217 tzsetwall_basic(int rdlocked)
1220 _RWLOCK_RDLOCK(&lcl_rwlock);
1221 if (lcl_is_set < 0) {
1223 _RWLOCK_UNLOCK(&lcl_rwlock);
1226 _RWLOCK_UNLOCK(&lcl_rwlock);
1228 _RWLOCK_WRLOCK(&lcl_rwlock);
1232 if (lclptr == NULL) {
1233 lclptr = (struct state *) calloc(1, sizeof *lclptr);
1234 if (lclptr == NULL) {
1235 settzname(); /* all we can do */
1236 _RWLOCK_UNLOCK(&lcl_rwlock);
1238 _RWLOCK_RDLOCK(&lcl_rwlock);
1242 #endif /* defined ALL_STATE */
1243 if (tzload((char *) NULL, lclptr, TRUE) != 0)
1246 _RWLOCK_UNLOCK(&lcl_rwlock);
1249 _RWLOCK_RDLOCK(&lcl_rwlock);
1259 tzset_basic(int rdlocked)
1263 name = getenv("TZ");
1265 tzsetwall_basic(rdlocked);
1270 _RWLOCK_RDLOCK(&lcl_rwlock);
1271 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
1273 _RWLOCK_UNLOCK(&lcl_rwlock);
1276 _RWLOCK_UNLOCK(&lcl_rwlock);
1278 _RWLOCK_WRLOCK(&lcl_rwlock);
1279 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1281 (void) strcpy(lcl_TZname, name);
1284 if (lclptr == NULL) {
1285 lclptr = (struct state *) calloc(1, sizeof *lclptr);
1286 if (lclptr == NULL) {
1287 settzname(); /* all we can do */
1288 _RWLOCK_UNLOCK(&lcl_rwlock);
1290 _RWLOCK_RDLOCK(&lcl_rwlock);
1294 #endif /* defined ALL_STATE */
1295 if (*name == '\0') {
1297 ** User wants it fast rather than right.
1299 lclptr->leapcnt = 0; /* so, we're off a little */
1300 lclptr->timecnt = 0;
1301 lclptr->typecnt = 0;
1302 lclptr->ttis[0].tt_isdst = 0;
1303 lclptr->ttis[0].tt_gmtoff = 0;
1304 lclptr->ttis[0].tt_abbrind = 0;
1305 (void) strcpy(lclptr->chars, gmt);
1306 } else if (tzload(name, lclptr, TRUE) != 0)
1307 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1308 (void) gmtload(lclptr);
1310 _RWLOCK_UNLOCK(&lcl_rwlock);
1313 _RWLOCK_RDLOCK(&lcl_rwlock);
1323 ** The easy way to behave "as if no library function calls" localtime
1324 ** is to not call it--so we drop its guts into "localsub", which can be
1325 ** freely called. (And no, the PANS doesn't require the above behavior--
1326 ** but it *is* desirable.)
1328 ** The unused offset argument is for the benefit of mktime variants.
1333 localsub(timep, offset, tmp)
1334 const time_t * const timep;
1336 struct tm * const tmp;
1339 const struct ttinfo * ttisp;
1342 const time_t t = *timep;
1347 return gmtsub(timep, offset, tmp);
1348 #endif /* defined ALL_STATE */
1349 if ((sp->goback && t < sp->ats[0]) ||
1350 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1352 register time_t seconds;
1353 register time_t tcycles;
1354 register int_fast64_t icycles;
1357 seconds = sp->ats[0] - t;
1358 else seconds = t - sp->ats[sp->timecnt - 1];
1360 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1363 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1366 seconds *= YEARSPERREPEAT;
1367 seconds *= AVGSECSPERYEAR;
1370 else newt -= seconds;
1371 if (newt < sp->ats[0] ||
1372 newt > sp->ats[sp->timecnt - 1])
1373 return NULL; /* "cannot happen" */
1374 result = localsub(&newt, offset, tmp);
1375 if (result == tmp) {
1376 register time_t newy;
1378 newy = tmp->tm_year;
1380 newy -= icycles * YEARSPERREPEAT;
1381 else newy += icycles * YEARSPERREPEAT;
1382 tmp->tm_year = newy;
1383 if (tmp->tm_year != newy)
1388 if (sp->timecnt == 0 || t < sp->ats[0]) {
1390 while (sp->ttis[i].tt_isdst)
1391 if (++i >= sp->typecnt) {
1396 register int lo = 1;
1397 register int hi = sp->timecnt;
1400 register int mid = (lo + hi) >> 1;
1402 if (t < sp->ats[mid])
1406 i = (int) sp->types[lo - 1];
1408 ttisp = &sp->ttis[i];
1410 ** To get (wrong) behavior that's compatible with System V Release 2.0
1411 ** you'd replace the statement below with
1412 ** t += ttisp->tt_gmtoff;
1413 ** timesub(&t, 0L, sp, tmp);
1415 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1416 tmp->tm_isdst = ttisp->tt_isdst;
1417 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1419 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1420 #endif /* defined TM_ZONE */
1425 localtime_key_init(void)
1428 localtime_key_error = _pthread_key_create(&localtime_key, free);
1433 const time_t * const timep;
1437 if (__isthreaded != 0) {
1438 _once(&localtime_once, localtime_key_init);
1439 if (localtime_key_error != 0) {
1440 errno = localtime_key_error;
1443 p_tm = _pthread_getspecific(localtime_key);
1445 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1448 _pthread_setspecific(localtime_key, p_tm);
1450 _RWLOCK_RDLOCK(&lcl_rwlock);
1452 localsub(timep, 0L, p_tm);
1453 _RWLOCK_UNLOCK(&lcl_rwlock);
1457 localsub(timep, 0L, &tm);
1463 ** Re-entrant version of localtime.
1467 localtime_r(timep, tmp)
1468 const time_t * const timep;
1471 _RWLOCK_RDLOCK(&lcl_rwlock);
1473 localsub(timep, 0L, tmp);
1474 _RWLOCK_UNLOCK(&lcl_rwlock);
1483 gmtptr = (struct state *) calloc(1, sizeof *gmtptr);
1485 #endif /* defined ALL_STATE */
1490 ** gmtsub is to gmtime as localsub is to localtime.
1494 gmtsub(timep, offset, tmp)
1495 const time_t * const timep;
1497 struct tm * const tmp;
1499 register struct tm * result;
1501 _once(&gmt_once, gmt_init);
1502 result = timesub(timep, offset, gmtptr, tmp);
1505 ** Could get fancy here and deliver something such as
1506 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1507 ** but this is no time for a treasure hunt.
1510 tmp->TM_ZONE = wildabbr;
1515 else tmp->TM_ZONE = gmtptr->chars;
1516 #endif /* defined ALL_STATE */
1518 tmp->TM_ZONE = gmtptr->chars;
1519 #endif /* State Farm */
1521 #endif /* defined TM_ZONE */
1526 gmtime_key_init(void)
1529 gmtime_key_error = _pthread_key_create(&gmtime_key, free);
1534 const time_t * const timep;
1538 if (__isthreaded != 0) {
1539 _once(&gmtime_once, gmtime_key_init);
1540 if (gmtime_key_error != 0) {
1541 errno = gmtime_key_error;
1545 * Changed to follow POSIX.1 threads standard, which
1546 * is what BSD currently has.
1548 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1549 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1553 _pthread_setspecific(gmtime_key, p_tm);
1555 gmtsub(timep, 0L, p_tm);
1559 gmtsub(timep, 0L, &tm);
1565 * Re-entrant version of gmtime.
1569 gmtime_r(timep, tmp)
1570 const time_t * const timep;
1573 return gmtsub(timep, 0L, tmp);
1579 offtime(timep, offset)
1580 const time_t * const timep;
1583 return gmtsub(timep, offset, &tm);
1586 #endif /* defined STD_INSPIRED */
1589 ** Return the number of leap years through the end of the given year
1590 ** where, to make the math easy, the answer for year zero is defined as zero.
1594 leaps_thru_end_of(y)
1595 register const int y;
1597 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1598 -(leaps_thru_end_of(-(y + 1)) + 1);
1602 timesub(timep, offset, sp, tmp)
1603 const time_t * const timep;
1605 const struct state * const sp;
1606 struct tm * const tmp;
1608 const struct lsinfo * lp;
1610 int idays; /* unsigned would be so 2003 */
1621 i = (sp == NULL) ? 0 : sp->leapcnt;
1622 #endif /* defined ALL_STATE */
1625 #endif /* State Farm */
1628 if (*timep >= lp->ls_trans) {
1629 if (*timep == lp->ls_trans) {
1630 hit = ((i == 0 && lp->ls_corr > 0) ||
1631 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1634 sp->lsis[i].ls_trans ==
1635 sp->lsis[i - 1].ls_trans + 1 &&
1636 sp->lsis[i].ls_corr ==
1637 sp->lsis[i - 1].ls_corr + 1) {
1647 tdays = *timep / SECSPERDAY;
1648 rem = *timep - tdays * SECSPERDAY;
1649 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1651 register time_t tdelta;
1652 register int idelta;
1653 register int leapdays;
1655 tdelta = tdays / DAYSPERLYEAR;
1657 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1660 idelta = (tdays < 0) ? -1 : 1;
1662 if (increment_overflow(&newy, idelta))
1664 leapdays = leaps_thru_end_of(newy - 1) -
1665 leaps_thru_end_of(y - 1);
1666 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1671 register long seconds;
1673 seconds = tdays * SECSPERDAY + 0.5;
1674 tdays = seconds / SECSPERDAY;
1675 rem += seconds - tdays * SECSPERDAY;
1678 ** Given the range, we can now fearlessly cast...
1681 rem += offset - corr;
1686 while (rem >= SECSPERDAY) {
1691 if (increment_overflow(&y, -1))
1693 idays += year_lengths[isleap(y)];
1695 while (idays >= year_lengths[isleap(y)]) {
1696 idays -= year_lengths[isleap(y)];
1697 if (increment_overflow(&y, 1))
1701 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1703 tmp->tm_yday = idays;
1705 ** The "extra" mods below avoid overflow problems.
1707 tmp->tm_wday = EPOCH_WDAY +
1708 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1709 (DAYSPERNYEAR % DAYSPERWEEK) +
1710 leaps_thru_end_of(y - 1) -
1711 leaps_thru_end_of(EPOCH_YEAR - 1) +
1713 tmp->tm_wday %= DAYSPERWEEK;
1714 if (tmp->tm_wday < 0)
1715 tmp->tm_wday += DAYSPERWEEK;
1716 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1718 tmp->tm_min = (int) (rem / SECSPERMIN);
1720 ** A positive leap second requires a special
1721 ** representation. This uses "... ??:59:60" et seq.
1723 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1724 ip = mon_lengths[isleap(y)];
1725 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1726 idays -= ip[tmp->tm_mon];
1727 tmp->tm_mday = (int) (idays + 1);
1730 tmp->TM_GMTOFF = offset;
1731 #endif /* defined TM_GMTOFF */
1737 const time_t * const timep;
1740 ** Section 4.12.3.2 of X3.159-1989 requires that
1741 ** The ctime function converts the calendar time pointed to by timer
1742 ** to local time in the form of a string. It is equivalent to
1743 ** asctime(localtime(timer))
1745 return asctime(localtime(timep));
1750 const time_t * const timep;
1755 return asctime_r(localtime_r(timep, &mytm), buf);
1759 ** Adapted from code provided by Robert Elz, who writes:
1760 ** The "best" way to do mktime I think is based on an idea of Bob
1761 ** Kridle's (so its said...) from a long time ago.
1762 ** It does a binary search of the time_t space. Since time_t's are
1763 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1764 ** would still be very reasonable).
1769 #endif /* !defined WRONG */
1772 ** Simplified normalize logic courtesy Paul Eggert.
1776 increment_overflow(number, delta)
1784 return (*number < number0) != (delta < 0);
1788 long_increment_overflow(number, delta)
1796 return (*number < number0) != (delta < 0);
1800 normalize_overflow(tensptr, unitsptr, base)
1801 int * const tensptr;
1802 int * const unitsptr;
1807 tensdelta = (*unitsptr >= 0) ?
1808 (*unitsptr / base) :
1809 (-1 - (-1 - *unitsptr) / base);
1810 *unitsptr -= tensdelta * base;
1811 return increment_overflow(tensptr, tensdelta);
1815 long_normalize_overflow(tensptr, unitsptr, base)
1816 long * const tensptr;
1817 int * const unitsptr;
1820 register int tensdelta;
1822 tensdelta = (*unitsptr >= 0) ?
1823 (*unitsptr / base) :
1824 (-1 - (-1 - *unitsptr) / base);
1825 *unitsptr -= tensdelta * base;
1826 return long_increment_overflow(tensptr, tensdelta);
1831 const struct tm * const atmp;
1832 const struct tm * const btmp;
1836 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1837 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1838 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1839 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1840 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1841 result = atmp->tm_sec - btmp->tm_sec;
1846 time2sub(tmp, funcp, offset, okayp, do_norm_secs)
1847 struct tm * const tmp;
1848 struct tm * (* const funcp)(const time_t*, long, struct tm*);
1851 const int do_norm_secs;
1853 const struct state * sp;
1863 struct tm yourtm, mytm;
1868 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1872 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1874 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1877 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
1880 ** Turn y into an actual year number for now.
1881 ** It is converted back to an offset from TM_YEAR_BASE later.
1883 if (long_increment_overflow(&y, TM_YEAR_BASE))
1885 while (yourtm.tm_mday <= 0) {
1886 if (long_increment_overflow(&y, -1))
1888 li = y + (1 < yourtm.tm_mon);
1889 yourtm.tm_mday += year_lengths[isleap(li)];
1891 while (yourtm.tm_mday > DAYSPERLYEAR) {
1892 li = y + (1 < yourtm.tm_mon);
1893 yourtm.tm_mday -= year_lengths[isleap(li)];
1894 if (long_increment_overflow(&y, 1))
1898 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1899 if (yourtm.tm_mday <= i)
1901 yourtm.tm_mday -= i;
1902 if (++yourtm.tm_mon >= MONSPERYEAR) {
1904 if (long_increment_overflow(&y, 1))
1908 if (long_increment_overflow(&y, -TM_YEAR_BASE))
1911 if (yourtm.tm_year != y)
1913 /* Don't go below 1900 for POLA */
1914 if (yourtm.tm_year < 0)
1916 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1918 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1920 ** We can't set tm_sec to 0, because that might push the
1921 ** time below the minimum representable time.
1922 ** Set tm_sec to 59 instead.
1923 ** This assumes that the minimum representable time is
1924 ** not in the same minute that a leap second was deleted from,
1925 ** which is a safer assumption than using 58 would be.
1927 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1929 saved_seconds = yourtm.tm_sec;
1930 yourtm.tm_sec = SECSPERMIN - 1;
1932 saved_seconds = yourtm.tm_sec;
1936 ** Do a binary search (this works whatever time_t's type is).
1938 if (!TYPE_SIGNED(time_t)) {
1941 } else if (!TYPE_INTEGRAL(time_t)) {
1942 if (sizeof(time_t) > sizeof(float))
1943 hi = (time_t) DBL_MAX;
1944 else hi = (time_t) FLT_MAX;
1948 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1953 t = lo / 2 + hi / 2;
1958 if ((*funcp)(&t, offset, &mytm) == NULL) {
1960 ** Assume that t is too extreme to be represented in
1961 ** a struct tm; arrange things so that it is less
1962 ** extreme on the next pass.
1964 dir = (t > 0) ? 1 : -1;
1965 } else dir = tmcomp(&mytm, &yourtm);
1972 } else if (t == hi) {
1985 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1988 ** Right time, wrong type.
1989 ** Hunt for right time, right type.
1990 ** It's okay to guess wrong since the guess
1993 sp = (const struct state *)
1994 ((funcp == localsub) ? lclptr : gmtptr);
1998 #endif /* defined ALL_STATE */
1999 for (i = sp->typecnt - 1; i >= 0; --i) {
2000 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
2002 for (j = sp->typecnt - 1; j >= 0; --j) {
2003 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
2005 newt = t + sp->ttis[j].tt_gmtoff -
2006 sp->ttis[i].tt_gmtoff;
2007 if ((*funcp)(&newt, offset, &mytm) == NULL)
2009 if (tmcomp(&mytm, &yourtm) != 0)
2011 if (mytm.tm_isdst != yourtm.tm_isdst)
2023 newt = t + saved_seconds;
2024 if ((newt < t) != (saved_seconds < 0))
2027 if ((*funcp)(&t, offset, tmp))
2033 time2(tmp, funcp, offset, okayp)
2034 struct tm * const tmp;
2035 struct tm * (* const funcp)(const time_t*, long, struct tm*);
2042 ** First try without normalization of seconds
2043 ** (in case tm_sec contains a value associated with a leap second).
2044 ** If that fails, try with normalization of seconds.
2046 t = time2sub(tmp, funcp, offset, okayp, FALSE);
2047 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
2051 time1(tmp, funcp, offset)
2052 struct tm * const tmp;
2053 struct tm * (* const funcp)(const time_t *, long, struct tm *);
2057 const struct state * sp;
2059 int sameind, otherind;
2062 int seen[TZ_MAX_TYPES];
2063 int types[TZ_MAX_TYPES];
2071 if (tmp->tm_isdst > 1)
2073 t = time2(tmp, funcp, offset, &okay);
2076 ** PCTS code courtesy Grant Sullivan.
2080 if (tmp->tm_isdst < 0)
2081 tmp->tm_isdst = 0; /* reset to std and try again */
2082 #endif /* defined PCTS */
2084 if (okay || tmp->tm_isdst < 0)
2086 #endif /* !defined PCTS */
2088 ** We're supposed to assume that somebody took a time of one type
2089 ** and did some math on it that yielded a "struct tm" that's bad.
2090 ** We try to divine the type they started from and adjust to the
2093 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
2097 #endif /* defined ALL_STATE */
2098 for (i = 0; i < sp->typecnt; ++i)
2101 for (i = sp->timecnt - 1; i >= 0; --i)
2102 if (!seen[sp->types[i]]) {
2103 seen[sp->types[i]] = TRUE;
2104 types[nseen++] = sp->types[i];
2106 for (sameind = 0; sameind < nseen; ++sameind) {
2107 samei = types[sameind];
2108 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2110 for (otherind = 0; otherind < nseen; ++otherind) {
2111 otheri = types[otherind];
2112 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2114 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2115 sp->ttis[samei].tt_gmtoff;
2116 tmp->tm_isdst = !tmp->tm_isdst;
2117 t = time2(tmp, funcp, offset, &okay);
2120 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2121 sp->ttis[samei].tt_gmtoff;
2122 tmp->tm_isdst = !tmp->tm_isdst;
2130 struct tm * const tmp;
2132 time_t mktime_return_value;
2133 _RWLOCK_RDLOCK(&lcl_rwlock);
2135 mktime_return_value = time1(tmp, localsub, 0L);
2136 _RWLOCK_UNLOCK(&lcl_rwlock);
2137 return(mktime_return_value);
2144 struct tm * const tmp;
2147 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2153 struct tm * const tmp;
2157 return time1(tmp, gmtsub, 0L);
2161 timeoff(tmp, offset)
2162 struct tm * const tmp;
2167 return time1(tmp, gmtsub, offset);
2170 #endif /* defined STD_INSPIRED */
2175 ** The following is supplied for compatibility with
2176 ** previous versions of the CMUCS runtime library.
2181 struct tm * const tmp;
2183 const time_t t = mktime(tmp);
2190 #endif /* defined CMUCS */
2193 ** XXX--is the below the right way to conditionalize??
2199 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2200 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2201 ** is not the case if we are accounting for leap seconds.
2202 ** So, we provide the following conversion routines for use
2203 ** when exchanging timestamps with POSIX conforming systems.
2218 if (*timep >= lp->ls_trans)
2229 return t - leapcorr(&t);
2241 ** For a positive leap second hit, the result
2242 ** is not unique. For a negative leap second
2243 ** hit, the corresponding time doesn't exist,
2244 ** so we return an adjacent second.
2246 x = t + leapcorr(&t);
2247 y = x - leapcorr(&x);
2251 y = x - leapcorr(&x);
2258 y = x - leapcorr(&x);
2266 #endif /* defined STD_INSPIRED */