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.2 2003/06/17 04:26:46 dillon 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 <sys/types.h>
27 #include "pthread_private.h"
31 ** SunOS 4.1.1 headers lack O_BINARY.
35 #define OPEN_MODE (O_RDONLY | O_BINARY)
36 #endif /* defined O_BINARY */
38 #define OPEN_MODE O_RDONLY
39 #endif /* !defined O_BINARY */
43 ** Someone might make incorrect use of a time zone abbreviation:
44 ** 1. They might reference tzname[0] before calling tzset (explicitly
46 ** 2. They might reference tzname[1] before calling tzset (explicitly
48 ** 3. They might reference tzname[1] after setting to a time zone
49 ** in which Daylight Saving Time is never observed.
50 ** 4. They might reference tzname[0] after setting to a time zone
51 ** in which Standard Time is never observed.
52 ** 5. They might reference tm.TM_ZONE after calling offtime.
53 ** What's best to do in the above cases is open to debate;
54 ** for now, we just set things up so that in any of the five cases
55 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
56 ** string "tzname[0] used before set", and similarly for the other cases.
57 ** And another: initialize tzname[0] to "ERA", with an explanation in the
58 ** manual page of what this "time zone abbreviation" means (doing this so
59 ** that tzname[0] has the "normal" length of three characters).
62 #endif /* !defined WILDABBR */
64 static char wildabbr[] = "WILDABBR";
66 static const char gmt[] = "GMT";
68 struct ttinfo { /* time type information */
69 long tt_gmtoff; /* GMT offset in seconds */
70 int tt_isdst; /* used to set tm_isdst */
71 int tt_abbrind; /* abbreviation list index */
72 int tt_ttisstd; /* TRUE if transition is std time */
73 int tt_ttisgmt; /* TRUE if transition is GMT */
76 struct lsinfo { /* leap second information */
77 time_t ls_trans; /* transition time */
78 long ls_corr; /* correction to apply */
81 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
84 #define MY_TZNAME_MAX TZNAME_MAX
85 #endif /* defined TZNAME_MAX */
87 #define MY_TZNAME_MAX 255
88 #endif /* !defined TZNAME_MAX */
95 time_t ats[TZ_MAX_TIMES];
96 unsigned char types[TZ_MAX_TIMES];
97 struct ttinfo ttis[TZ_MAX_TYPES];
98 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
99 (2 * (MY_TZNAME_MAX + 1)))];
100 struct lsinfo lsis[TZ_MAX_LEAPS];
104 int r_type; /* type of rule--see below */
105 int r_day; /* day number of rule */
106 int r_week; /* week number of rule */
107 int r_mon; /* month number of rule */
108 long r_time; /* transition time of rule */
111 #define JULIAN_DAY 0 /* Jn - Julian day */
112 #define DAY_OF_YEAR 1 /* n - day of year */
113 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
116 ** Prototypes for static functions.
119 static long detzcode P((const char * codep));
120 static const char * getzname P((const char * strp));
121 static const char * getnum P((const char * strp, int * nump, int min,
123 static const char * getsecs P((const char * strp, long * secsp));
124 static const char * getoffset P((const char * strp, long * offsetp));
125 static const char * getrule P((const char * strp, struct rule * rulep));
126 static void gmtload P((struct state * sp));
127 static void gmtsub P((const time_t * timep, long offset,
129 static void localsub P((const time_t * timep, long offset,
131 static int increment_overflow P((int * number, int delta));
132 static int normalize_overflow P((int * tensptr, int * unitsptr,
134 static void settzname P((void));
135 static time_t time1 P((struct tm * tmp,
136 void(*funcp) P((const time_t *,
139 static time_t time2 P((struct tm *tmp,
140 void(*funcp) P((const time_t *,
142 long offset, int * okayp));
143 static void timesub P((const time_t * timep, long offset,
144 const struct state * sp, struct tm * tmp));
145 static int tmcomp P((const struct tm * atmp,
146 const struct tm * btmp));
147 static time_t transtime P((time_t janfirst, int year,
148 const struct rule * rulep, long offset));
149 static int tzload P((const char * name, struct state * sp));
150 static int tzparse P((const char * name, struct state * sp,
154 static struct state * lclptr;
155 static struct state * gmtptr;
156 #endif /* defined ALL_STATE */
159 static struct state lclmem;
160 static struct state gmtmem;
161 #define lclptr (&lclmem)
162 #define gmtptr (&gmtmem)
163 #endif /* State Farm */
165 #ifndef TZ_STRLEN_MAX
166 #define TZ_STRLEN_MAX 255
167 #endif /* !defined TZ_STRLEN_MAX */
169 static char lcl_TZname[TZ_STRLEN_MAX + 1];
170 static int lcl_is_set;
171 static int gmt_is_set;
173 static struct pthread_mutex _lcl_mutexd = PTHREAD_MUTEX_STATIC_INITIALIZER;
174 static struct pthread_mutex _gmt_mutexd = PTHREAD_MUTEX_STATIC_INITIALIZER;
175 static pthread_mutex_t lcl_mutex = &_lcl_mutexd;
176 static pthread_mutex_t gmt_mutex = &_gmt_mutexd;
185 ** Section 4.12.3 of X3.159-1989 requires that
186 ** Except for the strftime function, these functions [asctime,
187 ** ctime, gmtime, localtime] return values in one of two static
188 ** objects: a broken-down time structure and an array of char.
189 ** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
197 #endif /* defined USG_COMPAT */
201 #endif /* defined ALTZONE */
205 const char * const codep;
207 register long result;
210 result = (codep[0] & 0x80) ? ~0L : 0L;
211 for (i = 0; i < 4; ++i)
212 result = (result << 8) | (codep[i] & 0xff);
219 register struct state * sp = lclptr;
222 tzname[0] = wildabbr;
223 tzname[1] = wildabbr;
227 #endif /* defined USG_COMPAT */
230 #endif /* defined ALTZONE */
233 tzname[0] = tzname[1] = gmt;
236 #endif /* defined ALL_STATE */
237 for (i = 0; i < sp->typecnt; ++i) {
238 register const struct ttinfo * const ttisp = &sp->ttis[i];
240 tzname[ttisp->tt_isdst] =
241 &sp->chars[ttisp->tt_abbrind];
245 if (i == 0 || !ttisp->tt_isdst)
246 timezone = -(ttisp->tt_gmtoff);
247 #endif /* defined USG_COMPAT */
249 if (i == 0 || ttisp->tt_isdst)
250 altzone = -(ttisp->tt_gmtoff);
251 #endif /* defined ALTZONE */
254 ** And to get the latest zone names into tzname. . .
256 for (i = 0; i < sp->timecnt; ++i) {
257 register const struct ttinfo * const ttisp =
261 tzname[ttisp->tt_isdst] =
262 &sp->chars[ttisp->tt_abbrind];
268 register const char * name;
269 register struct state * const sp;
271 register const char * p;
275 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
276 if (name != NULL && issetugid() != 0)
277 if ((name[0] == ':' && name[1] == '/') ||
278 name[0] == '/' || strchr(name, '.'))
280 if (name == NULL && (name = TZDEFAULT) == NULL)
283 register int doaccess;
286 ** Section 4.9.1 of the C standard says that
287 ** "FILENAME_MAX expands to an integral constant expression
288 ** that is the size needed for an array of char large enough
289 ** to hold the longest file name string that the implementation
290 ** guarantees can be opened."
292 char fullname[FILENAME_MAX + 1];
296 doaccess = name[0] == '/';
298 if ((p = TZDIR) == NULL)
300 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
302 (void) strcpy(fullname, p);
303 (void) strcat(fullname, "/");
304 (void) strcat(fullname, name);
306 ** Set doaccess if '.' (as in "../") shows up in name.
308 if (strchr(name, '.') != NULL)
312 if (doaccess && access(name, R_OK) != 0)
314 if ((fid = _open(name, OPEN_MODE)) == -1)
316 if ((fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
322 struct tzhead * tzhp;
323 char buf[sizeof *sp + sizeof *tzhp];
327 i = _read(fid, buf, sizeof buf);
328 if (_close(fid) != 0)
331 p += (sizeof tzhp->tzh_magic) + (sizeof tzhp->tzh_reserved);
332 ttisstdcnt = (int) detzcode(p);
334 ttisgmtcnt = (int) detzcode(p);
336 sp->leapcnt = (int) detzcode(p);
338 sp->timecnt = (int) detzcode(p);
340 sp->typecnt = (int) detzcode(p);
342 sp->charcnt = (int) detzcode(p);
344 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
345 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
346 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
347 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
348 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
349 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
351 if (i - (p - buf) < sp->timecnt * 4 + /* ats */
352 sp->timecnt + /* types */
353 sp->typecnt * (4 + 2) + /* ttinfos */
354 sp->charcnt + /* chars */
355 sp->leapcnt * (4 + 4) + /* lsinfos */
356 ttisstdcnt + /* ttisstds */
357 ttisgmtcnt) /* ttisgmts */
359 for (i = 0; i < sp->timecnt; ++i) {
360 sp->ats[i] = detzcode(p);
363 for (i = 0; i < sp->timecnt; ++i) {
364 sp->types[i] = (unsigned char) *p++;
365 if (sp->types[i] >= sp->typecnt)
368 for (i = 0; i < sp->typecnt; ++i) {
369 register struct ttinfo * ttisp;
371 ttisp = &sp->ttis[i];
372 ttisp->tt_gmtoff = detzcode(p);
374 ttisp->tt_isdst = (unsigned char) *p++;
375 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
377 ttisp->tt_abbrind = (unsigned char) *p++;
378 if (ttisp->tt_abbrind < 0 ||
379 ttisp->tt_abbrind > sp->charcnt)
382 for (i = 0; i < sp->charcnt; ++i)
384 sp->chars[i] = '\0'; /* ensure '\0' at end */
385 for (i = 0; i < sp->leapcnt; ++i) {
386 register struct lsinfo * lsisp;
388 lsisp = &sp->lsis[i];
389 lsisp->ls_trans = detzcode(p);
391 lsisp->ls_corr = detzcode(p);
394 for (i = 0; i < sp->typecnt; ++i) {
395 register struct ttinfo * ttisp;
397 ttisp = &sp->ttis[i];
399 ttisp->tt_ttisstd = FALSE;
401 ttisp->tt_ttisstd = *p++;
402 if (ttisp->tt_ttisstd != TRUE &&
403 ttisp->tt_ttisstd != FALSE)
407 for (i = 0; i < sp->typecnt; ++i) {
408 register struct ttinfo * ttisp;
410 ttisp = &sp->ttis[i];
412 ttisp->tt_ttisgmt = FALSE;
414 ttisp->tt_ttisgmt = *p++;
415 if (ttisp->tt_ttisgmt != TRUE &&
416 ttisp->tt_ttisgmt != FALSE)
424 static const int mon_lengths[2][MONSPERYEAR] = {
425 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
426 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
429 static const int year_lengths[2] = {
430 DAYSPERNYEAR, DAYSPERLYEAR
434 ** Given a pointer into a time zone string, scan until a character that is not
435 ** a valid character in a zone name is found. Return a pointer to that
441 register const char * strp;
445 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
452 ** Given a pointer into a time zone string, extract a number from that string.
453 ** Check that the number is within a specified range; if it is not, return
455 ** Otherwise, return a pointer to the first character not part of the number.
459 getnum(strp, nump, min, max)
460 register const char * strp;
468 if (strp == NULL || !is_digit(c = *strp))
472 num = num * 10 + (c - '0');
474 return NULL; /* illegal value */
476 } while (is_digit(c));
478 return NULL; /* illegal value */
484 ** Given a pointer into a time zone string, extract a number of seconds,
485 ** in hh[:mm[:ss]] form, from the string.
486 ** If any error occurs, return NULL.
487 ** Otherwise, return a pointer to the first character not part of the number
493 register const char * strp;
499 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
500 ** "M10.4.6/26", which does not conform to Posix,
501 ** but which specifies the equivalent of
502 ** ``02:00 on the first Sunday on or after 23 Oct''.
504 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
507 *secsp = num * (long) SECSPERHOUR;
510 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
513 *secsp += num * SECSPERMIN;
516 /* `SECSPERMIN' allows for leap seconds. */
517 strp = getnum(strp, &num, 0, SECSPERMIN);
527 ** Given a pointer into a time zone string, extract an offset, in
528 ** [+-]hh[:mm[:ss]] form, from the string.
529 ** If any error occurs, return NULL.
530 ** Otherwise, return a pointer to the first character not part of the time.
534 getoffset(strp, offsetp)
535 register const char * strp;
536 long * const offsetp;
538 register int neg = 0;
543 } else if (*strp == '+')
545 strp = getsecs(strp, offsetp);
547 return NULL; /* illegal time */
549 *offsetp = -*offsetp;
554 ** Given a pointer into a time zone string, extract a rule in the form
555 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
556 ** If a valid rule is not found, return NULL.
557 ** Otherwise, return a pointer to the first character not part of the rule.
563 register struct rule * const rulep;
569 rulep->r_type = JULIAN_DAY;
571 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
572 } else if (*strp == 'M') {
576 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
578 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
583 strp = getnum(strp, &rulep->r_week, 1, 5);
588 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
589 } else if (is_digit(*strp)) {
593 rulep->r_type = DAY_OF_YEAR;
594 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
595 } else return NULL; /* invalid format */
603 strp = getsecs(strp, &rulep->r_time);
604 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
609 ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
610 ** year, a rule, and the offset from GMT at the time that rule takes effect,
611 ** calculate the Epoch-relative time that rule takes effect.
615 transtime(janfirst, year, rulep, offset)
616 const time_t janfirst;
618 register const struct rule * const rulep;
621 register int leapyear;
622 register time_t value;
624 int d, m1, yy0, yy1, yy2, dow;
627 leapyear = isleap(year);
628 switch (rulep->r_type) {
632 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
634 ** In non-leap years, or if the day number is 59 or less, just
635 ** add SECSPERDAY times the day number-1 to the time of
636 ** January 1, midnight, to get the day.
638 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
639 if (leapyear && rulep->r_day >= 60)
646 ** Just add SECSPERDAY times the day number to the time of
647 ** January 1, midnight, to get the day.
649 value = janfirst + rulep->r_day * SECSPERDAY;
652 case MONTH_NTH_DAY_OF_WEEK:
654 ** Mm.n.d - nth "dth day" of month m.
657 for (i = 0; i < rulep->r_mon - 1; ++i)
658 value += mon_lengths[leapyear][i] * SECSPERDAY;
661 ** Use Zeller's Congruence to get day-of-week of first day of
664 m1 = (rulep->r_mon + 9) % 12 + 1;
665 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
668 dow = ((26 * m1 - 2) / 10 +
669 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
674 ** "dow" is the day-of-week of the first day of the month. Get
675 ** the day-of-month (zero-origin) of the first "dow" day of the
678 d = rulep->r_day - dow;
681 for (i = 1; i < rulep->r_week; ++i) {
682 if (d + DAYSPERWEEK >=
683 mon_lengths[leapyear][rulep->r_mon - 1])
689 ** "d" is the day-of-month (zero-origin) of the day we want.
691 value += d * SECSPERDAY;
696 ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
697 ** question. To get the Epoch-relative time of the specified local
698 ** time on that day, add the transition time and the current offset
701 return value + rulep->r_time + offset;
705 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
710 tzparse(name, sp, lastditch)
712 register struct state * const sp;
715 const char * stdname;
716 const char * dstname;
721 register time_t * atp;
722 register unsigned char * typep;
724 register int load_result;
729 stdlen = strlen(name); /* length of standard zone name */
731 if (stdlen >= sizeof sp->chars)
732 stdlen = (sizeof sp->chars) - 1;
735 name = getzname(name);
736 stdlen = name - stdname;
740 return -1; /* was "stdoffset = 0;" */
742 name = getoffset(name, &stdoffset);
747 load_result = tzload(TZDEFRULES, sp);
748 if (load_result != 0)
749 sp->leapcnt = 0; /* so, we're off a little */
752 name = getzname(name);
753 dstlen = name - dstname; /* length of DST zone name */
756 if (*name != '\0' && *name != ',' && *name != ';') {
757 name = getoffset(name, &dstoffset);
760 } else dstoffset = stdoffset - SECSPERHOUR;
761 if (*name == ',' || *name == ';') {
765 register time_t janfirst;
770 if ((name = getrule(name, &start)) == NULL)
774 if ((name = getrule(name, &end)) == NULL)
778 sp->typecnt = 2; /* standard time and DST */
780 ** Two transitions per year, from EPOCH_YEAR to 2037.
782 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
783 if (sp->timecnt > TZ_MAX_TIMES)
785 sp->ttis[0].tt_gmtoff = -dstoffset;
786 sp->ttis[0].tt_isdst = 1;
787 sp->ttis[0].tt_abbrind = stdlen + 1;
788 sp->ttis[1].tt_gmtoff = -stdoffset;
789 sp->ttis[1].tt_isdst = 0;
790 sp->ttis[1].tt_abbrind = 0;
794 for (year = EPOCH_YEAR; year <= 2037; ++year) {
795 starttime = transtime(janfirst, year, &start,
797 endtime = transtime(janfirst, year, &end,
799 if (starttime > endtime) {
801 *typep++ = 1; /* DST ends */
803 *typep++ = 0; /* DST begins */
806 *typep++ = 0; /* DST begins */
808 *typep++ = 1; /* DST ends */
810 janfirst += year_lengths[isleap(year)] *
814 register long theirstdoffset;
815 register long theirdstoffset;
816 register long theiroffset;
823 if (load_result != 0)
826 ** Initial values of theirstdoffset and theirdstoffset.
829 for (i = 0; i < sp->timecnt; ++i) {
831 if (!sp->ttis[j].tt_isdst) {
833 -sp->ttis[j].tt_gmtoff;
838 for (i = 0; i < sp->timecnt; ++i) {
840 if (sp->ttis[j].tt_isdst) {
842 -sp->ttis[j].tt_gmtoff;
847 ** Initially we're assumed to be in standard time.
850 theiroffset = theirstdoffset;
852 ** Now juggle transition times and types
853 ** tracking offsets as you do.
855 for (i = 0; i < sp->timecnt; ++i) {
857 sp->types[i] = sp->ttis[j].tt_isdst;
858 if (sp->ttis[j].tt_ttisgmt) {
859 /* No adjustment to transition time */
862 ** If summer time is in effect, and the
863 ** transition time was not specified as
864 ** standard time, add the summer time
865 ** offset to the transition time;
866 ** otherwise, add the standard time
867 ** offset to the transition time.
870 ** Transitions from DST to DDST
871 ** will effectively disappear since
872 ** POSIX provides for only one DST
875 if (isdst && !sp->ttis[j].tt_ttisstd) {
876 sp->ats[i] += dstoffset -
879 sp->ats[i] += stdoffset -
883 theiroffset = -sp->ttis[j].tt_gmtoff;
884 if (sp->ttis[j].tt_isdst)
885 theirdstoffset = theiroffset;
886 else theirstdoffset = theiroffset;
889 ** Finally, fill in ttis.
890 ** ttisstd and ttisgmt need not be handled.
892 sp->ttis[0].tt_gmtoff = -stdoffset;
893 sp->ttis[0].tt_isdst = FALSE;
894 sp->ttis[0].tt_abbrind = 0;
895 sp->ttis[1].tt_gmtoff = -dstoffset;
896 sp->ttis[1].tt_isdst = TRUE;
897 sp->ttis[1].tt_abbrind = stdlen + 1;
901 sp->typecnt = 1; /* only standard time */
903 sp->ttis[0].tt_gmtoff = -stdoffset;
904 sp->ttis[0].tt_isdst = 0;
905 sp->ttis[0].tt_abbrind = 0;
907 sp->charcnt = stdlen + 1;
909 sp->charcnt += dstlen + 1;
910 if (sp->charcnt > sizeof sp->chars)
913 (void) strncpy(cp, stdname, stdlen);
917 (void) strncpy(cp, dstname, dstlen);
918 *(cp + dstlen) = '\0';
925 struct state * const sp;
927 if (tzload(gmt, sp) != 0)
928 (void) tzparse(gmt, sp, TRUE);
933 ** A non-static declaration of tzsetwall in a system header file
934 ** may cause a warning about this upcoming static declaration...
937 #endif /* !defined STD_INSPIRED */
940 tzsetwall_basic P((void))
951 if (lclptr == NULL) {
952 lclptr = (struct state *) malloc(sizeof *lclptr);
953 if (lclptr == NULL) {
954 settzname(); /* all we can do */
958 #endif /* defined ALL_STATE */
959 if (tzload((char *) NULL, lclptr) != 0)
968 pthread_mutex_lock(&lcl_mutex);
970 pthread_mutex_unlock(&lcl_mutex);
976 tzset_basic P((void))
982 register const char * name;
990 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
992 lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
994 (void) strcpy(lcl_TZname, name);
997 if (lclptr == NULL) {
998 lclptr = (struct state *) malloc(sizeof *lclptr);
999 if (lclptr == NULL) {
1000 settzname(); /* all we can do */
1004 #endif /* defined ALL_STATE */
1005 if (*name == '\0') {
1007 ** User wants it fast rather than right.
1009 lclptr->leapcnt = 0; /* so, we're off a little */
1010 lclptr->timecnt = 0;
1011 lclptr->ttis[0].tt_gmtoff = 0;
1012 lclptr->ttis[0].tt_abbrind = 0;
1013 (void) strcpy(lclptr->chars, gmt);
1014 } else if (tzload(name, lclptr) != 0)
1015 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1016 (void) gmtload(lclptr);
1024 pthread_mutex_lock(&lcl_mutex);
1026 pthread_mutex_unlock(&lcl_mutex);
1031 ** The easy way to behave "as if no library function calls" localtime
1032 ** is to not call it--so we drop its guts into "localsub", which can be
1033 ** freely called. (And no, the PANS doesn't require the above behavior--
1034 ** but it *is* desirable.)
1036 ** The unused offset argument is for the benefit of mktime variants.
1041 localsub(timep, offset, tmp)
1042 const time_t * const timep;
1044 struct tm * const tmp;
1046 register struct state * sp;
1047 register const struct ttinfo * ttisp;
1049 const time_t t = *timep;
1054 gmtsub(timep, offset, tmp);
1057 #endif /* defined ALL_STATE */
1058 if (sp->timecnt == 0 || t < sp->ats[0]) {
1060 while (sp->ttis[i].tt_isdst)
1061 if (++i >= sp->typecnt) {
1066 for (i = 1; i < sp->timecnt; ++i)
1069 i = sp->types[i - 1];
1071 ttisp = &sp->ttis[i];
1073 ** To get (wrong) behavior that's compatible with System V Release 2.0
1074 ** you'd replace the statement below with
1075 ** t += ttisp->tt_gmtoff;
1076 ** timesub(&t, 0L, sp, tmp);
1078 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1079 tmp->tm_isdst = ttisp->tt_isdst;
1080 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1082 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1083 #endif /* defined TM_ZONE */
1087 localtime_r(timep, p_tm)
1088 const time_t * const timep;
1092 pthread_mutex_lock(&lcl_mutex);
1095 localsub(timep, 0L, p_tm);
1097 pthread_mutex_unlock(&lcl_mutex);
1104 const time_t * const timep;
1107 static struct pthread_mutex _localtime_mutex = PTHREAD_MUTEX_STATIC_INITIALIZER;
1108 static pthread_mutex_t localtime_mutex = &_localtime_mutex;
1109 static pthread_key_t localtime_key = -1;
1112 pthread_mutex_lock(&localtime_mutex);
1113 if (localtime_key < 0) {
1114 if (pthread_key_create(&localtime_key, free) < 0) {
1115 pthread_mutex_unlock(&localtime_mutex);
1119 pthread_mutex_unlock(&localtime_mutex);
1120 p_tm = pthread_getspecific(localtime_key);
1122 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) == NULL)
1124 pthread_setspecific(localtime_key, p_tm);
1126 pthread_mutex_lock(&lcl_mutex);
1128 localsub(timep, 0L, p_tm);
1129 pthread_mutex_unlock(&lcl_mutex);
1133 localsub(timep, 0L, &tm);
1139 ** gmtsub is to gmtime as localsub is to localtime.
1143 gmtsub(timep, offset, tmp)
1144 const time_t * const timep;
1146 struct tm * const tmp;
1149 pthread_mutex_lock(&gmt_mutex);
1154 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1156 #endif /* defined ALL_STATE */
1160 pthread_mutex_unlock(&gmt_mutex);
1162 timesub(timep, offset, gmtptr, tmp);
1165 ** Could get fancy here and deliver something such as
1166 ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1167 ** but this is no time for a treasure hunt.
1170 tmp->TM_ZONE = wildabbr;
1175 else tmp->TM_ZONE = gmtptr->chars;
1176 #endif /* defined ALL_STATE */
1178 tmp->TM_ZONE = gmtptr->chars;
1179 #endif /* State Farm */
1181 #endif /* defined TM_ZONE */
1186 const time_t * const timep;
1189 static struct pthread_mutex _gmtime_mutex = PTHREAD_MUTEX_STATIC_INITIALIZER;
1190 static pthread_mutex_t gmtime_mutex = &_gmtime_mutex;
1191 static pthread_key_t gmtime_key = -1;
1194 pthread_mutex_lock(&gmtime_mutex);
1195 if (gmtime_key < 0) {
1196 if (pthread_key_create(&gmtime_key, free) < 0) {
1197 pthread_mutex_unlock(&gmtime_mutex);
1201 pthread_mutex_unlock(&gmtime_mutex);
1203 * Changed to follow draft 4 pthreads standard, which
1204 * is what BSD currently has.
1206 if ((p_tm = pthread_getspecific(gmtime_key)) == NULL) {
1207 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) == NULL) {
1210 pthread_setspecific(gmtime_key, p_tm);
1212 gmtsub(timep, 0L, p_tm);
1215 gmtsub(timep, 0L, &tm);
1221 gmtime_r(const time_t * timep, struct tm * tm)
1223 gmtsub(timep, 0L, tm);
1230 offtime(timep, offset)
1231 const time_t * const timep;
1234 gmtsub(timep, offset, &tm);
1238 #endif /* defined STD_INSPIRED */
1241 timesub(timep, offset, sp, tmp)
1242 const time_t * const timep;
1244 register const struct state * const sp;
1245 register struct tm * const tmp;
1247 register const struct lsinfo * lp;
1252 register const int * ip;
1260 i = (sp == NULL) ? 0 : sp->leapcnt;
1261 #endif /* defined ALL_STATE */
1264 #endif /* State Farm */
1267 if (*timep >= lp->ls_trans) {
1268 if (*timep == lp->ls_trans) {
1269 hit = ((i == 0 && lp->ls_corr > 0) ||
1270 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1273 sp->lsis[i].ls_trans ==
1274 sp->lsis[i - 1].ls_trans + 1 &&
1275 sp->lsis[i].ls_corr ==
1276 sp->lsis[i - 1].ls_corr + 1) {
1285 days = *timep / SECSPERDAY;
1286 rem = *timep % SECSPERDAY;
1288 if (*timep == 0x80000000) {
1290 ** A 3B1 muffs the division on the most negative number.
1295 #endif /* defined mc68k */
1296 rem += (offset - corr);
1301 while (rem >= SECSPERDAY) {
1305 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1306 rem = rem % SECSPERHOUR;
1307 tmp->tm_min = (int) (rem / SECSPERMIN);
1309 ** A positive leap second requires a special
1310 ** representation. This uses "... ??:59:60" et seq.
1312 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1313 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1314 if (tmp->tm_wday < 0)
1315 tmp->tm_wday += DAYSPERWEEK;
1317 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1318 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1321 newy = y + days / DAYSPERNYEAR;
1324 days -= (newy - y) * DAYSPERNYEAR +
1325 LEAPS_THRU_END_OF(newy - 1) -
1326 LEAPS_THRU_END_OF(y - 1);
1329 tmp->tm_year = y - TM_YEAR_BASE;
1330 tmp->tm_yday = (int) days;
1331 ip = mon_lengths[yleap];
1332 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1333 days = days - (long) ip[tmp->tm_mon];
1334 tmp->tm_mday = (int) (days + 1);
1337 tmp->TM_GMTOFF = offset;
1338 #endif /* defined TM_GMTOFF */
1343 const time_t * const timep;
1346 ** Section 4.12.3.2 of X3.159-1989 requires that
1347 ** The ctime funciton converts the calendar time pointed to by timer
1348 ** to local time in the form of a string. It is equivalent to
1349 ** asctime(localtime(timer))
1351 return asctime(localtime(timep));
1356 const time_t * const timep;
1360 return asctime_r(localtime_r(timep, &tm), buf);
1364 ** Adapted from code provided by Robert Elz, who writes:
1365 ** The "best" way to do mktime I think is based on an idea of Bob
1366 ** Kridle's (so its said...) from a long time ago.
1367 ** [kridle@xinet.com as of 1996-01-16.]
1368 ** It does a binary search of the time_t space. Since time_t's are
1369 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1370 ** would still be very reasonable).
1375 #endif /* !defined WRONG */
1378 ** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1382 increment_overflow(number, delta)
1390 return (*number < number0) != (delta < 0);
1394 normalize_overflow(tensptr, unitsptr, base)
1395 int * const tensptr;
1396 int * const unitsptr;
1399 register int tensdelta;
1401 tensdelta = (*unitsptr >= 0) ?
1402 (*unitsptr / base) :
1403 (-1 - (-1 - *unitsptr) / base);
1404 *unitsptr -= tensdelta * base;
1405 return increment_overflow(tensptr, tensdelta);
1410 register const struct tm * const atmp;
1411 register const struct tm * const btmp;
1413 register int result;
1415 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1416 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1417 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1418 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1419 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1420 result = atmp->tm_sec - btmp->tm_sec;
1425 time2(tmp, funcp, offset, okayp)
1426 struct tm * const tmp;
1427 void (* const funcp) P((const time_t*, long, struct tm*));
1431 register const struct state * sp;
1435 register int saved_seconds;
1438 struct tm yourtm, mytm;
1442 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1444 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1446 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1449 ** Turn yourtm.tm_year into an actual year number for now.
1450 ** It is converted back to an offset from TM_YEAR_BASE later.
1452 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1454 while (yourtm.tm_mday <= 0) {
1455 if (increment_overflow(&yourtm.tm_year, -1))
1457 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1458 yourtm.tm_mday += year_lengths[isleap(i)];
1460 while (yourtm.tm_mday > DAYSPERLYEAR) {
1461 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1462 yourtm.tm_mday -= year_lengths[isleap(i)];
1463 if (increment_overflow(&yourtm.tm_year, 1))
1467 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1468 if (yourtm.tm_mday <= i)
1470 yourtm.tm_mday -= i;
1471 if (++yourtm.tm_mon >= MONSPERYEAR) {
1473 if (increment_overflow(&yourtm.tm_year, 1))
1477 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1479 if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1481 ** We can't set tm_sec to 0, because that might push the
1482 ** time below the minimum representable time.
1483 ** Set tm_sec to 59 instead.
1484 ** This assumes that the minimum representable time is
1485 ** not in the same minute that a leap second was deleted from,
1486 ** which is a safer assumption than using 58 would be.
1488 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1490 saved_seconds = yourtm.tm_sec;
1491 yourtm.tm_sec = SECSPERMIN - 1;
1493 saved_seconds = yourtm.tm_sec;
1497 ** Divide the search space in half
1498 ** (this works whether time_t is signed or unsigned).
1500 bits = TYPE_BIT(time_t) - 1;
1502 ** If time_t is signed, then 0 is just above the median,
1503 ** assuming two's complement arithmetic.
1504 ** If time_t is unsigned, then (1 << bits) is just above the median.
1506 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1508 (*funcp)(&t, offset, &mytm);
1509 dir = tmcomp(&mytm, &yourtm);
1514 --t; /* may be needed if new t is minimal */
1516 t -= ((time_t) 1) << bits;
1517 else t += ((time_t) 1) << bits;
1520 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1523 ** Right time, wrong type.
1524 ** Hunt for right time, right type.
1525 ** It's okay to guess wrong since the guess
1529 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1531 sp = (const struct state *)
1532 (((void *) funcp == (void *) localsub) ?
1537 #endif /* defined ALL_STATE */
1538 for (i = sp->typecnt - 1; i >= 0; --i) {
1539 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1541 for (j = sp->typecnt - 1; j >= 0; --j) {
1542 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1544 newt = t + sp->ttis[j].tt_gmtoff -
1545 sp->ttis[i].tt_gmtoff;
1546 (*funcp)(&newt, offset, &mytm);
1547 if (tmcomp(&mytm, &yourtm) != 0)
1549 if (mytm.tm_isdst != yourtm.tm_isdst)
1561 newt = t + saved_seconds;
1562 if ((newt < t) != (saved_seconds < 0))
1565 (*funcp)(&t, offset, tmp);
1571 time1(tmp, funcp, offset)
1572 struct tm * const tmp;
1573 void (* const funcp) P((const time_t *, long, struct tm *));
1577 register const struct state * sp;
1578 register int samei, otheri;
1581 if (tmp->tm_isdst > 1)
1583 t = time2(tmp, funcp, offset, &okay);
1586 ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1590 if (tmp->tm_isdst < 0)
1591 tmp->tm_isdst = 0; /* reset to std and try again */
1592 #endif /* defined PCTS */
1594 if (okay || tmp->tm_isdst < 0)
1596 #endif /* !defined PCTS */
1598 ** We're supposed to assume that somebody took a time of one type
1599 ** and did some math on it that yielded a "struct tm" that's bad.
1600 ** We try to divine the type they started from and adjust to the
1604 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1606 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1611 #endif /* defined ALL_STATE */
1612 for (samei = sp->typecnt - 1; samei >= 0; --samei) {
1613 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1615 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
1616 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1618 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1619 sp->ttis[samei].tt_gmtoff;
1620 tmp->tm_isdst = !tmp->tm_isdst;
1621 t = time2(tmp, funcp, offset, &okay);
1624 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1625 sp->ttis[samei].tt_gmtoff;
1626 tmp->tm_isdst = !tmp->tm_isdst;
1634 struct tm * const tmp;
1636 time_t mktime_return_value;
1638 pthread_mutex_lock(&lcl_mutex);
1641 mktime_return_value = time1(tmp, localsub, 0L);
1643 pthread_mutex_unlock(&lcl_mutex);
1645 return(mktime_return_value);
1652 struct tm * const tmp;
1654 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1660 struct tm * const tmp;
1663 return time1(tmp, gmtsub, 0L);
1667 timeoff(tmp, offset)
1668 struct tm * const tmp;
1672 return time1(tmp, gmtsub, offset);
1675 #endif /* defined STD_INSPIRED */
1680 ** The following is supplied for compatibility with
1681 ** previous versions of the CMUCS runtime library.
1686 struct tm * const tmp;
1688 const time_t t = mktime(tmp);
1695 #endif /* defined CMUCS */
1698 ** XXX--is the below the right way to conditionalize??
1704 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1705 ** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
1706 ** is not the case if we are accounting for leap seconds.
1707 ** So, we provide the following conversion routines for use
1708 ** when exchanging timestamps with POSIX conforming systems.
1715 register struct state * sp;
1716 register struct lsinfo * lp;
1723 if (*timep >= lp->ls_trans)
1734 return t - leapcorr(&t);
1746 ** For a positive leap second hit, the result
1747 ** is not unique. For a negative leap second
1748 ** hit, the corresponding time doesn't exist,
1749 ** so we return an adjacent second.
1751 x = t + leapcorr(&t);
1752 y = x - leapcorr(&x);
1756 y = x - leapcorr(&x);
1763 y = x - leapcorr(&x);
1771 #endif /* defined STD_INSPIRED */