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.6 2005/12/04 23:41:06 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>
28 #include <un-namespace.h>
32 #include "libc_private.h"
34 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
35 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
38 ** SunOS 4.1.1 headers lack O_BINARY.
42 #define OPEN_MODE (O_RDONLY | O_BINARY)
43 #endif /* defined O_BINARY */
45 #define OPEN_MODE O_RDONLY
46 #endif /* !defined O_BINARY */
50 ** Someone might make incorrect use of a time zone abbreviation:
51 ** 1. They might reference tzname[0] before calling tzset (explicitly
53 ** 2. They might reference tzname[1] before calling tzset (explicitly
55 ** 3. They might reference tzname[1] after setting to a time zone
56 ** in which Daylight Saving Time is never observed.
57 ** 4. They might reference tzname[0] after setting to a time zone
58 ** in which Standard Time is never observed.
59 ** 5. They might reference tm.TM_ZONE after calling offtime.
60 ** What's best to do in the above cases is open to debate;
61 ** for now, we just set things up so that in any of the five cases
62 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
63 ** string "tzname[0] used before set", and similarly for the other cases.
64 ** And another: initialize tzname[0] to "ERA", with an explanation in the
65 ** manual page of what this "time zone abbreviation" means (doing this so
66 ** that tzname[0] has the "normal" length of three characters).
69 #endif /* !defined WILDABBR */
71 static char wildabbr[] = "WILDABBR";
73 static const char gmt[] = "GMT";
75 struct ttinfo { /* time type information */
76 long tt_gmtoff; /* GMT offset in seconds */
77 int tt_isdst; /* used to set tm_isdst */
78 int tt_abbrind; /* abbreviation list index */
79 int tt_ttisstd; /* TRUE if transition is std time */
80 int tt_ttisgmt; /* TRUE if transition is GMT */
83 struct lsinfo { /* leap second information */
84 time_t ls_trans; /* transition time */
85 long ls_corr; /* correction to apply */
88 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
91 #define MY_TZNAME_MAX TZNAME_MAX
92 #endif /* defined TZNAME_MAX */
94 #define MY_TZNAME_MAX 255
95 #endif /* !defined TZNAME_MAX */
102 time_t ats[TZ_MAX_TIMES];
103 unsigned char types[TZ_MAX_TIMES];
104 struct ttinfo ttis[TZ_MAX_TYPES];
105 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
106 (2 * (MY_TZNAME_MAX + 1)))];
107 struct lsinfo lsis[TZ_MAX_LEAPS];
111 int r_type; /* type of rule--see below */
112 int r_day; /* day number of rule */
113 int r_week; /* week number of rule */
114 int r_mon; /* month number of rule */
115 long r_time; /* transition time of rule */
118 #define JULIAN_DAY 0 /* Jn - Julian day */
119 #define DAY_OF_YEAR 1 /* n - day of year */
120 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
123 ** Prototypes for static functions.
126 static long detzcode(const char * codep);
127 static const char * getzname(const char * strp);
128 static const char * getnum(const char * strp, int * nump, int min, int max);
129 static const char * getsecs(const char * strp, long * secsp);
130 static const char * getoffset(const char * strp, long * offsetp);
131 static const char * getrule(const char * strp, struct rule * rulep);
132 static void gmtload(struct state * sp);
133 static void gmtsub(const time_t * timep, long offset,
135 static void localsub(const time_t * timep, long offset,
137 static int increment_overflow(int * number, int delta);
138 static int normalize_overflow(int * tensptr, int * unitsptr,
140 static void settzname(void);
141 static time_t time1(struct tm * tmp,
142 void(*funcp)(const time_t *, long, struct tm *),
144 static time_t time2(struct tm *tmp,
145 void(*funcp)(const time_t *, long, struct tm *),
146 long offset, int * okayp);
147 static void timesub(const time_t * timep, long offset,
148 const struct state * sp, struct tm * tmp);
149 static int tmcomp(const struct tm * atmp,
150 const struct tm * btmp);
151 static time_t transtime(time_t janfirst, int year,
152 const struct rule * rulep, long offset);
153 static int tzload(const char * name, struct state * sp);
154 static int tzparse(const char * name, struct state * sp,
158 static struct state * lclptr;
159 static struct state * gmtptr;
160 #endif /* defined ALL_STATE */
163 static struct state lclmem;
164 static struct state gmtmem;
165 #define lclptr (&lclmem)
166 #define gmtptr (&gmtmem)
167 #endif /* State Farm */
169 #ifndef TZ_STRLEN_MAX
170 #define TZ_STRLEN_MAX 255
171 #endif /* !defined TZ_STRLEN_MAX */
173 static char lcl_TZname[TZ_STRLEN_MAX + 1];
174 static int lcl_is_set;
175 static int gmt_is_set;
176 static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
177 static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
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 */
204 detzcode(const char * const codep)
209 result = (codep[0] & 0x80) ? ~0L : 0L;
210 for (i = 0; i < 4; ++i)
211 result = (result << 8) | (codep[i] & 0xff);
218 struct state * sp = lclptr;
221 tzname[0] = wildabbr;
222 tzname[1] = wildabbr;
226 #endif /* defined USG_COMPAT */
229 #endif /* defined ALTZONE */
232 tzname[0] = tzname[1] = gmt;
235 #endif /* defined ALL_STATE */
236 for (i = 0; i < sp->typecnt; ++i) {
237 const struct ttinfo * const ttisp = &sp->ttis[i];
239 tzname[ttisp->tt_isdst] =
240 &sp->chars[ttisp->tt_abbrind];
244 if (i == 0 || !ttisp->tt_isdst)
245 timezone = -(ttisp->tt_gmtoff);
246 #endif /* defined USG_COMPAT */
248 if (i == 0 || ttisp->tt_isdst)
249 altzone = -(ttisp->tt_gmtoff);
250 #endif /* defined ALTZONE */
253 ** And to get the latest zone names into tzname. . .
255 for (i = 0; i < sp->timecnt; ++i) {
256 const struct ttinfo * const ttisp =
260 tzname[ttisp->tt_isdst] =
261 &sp->chars[ttisp->tt_abbrind];
266 tzload(const char *name, struct state * const sp)
272 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
273 if (name != NULL && issetugid() != 0)
274 if ((name[0] == ':' && name[1] == '/') ||
275 name[0] == '/' || strchr(name, '.'))
277 if (name == NULL && (name = TZDEFAULT) == NULL)
283 ** Section 4.9.1 of the C standard says that
284 ** "FILENAME_MAX expands to an integral constant expression
285 ** that is the size needed for an array of char large enough
286 ** to hold the longest file name string that the implementation
287 ** guarantees can be opened."
289 char fullname[FILENAME_MAX + 1];
293 doaccess = name[0] == '/';
295 if ((p = TZDIR) == NULL)
297 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
300 strcat(fullname, "/");
301 strcat(fullname, name);
303 ** Set doaccess if '.' (as in "../") shows up in name.
305 if (strchr(name, '.') != NULL)
309 if (doaccess && access(name, R_OK) != 0)
311 if ((fid = _open(name, OPEN_MODE)) == -1)
313 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
319 struct tzhead * tzhp;
320 char buf[sizeof *sp + sizeof *tzhp];
324 i = _read(fid, buf, sizeof buf);
325 if (_close(fid) != 0)
328 p += (sizeof tzhp->tzh_magic) + (sizeof tzhp->tzh_reserved);
329 ttisstdcnt = (int) detzcode(p);
331 ttisgmtcnt = (int) detzcode(p);
333 sp->leapcnt = (int) detzcode(p);
335 sp->timecnt = (int) detzcode(p);
337 sp->typecnt = (int) detzcode(p);
339 sp->charcnt = (int) detzcode(p);
341 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
342 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
343 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
344 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
345 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
346 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
348 if (i - (p - buf) < sp->timecnt * 4 + /* ats */
349 sp->timecnt + /* types */
350 sp->typecnt * (4 + 2) + /* ttinfos */
351 sp->charcnt + /* chars */
352 sp->leapcnt * (4 + 4) + /* lsinfos */
353 ttisstdcnt + /* ttisstds */
354 ttisgmtcnt) /* ttisgmts */
356 for (i = 0; i < sp->timecnt; ++i) {
357 sp->ats[i] = detzcode(p);
360 for (i = 0; i < sp->timecnt; ++i) {
361 sp->types[i] = (unsigned char) *p++;
362 if (sp->types[i] >= sp->typecnt)
365 for (i = 0; i < sp->typecnt; ++i) {
366 struct ttinfo * ttisp;
368 ttisp = &sp->ttis[i];
369 ttisp->tt_gmtoff = detzcode(p);
371 ttisp->tt_isdst = (unsigned char) *p++;
372 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
374 ttisp->tt_abbrind = (unsigned char) *p++;
375 if (ttisp->tt_abbrind < 0 ||
376 ttisp->tt_abbrind > sp->charcnt)
379 for (i = 0; i < sp->charcnt; ++i)
381 sp->chars[i] = '\0'; /* ensure '\0' at end */
382 for (i = 0; i < sp->leapcnt; ++i) {
383 struct lsinfo * lsisp;
385 lsisp = &sp->lsis[i];
386 lsisp->ls_trans = detzcode(p);
388 lsisp->ls_corr = detzcode(p);
391 for (i = 0; i < sp->typecnt; ++i) {
392 struct ttinfo * ttisp;
394 ttisp = &sp->ttis[i];
396 ttisp->tt_ttisstd = FALSE;
398 ttisp->tt_ttisstd = *p++;
399 if (ttisp->tt_ttisstd != TRUE &&
400 ttisp->tt_ttisstd != FALSE)
404 for (i = 0; i < sp->typecnt; ++i) {
405 struct ttinfo * ttisp;
407 ttisp = &sp->ttis[i];
409 ttisp->tt_ttisgmt = FALSE;
411 ttisp->tt_ttisgmt = *p++;
412 if (ttisp->tt_ttisgmt != TRUE &&
413 ttisp->tt_ttisgmt != FALSE)
421 static const int mon_lengths[2][MONSPERYEAR] = {
422 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
423 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
426 static const int year_lengths[2] = {
427 DAYSPERNYEAR, DAYSPERLYEAR
431 ** Given a pointer into a time zone string, scan until a character that is not
432 ** a valid character in a zone name is found. Return a pointer to that
437 getzname(const char *strp)
441 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
448 ** Given a pointer into a time zone string, extract a number from that string.
449 ** Check that the number is within a specified range; if it is not, return
451 ** Otherwise, return a pointer to the first character not part of the number.
455 getnum(const char *strp, int * const nump, const int min, const int max)
460 if (strp == NULL || !is_digit(c = *strp))
464 num = num * 10 + (c - '0');
466 return NULL; /* illegal value */
468 } while (is_digit(c));
470 return NULL; /* illegal value */
476 ** Given a pointer into a time zone string, extract a number of seconds,
477 ** in hh[:mm[:ss]] form, from the string.
478 ** If any error occurs, return NULL.
479 ** Otherwise, return a pointer to the first character not part of the number
484 getsecs(const char *strp, long * const secsp)
489 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
490 ** "M10.4.6/26", which does not conform to Posix,
491 ** but which specifies the equivalent of
492 ** ``02:00 on the first Sunday on or after 23 Oct''.
494 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
497 *secsp = num * (long) SECSPERHOUR;
500 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
503 *secsp += num * SECSPERMIN;
506 /* `SECSPERMIN' allows for leap seconds. */
507 strp = getnum(strp, &num, 0, SECSPERMIN);
517 ** Given a pointer into a time zone string, extract an offset, in
518 ** [+-]hh[:mm[:ss]] form, from the string.
519 ** If any error occurs, return NULL.
520 ** Otherwise, return a pointer to the first character not part of the time.
524 getoffset(const char *strp, long * const offsetp)
531 } else if (*strp == '+')
533 strp = getsecs(strp, offsetp);
535 return NULL; /* illegal time */
537 *offsetp = -*offsetp;
542 ** Given a pointer into a time zone string, extract a rule in the form
543 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
544 ** If a valid rule is not found, return NULL.
545 ** Otherwise, return a pointer to the first character not part of the rule.
549 getrule(const char *strp, struct rule * const rulep)
555 rulep->r_type = JULIAN_DAY;
557 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
558 } else if (*strp == 'M') {
562 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
564 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
569 strp = getnum(strp, &rulep->r_week, 1, 5);
574 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
575 } else if (is_digit(*strp)) {
579 rulep->r_type = DAY_OF_YEAR;
580 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
581 } else return NULL; /* invalid format */
589 strp = getsecs(strp, &rulep->r_time);
590 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
595 ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
596 ** year, a rule, and the offset from GMT at the time that rule takes effect,
597 ** calculate the Epoch-relative time that rule takes effect.
601 transtime(const time_t janfirst, const int year,
602 const struct rule * const rulep, const long offset)
607 int d, m1, yy0, yy1, yy2, dow;
610 leapyear = isleap(year);
611 switch (rulep->r_type) {
615 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
617 ** In non-leap years, or if the day number is 59 or less, just
618 ** add SECSPERDAY times the day number-1 to the time of
619 ** January 1, midnight, to get the day.
621 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
622 if (leapyear && rulep->r_day >= 60)
629 ** Just add SECSPERDAY times the day number to the time of
630 ** January 1, midnight, to get the day.
632 value = janfirst + rulep->r_day * SECSPERDAY;
635 case MONTH_NTH_DAY_OF_WEEK:
637 ** Mm.n.d - nth "dth day" of month m.
640 for (i = 0; i < rulep->r_mon - 1; ++i)
641 value += mon_lengths[leapyear][i] * SECSPERDAY;
644 ** Use Zeller's Congruence to get day-of-week of first day of
647 m1 = (rulep->r_mon + 9) % 12 + 1;
648 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
651 dow = ((26 * m1 - 2) / 10 +
652 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
657 ** "dow" is the day-of-week of the first day of the month. Get
658 ** the day-of-month (zero-origin) of the first "dow" day of the
661 d = rulep->r_day - dow;
664 for (i = 1; i < rulep->r_week; ++i) {
665 if (d + DAYSPERWEEK >=
666 mon_lengths[leapyear][rulep->r_mon - 1])
672 ** "d" is the day-of-month (zero-origin) of the day we want.
674 value += d * SECSPERDAY;
679 ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
680 ** question. To get the Epoch-relative time of the specified local
681 ** time on that day, add the transition time and the current offset
684 return value + rulep->r_time + offset;
688 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
693 tzparse(const char *name, struct state * const sp, const int lastditch)
695 const char * stdname;
696 const char * dstname;
702 unsigned char * typep;
709 stdlen = strlen(name); /* length of standard zone name */
711 if (stdlen >= sizeof sp->chars)
712 stdlen = (sizeof sp->chars) - 1;
715 name = getzname(name);
716 stdlen = name - stdname;
720 return -1; /* was "stdoffset = 0;" */
722 name = getoffset(name, &stdoffset);
727 load_result = tzload(TZDEFRULES, sp);
728 if (load_result != 0)
729 sp->leapcnt = 0; /* so, we're off a little */
732 name = getzname(name);
733 dstlen = name - dstname; /* length of DST zone name */
736 if (*name != '\0' && *name != ',' && *name != ';') {
737 name = getoffset(name, &dstoffset);
740 } else dstoffset = stdoffset - SECSPERHOUR;
741 if (*name == ',' || *name == ';') {
750 if ((name = getrule(name, &start)) == NULL)
754 if ((name = getrule(name, &end)) == NULL)
758 sp->typecnt = 2; /* standard time and DST */
760 ** Two transitions per year, from EPOCH_YEAR to 2037.
762 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
763 if (sp->timecnt > TZ_MAX_TIMES)
765 sp->ttis[0].tt_gmtoff = -dstoffset;
766 sp->ttis[0].tt_isdst = 1;
767 sp->ttis[0].tt_abbrind = stdlen + 1;
768 sp->ttis[1].tt_gmtoff = -stdoffset;
769 sp->ttis[1].tt_isdst = 0;
770 sp->ttis[1].tt_abbrind = 0;
774 for (year = EPOCH_YEAR; year <= 2037; ++year) {
775 starttime = transtime(janfirst, year, &start,
777 endtime = transtime(janfirst, year, &end,
779 if (starttime > endtime) {
781 *typep++ = 1; /* DST ends */
783 *typep++ = 0; /* DST begins */
786 *typep++ = 0; /* DST begins */
788 *typep++ = 1; /* DST ends */
790 janfirst += year_lengths[isleap(year)] *
803 if (load_result != 0)
806 ** Initial values of theirstdoffset and theirdstoffset.
809 for (i = 0; i < sp->timecnt; ++i) {
811 if (!sp->ttis[j].tt_isdst) {
813 -sp->ttis[j].tt_gmtoff;
818 for (i = 0; i < sp->timecnt; ++i) {
820 if (sp->ttis[j].tt_isdst) {
822 -sp->ttis[j].tt_gmtoff;
827 ** Initially we're assumed to be in standard time.
830 theiroffset = theirstdoffset;
832 ** Now juggle transition times and types
833 ** tracking offsets as you do.
835 for (i = 0; i < sp->timecnt; ++i) {
837 sp->types[i] = sp->ttis[j].tt_isdst;
838 if (sp->ttis[j].tt_ttisgmt) {
839 /* No adjustment to transition time */
842 ** If summer time is in effect, and the
843 ** transition time was not specified as
844 ** standard time, add the summer time
845 ** offset to the transition time;
846 ** otherwise, add the standard time
847 ** offset to the transition time.
850 ** Transitions from DST to DDST
851 ** will effectively disappear since
852 ** POSIX provides for only one DST
855 if (isdst && !sp->ttis[j].tt_ttisstd) {
856 sp->ats[i] += dstoffset -
859 sp->ats[i] += stdoffset -
863 theiroffset = -sp->ttis[j].tt_gmtoff;
864 if (sp->ttis[j].tt_isdst)
865 theirdstoffset = theiroffset;
866 else theirstdoffset = theiroffset;
869 ** Finally, fill in ttis.
870 ** ttisstd and ttisgmt need not be handled.
872 sp->ttis[0].tt_gmtoff = -stdoffset;
873 sp->ttis[0].tt_isdst = FALSE;
874 sp->ttis[0].tt_abbrind = 0;
875 sp->ttis[1].tt_gmtoff = -dstoffset;
876 sp->ttis[1].tt_isdst = TRUE;
877 sp->ttis[1].tt_abbrind = stdlen + 1;
881 sp->typecnt = 1; /* only standard time */
883 sp->ttis[0].tt_gmtoff = -stdoffset;
884 sp->ttis[0].tt_isdst = 0;
885 sp->ttis[0].tt_abbrind = 0;
887 sp->charcnt = stdlen + 1;
889 sp->charcnt += dstlen + 1;
890 if (sp->charcnt > sizeof sp->chars)
893 strncpy(cp, stdname, stdlen);
897 strncpy(cp, dstname, dstlen);
898 *(cp + dstlen) = '\0';
904 gmtload(struct state * const sp)
906 if (tzload(gmt, sp) != 0)
907 tzparse(gmt, sp, TRUE);
911 tzsetwall_basic(void)
918 if (lclptr == NULL) {
919 lclptr = (struct state *) malloc(sizeof *lclptr);
920 if (lclptr == NULL) {
921 settzname(); /* all we can do */
925 #endif /* defined ALL_STATE */
926 if (tzload((char *) NULL, lclptr) != 0)
934 _MUTEX_LOCK(&lcl_mutex);
936 _MUTEX_UNLOCK(&lcl_mutex);
950 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
952 lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
954 strcpy(lcl_TZname, name);
957 if (lclptr == NULL) {
958 lclptr = (struct state *) malloc(sizeof *lclptr);
959 if (lclptr == NULL) {
960 settzname(); /* all we can do */
964 #endif /* defined ALL_STATE */
967 ** User wants it fast rather than right.
969 lclptr->leapcnt = 0; /* so, we're off a little */
971 lclptr->ttis[0].tt_gmtoff = 0;
972 lclptr->ttis[0].tt_abbrind = 0;
973 strcpy(lclptr->chars, gmt);
974 } else if (tzload(name, lclptr) != 0)
975 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
983 _MUTEX_LOCK(&lcl_mutex);
985 _MUTEX_UNLOCK(&lcl_mutex);
989 ** The easy way to behave "as if no library function calls" localtime
990 ** is to not call it--so we drop its guts into "localsub", which can be
991 ** freely called. (And no, the PANS doesn't require the above behavior--
992 ** but it *is* desirable.)
994 ** The unused offset argument is for the benefit of mktime variants.
999 localsub(const time_t * const timep, const long offset __unused,
1000 struct tm * const tmp)
1003 const struct ttinfo * ttisp;
1005 const time_t t = *timep;
1010 gmtsub(timep, offset, tmp);
1013 #endif /* defined ALL_STATE */
1014 if (sp->timecnt == 0 || t < sp->ats[0]) {
1016 while (sp->ttis[i].tt_isdst)
1017 if (++i >= sp->typecnt) {
1022 for (i = 1; i < sp->timecnt; ++i)
1025 i = sp->types[i - 1];
1027 ttisp = &sp->ttis[i];
1029 ** To get (wrong) behavior that's compatible with System V Release 2.0
1030 ** you'd replace the statement below with
1031 ** t += ttisp->tt_gmtoff;
1032 ** timesub(&t, 0L, sp, tmp);
1034 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1035 tmp->tm_isdst = ttisp->tt_isdst;
1036 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1038 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1039 #endif /* defined TM_ZONE */
1043 localtime_r(const time_t * const timep, struct tm *p_tm)
1045 _MUTEX_LOCK(&lcl_mutex);
1047 localsub(timep, 0L, p_tm);
1048 _MUTEX_UNLOCK(&lcl_mutex);
1053 localtime(const time_t * const timep)
1055 static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1056 static pthread_key_t localtime_key = -1;
1059 if (__isthreaded != 0) {
1060 _pthread_mutex_lock(&localtime_mutex);
1061 if (localtime_key < 0) {
1062 if (_pthread_key_create(&localtime_key, free) < 0) {
1063 _pthread_mutex_unlock(&localtime_mutex);
1067 _pthread_mutex_unlock(&localtime_mutex);
1068 p_tm = _pthread_getspecific(localtime_key);
1070 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1073 _pthread_setspecific(localtime_key, p_tm);
1075 _pthread_mutex_lock(&lcl_mutex);
1077 localsub(timep, 0L, p_tm);
1078 _pthread_mutex_unlock(&lcl_mutex);
1082 localsub(timep, 0L, &tm);
1088 ** gmtsub is to gmtime as localsub is to localtime.
1092 gmtsub(const time_t * const timep, const long offset, struct tm * const tmp)
1094 _MUTEX_LOCK(&gmt_mutex);
1098 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1100 #endif /* defined ALL_STATE */
1103 _MUTEX_UNLOCK(&gmt_mutex);
1104 timesub(timep, offset, gmtptr, tmp);
1107 ** Could get fancy here and deliver something such as
1108 ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1109 ** but this is no time for a treasure hunt.
1112 tmp->TM_ZONE = wildabbr;
1117 else tmp->TM_ZONE = gmtptr->chars;
1118 #endif /* defined ALL_STATE */
1120 tmp->TM_ZONE = gmtptr->chars;
1121 #endif /* State Farm */
1123 #endif /* defined TM_ZONE */
1127 gmtime(const time_t * const timep)
1129 static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1130 static pthread_key_t gmtime_key = -1;
1133 if (__isthreaded != 0) {
1134 _pthread_mutex_lock(&gmtime_mutex);
1135 if (gmtime_key < 0) {
1136 if (_pthread_key_create(&gmtime_key, free) < 0) {
1137 _pthread_mutex_unlock(&gmtime_mutex);
1141 _pthread_mutex_unlock(&gmtime_mutex);
1143 * Changed to follow POSIX.1 threads standard, which
1144 * is what BSD currently has.
1146 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1147 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1151 _pthread_setspecific(gmtime_key, p_tm);
1153 gmtsub(timep, 0L, p_tm);
1157 gmtsub(timep, 0L, &tm);
1163 gmtime_r(const time_t * timep, struct tm * tm_p)
1165 gmtsub(timep, 0L, tm_p);
1172 offtime(const time_t * const timep, const long offset)
1174 gmtsub(timep, offset, &tm);
1178 #endif /* defined STD_INSPIRED */
1181 timesub(const time_t * const timep, const long offset,
1182 const struct state * const sp, struct tm * const tmp)
1184 const struct lsinfo * lp;
1197 i = (sp == NULL) ? 0 : sp->leapcnt;
1198 #endif /* defined ALL_STATE */
1201 #endif /* State Farm */
1204 if (*timep >= lp->ls_trans) {
1205 if (*timep == lp->ls_trans) {
1206 hit = ((i == 0 && lp->ls_corr > 0) ||
1207 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1210 sp->lsis[i].ls_trans ==
1211 sp->lsis[i - 1].ls_trans + 1 &&
1212 sp->lsis[i].ls_corr ==
1213 sp->lsis[i - 1].ls_corr + 1) {
1222 days = *timep / SECSPERDAY;
1223 rem = *timep % SECSPERDAY;
1225 if (*timep == 0x80000000) {
1227 ** A 3B1 muffs the division on the most negative number.
1232 #endif /* defined mc68k */
1233 rem += (offset - corr);
1238 while (rem >= SECSPERDAY) {
1242 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1243 rem = rem % SECSPERHOUR;
1244 tmp->tm_min = (int) (rem / SECSPERMIN);
1246 ** A positive leap second requires a special
1247 ** representation. This uses "... ??:59:60" et seq.
1249 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1250 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1251 if (tmp->tm_wday < 0)
1252 tmp->tm_wday += DAYSPERWEEK;
1254 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1255 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1258 newy = y + days / DAYSPERNYEAR;
1261 days -= (newy - y) * DAYSPERNYEAR +
1262 LEAPS_THRU_END_OF(newy - 1) -
1263 LEAPS_THRU_END_OF(y - 1);
1266 tmp->tm_year = y - TM_YEAR_BASE;
1267 tmp->tm_yday = (int) days;
1268 ip = mon_lengths[yleap];
1269 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1270 days = days - (long) ip[tmp->tm_mon];
1271 tmp->tm_mday = (int) (days + 1);
1274 tmp->TM_GMTOFF = offset;
1275 #endif /* defined TM_GMTOFF */
1279 ctime(const time_t * const timep)
1282 ** Section 4.12.3.2 of X3.159-1989 requires that
1283 ** The ctime funciton converts the calendar time pointed to by timer
1284 ** to local time in the form of a string. It is equivalent to
1285 ** asctime(localtime(timer))
1287 return asctime(localtime(timep));
1291 ctime_r(const time_t * const timep, char *buf)
1294 return asctime_r(localtime_r(timep, &tm1), buf);
1298 ** Adapted from code provided by Robert Elz, who writes:
1299 ** The "best" way to do mktime I think is based on an idea of Bob
1300 ** Kridle's (so its said...) from a long time ago.
1301 ** [kridle@xinet.com as of 1996-01-16.]
1302 ** It does a binary search of the time_t space. Since time_t's are
1303 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1304 ** would still be very reasonable).
1309 #endif /* !defined WRONG */
1312 ** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1316 increment_overflow(int *number, int delta)
1322 return (*number < number0) != (delta < 0);
1326 normalize_overflow(int * const tensptr, int * const unitsptr, const int base)
1330 tensdelta = (*unitsptr >= 0) ?
1331 (*unitsptr / base) :
1332 (-1 - (-1 - *unitsptr) / base);
1333 *unitsptr -= tensdelta * base;
1334 return increment_overflow(tensptr, tensdelta);
1338 tmcomp(const struct tm * const atmp, const struct tm * const btmp)
1342 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1343 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1344 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1345 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1346 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1347 result = atmp->tm_sec - btmp->tm_sec;
1352 time2(struct tm * const tmp,
1353 void (* const funcp)(const time_t *, long, struct tm *),
1354 const long offset, int * const okayp)
1356 const struct state * sp;
1363 struct tm yourtm, mytm;
1367 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1369 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1371 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1374 ** Turn yourtm.tm_year into an actual year number for now.
1375 ** It is converted back to an offset from TM_YEAR_BASE later.
1377 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1379 while (yourtm.tm_mday <= 0) {
1380 if (increment_overflow(&yourtm.tm_year, -1))
1382 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1383 yourtm.tm_mday += year_lengths[isleap(i)];
1385 while (yourtm.tm_mday > DAYSPERLYEAR) {
1386 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1387 yourtm.tm_mday -= year_lengths[isleap(i)];
1388 if (increment_overflow(&yourtm.tm_year, 1))
1392 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1393 if (yourtm.tm_mday <= i)
1395 yourtm.tm_mday -= i;
1396 if (++yourtm.tm_mon >= MONSPERYEAR) {
1398 if (increment_overflow(&yourtm.tm_year, 1))
1402 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1404 if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1406 ** We can't set tm_sec to 0, because that might push the
1407 ** time below the minimum representable time.
1408 ** Set tm_sec to 59 instead.
1409 ** This assumes that the minimum representable time is
1410 ** not in the same minute that a leap second was deleted from,
1411 ** which is a safer assumption than using 58 would be.
1413 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1415 saved_seconds = yourtm.tm_sec;
1416 yourtm.tm_sec = SECSPERMIN - 1;
1418 saved_seconds = yourtm.tm_sec;
1422 ** Divide the search space in half
1423 ** (this works whether time_t is signed or unsigned).
1425 bits = TYPE_BIT(time_t) - 1;
1427 ** If time_t is signed, then 0 is just above the median,
1428 ** assuming two's complement arithmetic.
1429 ** If time_t is unsigned, then (1 << bits) is just above the median.
1431 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1433 (*funcp)(&t, offset, &mytm);
1434 dir = tmcomp(&mytm, &yourtm);
1439 --t; /* may be needed if new t is minimal */
1441 t -= ((time_t) 1) << bits;
1442 else t += ((time_t) 1) << bits;
1445 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1448 ** Right time, wrong type.
1449 ** Hunt for right time, right type.
1450 ** It's okay to guess wrong since the guess
1454 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1456 sp = (const struct state *)
1457 (((void *) funcp == (void *) localsub) ?
1462 #endif /* defined ALL_STATE */
1463 for (i = sp->typecnt - 1; i >= 0; --i) {
1464 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1466 for (j = sp->typecnt - 1; j >= 0; --j) {
1467 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1469 newt = t + sp->ttis[j].tt_gmtoff -
1470 sp->ttis[i].tt_gmtoff;
1471 (*funcp)(&newt, offset, &mytm);
1472 if (tmcomp(&mytm, &yourtm) != 0)
1474 if (mytm.tm_isdst != yourtm.tm_isdst)
1486 newt = t + saved_seconds;
1487 if ((newt < t) != (saved_seconds < 0))
1490 (*funcp)(&t, offset, tmp);
1496 time1(struct tm * const tmp,
1497 void (* const funcp)(const time_t *, long, struct tm *),
1501 const struct state * sp;
1505 if (tmp->tm_isdst > 1)
1507 t = time2(tmp, funcp, offset, &okay);
1510 ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1514 if (tmp->tm_isdst < 0)
1515 tmp->tm_isdst = 0; /* reset to std and try again */
1516 #endif /* defined PCTS */
1518 if (okay || tmp->tm_isdst < 0)
1520 #endif /* !defined PCTS */
1522 ** We're supposed to assume that somebody took a time of one type
1523 ** and did some math on it that yielded a "struct tm" that's bad.
1524 ** We try to divine the type they started from and adjust to the
1528 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1530 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1535 #endif /* defined ALL_STATE */
1536 for (samei = sp->typecnt - 1; samei >= 0; --samei) {
1537 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1539 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
1540 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1542 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1543 sp->ttis[samei].tt_gmtoff;
1544 tmp->tm_isdst = !tmp->tm_isdst;
1545 t = time2(tmp, funcp, offset, &okay);
1548 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1549 sp->ttis[samei].tt_gmtoff;
1550 tmp->tm_isdst = !tmp->tm_isdst;
1557 mktime(struct tm * const tmp)
1559 time_t mktime_return_value;
1560 _MUTEX_LOCK(&lcl_mutex);
1562 mktime_return_value = time1(tmp, localsub, 0L);
1563 _MUTEX_UNLOCK(&lcl_mutex);
1564 return(mktime_return_value);
1570 timelocal(struct tm * const tmp)
1572 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1577 timegm(struct tm * const tmp)
1580 return time1(tmp, gmtsub, 0L);
1584 timeoff(struct tm * const tmp, const long offset)
1587 return time1(tmp, gmtsub, offset);
1590 #endif /* defined STD_INSPIRED */
1595 ** The following is supplied for compatibility with
1596 ** previous versions of the CMUCS runtime library.
1600 gtime(struct tm * const tmp)
1602 const time_t t = mktime(tmp);
1609 #endif /* defined CMUCS */
1612 ** XXX--is the below the right way to conditionalize??
1618 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1619 ** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
1620 ** is not the case if we are accounting for leap seconds.
1621 ** So, we provide the following conversion routines for use
1622 ** when exchanging timestamps with POSIX conforming systems.
1626 leapcorr(time_t *timep)
1636 if (*timep >= lp->ls_trans)
1643 time2posix(time_t t)
1646 return t - leapcorr(&t);
1650 posix2time(time_t t)
1657 ** For a positive leap second hit, the result
1658 ** is not unique. For a negative leap second
1659 ** hit, the corresponding time doesn't exist,
1660 ** so we return an adjacent second.
1662 x = t + leapcorr(&t);
1663 y = x - leapcorr(&x);
1667 y = x - leapcorr(&x);
1674 y = x - leapcorr(&x);
1682 #endif /* defined STD_INSPIRED */