2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
6 ** $FreeBSD: src/lib/libc/stdtime/localtime.c,v 1.25.2.2 2002/08/13 16:08:07 bmilekic Exp $
7 ** $DragonFly: src/lib/libc/stdtime/localtime.c,v 1.7 2008/10/19 20:15:58 swildner Exp $
11 ** Leap second handling from Bradley White.
12 ** POSIX-style TZ environment variable handling from Guy Harris.
17 #include "namespace.h"
18 #include <sys/types.h>
22 #include <float.h> /* for FLT_MAX and DBL_MAX */
26 #include <un-namespace.h>
30 #include "libc_private.h"
32 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
33 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
35 #ifndef TZ_ABBR_MAX_LEN
36 #define TZ_ABBR_MAX_LEN 16
37 #endif /* !defined TZ_ABBR_MAX_LEN */
39 #ifndef TZ_ABBR_CHAR_SET
40 #define TZ_ABBR_CHAR_SET \
41 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
42 #endif /* !defined TZ_ABBR_CHAR_SET */
44 #ifndef TZ_ABBR_ERR_CHAR
45 #define TZ_ABBR_ERR_CHAR '_'
46 #endif /* !defined TZ_ABBR_ERR_CHAR */
49 ** SunOS 4.1.1 headers lack O_BINARY.
53 #define OPEN_MODE (O_RDONLY | O_BINARY)
54 #endif /* defined O_BINARY */
56 #define OPEN_MODE O_RDONLY
57 #endif /* !defined O_BINARY */
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).
80 #endif /* !defined WILDABBR */
82 static char wildabbr[] = WILDABBR;
84 static const char gmt[] = "GMT";
87 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
88 ** We default to US rules as of 1999-08-17.
89 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
90 ** implementation dependent; for historical reasons, US rules are a
93 #ifndef TZDEFRULESTRING
94 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
95 #endif /* !defined TZDEFDST */
97 struct ttinfo { /* time type information */
98 long tt_gmtoff; /* UTC offset in seconds */
99 int tt_isdst; /* used to set tm_isdst */
100 int tt_abbrind; /* abbreviation list index */
101 int tt_ttisstd; /* TRUE if transition is std time */
102 int tt_ttisgmt; /* TRUE if transition is UTC */
105 struct lsinfo { /* leap second information */
106 time_t ls_trans; /* transition time */
107 long ls_corr; /* correction to apply */
110 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
113 #define MY_TZNAME_MAX TZNAME_MAX
114 #endif /* defined TZNAME_MAX */
116 #define MY_TZNAME_MAX 255
117 #endif /* !defined TZNAME_MAX */
126 time_t ats[TZ_MAX_TIMES];
127 unsigned char types[TZ_MAX_TIMES];
128 struct ttinfo ttis[TZ_MAX_TYPES];
129 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
130 (2 * (MY_TZNAME_MAX + 1)))];
131 struct lsinfo lsis[TZ_MAX_LEAPS];
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 long 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 long detzcode(const char * codep);
151 static time_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);
154 static const char * getqzname(const char * strp, const int delim);
155 static const char * getnum(const char * strp, int * nump, int min,
157 static const char * getsecs(const char * strp, long * secsp);
158 static const char * getoffset(const char * strp, long * 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, long offset,
163 static struct tm * localsub(const time_t * timep, long offset,
165 static int increment_overflow(int * number, int delta);
166 static int leaps_thru_end_of(int y);
167 static int long_increment_overflow(long * number, int delta);
168 static int long_normalize_overflow(long * tensptr,
169 int * unitsptr, int base);
170 static int normalize_overflow(int * tensptr, int * unitsptr,
172 static void settzname(void);
173 static time_t time1(struct tm * tmp,
174 struct tm * (*funcp)(const time_t *,
177 static time_t time2(struct tm *tmp,
178 struct tm * (*funcp)(const time_t *,
180 long offset, int * okayp);
181 static time_t time2sub(struct tm *tmp,
182 struct tm * (*funcp)(const time_t *,
184 long offset, int * okayp, int do_norm_secs);
185 static struct tm * timesub(const time_t * timep, long offset,
186 const struct state * sp, struct tm * tmp);
187 static int tmcomp(const struct tm * atmp,
188 const struct tm * btmp);
189 static time_t transtime(time_t janfirst, int year,
190 const struct rule * rulep, long offset);
191 static int typesequiv(const struct state * sp, int a, int b);
192 static int tzload(const char * name, struct state * sp,
194 static int tzparse(const char * name, struct state * sp,
198 static struct state * lclptr;
199 static struct state * gmtptr;
200 #endif /* defined ALL_STATE */
203 static struct state lclmem;
204 static struct state gmtmem;
205 #define lclptr (&lclmem)
206 #define gmtptr (&gmtmem)
207 #endif /* State Farm */
209 #ifndef TZ_STRLEN_MAX
210 #define TZ_STRLEN_MAX 255
211 #endif /* !defined TZ_STRLEN_MAX */
213 static char lcl_TZname[TZ_STRLEN_MAX + 1];
214 static int lcl_is_set;
215 static int gmt_is_set;
216 static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
217 static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
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.
237 #endif /* defined USG_COMPAT */
241 #endif /* defined ALTZONE */
244 detzcode(const char * const codep)
249 result = (codep[0] & 0x80) ? ~0L : 0;
250 for (i = 0; i < 4; ++i)
251 result = (result << 8) | (codep[i] & 0xff);
256 detzcode64(const char * const codep)
261 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
262 for (i = 0; i < 8; ++i)
263 result = result * 256 + (codep[i] & 0xff);
270 struct state * const sp = lclptr;
273 tzname[0] = wildabbr;
274 tzname[1] = wildabbr;
278 #endif /* defined USG_COMPAT */
281 #endif /* defined ALTZONE */
284 tzname[0] = tzname[1] = gmt;
287 #endif /* defined ALL_STATE */
288 for (i = 0; i < sp->typecnt; ++i) {
289 const struct ttinfo * const ttisp = &sp->ttis[i];
291 tzname[ttisp->tt_isdst] =
292 &sp->chars[ttisp->tt_abbrind];
296 if (i == 0 || !ttisp->tt_isdst)
297 timezone = -(ttisp->tt_gmtoff);
298 #endif /* defined USG_COMPAT */
300 if (i == 0 || ttisp->tt_isdst)
301 altzone = -(ttisp->tt_gmtoff);
302 #endif /* defined ALTZONE */
305 ** And to get the latest zone names into tzname. . .
307 for (i = 0; i < sp->timecnt; ++i) {
308 const struct ttinfo * const ttisp =
312 tzname[ttisp->tt_isdst] =
313 &sp->chars[ttisp->tt_abbrind];
316 ** Finally, scrub the abbreviations.
317 ** First, replace bogus characters.
319 for (i = 0; i < sp->charcnt; ++i)
320 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
321 sp->chars[i] = TZ_ABBR_ERR_CHAR;
323 ** Second, truncate long abbreviations.
325 for (i = 0; i < sp->typecnt; ++i) {
326 const struct ttinfo * const ttisp = &sp->ttis[i];
327 char * cp = &sp->chars[ttisp->tt_abbrind];
329 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
330 strcmp(cp, GRANDPARENTED) != 0)
331 *(cp + TZ_ABBR_MAX_LEN) = '\0';
336 differ_by_repeat(const time_t t1, const time_t t0)
338 if (TYPE_INTEGRAL(time_t) &&
339 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
341 return t1 - t0 == SECSPERREPEAT;
345 tzload(const char *name, struct state * const sp, const int doextend)
353 struct tzhead tzhead;
354 char buf[2 * sizeof(struct tzhead) +
359 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
360 if (name != NULL && issetugid() != 0)
361 if ((name[0] == ':' && name[1] == '/') ||
362 name[0] == '/' || strchr(name, '.'))
364 if (name == NULL && (name = TZDEFAULT) == NULL)
370 ** Section 4.9.1 of the C standard says that
371 ** "FILENAME_MAX expands to an integral constant expression
372 ** that is the size needed for an array of char large enough
373 ** to hold the longest file name string that the implementation
374 ** guarantees can be opened."
376 char fullname[FILENAME_MAX + 1];
380 doaccess = name[0] == '/';
382 if ((p = TZDIR) == NULL)
384 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
387 strcat(fullname, "/");
388 strcat(fullname, name);
390 ** Set doaccess if '.' (as in "../") shows up in name.
392 if (strchr(name, '.') != NULL)
396 if (doaccess && access(name, R_OK) != 0)
398 if ((fid = _open(name, OPEN_MODE)) == -1)
400 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
405 nread = read(fid, u.buf, sizeof u.buf);
406 if (close(fid) < 0 || nread <= 0)
408 for (stored = 4; stored <= 8; stored *= 2) {
412 ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
413 ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
414 sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
415 sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
416 sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
417 sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
418 p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
419 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
420 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
421 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
422 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
423 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
424 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
426 if (nread - (p - u.buf) <
427 sp->timecnt * stored + /* ats */
428 sp->timecnt + /* types */
429 sp->typecnt * 6 + /* ttinfos */
430 sp->charcnt + /* chars */
431 sp->leapcnt * (stored + 4) + /* lsinfos */
432 ttisstdcnt + /* ttisstds */
433 ttisgmtcnt) /* ttisgmts */
435 for (i = 0; i < sp->timecnt; ++i) {
436 sp->ats[i] = (stored == 4) ?
437 detzcode(p) : detzcode64(p);
440 for (i = 0; i < sp->timecnt; ++i) {
441 sp->types[i] = (unsigned char) *p++;
442 if (sp->types[i] >= sp->typecnt)
445 for (i = 0; i < sp->typecnt; ++i) {
446 struct ttinfo * ttisp;
448 ttisp = &sp->ttis[i];
449 ttisp->tt_gmtoff = detzcode(p);
451 ttisp->tt_isdst = (unsigned char) *p++;
452 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
454 ttisp->tt_abbrind = (unsigned char) *p++;
455 if (ttisp->tt_abbrind < 0 ||
456 ttisp->tt_abbrind > sp->charcnt)
459 for (i = 0; i < sp->charcnt; ++i)
461 sp->chars[i] = '\0'; /* ensure '\0' at end */
462 for (i = 0; i < sp->leapcnt; ++i) {
463 struct lsinfo * lsisp;
465 lsisp = &sp->lsis[i];
466 lsisp->ls_trans = (stored == 4) ?
467 detzcode(p) : detzcode64(p);
469 lsisp->ls_corr = detzcode(p);
472 for (i = 0; i < sp->typecnt; ++i) {
473 struct ttinfo * ttisp;
475 ttisp = &sp->ttis[i];
477 ttisp->tt_ttisstd = FALSE;
479 ttisp->tt_ttisstd = *p++;
480 if (ttisp->tt_ttisstd != TRUE &&
481 ttisp->tt_ttisstd != FALSE)
485 for (i = 0; i < sp->typecnt; ++i) {
486 struct ttinfo * ttisp;
488 ttisp = &sp->ttis[i];
490 ttisp->tt_ttisgmt = FALSE;
492 ttisp->tt_ttisgmt = *p++;
493 if (ttisp->tt_ttisgmt != TRUE &&
494 ttisp->tt_ttisgmt != FALSE)
499 ** Out-of-sort ats should mean we're running on a
500 ** signed time_t system but using a data file with
501 ** unsigned values (or vice versa).
503 for (i = 0; i < sp->timecnt - 2; ++i)
504 if (sp->ats[i] > sp->ats[i + 1]) {
506 if (TYPE_SIGNED(time_t)) {
508 ** Ignore the end (easy).
513 ** Ignore the beginning (harder).
517 for (j = 0; j + i < sp->timecnt; ++j) {
518 sp->ats[j] = sp->ats[j + i];
519 sp->types[j] = sp->types[j + i];
526 ** If this is an old file, we're done.
528 if (u.tzhead.tzh_version[0] == '\0')
531 for (i = 0; i < nread; ++i)
534 ** If this is a narrow integer time_t system, we're done.
536 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
539 if (doextend && nread > 2 &&
540 u.buf[0] == '\n' && u.buf[nread - 1] == '\n' &&
541 sp->typecnt + 2 <= TZ_MAX_TYPES) {
545 u.buf[nread - 1] = '\0';
546 result = tzparse(&u.buf[1], &ts, FALSE);
547 if (result == 0 && ts.typecnt == 2 &&
548 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
549 for (i = 0; i < 2; ++i)
550 ts.ttis[i].tt_abbrind +=
552 for (i = 0; i < ts.charcnt; ++i)
553 sp->chars[sp->charcnt++] =
556 while (i < ts.timecnt &&
558 sp->ats[sp->timecnt - 1])
560 while (i < ts.timecnt &&
561 sp->timecnt < TZ_MAX_TIMES) {
562 sp->ats[sp->timecnt] =
564 sp->types[sp->timecnt] =
570 sp->ttis[sp->typecnt++] = ts.ttis[0];
571 sp->ttis[sp->typecnt++] = ts.ttis[1];
574 sp->goback = sp->goahead = FALSE;
575 if (sp->timecnt > 1) {
576 for (i = 1; i < sp->timecnt; ++i)
577 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
578 differ_by_repeat(sp->ats[i], sp->ats[0])) {
582 for (i = sp->timecnt - 2; i >= 0; --i)
583 if (typesequiv(sp, sp->types[sp->timecnt - 1],
585 differ_by_repeat(sp->ats[sp->timecnt - 1],
595 typesequiv(const struct state * const sp, const int a, const int b)
600 a < 0 || a >= sp->typecnt ||
601 b < 0 || b >= sp->typecnt)
604 const struct ttinfo * ap = &sp->ttis[a];
605 const struct ttinfo * bp = &sp->ttis[b];
606 result = ap->tt_gmtoff == bp->tt_gmtoff &&
607 ap->tt_isdst == bp->tt_isdst &&
608 ap->tt_ttisstd == bp->tt_ttisstd &&
609 ap->tt_ttisgmt == bp->tt_ttisgmt &&
610 strcmp(&sp->chars[ap->tt_abbrind],
611 &sp->chars[bp->tt_abbrind]) == 0;
616 static const int mon_lengths[2][MONSPERYEAR] = {
617 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
618 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
621 static const int year_lengths[2] = {
622 DAYSPERNYEAR, DAYSPERLYEAR
626 ** Given a pointer into a time zone string, scan until a character that is not
627 ** a valid character in a zone name is found. Return a pointer to that
632 getzname(const char *strp)
636 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
643 ** Given a pointer into an extended time zone string, scan until the ending
644 ** delimiter of the zone name is located. Return a pointer to the delimiter.
646 ** As with getzname above, the legal character set is actually quite
647 ** restricted, with other characters producing undefined results.
648 ** We don't do any checking here; checking is done later in common-case code.
652 getqzname(const char *strp, const int delim)
656 while ((c = *strp) != '\0' && c != delim)
662 ** Given a pointer into a time zone string, extract a number from that string.
663 ** Check that the number is within a specified range; if it is not, return
665 ** Otherwise, return a pointer to the first character not part of the number.
669 getnum(const char *strp, int * const nump, const int min, const int max)
674 if (strp == NULL || !is_digit(c = *strp))
678 num = num * 10 + (c - '0');
680 return NULL; /* illegal value */
682 } while (is_digit(c));
684 return NULL; /* illegal value */
690 ** Given a pointer into a time zone string, extract a number of seconds,
691 ** in hh[:mm[:ss]] form, from the string.
692 ** If any error occurs, return NULL.
693 ** Otherwise, return a pointer to the first character not part of the number
698 getsecs(const char *strp, long * const secsp)
703 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
704 ** "M10.4.6/26", which does not conform to Posix,
705 ** but which specifies the equivalent of
706 ** ``02:00 on the first Sunday on or after 23 Oct''.
708 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
711 *secsp = num * (long) SECSPERHOUR;
714 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
717 *secsp += num * SECSPERMIN;
720 /* `SECSPERMIN' allows for leap seconds. */
721 strp = getnum(strp, &num, 0, SECSPERMIN);
731 ** Given a pointer into a time zone string, extract an offset, in
732 ** [+-]hh[:mm[:ss]] form, from the string.
733 ** If any error occurs, return NULL.
734 ** Otherwise, return a pointer to the first character not part of the time.
738 getoffset(const char *strp, long * const offsetp)
745 } else if (*strp == '+')
747 strp = getsecs(strp, offsetp);
749 return NULL; /* illegal time */
751 *offsetp = -*offsetp;
756 ** Given a pointer into a time zone string, extract a rule in the form
757 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
758 ** If a valid rule is not found, return NULL.
759 ** Otherwise, return a pointer to the first character not part of the rule.
763 getrule(const char *strp, struct rule * const rulep)
769 rulep->r_type = JULIAN_DAY;
771 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
772 } else if (*strp == 'M') {
776 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
778 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
783 strp = getnum(strp, &rulep->r_week, 1, 5);
788 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
789 } else if (is_digit(*strp)) {
793 rulep->r_type = DAY_OF_YEAR;
794 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
795 } else return NULL; /* invalid format */
803 strp = getsecs(strp, &rulep->r_time);
804 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
809 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
810 ** year, a rule, and the offset from UTC at the time that rule takes effect,
811 ** calculate the Epoch-relative time that rule takes effect.
815 transtime(const time_t janfirst, const int year,
816 const struct rule * const rulep, const long offset)
821 int d, m1, yy0, yy1, yy2, dow;
824 leapyear = isleap(year);
825 switch (rulep->r_type) {
829 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
831 ** In non-leap years, or if the day number is 59 or less, just
832 ** add SECSPERDAY times the day number-1 to the time of
833 ** January 1, midnight, to get the day.
835 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
836 if (leapyear && rulep->r_day >= 60)
843 ** Just add SECSPERDAY times the day number to the time of
844 ** January 1, midnight, to get the day.
846 value = janfirst + rulep->r_day * SECSPERDAY;
849 case MONTH_NTH_DAY_OF_WEEK:
851 ** Mm.n.d - nth "dth day" of month m.
854 for (i = 0; i < rulep->r_mon - 1; ++i)
855 value += mon_lengths[leapyear][i] * SECSPERDAY;
858 ** Use Zeller's Congruence to get day-of-week of first day of
861 m1 = (rulep->r_mon + 9) % 12 + 1;
862 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
865 dow = ((26 * m1 - 2) / 10 +
866 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
871 ** "dow" is the day-of-week of the first day of the month. Get
872 ** the day-of-month (zero-origin) of the first "dow" day of the
875 d = rulep->r_day - dow;
878 for (i = 1; i < rulep->r_week; ++i) {
879 if (d + DAYSPERWEEK >=
880 mon_lengths[leapyear][rulep->r_mon - 1])
886 ** "d" is the day-of-month (zero-origin) of the day we want.
888 value += d * SECSPERDAY;
893 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
894 ** question. To get the Epoch-relative time of the specified local
895 ** time on that day, add the transition time and the current offset
898 return value + rulep->r_time + offset;
902 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
907 tzparse(const char *name, struct state * const sp, const int lastditch)
909 const char * stdname;
910 const char * dstname;
916 unsigned char * typep;
923 stdlen = strlen(name); /* length of standard zone name */
925 if (stdlen >= sizeof sp->chars)
926 stdlen = (sizeof sp->chars) - 1;
932 name = getqzname(name, '>');
935 stdlen = name - stdname;
938 name = getzname(name);
939 stdlen = name - stdname;
943 name = getoffset(name, &stdoffset);
947 load_result = tzload(TZDEFRULES, sp, FALSE);
948 if (load_result != 0)
949 sp->leapcnt = 0; /* so, we're off a little */
953 name = getqzname(name, '>');
956 dstlen = name - dstname;
960 name = getzname(name);
961 dstlen = name - dstname; /* length of DST zone name */
963 if (*name != '\0' && *name != ',' && *name != ';') {
964 name = getoffset(name, &dstoffset);
967 } else dstoffset = stdoffset - SECSPERHOUR;
968 if (*name == '\0' && load_result != 0)
969 name = TZDEFRULESTRING;
970 if (*name == ',' || *name == ';') {
979 if ((name = getrule(name, &start)) == NULL)
983 if ((name = getrule(name, &end)) == NULL)
987 sp->typecnt = 2; /* standard time and DST */
989 ** Two transitions per year, from EPOCH_YEAR forward.
991 sp->ttis[0].tt_gmtoff = -dstoffset;
992 sp->ttis[0].tt_isdst = 1;
993 sp->ttis[0].tt_abbrind = stdlen + 1;
994 sp->ttis[1].tt_gmtoff = -stdoffset;
995 sp->ttis[1].tt_isdst = 0;
996 sp->ttis[1].tt_abbrind = 0;
1001 for (year = EPOCH_YEAR;
1002 sp->timecnt + 2 <= TZ_MAX_TIMES;
1006 starttime = transtime(janfirst, year, &start,
1008 endtime = transtime(janfirst, year, &end,
1010 if (starttime > endtime) {
1012 *typep++ = 1; /* DST ends */
1014 *typep++ = 0; /* DST begins */
1017 *typep++ = 0; /* DST begins */
1019 *typep++ = 1; /* DST ends */
1022 newfirst = janfirst;
1023 newfirst += year_lengths[isleap(year)] *
1025 if (newfirst <= janfirst)
1027 janfirst = newfirst;
1030 long theirstdoffset;
1031 long theirdstoffset;
1040 ** Initial values of theirstdoffset and theirdstoffset.
1043 for (i = 0; i < sp->timecnt; ++i) {
1045 if (!sp->ttis[j].tt_isdst) {
1047 -sp->ttis[j].tt_gmtoff;
1052 for (i = 0; i < sp->timecnt; ++i) {
1054 if (sp->ttis[j].tt_isdst) {
1056 -sp->ttis[j].tt_gmtoff;
1061 ** Initially we're assumed to be in standard time.
1064 theiroffset = theirstdoffset;
1066 ** Now juggle transition times and types
1067 ** tracking offsets as you do.
1069 for (i = 0; i < sp->timecnt; ++i) {
1071 sp->types[i] = sp->ttis[j].tt_isdst;
1072 if (sp->ttis[j].tt_ttisgmt) {
1073 /* No adjustment to transition time */
1076 ** If summer time is in effect, and the
1077 ** transition time was not specified as
1078 ** standard time, add the summer time
1079 ** offset to the transition time;
1080 ** otherwise, add the standard time
1081 ** offset to the transition time.
1084 ** Transitions from DST to DDST
1085 ** will effectively disappear since
1086 ** POSIX provides for only one DST
1089 if (isdst && !sp->ttis[j].tt_ttisstd) {
1090 sp->ats[i] += dstoffset -
1093 sp->ats[i] += stdoffset -
1097 theiroffset = -sp->ttis[j].tt_gmtoff;
1098 if (sp->ttis[j].tt_isdst)
1099 theirdstoffset = theiroffset;
1100 else theirstdoffset = theiroffset;
1103 ** Finally, fill in ttis.
1104 ** ttisstd and ttisgmt need not be handled.
1106 sp->ttis[0].tt_gmtoff = -stdoffset;
1107 sp->ttis[0].tt_isdst = FALSE;
1108 sp->ttis[0].tt_abbrind = 0;
1109 sp->ttis[1].tt_gmtoff = -dstoffset;
1110 sp->ttis[1].tt_isdst = TRUE;
1111 sp->ttis[1].tt_abbrind = stdlen + 1;
1116 sp->typecnt = 1; /* only standard time */
1118 sp->ttis[0].tt_gmtoff = -stdoffset;
1119 sp->ttis[0].tt_isdst = 0;
1120 sp->ttis[0].tt_abbrind = 0;
1122 sp->charcnt = stdlen + 1;
1124 sp->charcnt += dstlen + 1;
1125 if ((size_t) sp->charcnt > sizeof sp->chars)
1128 strncpy(cp, stdname, stdlen);
1132 strncpy(cp, dstname, dstlen);
1133 *(cp + dstlen) = '\0';
1139 gmtload(struct state * const sp)
1141 if (tzload(gmt, sp, TRUE) != 0)
1142 tzparse(gmt, sp, TRUE);
1146 tzsetwall_basic(void)
1153 if (lclptr == NULL) {
1154 lclptr = (struct state *) malloc(sizeof *lclptr);
1155 if (lclptr == NULL) {
1156 settzname(); /* all we can do */
1160 #endif /* defined ALL_STATE */
1161 if (tzload((char *) NULL, lclptr, TRUE) != 0)
1169 _MUTEX_LOCK(&lcl_mutex);
1171 _MUTEX_UNLOCK(&lcl_mutex);
1179 name = getenv("TZ");
1185 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1187 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1189 strcpy(lcl_TZname, name);
1192 if (lclptr == NULL) {
1193 lclptr = (struct state *) malloc(sizeof *lclptr);
1194 if (lclptr == NULL) {
1195 settzname(); /* all we can do */
1199 #endif /* defined ALL_STATE */
1200 if (*name == '\0') {
1202 ** User wants it fast rather than right.
1204 lclptr->leapcnt = 0; /* so, we're off a little */
1205 lclptr->timecnt = 0;
1206 lclptr->typecnt = 0;
1207 lclptr->ttis[0].tt_isdst = 0;
1208 lclptr->ttis[0].tt_gmtoff = 0;
1209 lclptr->ttis[0].tt_abbrind = 0;
1210 strcpy(lclptr->chars, gmt);
1211 } else if (tzload(name, lclptr, TRUE) != 0)
1212 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1220 _MUTEX_LOCK(&lcl_mutex);
1222 _MUTEX_UNLOCK(&lcl_mutex);
1226 ** The easy way to behave "as if no library function calls" localtime
1227 ** is to not call it--so we drop its guts into "localsub", which can be
1228 ** freely called. (And no, the PANS doesn't require the above behavior--
1229 ** but it *is* desirable.)
1231 ** The unused offset argument is for the benefit of mktime variants.
1236 localsub(const time_t * const timep, const long offset __unused,
1237 struct tm * const tmp)
1240 const struct ttinfo * ttisp;
1243 const time_t t = *timep;
1248 return gmtsub(timep, offset, tmp);
1249 #endif /* defined ALL_STATE */
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_r(const time_t * const timep, struct tm *p_tm)
1328 _MUTEX_LOCK(&lcl_mutex);
1330 localsub(timep, 0L, p_tm);
1331 _MUTEX_UNLOCK(&lcl_mutex);
1336 localtime(const time_t * const timep)
1338 static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1339 static pthread_key_t localtime_key = -1;
1342 if (__isthreaded != 0) {
1343 _pthread_mutex_lock(&localtime_mutex);
1344 if (localtime_key < 0) {
1345 if (_pthread_key_create(&localtime_key, free) < 0) {
1346 _pthread_mutex_unlock(&localtime_mutex);
1350 _pthread_mutex_unlock(&localtime_mutex);
1351 p_tm = _pthread_getspecific(localtime_key);
1353 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1356 _pthread_setspecific(localtime_key, p_tm);
1358 _pthread_mutex_lock(&lcl_mutex);
1360 localsub(timep, 0L, p_tm);
1361 _pthread_mutex_unlock(&lcl_mutex);
1365 localsub(timep, 0L, &tm);
1371 ** gmtsub is to gmtime as localsub is to localtime.
1375 gmtsub(const time_t * const timep, const long offset, struct tm * const tmp)
1379 _MUTEX_LOCK(&gmt_mutex);
1383 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1385 #endif /* defined ALL_STATE */
1388 _MUTEX_UNLOCK(&gmt_mutex);
1389 result = timesub(timep, offset, gmtptr, tmp);
1392 ** Could get fancy here and deliver something such as
1393 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1394 ** but this is no time for a treasure hunt.
1397 tmp->TM_ZONE = wildabbr;
1402 else tmp->TM_ZONE = gmtptr->chars;
1403 #endif /* defined ALL_STATE */
1405 tmp->TM_ZONE = gmtptr->chars;
1406 #endif /* State Farm */
1408 #endif /* defined TM_ZONE */
1413 gmtime(const time_t * const timep)
1415 static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1416 static pthread_key_t gmtime_key = -1;
1419 if (__isthreaded != 0) {
1420 _pthread_mutex_lock(&gmtime_mutex);
1421 if (gmtime_key < 0) {
1422 if (_pthread_key_create(&gmtime_key, free) < 0) {
1423 _pthread_mutex_unlock(&gmtime_mutex);
1427 _pthread_mutex_unlock(&gmtime_mutex);
1429 * Changed to follow POSIX.1 threads standard, which
1430 * is what BSD currently has.
1432 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1433 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1437 _pthread_setspecific(gmtime_key, p_tm);
1439 return gmtsub(timep, 0L, p_tm);
1441 return gmtsub(timep, 0L, &tm);
1446 gmtime_r(const time_t * timep, struct tm * tmp)
1448 return gmtsub(timep, 0L, tmp);
1454 offtime(const time_t * const timep, const long offset)
1456 return gmtsub(timep, offset, &tm);
1459 #endif /* defined STD_INSPIRED */
1462 ** Return the number of leap years through the end of the given year
1463 ** where, to make the math easy, the answer for year zero is defined as zero.
1467 leaps_thru_end_of(const int y)
1469 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1470 -(leaps_thru_end_of(-(y + 1)) + 1);
1474 timesub(const time_t * const timep, const long offset,
1475 const struct state * const sp, struct tm * const tmp)
1477 const struct lsinfo * lp;
1479 int idays; /* unsigned would be so 2003 */
1491 i = (sp == NULL) ? 0 : sp->leapcnt;
1492 #endif /* defined ALL_STATE */
1495 #endif /* State Farm */
1498 if (*timep >= lp->ls_trans) {
1499 if (*timep == lp->ls_trans) {
1500 hit = ((i == 0 && lp->ls_corr > 0) ||
1501 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1504 sp->lsis[i].ls_trans ==
1505 sp->lsis[i - 1].ls_trans + 1 &&
1506 sp->lsis[i].ls_corr ==
1507 sp->lsis[i - 1].ls_corr + 1) {
1517 tdays = *timep / SECSPERDAY;
1518 rem = *timep - tdays * SECSPERDAY;
1519 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1525 tdelta = tdays / DAYSPERLYEAR;
1527 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1530 idelta = (tdays < 0) ? -1 : 1;
1532 if (increment_overflow(&newy, idelta))
1534 leapdays = leaps_thru_end_of(newy - 1) -
1535 leaps_thru_end_of(y - 1);
1536 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1543 seconds = tdays * SECSPERDAY + 0.5;
1544 tdays = seconds / SECSPERDAY;
1545 rem += seconds - tdays * SECSPERDAY;
1548 ** Given the range, we can now fearlessly cast...
1551 rem += offset - corr;
1556 while (rem >= SECSPERDAY) {
1561 if (increment_overflow(&y, -1))
1563 idays += year_lengths[isleap(y)];
1565 while (idays >= year_lengths[isleap(y)]) {
1566 idays -= year_lengths[isleap(y)];
1567 if (increment_overflow(&y, 1))
1571 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1573 tmp->tm_yday = idays;
1575 ** The "extra" mods below avoid overflow problems.
1577 tmp->tm_wday = EPOCH_WDAY +
1578 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1579 (DAYSPERNYEAR % DAYSPERWEEK) +
1580 leaps_thru_end_of(y - 1) -
1581 leaps_thru_end_of(EPOCH_YEAR - 1) +
1583 tmp->tm_wday %= DAYSPERWEEK;
1584 if (tmp->tm_wday < 0)
1585 tmp->tm_wday += DAYSPERWEEK;
1586 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1588 tmp->tm_min = (int) (rem / SECSPERMIN);
1590 ** A positive leap second requires a special
1591 ** representation. This uses "... ??:59:60" et seq.
1593 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1594 ip = mon_lengths[isleap(y)];
1595 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1596 idays -= ip[tmp->tm_mon];
1597 tmp->tm_mday = (int) (idays + 1);
1600 tmp->TM_GMTOFF = offset;
1601 #endif /* defined TM_GMTOFF */
1606 ctime(const time_t * const timep)
1609 ** Section 4.12.3.2 of X3.159-1989 requires that
1610 ** The ctime function converts the calendar time pointed to by timer
1611 ** to local time in the form of a string. It is equivalent to
1612 ** asctime(localtime(timer))
1614 return asctime(localtime(timep));
1618 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 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1767 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1769 ** We can't set tm_sec to 0, because that might push the
1770 ** time below the minimum representable time.
1771 ** Set tm_sec to 59 instead.
1772 ** This assumes that the minimum representable time is
1773 ** not in the same minute that a leap second was deleted from,
1774 ** which is a safer assumption than using 58 would be.
1776 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1778 saved_seconds = yourtm.tm_sec;
1779 yourtm.tm_sec = SECSPERMIN - 1;
1781 saved_seconds = yourtm.tm_sec;
1785 ** Do a binary search (this works whatever time_t's type is).
1787 if (!TYPE_SIGNED(time_t)) {
1790 } else if (!TYPE_INTEGRAL(time_t)) {
1791 if (sizeof(time_t) > sizeof(float))
1792 hi = (time_t) DBL_MAX;
1793 else hi = (time_t) FLT_MAX;
1797 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1802 t = lo / 2 + hi / 2;
1807 if ((*funcp)(&t, offset, &mytm) == NULL) {
1809 ** Assume that t is too extreme to be represented in
1810 ** a struct tm; arrange things so that it is less
1811 ** extreme on the next pass.
1813 dir = (t > 0) ? 1 : -1;
1814 } else dir = tmcomp(&mytm, &yourtm);
1821 } else if (t == hi) {
1834 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1837 ** Right time, wrong type.
1838 ** Hunt for right time, right type.
1839 ** It's okay to guess wrong since the guess
1842 sp = (const struct state *)
1843 ((funcp == localsub) ? lclptr : gmtptr);
1847 #endif /* defined ALL_STATE */
1848 for (i = sp->typecnt - 1; i >= 0; --i) {
1849 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1851 for (j = sp->typecnt - 1; j >= 0; --j) {
1852 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1854 newt = t + sp->ttis[j].tt_gmtoff -
1855 sp->ttis[i].tt_gmtoff;
1856 if ((*funcp)(&newt, offset, &mytm) == NULL)
1858 if (tmcomp(&mytm, &yourtm) != 0)
1860 if (mytm.tm_isdst != yourtm.tm_isdst)
1872 newt = t + saved_seconds;
1873 if ((newt < t) != (saved_seconds < 0))
1876 if ((*funcp)(&t, offset, tmp))
1882 time2(struct tm * const tmp,
1883 struct tm * (* const funcp)(const time_t *, long, struct tm *),
1884 const long offset, int * const okayp)
1889 ** First try without normalization of seconds
1890 ** (in case tm_sec contains a value associated with a leap second).
1891 ** If that fails, try with normalization of seconds.
1893 t = time2sub(tmp, funcp, offset, okayp, FALSE);
1894 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1898 time1(struct tm * const tmp,
1899 struct tm * (* const funcp)(const time_t *, long, struct tm *),
1903 const struct state * sp;
1905 int sameind, otherind;
1908 int seen[TZ_MAX_TYPES];
1909 int types[TZ_MAX_TYPES];
1912 if (tmp->tm_isdst > 1)
1914 t = time2(tmp, funcp, offset, &okay);
1917 ** PCTS code courtesy Grant Sullivan.
1921 if (tmp->tm_isdst < 0)
1922 tmp->tm_isdst = 0; /* reset to std and try again */
1923 #endif /* defined PCTS */
1925 if (okay || tmp->tm_isdst < 0)
1927 #endif /* !defined PCTS */
1929 ** We're supposed to assume that somebody took a time of one type
1930 ** and did some math on it that yielded a "struct tm" that's bad.
1931 ** We try to divine the type they started from and adjust to the
1934 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
1938 #endif /* defined ALL_STATE */
1939 for (i = 0; i < sp->typecnt; ++i)
1942 for (i = sp->timecnt - 1; i >= 0; --i)
1943 if (!seen[sp->types[i]]) {
1944 seen[sp->types[i]] = TRUE;
1945 types[nseen++] = sp->types[i];
1947 for (sameind = 0; sameind < nseen; ++sameind) {
1948 samei = types[sameind];
1949 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1951 for (otherind = 0; otherind < nseen; ++otherind) {
1952 otheri = types[otherind];
1953 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1955 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1956 sp->ttis[samei].tt_gmtoff;
1957 tmp->tm_isdst = !tmp->tm_isdst;
1958 t = time2(tmp, funcp, offset, &okay);
1961 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1962 sp->ttis[samei].tt_gmtoff;
1963 tmp->tm_isdst = !tmp->tm_isdst;
1970 mktime(struct tm * const tmp)
1972 time_t mktime_return_value;
1973 _MUTEX_LOCK(&lcl_mutex);
1975 mktime_return_value = time1(tmp, localsub, 0L);
1976 _MUTEX_UNLOCK(&lcl_mutex);
1977 return(mktime_return_value);
1983 timelocal(struct tm * const tmp)
1985 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1990 timegm(struct tm * const tmp)
1993 return time1(tmp, gmtsub, 0L);
1997 timeoff(struct tm * const tmp, const long offset)
2000 return time1(tmp, gmtsub, offset);
2003 #endif /* defined STD_INSPIRED */
2008 ** The following is supplied for compatibility with
2009 ** previous versions of the CMUCS runtime library.
2013 gtime(struct tm * const tmp)
2015 const time_t t = mktime(tmp);
2022 #endif /* defined CMUCS */
2025 ** XXX--is the below the right way to conditionalize??
2031 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2032 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2033 ** is not the case if we are accounting for leap seconds.
2034 ** So, we provide the following conversion routines for use
2035 ** when exchanging timestamps with POSIX conforming systems.
2039 leapcorr(time_t *timep)
2049 if (*timep >= lp->ls_trans)
2056 time2posix(time_t t)
2059 return t - leapcorr(&t);
2063 posix2time(time_t t)
2070 ** For a positive leap second hit, the result
2071 ** is not unique. For a negative leap second
2072 ** hit, the corresponding time doesn't exist,
2073 ** so we return an adjacent second.
2075 x = t + leapcorr(&t);
2076 y = x - leapcorr(&x);
2080 y = x - leapcorr(&x);
2087 y = x - leapcorr(&x);
2095 #endif /* defined STD_INSPIRED */