2 ** This file is in the public domain, so clarified as of
3 ** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
5 ** $FreeBSD: src/lib/libc/stdtime/localtime.c,v 1.25.2.2 2002/08/13 16:08:07 bmilekic Exp $
6 ** $DragonFly: src/lib/libc/stdtime/localtime.c,v 1.5 2005/12/04 23:25:40 swildner Exp $
10 * @(#)localtime.c 7.57
13 ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
14 ** POSIX-style TZ environment variable handling from Guy Harris
20 #include "namespace.h"
21 #include <sys/types.h>
27 #include <un-namespace.h>
31 #include "libc_private.h"
33 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
34 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
37 ** SunOS 4.1.1 headers lack O_BINARY.
41 #define OPEN_MODE (O_RDONLY | O_BINARY)
42 #endif /* defined O_BINARY */
44 #define OPEN_MODE O_RDONLY
45 #endif /* !defined O_BINARY */
49 ** Someone might make incorrect use of a time zone abbreviation:
50 ** 1. They might reference tzname[0] before calling tzset (explicitly
52 ** 2. They might reference tzname[1] before calling tzset (explicitly
54 ** 3. They might reference tzname[1] after setting to a time zone
55 ** in which Daylight Saving Time is never observed.
56 ** 4. They might reference tzname[0] after setting to a time zone
57 ** in which Standard Time is never observed.
58 ** 5. They might reference tm.TM_ZONE after calling offtime.
59 ** What's best to do in the above cases is open to debate;
60 ** for now, we just set things up so that in any of the five cases
61 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
62 ** string "tzname[0] used before set", and similarly for the other cases.
63 ** And another: initialize tzname[0] to "ERA", with an explanation in the
64 ** manual page of what this "time zone abbreviation" means (doing this so
65 ** that tzname[0] has the "normal" length of three characters).
68 #endif /* !defined WILDABBR */
70 static char wildabbr[] = "WILDABBR";
72 static const char gmt[] = "GMT";
74 struct ttinfo { /* time type information */
75 long tt_gmtoff; /* GMT offset in seconds */
76 int tt_isdst; /* used to set tm_isdst */
77 int tt_abbrind; /* abbreviation list index */
78 int tt_ttisstd; /* TRUE if transition is std time */
79 int tt_ttisgmt; /* TRUE if transition is GMT */
82 struct lsinfo { /* leap second information */
83 time_t ls_trans; /* transition time */
84 long ls_corr; /* correction to apply */
87 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
90 #define MY_TZNAME_MAX TZNAME_MAX
91 #endif /* defined TZNAME_MAX */
93 #define MY_TZNAME_MAX 255
94 #endif /* !defined TZNAME_MAX */
101 time_t ats[TZ_MAX_TIMES];
102 unsigned char types[TZ_MAX_TIMES];
103 struct ttinfo ttis[TZ_MAX_TYPES];
104 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
105 (2 * (MY_TZNAME_MAX + 1)))];
106 struct lsinfo lsis[TZ_MAX_LEAPS];
110 int r_type; /* type of rule--see below */
111 int r_day; /* day number of rule */
112 int r_week; /* week number of rule */
113 int r_mon; /* month number of rule */
114 long r_time; /* transition time of rule */
117 #define JULIAN_DAY 0 /* Jn - Julian day */
118 #define DAY_OF_YEAR 1 /* n - day of year */
119 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
122 ** Prototypes for static functions.
125 static long detzcode(const char * codep);
126 static const char * getzname(const char * strp);
127 static const char * getnum(const char * strp, int * nump, int min, int max);
128 static const char * getsecs(const char * strp, long * secsp);
129 static const char * getoffset(const char * strp, long * offsetp);
130 static const char * getrule(const char * strp, struct rule * rulep);
131 static void gmtload(struct state * sp);
132 static void gmtsub(const time_t * timep, long offset,
134 static void localsub(const time_t * timep, long offset,
136 static int increment_overflow(int * number, int delta);
137 static int normalize_overflow(int * tensptr, int * unitsptr,
139 static void settzname(void);
140 static time_t time1(struct tm * tmp,
141 void(*funcp)(const time_t *, long, struct tm *),
143 static time_t time2(struct tm *tmp,
144 void(*funcp)(const time_t *, long, struct tm *),
145 long offset, int * okayp);
146 static void timesub(const time_t * timep, long offset,
147 const struct state * sp, struct tm * tmp);
148 static int tmcomp(const struct tm * atmp,
149 const struct tm * btmp);
150 static time_t transtime(time_t janfirst, int year,
151 const struct rule * rulep, long offset);
152 static int tzload(const char * name, struct state * sp);
153 static int tzparse(const char * name, struct state * sp,
157 static struct state * lclptr;
158 static struct state * gmtptr;
159 #endif /* defined ALL_STATE */
162 static struct state lclmem;
163 static struct state gmtmem;
164 #define lclptr (&lclmem)
165 #define gmtptr (&gmtmem)
166 #endif /* State Farm */
168 #ifndef TZ_STRLEN_MAX
169 #define TZ_STRLEN_MAX 255
170 #endif /* !defined TZ_STRLEN_MAX */
172 static char lcl_TZname[TZ_STRLEN_MAX + 1];
173 static int lcl_is_set;
174 static int gmt_is_set;
175 static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
176 static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
184 ** Section 4.12.3 of X3.159-1989 requires that
185 ** Except for the strftime function, these functions [asctime,
186 ** ctime, gmtime, localtime] return values in one of two static
187 ** objects: a broken-down time structure and an array of char.
188 ** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
196 #endif /* defined USG_COMPAT */
200 #endif /* defined ALTZONE */
203 detzcode(const char * const codep)
208 result = (codep[0] & 0x80) ? ~0L : 0L;
209 for (i = 0; i < 4; ++i)
210 result = (result << 8) | (codep[i] & 0xff);
217 struct state * sp = lclptr;
220 tzname[0] = wildabbr;
221 tzname[1] = wildabbr;
225 #endif /* defined USG_COMPAT */
228 #endif /* defined ALTZONE */
231 tzname[0] = tzname[1] = gmt;
234 #endif /* defined ALL_STATE */
235 for (i = 0; i < sp->typecnt; ++i) {
236 const struct ttinfo * const ttisp = &sp->ttis[i];
238 tzname[ttisp->tt_isdst] =
239 &sp->chars[ttisp->tt_abbrind];
243 if (i == 0 || !ttisp->tt_isdst)
244 timezone = -(ttisp->tt_gmtoff);
245 #endif /* defined USG_COMPAT */
247 if (i == 0 || ttisp->tt_isdst)
248 altzone = -(ttisp->tt_gmtoff);
249 #endif /* defined ALTZONE */
252 ** And to get the latest zone names into tzname. . .
254 for (i = 0; i < sp->timecnt; ++i) {
255 const struct ttinfo * const ttisp =
259 tzname[ttisp->tt_isdst] =
260 &sp->chars[ttisp->tt_abbrind];
265 tzload(const char *name, struct state * const sp)
271 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
272 if (name != NULL && issetugid() != 0)
273 if ((name[0] == ':' && name[1] == '/') ||
274 name[0] == '/' || strchr(name, '.'))
276 if (name == NULL && (name = TZDEFAULT) == NULL)
282 ** Section 4.9.1 of the C standard says that
283 ** "FILENAME_MAX expands to an integral constant expression
284 ** that is the size needed for an array of char large enough
285 ** to hold the longest file name string that the implementation
286 ** guarantees can be opened."
288 char fullname[FILENAME_MAX + 1];
292 doaccess = name[0] == '/';
294 if ((p = TZDIR) == NULL)
296 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
299 strcat(fullname, "/");
300 strcat(fullname, name);
302 ** Set doaccess if '.' (as in "../") shows up in name.
304 if (strchr(name, '.') != NULL)
308 if (doaccess && access(name, R_OK) != 0)
310 if ((fid = _open(name, OPEN_MODE)) == -1)
312 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
318 struct tzhead * tzhp;
319 char buf[sizeof *sp + sizeof *tzhp];
323 i = _read(fid, buf, sizeof buf);
324 if (_close(fid) != 0)
327 p += (sizeof tzhp->tzh_magic) + (sizeof tzhp->tzh_reserved);
328 ttisstdcnt = (int) detzcode(p);
330 ttisgmtcnt = (int) detzcode(p);
332 sp->leapcnt = (int) detzcode(p);
334 sp->timecnt = (int) detzcode(p);
336 sp->typecnt = (int) detzcode(p);
338 sp->charcnt = (int) detzcode(p);
340 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
341 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
342 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
343 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
344 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
345 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
347 if (i - (p - buf) < sp->timecnt * 4 + /* ats */
348 sp->timecnt + /* types */
349 sp->typecnt * (4 + 2) + /* ttinfos */
350 sp->charcnt + /* chars */
351 sp->leapcnt * (4 + 4) + /* lsinfos */
352 ttisstdcnt + /* ttisstds */
353 ttisgmtcnt) /* ttisgmts */
355 for (i = 0; i < sp->timecnt; ++i) {
356 sp->ats[i] = detzcode(p);
359 for (i = 0; i < sp->timecnt; ++i) {
360 sp->types[i] = (unsigned char) *p++;
361 if (sp->types[i] >= sp->typecnt)
364 for (i = 0; i < sp->typecnt; ++i) {
365 struct ttinfo * ttisp;
367 ttisp = &sp->ttis[i];
368 ttisp->tt_gmtoff = detzcode(p);
370 ttisp->tt_isdst = (unsigned char) *p++;
371 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
373 ttisp->tt_abbrind = (unsigned char) *p++;
374 if (ttisp->tt_abbrind < 0 ||
375 ttisp->tt_abbrind > sp->charcnt)
378 for (i = 0; i < sp->charcnt; ++i)
380 sp->chars[i] = '\0'; /* ensure '\0' at end */
381 for (i = 0; i < sp->leapcnt; ++i) {
382 struct lsinfo * lsisp;
384 lsisp = &sp->lsis[i];
385 lsisp->ls_trans = detzcode(p);
387 lsisp->ls_corr = detzcode(p);
390 for (i = 0; i < sp->typecnt; ++i) {
391 struct ttinfo * ttisp;
393 ttisp = &sp->ttis[i];
395 ttisp->tt_ttisstd = FALSE;
397 ttisp->tt_ttisstd = *p++;
398 if (ttisp->tt_ttisstd != TRUE &&
399 ttisp->tt_ttisstd != FALSE)
403 for (i = 0; i < sp->typecnt; ++i) {
404 struct ttinfo * ttisp;
406 ttisp = &sp->ttis[i];
408 ttisp->tt_ttisgmt = FALSE;
410 ttisp->tt_ttisgmt = *p++;
411 if (ttisp->tt_ttisgmt != TRUE &&
412 ttisp->tt_ttisgmt != FALSE)
420 static const int mon_lengths[2][MONSPERYEAR] = {
421 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
422 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
425 static const int year_lengths[2] = {
426 DAYSPERNYEAR, DAYSPERLYEAR
430 ** Given a pointer into a time zone string, scan until a character that is not
431 ** a valid character in a zone name is found. Return a pointer to that
436 getzname(const char *strp)
440 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
447 ** Given a pointer into a time zone string, extract a number from that string.
448 ** Check that the number is within a specified range; if it is not, return
450 ** Otherwise, return a pointer to the first character not part of the number.
454 getnum(const char *strp, int * const nump, const int min, const int max)
459 if (strp == NULL || !is_digit(c = *strp))
463 num = num * 10 + (c - '0');
465 return NULL; /* illegal value */
467 } while (is_digit(c));
469 return NULL; /* illegal value */
475 ** Given a pointer into a time zone string, extract a number of seconds,
476 ** in hh[:mm[:ss]] form, from the string.
477 ** If any error occurs, return NULL.
478 ** Otherwise, return a pointer to the first character not part of the number
483 getsecs(const char *strp, long * const secsp)
488 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
489 ** "M10.4.6/26", which does not conform to Posix,
490 ** but which specifies the equivalent of
491 ** ``02:00 on the first Sunday on or after 23 Oct''.
493 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
496 *secsp = num * (long) SECSPERHOUR;
499 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
502 *secsp += num * SECSPERMIN;
505 /* `SECSPERMIN' allows for leap seconds. */
506 strp = getnum(strp, &num, 0, SECSPERMIN);
516 ** Given a pointer into a time zone string, extract an offset, in
517 ** [+-]hh[:mm[:ss]] form, from the string.
518 ** If any error occurs, return NULL.
519 ** Otherwise, return a pointer to the first character not part of the time.
523 getoffset(const char *strp, long * const offsetp)
530 } else if (*strp == '+')
532 strp = getsecs(strp, offsetp);
534 return NULL; /* illegal time */
536 *offsetp = -*offsetp;
541 ** Given a pointer into a time zone string, extract a rule in the form
542 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
543 ** If a valid rule is not found, return NULL.
544 ** Otherwise, return a pointer to the first character not part of the rule.
548 getrule(const char *strp, struct rule * const rulep)
554 rulep->r_type = JULIAN_DAY;
556 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
557 } else if (*strp == 'M') {
561 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
563 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
568 strp = getnum(strp, &rulep->r_week, 1, 5);
573 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
574 } else if (is_digit(*strp)) {
578 rulep->r_type = DAY_OF_YEAR;
579 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
580 } else return NULL; /* invalid format */
588 strp = getsecs(strp, &rulep->r_time);
589 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
594 ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
595 ** year, a rule, and the offset from GMT at the time that rule takes effect,
596 ** calculate the Epoch-relative time that rule takes effect.
600 transtime(const time_t janfirst, const int year,
601 const struct rule * const rulep, const long offset)
606 int d, m1, yy0, yy1, yy2, dow;
609 leapyear = isleap(year);
610 switch (rulep->r_type) {
614 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
616 ** In non-leap years, or if the day number is 59 or less, just
617 ** add SECSPERDAY times the day number-1 to the time of
618 ** January 1, midnight, to get the day.
620 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
621 if (leapyear && rulep->r_day >= 60)
628 ** Just add SECSPERDAY times the day number to the time of
629 ** January 1, midnight, to get the day.
631 value = janfirst + rulep->r_day * SECSPERDAY;
634 case MONTH_NTH_DAY_OF_WEEK:
636 ** Mm.n.d - nth "dth day" of month m.
639 for (i = 0; i < rulep->r_mon - 1; ++i)
640 value += mon_lengths[leapyear][i] * SECSPERDAY;
643 ** Use Zeller's Congruence to get day-of-week of first day of
646 m1 = (rulep->r_mon + 9) % 12 + 1;
647 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
650 dow = ((26 * m1 - 2) / 10 +
651 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
656 ** "dow" is the day-of-week of the first day of the month. Get
657 ** the day-of-month (zero-origin) of the first "dow" day of the
660 d = rulep->r_day - dow;
663 for (i = 1; i < rulep->r_week; ++i) {
664 if (d + DAYSPERWEEK >=
665 mon_lengths[leapyear][rulep->r_mon - 1])
671 ** "d" is the day-of-month (zero-origin) of the day we want.
673 value += d * SECSPERDAY;
678 ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
679 ** question. To get the Epoch-relative time of the specified local
680 ** time on that day, add the transition time and the current offset
683 return value + rulep->r_time + offset;
687 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
692 tzparse(const char *name, struct state * const sp, const int lastditch)
694 const char * stdname;
695 const char * dstname;
701 unsigned char * typep;
708 stdlen = strlen(name); /* length of standard zone name */
710 if (stdlen >= sizeof sp->chars)
711 stdlen = (sizeof sp->chars) - 1;
714 name = getzname(name);
715 stdlen = name - stdname;
719 return -1; /* was "stdoffset = 0;" */
721 name = getoffset(name, &stdoffset);
726 load_result = tzload(TZDEFRULES, sp);
727 if (load_result != 0)
728 sp->leapcnt = 0; /* so, we're off a little */
731 name = getzname(name);
732 dstlen = name - dstname; /* length of DST zone name */
735 if (*name != '\0' && *name != ',' && *name != ';') {
736 name = getoffset(name, &dstoffset);
739 } else dstoffset = stdoffset - SECSPERHOUR;
740 if (*name == ',' || *name == ';') {
749 if ((name = getrule(name, &start)) == NULL)
753 if ((name = getrule(name, &end)) == NULL)
757 sp->typecnt = 2; /* standard time and DST */
759 ** Two transitions per year, from EPOCH_YEAR to 2037.
761 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
762 if (sp->timecnt > TZ_MAX_TIMES)
764 sp->ttis[0].tt_gmtoff = -dstoffset;
765 sp->ttis[0].tt_isdst = 1;
766 sp->ttis[0].tt_abbrind = stdlen + 1;
767 sp->ttis[1].tt_gmtoff = -stdoffset;
768 sp->ttis[1].tt_isdst = 0;
769 sp->ttis[1].tt_abbrind = 0;
773 for (year = EPOCH_YEAR; year <= 2037; ++year) {
774 starttime = transtime(janfirst, year, &start,
776 endtime = transtime(janfirst, year, &end,
778 if (starttime > endtime) {
780 *typep++ = 1; /* DST ends */
782 *typep++ = 0; /* DST begins */
785 *typep++ = 0; /* DST begins */
787 *typep++ = 1; /* DST ends */
789 janfirst += year_lengths[isleap(year)] *
802 if (load_result != 0)
805 ** Initial values of theirstdoffset and theirdstoffset.
808 for (i = 0; i < sp->timecnt; ++i) {
810 if (!sp->ttis[j].tt_isdst) {
812 -sp->ttis[j].tt_gmtoff;
817 for (i = 0; i < sp->timecnt; ++i) {
819 if (sp->ttis[j].tt_isdst) {
821 -sp->ttis[j].tt_gmtoff;
826 ** Initially we're assumed to be in standard time.
829 theiroffset = theirstdoffset;
831 ** Now juggle transition times and types
832 ** tracking offsets as you do.
834 for (i = 0; i < sp->timecnt; ++i) {
836 sp->types[i] = sp->ttis[j].tt_isdst;
837 if (sp->ttis[j].tt_ttisgmt) {
838 /* No adjustment to transition time */
841 ** If summer time is in effect, and the
842 ** transition time was not specified as
843 ** standard time, add the summer time
844 ** offset to the transition time;
845 ** otherwise, add the standard time
846 ** offset to the transition time.
849 ** Transitions from DST to DDST
850 ** will effectively disappear since
851 ** POSIX provides for only one DST
854 if (isdst && !sp->ttis[j].tt_ttisstd) {
855 sp->ats[i] += dstoffset -
858 sp->ats[i] += stdoffset -
862 theiroffset = -sp->ttis[j].tt_gmtoff;
863 if (sp->ttis[j].tt_isdst)
864 theirdstoffset = theiroffset;
865 else theirstdoffset = theiroffset;
868 ** Finally, fill in ttis.
869 ** ttisstd and ttisgmt need not be handled.
871 sp->ttis[0].tt_gmtoff = -stdoffset;
872 sp->ttis[0].tt_isdst = FALSE;
873 sp->ttis[0].tt_abbrind = 0;
874 sp->ttis[1].tt_gmtoff = -dstoffset;
875 sp->ttis[1].tt_isdst = TRUE;
876 sp->ttis[1].tt_abbrind = stdlen + 1;
880 sp->typecnt = 1; /* only standard time */
882 sp->ttis[0].tt_gmtoff = -stdoffset;
883 sp->ttis[0].tt_isdst = 0;
884 sp->ttis[0].tt_abbrind = 0;
886 sp->charcnt = stdlen + 1;
888 sp->charcnt += dstlen + 1;
889 if (sp->charcnt > sizeof sp->chars)
892 strncpy(cp, stdname, stdlen);
896 strncpy(cp, dstname, dstlen);
897 *(cp + dstlen) = '\0';
903 gmtload(struct state * const sp)
905 if (tzload(gmt, sp) != 0)
906 tzparse(gmt, sp, TRUE);
910 tzsetwall_basic(void)
917 if (lclptr == NULL) {
918 lclptr = (struct state *) malloc(sizeof *lclptr);
919 if (lclptr == NULL) {
920 settzname(); /* all we can do */
924 #endif /* defined ALL_STATE */
925 if (tzload((char *) NULL, lclptr) != 0)
933 _MUTEX_LOCK(&lcl_mutex);
935 _MUTEX_UNLOCK(&lcl_mutex);
949 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
951 lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
953 strcpy(lcl_TZname, name);
956 if (lclptr == NULL) {
957 lclptr = (struct state *) malloc(sizeof *lclptr);
958 if (lclptr == NULL) {
959 settzname(); /* all we can do */
963 #endif /* defined ALL_STATE */
966 ** User wants it fast rather than right.
968 lclptr->leapcnt = 0; /* so, we're off a little */
970 lclptr->ttis[0].tt_gmtoff = 0;
971 lclptr->ttis[0].tt_abbrind = 0;
972 strcpy(lclptr->chars, gmt);
973 } else if (tzload(name, lclptr) != 0)
974 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
982 _MUTEX_LOCK(&lcl_mutex);
984 _MUTEX_UNLOCK(&lcl_mutex);
988 ** The easy way to behave "as if no library function calls" localtime
989 ** is to not call it--so we drop its guts into "localsub", which can be
990 ** freely called. (And no, the PANS doesn't require the above behavior--
991 ** but it *is* desirable.)
993 ** The unused offset argument is for the benefit of mktime variants.
998 localsub(const time_t * const timep, const long offset __unused,
999 struct tm * const tmp)
1002 const struct ttinfo * ttisp;
1004 const time_t t = *timep;
1009 gmtsub(timep, offset, tmp);
1012 #endif /* defined ALL_STATE */
1013 if (sp->timecnt == 0 || t < sp->ats[0]) {
1015 while (sp->ttis[i].tt_isdst)
1016 if (++i >= sp->typecnt) {
1021 for (i = 1; i < sp->timecnt; ++i)
1024 i = sp->types[i - 1];
1026 ttisp = &sp->ttis[i];
1028 ** To get (wrong) behavior that's compatible with System V Release 2.0
1029 ** you'd replace the statement below with
1030 ** t += ttisp->tt_gmtoff;
1031 ** timesub(&t, 0L, sp, tmp);
1033 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1034 tmp->tm_isdst = ttisp->tt_isdst;
1035 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1037 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1038 #endif /* defined TM_ZONE */
1042 localtime_r(const time_t * const timep, struct tm *p_tm)
1044 _MUTEX_LOCK(&lcl_mutex);
1046 localsub(timep, 0L, p_tm);
1047 _MUTEX_UNLOCK(&lcl_mutex);
1052 localtime(const time_t * const timep)
1054 static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1055 static pthread_key_t localtime_key = -1;
1058 if (__isthreaded != 0) {
1059 _pthread_mutex_lock(&localtime_mutex);
1060 if (localtime_key < 0) {
1061 if (_pthread_key_create(&localtime_key, free) < 0) {
1062 _pthread_mutex_unlock(&localtime_mutex);
1066 _pthread_mutex_unlock(&localtime_mutex);
1067 p_tm = _pthread_getspecific(localtime_key);
1069 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1072 _pthread_setspecific(localtime_key, p_tm);
1074 _pthread_mutex_lock(&lcl_mutex);
1076 localsub(timep, 0L, p_tm);
1077 _pthread_mutex_unlock(&lcl_mutex);
1081 localsub(timep, 0L, &tm);
1087 ** gmtsub is to gmtime as localsub is to localtime.
1091 gmtsub(const time_t * const timep, const long offset, struct tm * const tmp)
1093 _MUTEX_LOCK(&gmt_mutex);
1097 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1099 #endif /* defined ALL_STATE */
1102 _MUTEX_UNLOCK(&gmt_mutex);
1103 timesub(timep, offset, gmtptr, tmp);
1106 ** Could get fancy here and deliver something such as
1107 ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1108 ** but this is no time for a treasure hunt.
1111 tmp->TM_ZONE = wildabbr;
1116 else tmp->TM_ZONE = gmtptr->chars;
1117 #endif /* defined ALL_STATE */
1119 tmp->TM_ZONE = gmtptr->chars;
1120 #endif /* State Farm */
1122 #endif /* defined TM_ZONE */
1126 gmtime(const time_t * const timep)
1128 static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1129 static pthread_key_t gmtime_key = -1;
1132 if (__isthreaded != 0) {
1133 _pthread_mutex_lock(&gmtime_mutex);
1134 if (gmtime_key < 0) {
1135 if (_pthread_key_create(&gmtime_key, free) < 0) {
1136 _pthread_mutex_unlock(&gmtime_mutex);
1140 _pthread_mutex_unlock(&gmtime_mutex);
1142 * Changed to follow POSIX.1 threads standard, which
1143 * is what BSD currently has.
1145 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1146 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1150 _pthread_setspecific(gmtime_key, p_tm);
1152 gmtsub(timep, 0L, p_tm);
1156 gmtsub(timep, 0L, &tm);
1162 gmtime_r(const time_t * timep, struct tm * tm_p)
1164 gmtsub(timep, 0L, tm_p);
1171 offtime(const time_t * const timep, const long offset)
1173 gmtsub(timep, offset, &tm);
1177 #endif /* defined STD_INSPIRED */
1180 timesub(const time_t * const timep, const long offset,
1181 const struct state * const sp, struct tm * const tmp)
1183 const struct lsinfo * lp;
1196 i = (sp == NULL) ? 0 : sp->leapcnt;
1197 #endif /* defined ALL_STATE */
1200 #endif /* State Farm */
1203 if (*timep >= lp->ls_trans) {
1204 if (*timep == lp->ls_trans) {
1205 hit = ((i == 0 && lp->ls_corr > 0) ||
1206 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1209 sp->lsis[i].ls_trans ==
1210 sp->lsis[i - 1].ls_trans + 1 &&
1211 sp->lsis[i].ls_corr ==
1212 sp->lsis[i - 1].ls_corr + 1) {
1221 days = *timep / SECSPERDAY;
1222 rem = *timep % SECSPERDAY;
1224 if (*timep == 0x80000000) {
1226 ** A 3B1 muffs the division on the most negative number.
1231 #endif /* defined mc68k */
1232 rem += (offset - corr);
1237 while (rem >= SECSPERDAY) {
1241 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1242 rem = rem % SECSPERHOUR;
1243 tmp->tm_min = (int) (rem / SECSPERMIN);
1245 ** A positive leap second requires a special
1246 ** representation. This uses "... ??:59:60" et seq.
1248 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1249 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1250 if (tmp->tm_wday < 0)
1251 tmp->tm_wday += DAYSPERWEEK;
1253 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1254 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1257 newy = y + days / DAYSPERNYEAR;
1260 days -= (newy - y) * DAYSPERNYEAR +
1261 LEAPS_THRU_END_OF(newy - 1) -
1262 LEAPS_THRU_END_OF(y - 1);
1265 tmp->tm_year = y - TM_YEAR_BASE;
1266 tmp->tm_yday = (int) days;
1267 ip = mon_lengths[yleap];
1268 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1269 days = days - (long) ip[tmp->tm_mon];
1270 tmp->tm_mday = (int) (days + 1);
1273 tmp->TM_GMTOFF = offset;
1274 #endif /* defined TM_GMTOFF */
1278 ctime(const time_t * const timep)
1281 ** Section 4.12.3.2 of X3.159-1989 requires that
1282 ** The ctime funciton converts the calendar time pointed to by timer
1283 ** to local time in the form of a string. It is equivalent to
1284 ** asctime(localtime(timer))
1286 return asctime(localtime(timep));
1290 ctime_r(const time_t * const timep, char *buf)
1293 return asctime_r(localtime_r(timep, &tm1), buf);
1297 ** Adapted from code provided by Robert Elz, who writes:
1298 ** The "best" way to do mktime I think is based on an idea of Bob
1299 ** Kridle's (so its said...) from a long time ago.
1300 ** [kridle@xinet.com as of 1996-01-16.]
1301 ** It does a binary search of the time_t space. Since time_t's are
1302 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1303 ** would still be very reasonable).
1308 #endif /* !defined WRONG */
1311 ** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1315 increment_overflow(int *number, int delta)
1321 return (*number < number0) != (delta < 0);
1325 normalize_overflow(int * const tensptr, int * const unitsptr, const int base)
1329 tensdelta = (*unitsptr >= 0) ?
1330 (*unitsptr / base) :
1331 (-1 - (-1 - *unitsptr) / base);
1332 *unitsptr -= tensdelta * base;
1333 return increment_overflow(tensptr, tensdelta);
1337 tmcomp(const struct tm * const atmp, const struct tm * const btmp)
1341 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1342 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1343 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1344 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1345 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1346 result = atmp->tm_sec - btmp->tm_sec;
1351 time2(struct tm * const tmp,
1352 void (* const funcp)(const time_t *, long, struct tm *),
1353 const long offset, int * const okayp)
1355 const struct state * sp;
1362 struct tm yourtm, mytm;
1366 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1368 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1370 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1373 ** Turn yourtm.tm_year into an actual year number for now.
1374 ** It is converted back to an offset from TM_YEAR_BASE later.
1376 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1378 while (yourtm.tm_mday <= 0) {
1379 if (increment_overflow(&yourtm.tm_year, -1))
1381 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1382 yourtm.tm_mday += year_lengths[isleap(i)];
1384 while (yourtm.tm_mday > DAYSPERLYEAR) {
1385 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1386 yourtm.tm_mday -= year_lengths[isleap(i)];
1387 if (increment_overflow(&yourtm.tm_year, 1))
1391 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1392 if (yourtm.tm_mday <= i)
1394 yourtm.tm_mday -= i;
1395 if (++yourtm.tm_mon >= MONSPERYEAR) {
1397 if (increment_overflow(&yourtm.tm_year, 1))
1401 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1403 if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1405 ** We can't set tm_sec to 0, because that might push the
1406 ** time below the minimum representable time.
1407 ** Set tm_sec to 59 instead.
1408 ** This assumes that the minimum representable time is
1409 ** not in the same minute that a leap second was deleted from,
1410 ** which is a safer assumption than using 58 would be.
1412 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1414 saved_seconds = yourtm.tm_sec;
1415 yourtm.tm_sec = SECSPERMIN - 1;
1417 saved_seconds = yourtm.tm_sec;
1421 ** Divide the search space in half
1422 ** (this works whether time_t is signed or unsigned).
1424 bits = TYPE_BIT(time_t) - 1;
1426 ** If time_t is signed, then 0 is just above the median,
1427 ** assuming two's complement arithmetic.
1428 ** If time_t is unsigned, then (1 << bits) is just above the median.
1430 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1432 (*funcp)(&t, offset, &mytm);
1433 dir = tmcomp(&mytm, &yourtm);
1438 --t; /* may be needed if new t is minimal */
1440 t -= ((time_t) 1) << bits;
1441 else t += ((time_t) 1) << bits;
1444 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1447 ** Right time, wrong type.
1448 ** Hunt for right time, right type.
1449 ** It's okay to guess wrong since the guess
1453 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1455 sp = (const struct state *)
1456 (((void *) funcp == (void *) localsub) ?
1461 #endif /* defined ALL_STATE */
1462 for (i = sp->typecnt - 1; i >= 0; --i) {
1463 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1465 for (j = sp->typecnt - 1; j >= 0; --j) {
1466 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1468 newt = t + sp->ttis[j].tt_gmtoff -
1469 sp->ttis[i].tt_gmtoff;
1470 (*funcp)(&newt, offset, &mytm);
1471 if (tmcomp(&mytm, &yourtm) != 0)
1473 if (mytm.tm_isdst != yourtm.tm_isdst)
1485 newt = t + saved_seconds;
1486 if ((newt < t) != (saved_seconds < 0))
1489 (*funcp)(&t, offset, tmp);
1495 time1(struct tm * const tmp,
1496 void (* const funcp)(const time_t *, long, struct tm *),
1500 const struct state * sp;
1504 if (tmp->tm_isdst > 1)
1506 t = time2(tmp, funcp, offset, &okay);
1509 ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1513 if (tmp->tm_isdst < 0)
1514 tmp->tm_isdst = 0; /* reset to std and try again */
1515 #endif /* defined PCTS */
1517 if (okay || tmp->tm_isdst < 0)
1519 #endif /* !defined PCTS */
1521 ** We're supposed to assume that somebody took a time of one type
1522 ** and did some math on it that yielded a "struct tm" that's bad.
1523 ** We try to divine the type they started from and adjust to the
1527 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1529 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1534 #endif /* defined ALL_STATE */
1535 for (samei = sp->typecnt - 1; samei >= 0; --samei) {
1536 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1538 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
1539 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1541 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1542 sp->ttis[samei].tt_gmtoff;
1543 tmp->tm_isdst = !tmp->tm_isdst;
1544 t = time2(tmp, funcp, offset, &okay);
1547 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1548 sp->ttis[samei].tt_gmtoff;
1549 tmp->tm_isdst = !tmp->tm_isdst;
1556 mktime(struct tm * const tmp)
1558 time_t mktime_return_value;
1559 _MUTEX_LOCK(&lcl_mutex);
1561 mktime_return_value = time1(tmp, localsub, 0L);
1562 _MUTEX_UNLOCK(&lcl_mutex);
1563 return(mktime_return_value);
1569 timelocal(struct tm * const tmp)
1571 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1576 timegm(struct tm * const tmp)
1579 return time1(tmp, gmtsub, 0L);
1583 timeoff(struct tm * const tmp, const long offset)
1586 return time1(tmp, gmtsub, offset);
1589 #endif /* defined STD_INSPIRED */
1594 ** The following is supplied for compatibility with
1595 ** previous versions of the CMUCS runtime library.
1599 gtime(struct tm * const tmp)
1601 const time_t t = mktime(tmp);
1608 #endif /* defined CMUCS */
1611 ** XXX--is the below the right way to conditionalize??
1617 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1618 ** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
1619 ** is not the case if we are accounting for leap seconds.
1620 ** So, we provide the following conversion routines for use
1621 ** when exchanging timestamps with POSIX conforming systems.
1625 leapcorr(time_t *timep)
1635 if (*timep >= lp->ls_trans)
1642 time2posix(time_t t)
1645 return t - leapcorr(&t);
1649 posix2time(time_t t)
1656 ** For a positive leap second hit, the result
1657 ** is not unique. For a negative leap second
1658 ** hit, the corresponding time doesn't exist,
1659 ** so we return an adjacent second.
1661 x = t + leapcorr(&t);
1662 y = x - leapcorr(&x);
1666 y = x - leapcorr(&x);
1673 y = x - leapcorr(&x);
1681 #endif /* defined STD_INSPIRED */