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>
24 #include "libc_private.h"
25 #include <un-namespace.h>
29 #ifndef TZ_ABBR_MAX_LEN
30 #define TZ_ABBR_MAX_LEN 16
31 #endif /* !defined TZ_ABBR_MAX_LEN */
33 #ifndef TZ_ABBR_CHAR_SET
34 #define TZ_ABBR_CHAR_SET \
35 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
36 #endif /* !defined TZ_ABBR_CHAR_SET */
38 #ifndef TZ_ABBR_ERR_CHAR
39 #define TZ_ABBR_ERR_CHAR '_'
40 #endif /* !defined TZ_ABBR_ERR_CHAR */
42 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
43 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
45 #define _RWLOCK_RDLOCK(x) \
47 if (__isthreaded) _pthread_rwlock_rdlock(x); \
50 #define _RWLOCK_WRLOCK(x) \
52 if (__isthreaded) _pthread_rwlock_wrlock(x); \
55 #define _RWLOCK_UNLOCK(x) \
57 if (__isthreaded) _pthread_rwlock_unlock(x); \
61 ** Someone might make incorrect use of a time zone abbreviation:
62 ** 1. They might reference tzname[0] before calling tzset (explicitly
64 ** 2. They might reference tzname[1] before calling tzset (explicitly
66 ** 3. They might reference tzname[1] after setting to a time zone
67 ** in which Daylight Saving Time is never observed.
68 ** 4. They might reference tzname[0] after setting to a time zone
69 ** in which Standard Time is never observed.
70 ** 5. They might reference tm.TM_ZONE after calling offtime.
71 ** What's best to do in the above cases is open to debate;
72 ** for now, we just set things up so that in any of the five cases
73 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
74 ** string "tzname[0] used before set", and similarly for the other cases.
75 ** And another: initialize tzname[0] to "ERA", with an explanation in the
76 ** manual page of what this "time zone abbreviation" means (doing this so
77 ** that tzname[0] has the "normal" length of three characters).
81 static char wildabbr[] = WILDABBR;
83 static const char gmt[] = "UTC";
86 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
87 ** We default to US rules as of 1999-08-17.
88 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
89 ** implementation dependent; for historical reasons, US rules are a
92 #ifndef TZDEFRULESTRING
93 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
94 #endif /* !defined TZDEFDST */
96 struct ttinfo { /* time type information */
97 int_fast32_t tt_gmtoff; /* UT offset in seconds */
98 int tt_isdst; /* used to set tm_isdst */
99 int tt_abbrind; /* abbreviation list index */
100 int tt_ttisstd; /* TRUE if transition is std time */
101 int tt_ttisgmt; /* TRUE if transition is UT */
104 struct lsinfo { /* leap second information */
105 time_t ls_trans; /* transition time */
106 int_fast64_t ls_corr; /* correction to apply */
109 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
112 #define MY_TZNAME_MAX TZNAME_MAX
113 #endif /* defined TZNAME_MAX */
115 #define MY_TZNAME_MAX 255
116 #endif /* !defined TZNAME_MAX */
125 time_t ats[TZ_MAX_TIMES];
126 unsigned char types[TZ_MAX_TIMES];
127 struct ttinfo ttis[TZ_MAX_TYPES];
128 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
129 (2 * (MY_TZNAME_MAX + 1)))];
130 struct lsinfo lsis[TZ_MAX_LEAPS];
131 int defaulttype; /* for early times or if no transitions */
135 int r_type; /* type of rule--see below */
136 int r_day; /* day number of rule */
137 int r_week; /* week number of rule */
138 int r_mon; /* month number of rule */
139 int_fast32_t r_time; /* transition time of rule */
142 #define JULIAN_DAY 0 /* Jn - Julian day */
143 #define DAY_OF_YEAR 1 /* n - day of year */
144 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
147 ** Prototypes for static functions.
150 static int_fast32_t detzcode(const char * codep);
151 static int_fast64_t detzcode64(const char * codep);
152 static int differ_by_repeat(time_t t1, time_t t0);
153 static const char * getzname(const char * strp) __pure;
154 static const char * getqzname(const char * strp, const int delim) __pure;
155 static const char * getnum(const char * strp, int * nump, int min,
157 static const char * getsecs(const char * strp, int_fast32_t * secsp);
158 static const char * getoffset(const char * strp, int_fast32_t * offsetp);
159 static const char * getrule(const char * strp, struct rule * rulep);
160 static void gmtload(struct state * sp);
161 static struct tm * gmtsub(const time_t * timep, int_fast32_t offset,
163 static struct tm * localsub(const time_t * timep, int_fast32_t offset,
165 static int increment_overflow(int * number, int delta);
166 static int leaps_thru_end_of(int y) __pure;
167 static int increment_overflow32(int_fast32_t * number, int delta);
168 static int increment_overflow_time(time_t *t, int_fast32_t delta);
169 static int normalize_overflow32(int_fast32_t * tensptr,
170 int * unitsptr, int base);
171 static int normalize_overflow(int * tensptr, int * unitsptr,
173 static void settzname(void);
174 static time_t time1(struct tm * tmp,
175 struct tm * (*funcp)(const time_t *,
176 int_fast32_t, struct tm *),
177 int_fast32_t offset);
178 static time_t time2(struct tm *tmp,
179 struct tm * (*funcp)(const time_t *,
180 int_fast32_t, struct tm*),
181 int_fast32_t offset, int * okayp);
182 static time_t time2sub(struct tm *tmp,
183 struct tm * (*funcp)(const time_t *,
184 int_fast32_t, struct tm*),
185 int_fast32_t offset, int * okayp, int do_norm_secs);
186 static struct tm * timesub(const time_t * timep, int_fast32_t offset,
187 const struct state * sp, struct tm * tmp);
188 static int tmcomp(const struct tm * atmp,
189 const struct tm * btmp);
190 static int_fast32_t transtime(int year, const struct rule * rulep,
191 int_fast32_t offset) __pure;
192 static int typesequiv(const struct state * sp, int a, int b);
193 static int tzload(const char * name, struct state * sp,
195 static int tzparse(const char * name, struct state * sp,
198 static struct state lclmem;
199 static struct state gmtmem;
200 #define lclptr (&lclmem)
201 #define gmtptr (&gmtmem)
203 #ifndef TZ_STRLEN_MAX
204 #define TZ_STRLEN_MAX 255
205 #endif /* !defined TZ_STRLEN_MAX */
207 static char lcl_TZname[TZ_STRLEN_MAX + 1];
208 static int lcl_is_set;
209 static pthread_once_t gmt_once = PTHREAD_ONCE_INIT;
210 static pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
211 static pthread_once_t gmtime_once = PTHREAD_ONCE_INIT;
212 static pthread_key_t gmtime_key;
213 static int gmtime_key_error;
214 static pthread_once_t localtime_once = PTHREAD_ONCE_INIT;
215 static pthread_key_t localtime_key;
216 static int localtime_key_error;
224 ** Section 4.12.3 of X3.159-1989 requires that
225 ** Except for the strftime function, these functions [asctime,
226 ** ctime, gmtime, localtime] return values in one of two static
227 ** objects: a broken-down time structure and an array of char.
228 ** Thanks to Paul Eggert for noting this.
237 detzcode(const char * const codep)
242 result = (codep[0] & 0x80) ? -1 : 0;
243 for (i = 0; i < 4; ++i)
244 result = (result << 8) | (codep[i] & 0xff);
249 detzcode64(const char * const codep)
254 result = (codep[0] & 0x80) ? -1 : 0;
255 for (i = 0; i < 8; ++i)
256 result = (result << 8) | (codep[i] & 0xff);
263 struct state * const sp = lclptr;
266 tzname[0] = wildabbr;
267 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[i];
277 tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind];
279 for (i = 0; i < sp->timecnt; ++i) {
280 const struct ttinfo * const ttisp = &sp->ttis[sp->types[i]];
282 tzname[ttisp->tt_isdst] =
283 &sp->chars[ttisp->tt_abbrind];
286 if (!ttisp->tt_isdst)
287 timezone = -(ttisp->tt_gmtoff);
290 ** Finally, scrub the abbreviations.
291 ** First, replace bogus characters.
293 for (i = 0; i < sp->charcnt; ++i)
294 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
295 sp->chars[i] = TZ_ABBR_ERR_CHAR;
297 ** Second, truncate long abbreviations.
299 for (i = 0; i < sp->typecnt; ++i) {
300 const struct ttinfo * const ttisp = &sp->ttis[i];
301 char * cp = &sp->chars[ttisp->tt_abbrind];
303 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
304 strcmp(cp, GRANDPARENTED) != 0)
305 *(cp + TZ_ABBR_MAX_LEN) = '\0';
310 differ_by_repeat(const time_t t1, const time_t t0)
312 if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
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) +
336 sp->goback = sp->goahead = FALSE;
338 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
339 if (name != NULL && issetugid() != 0)
340 if ((name[0] == ':' && name[1] == '/') ||
341 name[0] == '/' || strchr(name, '.'))
343 if (name == NULL && (name = TZDEFAULT) == NULL)
349 ** Section 4.9.1 of the C standard says that
350 ** "FILENAME_MAX expands to an integral constant expression
351 ** that is the size needed for an array of char large enough
352 ** to hold the longest file name string that the implementation
353 ** guarantees can be opened."
357 fullname = malloc(FILENAME_MAX + 1);
358 if (fullname == NULL)
363 doaccess = name[0] == '/';
365 if ((p = TZDIR) == NULL) {
369 if (strlen(p) + 1 + strlen(name) >= FILENAME_MAX) {
374 strcat(fullname, "/");
375 strcat(fullname, name);
377 ** Set doaccess if '.' (as in "../") shows up in name.
379 if (strchr(name, '.') != NULL)
383 if (doaccess && access(name, R_OK) != 0) {
387 if ((fid = _open(name, O_RDONLY)) == -1) {
391 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
398 u = malloc(sizeof(*u));
401 nread = _read(fid, u->buf, sizeof u->buf);
402 if (_close(fid) < 0 || nread <= 0)
404 for (stored = 4; stored <= 8; stored *= 2) {
409 ttisstdcnt = (int) detzcode(u->tzhead.tzh_ttisstdcnt);
410 ttisgmtcnt = (int) detzcode(u->tzhead.tzh_ttisgmtcnt);
411 sp->leapcnt = (int) detzcode(u->tzhead.tzh_leapcnt);
412 sp->timecnt = (int) detzcode(u->tzhead.tzh_timecnt);
413 sp->typecnt = (int) detzcode(u->tzhead.tzh_typecnt);
414 sp->charcnt = (int) detzcode(u->tzhead.tzh_charcnt);
415 p = u->tzhead.tzh_charcnt + sizeof u->tzhead.tzh_charcnt;
416 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
417 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
418 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
419 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
420 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
421 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
423 if (nread - (p - u->buf) <
424 sp->timecnt * stored + /* ats */
425 sp->timecnt + /* types */
426 sp->typecnt * 6 + /* ttinfos */
427 sp->charcnt + /* chars */
428 sp->leapcnt * (stored + 4) + /* lsinfos */
429 ttisstdcnt + /* ttisstds */
430 ttisgmtcnt) /* ttisgmts */
433 for (i = 0; i < sp->timecnt; ++i) {
435 = stored == 4 ? detzcode(p) : detzcode64(p);
436 sp->types[i] = ((TYPE_SIGNED(time_t)
439 && at <= time_t_max);
441 if (i && !timecnt && at != time_t_min) {
443 ** Keep the earlier record, but tweak
444 ** it so that it starts with the
445 ** minimum time_t value.
447 sp->types[i - 1] = 1;
448 sp->ats[timecnt++] = time_t_min;
450 sp->ats[timecnt++] = at;
455 for (i = 0; i < sp->timecnt; ++i) {
456 unsigned char typ = *p++;
457 if (sp->typecnt <= typ)
460 sp->types[timecnt++] = typ;
462 sp->timecnt = timecnt;
463 for (i = 0; i < sp->typecnt; ++i) {
464 struct ttinfo * ttisp;
466 ttisp = &sp->ttis[i];
467 ttisp->tt_gmtoff = detzcode(p);
469 ttisp->tt_isdst = (unsigned char) *p++;
470 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
472 ttisp->tt_abbrind = (unsigned char) *p++;
473 if (ttisp->tt_abbrind < 0 ||
474 ttisp->tt_abbrind > sp->charcnt)
477 for (i = 0; i < sp->charcnt; ++i)
479 sp->chars[i] = '\0'; /* ensure '\0' at end */
480 for (i = 0; i < sp->leapcnt; ++i) {
481 struct lsinfo * lsisp;
483 lsisp = &sp->lsis[i];
484 lsisp->ls_trans = (stored == 4) ?
485 detzcode(p) : detzcode64(p);
487 lsisp->ls_corr = detzcode(p);
490 for (i = 0; i < sp->typecnt; ++i) {
491 struct ttinfo * ttisp;
493 ttisp = &sp->ttis[i];
495 ttisp->tt_ttisstd = FALSE;
497 ttisp->tt_ttisstd = *p++;
498 if (ttisp->tt_ttisstd != TRUE &&
499 ttisp->tt_ttisstd != FALSE)
503 for (i = 0; i < sp->typecnt; ++i) {
504 struct ttinfo * ttisp;
506 ttisp = &sp->ttis[i];
508 ttisp->tt_ttisgmt = FALSE;
510 ttisp->tt_ttisgmt = *p++;
511 if (ttisp->tt_ttisgmt != TRUE &&
512 ttisp->tt_ttisgmt != FALSE)
517 ** If this is an old file, we're done.
519 if (u->tzhead.tzh_version[0] == '\0')
522 for (i = 0; i < nread; ++i)
525 ** If this is a signed narrow time_t system, we're done.
527 if (TYPE_SIGNED(time_t) && stored >= (int) sizeof(time_t))
530 if (doextend && nread > 2 &&
531 u->buf[0] == '\n' && u->buf[nread - 1] == '\n' &&
532 sp->typecnt + 2 <= TZ_MAX_TYPES) {
536 ts = malloc(sizeof(*ts));
539 u->buf[nread - 1] = '\0';
540 result = tzparse(&u->buf[1], ts, FALSE);
541 if (result == 0 && ts->typecnt == 2 &&
542 sp->charcnt + ts->charcnt <= TZ_MAX_CHARS) {
543 for (i = 0; i < 2; ++i)
544 ts->ttis[i].tt_abbrind +=
546 for (i = 0; i < ts->charcnt; ++i)
547 sp->chars[sp->charcnt++] =
550 while (i < ts->timecnt &&
552 sp->ats[sp->timecnt - 1])
554 while (i < ts->timecnt &&
555 sp->timecnt < TZ_MAX_TIMES) {
556 sp->ats[sp->timecnt] =
558 sp->types[sp->timecnt] =
564 sp->ttis[sp->typecnt++] = ts->ttis[0];
565 sp->ttis[sp->typecnt++] = ts->ttis[1];
569 if (sp->timecnt > 1) {
570 for (i = 1; i < sp->timecnt; ++i)
571 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
572 differ_by_repeat(sp->ats[i], sp->ats[0])) {
576 for (i = sp->timecnt - 2; i >= 0; --i)
577 if (typesequiv(sp, sp->types[sp->timecnt - 1],
579 differ_by_repeat(sp->ats[sp->timecnt - 1],
586 ** If type 0 is is unused in transitions,
587 ** it's the type to use for early times.
589 for (i = 0; i < sp->typecnt; ++i)
590 if (sp->types[i] == 0)
592 i = (i >= sp->typecnt) ? 0 : -1;
595 ** if there are transition times
596 ** and the first transition is to a daylight time
597 ** find the standard type less than and closest to
598 ** the type of the first transition.
600 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
603 if (!sp->ttis[i].tt_isdst)
607 ** If no result yet, find the first standard type.
608 ** If there is none, punt to type zero.
612 while (sp->ttis[i].tt_isdst)
613 if (++i >= sp->typecnt) {
626 typesequiv(const struct state * const sp, const int a, const int b)
631 a < 0 || a >= sp->typecnt ||
632 b < 0 || b >= sp->typecnt)
635 const struct ttinfo * ap = &sp->ttis[a];
636 const struct ttinfo * bp = &sp->ttis[b];
637 result = ap->tt_gmtoff == bp->tt_gmtoff &&
638 ap->tt_isdst == bp->tt_isdst &&
639 ap->tt_ttisstd == bp->tt_ttisstd &&
640 ap->tt_ttisgmt == bp->tt_ttisgmt &&
641 strcmp(&sp->chars[ap->tt_abbrind],
642 &sp->chars[bp->tt_abbrind]) == 0;
647 static const int mon_lengths[2][MONSPERYEAR] = {
648 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
649 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
652 static const int year_lengths[2] = {
653 DAYSPERNYEAR, DAYSPERLYEAR
657 ** Given a pointer into a time zone string, scan until a character that is not
658 ** a valid character in a zone name is found. Return a pointer to that
663 getzname(const char *strp)
667 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
674 ** Given a pointer into an extended time zone string, scan until the ending
675 ** delimiter of the zone name is located. Return a pointer to the delimiter.
677 ** As with getzname above, the legal character set is actually quite
678 ** restricted, with other characters producing undefined results.
679 ** We don't do any checking here; checking is done later in common-case code.
683 getqzname(const char *strp, const int delim)
687 while ((c = *strp) != '\0' && c != delim)
693 ** Given a pointer into a time zone string, extract a number from that string.
694 ** Check that the number is within a specified range; if it is not, return
696 ** Otherwise, return a pointer to the first character not part of the number.
700 getnum(const char *strp, int * const nump, const int min, const int max)
705 if (strp == NULL || !is_digit(c = *strp))
709 num = num * 10 + (c - '0');
711 return NULL; /* illegal value */
713 } while (is_digit(c));
715 return NULL; /* illegal value */
721 ** Given a pointer into a time zone string, extract a number of seconds,
722 ** in hh[:mm[:ss]] form, from the string.
723 ** If any error occurs, return NULL.
724 ** Otherwise, return a pointer to the first character not part of the number
729 getsecs(const char *strp, int_fast32_t * const secsp)
734 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
735 ** "M10.4.6/26", which does not conform to Posix,
736 ** but which specifies the equivalent of
737 ** ``02:00 on the first Sunday on or after 23 Oct''.
739 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
742 *secsp = num * (int_fast32_t) SECSPERHOUR;
745 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
748 *secsp += num * SECSPERMIN;
751 /* `SECSPERMIN' allows for leap seconds. */
752 strp = getnum(strp, &num, 0, SECSPERMIN);
762 ** Given a pointer into a time zone string, extract an offset, in
763 ** [+-]hh[:mm[:ss]] form, from the string.
764 ** If any error occurs, return NULL.
765 ** Otherwise, return a pointer to the first character not part of the time.
769 getoffset(const char *strp, int_fast32_t * const offsetp)
776 } else if (*strp == '+')
778 strp = getsecs(strp, offsetp);
780 return NULL; /* illegal time */
782 *offsetp = -*offsetp;
787 ** Given a pointer into a time zone string, extract a rule in the form
788 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
789 ** If a valid rule is not found, return NULL.
790 ** Otherwise, return a pointer to the first character not part of the rule.
794 getrule(const char *strp, struct rule * const rulep)
800 rulep->r_type = JULIAN_DAY;
802 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
803 } else if (*strp == 'M') {
807 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
809 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
814 strp = getnum(strp, &rulep->r_week, 1, 5);
819 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
820 } else if (is_digit(*strp)) {
824 rulep->r_type = DAY_OF_YEAR;
825 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
826 } else return NULL; /* invalid format */
834 strp = getoffset(strp, &rulep->r_time);
835 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
840 ** Given a year, a rule, and the offset from UT at the time that rule takes
841 ** effect, calculate the year-relative time that rule takes effect.
845 transtime(const int year, const struct rule * const rulep,
846 const int_fast32_t offset)
851 int d, m1, yy0, yy1, yy2, dow;
854 leapyear = isleap(year);
855 switch (rulep->r_type) {
859 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
861 ** In non-leap years, or if the day number is 59 or less, just
862 ** add SECSPERDAY times the day number-1 to the time of
863 ** January 1, midnight, to get the day.
865 value = (rulep->r_day - 1) * SECSPERDAY;
866 if (leapyear && rulep->r_day >= 60)
873 ** Just add SECSPERDAY times the day number to the time of
874 ** January 1, midnight, to get the day.
876 value = rulep->r_day * SECSPERDAY;
879 case MONTH_NTH_DAY_OF_WEEK:
881 ** Mm.n.d - nth "dth day" of month m.
885 ** Use Zeller's Congruence to get day-of-week of first day of
888 m1 = (rulep->r_mon + 9) % 12 + 1;
889 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
892 dow = ((26 * m1 - 2) / 10 +
893 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
898 ** "dow" is the day-of-week of the first day of the month. Get
899 ** the day-of-month (zero-origin) of the first "dow" day of the
902 d = rulep->r_day - dow;
905 for (i = 1; i < rulep->r_week; ++i) {
906 if (d + DAYSPERWEEK >=
907 mon_lengths[leapyear][rulep->r_mon - 1])
913 ** "d" is the day-of-month (zero-origin) of the day we want.
915 value = d * SECSPERDAY;
916 for (i = 0; i < rulep->r_mon - 1; ++i)
917 value += mon_lengths[leapyear][i] * SECSPERDAY;
922 ** "value" is the year-relative time of 00:00:00 UT on the day in
923 ** question. To get the year-relative time of the specified local
924 ** time on that day, add the transition time and the current offset
927 return value + rulep->r_time + offset;
931 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
936 tzparse(const char *name, struct state * const sp, const int lastditch)
938 const char * stdname;
939 const char * dstname;
942 int_fast32_t stdoffset;
943 int_fast32_t dstoffset;
946 static struct ttinfo zttinfo;
951 stdlen = strlen(name); /* length of standard zone name */
953 if (stdlen >= sizeof sp->chars)
954 stdlen = (sizeof sp->chars) - 1;
960 name = getqzname(name, '>');
963 stdlen = name - stdname;
966 name = getzname(name);
967 stdlen = name - stdname;
971 name = getoffset(name, &stdoffset);
975 load_result = tzload(TZDEFRULES, sp, FALSE);
976 if (load_result != 0)
977 sp->leapcnt = 0; /* so, we're off a little */
981 name = getqzname(name, '>');
984 dstlen = name - dstname;
988 name = getzname(name);
989 dstlen = name - dstname; /* length of DST zone name */
991 if (*name != '\0' && *name != ',' && *name != ';') {
992 name = getoffset(name, &dstoffset);
995 } else dstoffset = stdoffset - SECSPERHOUR;
996 if (*name == '\0' && load_result != 0)
997 name = TZDEFRULESTRING;
998 if (*name == ',' || *name == ';') {
1007 if ((name = getrule(name, &start)) == NULL)
1011 if ((name = getrule(name, &end)) == NULL)
1015 sp->typecnt = 2; /* standard time and DST */
1017 ** Two transitions per year, from EPOCH_YEAR forward.
1019 sp->ttis[0] = sp->ttis[1] = zttinfo;
1020 sp->ttis[0].tt_gmtoff = -dstoffset;
1021 sp->ttis[0].tt_isdst = 1;
1022 sp->ttis[0].tt_abbrind = stdlen + 1;
1023 sp->ttis[1].tt_gmtoff = -stdoffset;
1024 sp->ttis[1].tt_isdst = 0;
1025 sp->ttis[1].tt_abbrind = 0;
1028 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1029 for (year = EPOCH_YEAR; year < yearlim; year++) {
1031 starttime = transtime(year, &start, stdoffset),
1032 endtime = transtime(year, &end, dstoffset);
1034 yearsecs = (year_lengths[isleap(year)]
1036 int reversed = endtime < starttime;
1038 int_fast32_t swap = starttime;
1039 starttime = endtime;
1043 || (starttime < endtime
1044 && (endtime - starttime
1046 + (stdoffset - dstoffset))))) {
1047 if (TZ_MAX_TIMES - 2 < timecnt)
1049 yearlim = year + YEARSPERREPEAT + 1;
1050 sp->ats[timecnt] = janfirst;
1051 if (increment_overflow_time
1052 (&sp->ats[timecnt], starttime))
1054 sp->types[timecnt++] = reversed;
1055 sp->ats[timecnt] = janfirst;
1056 if (increment_overflow_time
1057 (&sp->ats[timecnt], endtime))
1059 sp->types[timecnt++] = !reversed;
1061 if (increment_overflow_time(&janfirst, yearsecs))
1064 sp->timecnt = timecnt;
1066 sp->typecnt = 1; /* Perpetual DST. */
1068 int_fast32_t theirstdoffset;
1069 int_fast32_t theirdstoffset;
1070 int_fast32_t theiroffset;
1078 ** Initial values of theirstdoffset and theirdstoffset.
1081 for (i = 0; i < sp->timecnt; ++i) {
1083 if (!sp->ttis[j].tt_isdst) {
1085 -sp->ttis[j].tt_gmtoff;
1090 for (i = 0; i < sp->timecnt; ++i) {
1092 if (sp->ttis[j].tt_isdst) {
1094 -sp->ttis[j].tt_gmtoff;
1099 ** Initially we're assumed to be in standard time.
1102 theiroffset = theirstdoffset;
1104 ** Now juggle transition times and types
1105 ** tracking offsets as you do.
1107 for (i = 0; i < sp->timecnt; ++i) {
1109 sp->types[i] = sp->ttis[j].tt_isdst;
1110 if (sp->ttis[j].tt_ttisgmt) {
1111 /* No adjustment to transition time */
1114 ** If summer time is in effect, and the
1115 ** transition time was not specified as
1116 ** standard time, add the summer time
1117 ** offset to the transition time;
1118 ** otherwise, add the standard time
1119 ** offset to the transition time.
1122 ** Transitions from DST to DDST
1123 ** will effectively disappear since
1124 ** POSIX provides for only one DST
1127 if (isdst && !sp->ttis[j].tt_ttisstd) {
1128 sp->ats[i] += dstoffset -
1131 sp->ats[i] += stdoffset -
1135 theiroffset = -sp->ttis[j].tt_gmtoff;
1136 if (sp->ttis[j].tt_isdst)
1137 theirdstoffset = theiroffset;
1138 else theirstdoffset = theiroffset;
1141 ** Finally, fill in ttis.
1143 sp->ttis[0] = sp->ttis[1] = zttinfo;
1144 sp->ttis[0].tt_gmtoff = -stdoffset;
1145 sp->ttis[0].tt_isdst = FALSE;
1146 sp->ttis[0].tt_abbrind = 0;
1147 sp->ttis[1].tt_gmtoff = -dstoffset;
1148 sp->ttis[1].tt_isdst = TRUE;
1149 sp->ttis[1].tt_abbrind = stdlen + 1;
1154 sp->typecnt = 1; /* only standard time */
1156 sp->ttis[0] = zttinfo;
1157 sp->ttis[0].tt_gmtoff = -stdoffset;
1158 sp->ttis[0].tt_isdst = 0;
1159 sp->ttis[0].tt_abbrind = 0;
1161 sp->charcnt = stdlen + 1;
1163 sp->charcnt += dstlen + 1;
1164 if ((size_t) sp->charcnt > sizeof sp->chars)
1167 strncpy(cp, stdname, stdlen);
1171 strncpy(cp, dstname, dstlen);
1172 *(cp + dstlen) = '\0';
1178 gmtload(struct state * const sp)
1180 if (tzload(gmt, sp, TRUE) != 0)
1181 tzparse(gmt, sp, TRUE);
1185 tzsetwall_basic(int rdlocked)
1188 _RWLOCK_RDLOCK(&lcl_rwlock);
1189 if (lcl_is_set < 0) {
1191 _RWLOCK_UNLOCK(&lcl_rwlock);
1194 _RWLOCK_UNLOCK(&lcl_rwlock);
1196 _RWLOCK_WRLOCK(&lcl_rwlock);
1199 if (tzload(NULL, lclptr, TRUE) != 0)
1202 _RWLOCK_UNLOCK(&lcl_rwlock);
1205 _RWLOCK_RDLOCK(&lcl_rwlock);
1215 tzset_basic(int rdlocked)
1219 name = getenv("TZ");
1221 tzsetwall_basic(rdlocked);
1226 _RWLOCK_RDLOCK(&lcl_rwlock);
1227 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
1229 _RWLOCK_UNLOCK(&lcl_rwlock);
1232 _RWLOCK_UNLOCK(&lcl_rwlock);
1234 _RWLOCK_WRLOCK(&lcl_rwlock);
1235 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1237 strcpy(lcl_TZname, name);
1239 if (*name == '\0') {
1241 ** User wants it fast rather than right.
1243 lclptr->leapcnt = 0; /* so, we're off a little */
1244 lclptr->timecnt = 0;
1245 lclptr->typecnt = 0;
1246 lclptr->ttis[0].tt_isdst = 0;
1247 lclptr->ttis[0].tt_gmtoff = 0;
1248 lclptr->ttis[0].tt_abbrind = 0;
1249 strcpy(lclptr->chars, gmt);
1250 } else if (tzload(name, lclptr, TRUE) != 0)
1251 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1254 _RWLOCK_UNLOCK(&lcl_rwlock);
1257 _RWLOCK_RDLOCK(&lcl_rwlock);
1267 ** The easy way to behave "as if no library function calls" localtime
1268 ** is to not call it--so we drop its guts into "localsub", which can be
1269 ** freely called. (And no, the PANS doesn't require the above behavior--
1270 ** but it *is* desirable.)
1272 ** The unused offset argument is for the benefit of mktime variants.
1277 localsub(const time_t * const timep, const int_fast32_t offset __unused,
1278 struct tm * const tmp)
1281 const struct ttinfo * ttisp;
1284 const time_t t = *timep;
1288 if ((sp->goback && t < sp->ats[0]) ||
1289 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1295 seconds = sp->ats[0] - t;
1296 else seconds = t - sp->ats[sp->timecnt - 1];
1298 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1299 seconds = years * AVGSECSPERYEAR;
1302 else newt -= seconds;
1303 if (newt < sp->ats[0] ||
1304 newt > sp->ats[sp->timecnt - 1])
1305 return NULL; /* "cannot happen" */
1306 result = localsub(&newt, offset, tmp);
1307 if (result == tmp) {
1310 newy = tmp->tm_year;
1314 tmp->tm_year = newy;
1315 if (tmp->tm_year != newy)
1320 if (sp->timecnt == 0 || t < sp->ats[0]) {
1321 i = sp->defaulttype;
1324 int hi = sp->timecnt;
1327 int mid = (lo + hi) >> 1;
1329 if (t < sp->ats[mid])
1333 i = (int) sp->types[lo - 1];
1335 ttisp = &sp->ttis[i];
1337 ** To get (wrong) behavior that's compatible with System V Release 2.0
1338 ** you'd replace the statement below with
1339 ** t += ttisp->tt_gmtoff;
1340 ** timesub(&t, 0L, sp, tmp);
1342 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1343 tmp->tm_isdst = ttisp->tt_isdst;
1344 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1346 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1347 #endif /* defined TM_ZONE */
1352 localtime_key_init(void)
1355 localtime_key_error = _pthread_key_create(&localtime_key, free);
1359 localtime(const time_t * const timep)
1363 if (__isthreaded != 0) {
1364 _pthread_once(&localtime_once, localtime_key_init);
1365 if (localtime_key_error != 0) {
1366 errno = localtime_key_error;
1369 p_tm = _pthread_getspecific(localtime_key);
1371 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1374 _pthread_setspecific(localtime_key, p_tm);
1376 _RWLOCK_RDLOCK(&lcl_rwlock);
1378 localsub(timep, 0L, p_tm);
1379 _RWLOCK_UNLOCK(&lcl_rwlock);
1383 localsub(timep, 0L, &tm);
1389 ** Re-entrant version of localtime.
1393 localtime_r(const time_t * const timep, struct tm *tmp)
1395 _RWLOCK_RDLOCK(&lcl_rwlock);
1397 localsub(timep, 0L, tmp);
1398 _RWLOCK_UNLOCK(&lcl_rwlock);
1409 ** gmtsub is to gmtime as localsub is to localtime.
1413 gmtsub(const time_t * const timep, const int_fast32_t offset,
1414 struct tm * const tmp)
1418 _once(&gmt_once, gmt_init);
1419 result = timesub(timep, offset, gmtptr, tmp);
1422 ** Could get fancy here and deliver something such as
1423 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
1424 ** but this is no time for a treasure hunt.
1427 tmp->TM_ZONE = wildabbr;
1429 tmp->TM_ZONE = gmtptr->chars;
1430 #endif /* defined TM_ZONE */
1435 gmtime_key_init(void)
1437 gmtime_key_error = _pthread_key_create(&gmtime_key, free);
1441 gmtime(const time_t * const timep)
1445 if (__isthreaded != 0) {
1446 _pthread_once(&gmtime_once, gmtime_key_init);
1447 if (gmtime_key_error != 0) {
1448 errno = gmtime_key_error;
1452 * Changed to follow POSIX.1 threads standard, which
1453 * is what BSD currently has.
1455 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1456 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1460 _pthread_setspecific(gmtime_key, p_tm);
1462 return gmtsub(timep, 0L, p_tm);
1464 return gmtsub(timep, 0L, &tm);
1469 * Re-entrant version of gmtime.
1473 gmtime_r(const time_t * timep, struct tm * tmp)
1475 return gmtsub(timep, 0L, tmp);
1479 offtime(const time_t * const timep, const long offset)
1481 return gmtsub(timep, offset, &tm);
1485 ** Return the number of leap years through the end of the given year
1486 ** where, to make the math easy, the answer for year zero is defined as zero.
1490 leaps_thru_end_of(const int y)
1492 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1493 -(leaps_thru_end_of(-(y + 1)) + 1);
1497 timesub(const time_t * const timep, const int_fast32_t offset,
1498 const struct state * const sp, struct tm * const tmp)
1500 const struct lsinfo * lp;
1502 int idays; /* unsigned would be so 2003 */
1516 if (*timep >= lp->ls_trans) {
1517 if (*timep == lp->ls_trans) {
1518 hit = ((i == 0 && lp->ls_corr > 0) ||
1519 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1522 sp->lsis[i].ls_trans ==
1523 sp->lsis[i - 1].ls_trans + 1 &&
1524 sp->lsis[i].ls_corr ==
1525 sp->lsis[i - 1].ls_corr + 1) {
1535 tdays = *timep / SECSPERDAY;
1536 rem = *timep - tdays * SECSPERDAY;
1537 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1543 tdelta = tdays / DAYSPERLYEAR;
1544 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1545 && tdelta <= INT_MAX))
1549 idelta = (tdays < 0) ? -1 : 1;
1551 if (increment_overflow(&newy, idelta))
1553 leapdays = leaps_thru_end_of(newy - 1) -
1554 leaps_thru_end_of(y - 1);
1555 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1560 int_fast32_t seconds;
1562 seconds = tdays * SECSPERDAY;
1563 tdays = seconds / SECSPERDAY;
1564 rem += seconds - tdays * SECSPERDAY;
1567 ** Given the range, we can now fearlessly cast...
1570 rem += offset - corr;
1575 while (rem >= SECSPERDAY) {
1580 if (increment_overflow(&y, -1))
1582 idays += year_lengths[isleap(y)];
1584 while (idays >= year_lengths[isleap(y)]) {
1585 idays -= year_lengths[isleap(y)];
1586 if (increment_overflow(&y, 1))
1590 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1592 tmp->tm_yday = idays;
1594 ** The "extra" mods below avoid overflow problems.
1596 tmp->tm_wday = EPOCH_WDAY +
1597 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1598 (DAYSPERNYEAR % DAYSPERWEEK) +
1599 leaps_thru_end_of(y - 1) -
1600 leaps_thru_end_of(EPOCH_YEAR - 1) +
1602 tmp->tm_wday %= DAYSPERWEEK;
1603 if (tmp->tm_wday < 0)
1604 tmp->tm_wday += DAYSPERWEEK;
1605 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1607 tmp->tm_min = (int) (rem / SECSPERMIN);
1609 ** A positive leap second requires a special
1610 ** representation. This uses "... ??:59:60" et seq.
1612 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1613 ip = mon_lengths[isleap(y)];
1614 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1615 idays -= ip[tmp->tm_mon];
1616 tmp->tm_mday = (int) (idays + 1);
1619 tmp->TM_GMTOFF = offset;
1620 #endif /* defined TM_GMTOFF */
1625 ctime(const time_t * const timep)
1628 ** Section 4.12.3.2 of X3.159-1989 requires that
1629 ** The ctime function converts the calendar time pointed to by timer
1630 ** to local time in the form of a string. It is equivalent to
1631 ** asctime(localtime(timer))
1633 return asctime(localtime(timep));
1637 ctime_r(const time_t * const timep, char *buf)
1641 return asctime_r(localtime_r(timep, &mytm), buf);
1645 ** Adapted from code provided by Robert Elz, who writes:
1646 ** The "best" way to do mktime I think is based on an idea of Bob
1647 ** Kridle's (so its said...) from a long time ago.
1648 ** It does a binary search of the time_t space. Since time_t's are
1649 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1650 ** would still be very reasonable).
1655 #endif /* !defined WRONG */
1658 ** Normalize logic courtesy Paul Eggert.
1662 increment_overflow(int * const ip, int j)
1667 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1668 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1669 ** If i < 0 there can only be overflow if i + j < INT_MIN
1670 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1672 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1679 increment_overflow32(int_fast32_t * const lp, int const m)
1681 int_fast32_t const l = *lp;
1683 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1690 increment_overflow_time(time_t *tp, int_fast32_t j)
1694 ** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
1695 ** except that it does the right thing even if *tp + j would overflow.
1698 ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
1699 : *tp <= time_t_max - j))
1706 normalize_overflow(int * const tensptr, int * const unitsptr, const int base)
1710 tensdelta = (*unitsptr >= 0) ?
1711 (*unitsptr / base) :
1712 (-1 - (-1 - *unitsptr) / base);
1713 *unitsptr -= tensdelta * base;
1714 return increment_overflow(tensptr, tensdelta);
1718 normalize_overflow32(int_fast32_t * const tensptr, int * const unitsptr,
1723 tensdelta = (*unitsptr >= 0) ?
1724 (*unitsptr / base) :
1725 (-1 - (-1 - *unitsptr) / base);
1726 *unitsptr -= tensdelta * base;
1727 return increment_overflow32(tensptr, tensdelta);
1731 tmcomp(const struct tm * const atmp, const struct tm * const btmp)
1735 if (atmp->tm_year != btmp->tm_year)
1736 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1737 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1738 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1739 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1740 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1741 result = atmp->tm_sec - btmp->tm_sec;
1746 time2sub(struct tm * const tmp,
1747 struct tm * (* const funcp)(const time_t *, int_fast32_t, struct tm *),
1748 const int_fast32_t offset, int * const okayp, const int do_norm_secs)
1750 const struct state * sp;
1760 struct tm yourtm, mytm;
1765 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1769 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1771 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1774 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
1777 ** Turn y into an actual year number for now.
1778 ** It is converted back to an offset from TM_YEAR_BASE later.
1780 if (increment_overflow32(&y, TM_YEAR_BASE))
1782 while (yourtm.tm_mday <= 0) {
1783 if (increment_overflow32(&y, -1))
1785 li = y + (1 < yourtm.tm_mon);
1786 yourtm.tm_mday += year_lengths[isleap(li)];
1788 while (yourtm.tm_mday > DAYSPERLYEAR) {
1789 li = y + (1 < yourtm.tm_mon);
1790 yourtm.tm_mday -= year_lengths[isleap(li)];
1791 if (increment_overflow32(&y, 1))
1795 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1796 if (yourtm.tm_mday <= i)
1798 yourtm.tm_mday -= i;
1799 if (++yourtm.tm_mon >= MONSPERYEAR) {
1801 if (increment_overflow32(&y, 1))
1805 if (increment_overflow32(&y, -TM_YEAR_BASE))
1808 if (yourtm.tm_year != y)
1810 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1812 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1814 ** We can't set tm_sec to 0, because that might push the
1815 ** time below the minimum representable time.
1816 ** Set tm_sec to 59 instead.
1817 ** This assumes that the minimum representable time is
1818 ** not in the same minute that a leap second was deleted from,
1819 ** which is a safer assumption than using 58 would be.
1821 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1823 saved_seconds = yourtm.tm_sec;
1824 yourtm.tm_sec = SECSPERMIN - 1;
1826 saved_seconds = yourtm.tm_sec;
1830 ** Do a binary search (this works whatever time_t's type is).
1832 if (!TYPE_SIGNED(time_t)) {
1837 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1842 t = lo / 2 + hi / 2;
1847 if ((*funcp)(&t, offset, &mytm) == NULL) {
1849 ** Assume that t is too extreme to be represented in
1850 ** a struct tm; arrange things so that it is less
1851 ** extreme on the next pass.
1853 dir = (t > 0) ? 1 : -1;
1854 } else dir = tmcomp(&mytm, &yourtm);
1857 if (t == time_t_max)
1861 } else if (t == hi) {
1862 if (t == time_t_min)
1874 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1877 ** Right time, wrong type.
1878 ** Hunt for right time, right type.
1879 ** It's okay to guess wrong since the guess
1882 sp = (const struct state *)
1883 ((funcp == localsub) ? lclptr : gmtptr);
1885 for (i = sp->typecnt - 1; i >= 0; --i) {
1886 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1888 for (j = sp->typecnt - 1; j >= 0; --j) {
1889 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1891 newt = t + sp->ttis[j].tt_gmtoff -
1892 sp->ttis[i].tt_gmtoff;
1893 if ((*funcp)(&newt, offset, &mytm) == NULL)
1895 if (tmcomp(&mytm, &yourtm) != 0)
1897 if (mytm.tm_isdst != yourtm.tm_isdst)
1909 newt = t + saved_seconds;
1910 if ((newt < t) != (saved_seconds < 0))
1913 if ((*funcp)(&t, offset, tmp))
1919 time2(struct tm * const tmp,
1920 struct tm * (* const funcp)(const time_t *, int_fast32_t, struct tm *),
1921 const int_fast32_t offset, int * const okayp)
1926 ** First try without normalization of seconds
1927 ** (in case tm_sec contains a value associated with a leap second).
1928 ** If that fails, try with normalization of seconds.
1930 t = time2sub(tmp, funcp, offset, okayp, FALSE);
1931 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1935 time1(struct tm * const tmp,
1936 struct tm * (* const funcp)(const time_t *, int_fast32_t, struct tm *),
1937 const int_fast32_t offset)
1940 const struct state * sp;
1942 int sameind, otherind;
1945 int seen[TZ_MAX_TYPES];
1946 int types[TZ_MAX_TYPES];
1953 if (tmp->tm_isdst > 1)
1955 t = time2(tmp, funcp, offset, &okay);
1958 ** PCTS code courtesy Grant Sullivan.
1962 if (tmp->tm_isdst < 0)
1963 tmp->tm_isdst = 0; /* reset to std and try again */
1966 ** We're supposed to assume that somebody took a time of one type
1967 ** and did some math on it that yielded a "struct tm" that's bad.
1968 ** We try to divine the type they started from and adjust to the
1971 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
1973 for (i = 0; i < sp->typecnt; ++i)
1976 for (i = sp->timecnt - 1; i >= 0; --i)
1977 if (!seen[sp->types[i]]) {
1978 seen[sp->types[i]] = TRUE;
1979 types[nseen++] = sp->types[i];
1981 for (sameind = 0; sameind < nseen; ++sameind) {
1982 samei = types[sameind];
1983 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1985 for (otherind = 0; otherind < nseen; ++otherind) {
1986 otheri = types[otherind];
1987 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1989 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1990 sp->ttis[samei].tt_gmtoff;
1991 tmp->tm_isdst = !tmp->tm_isdst;
1992 t = time2(tmp, funcp, offset, &okay);
1995 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1996 sp->ttis[samei].tt_gmtoff;
1997 tmp->tm_isdst = !tmp->tm_isdst;
2004 mktime(struct tm * const tmp)
2006 time_t mktime_return_value;
2007 _RWLOCK_RDLOCK(&lcl_rwlock);
2009 mktime_return_value = time1(tmp, localsub, 0L);
2010 _RWLOCK_UNLOCK(&lcl_rwlock);
2011 return(mktime_return_value);
2015 timelocal(struct tm * const tmp)
2018 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2023 timegm(struct tm * const tmp)
2027 return time1(tmp, gmtsub, 0L);
2031 timeoff(struct tm * const tmp, const long offset)
2035 return time1(tmp, gmtsub, offset);
2039 ** XXX--is the below the right way to conditionalize??
2043 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2044 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2045 ** is not the case if we are accounting for leap seconds.
2046 ** So, we provide the following conversion routines for use
2047 ** when exchanging timestamps with POSIX conforming systems.
2051 leapcorr(time_t *timep)
2061 if (*timep >= lp->ls_trans)
2068 time2posix(time_t t)
2071 return t - leapcorr(&t);
2075 posix2time(time_t t)
2082 ** For a positive leap second hit, the result
2083 ** is not unique. For a negative leap second
2084 ** hit, the corresponding time doesn't exist,
2085 ** so we return an adjacent second.
2087 x = t + leapcorr(&t);
2088 y = x - leapcorr(&x);
2092 y = x - leapcorr(&x);
2099 y = x - leapcorr(&x);