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.4 2005/01/31 22:29:44 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 "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 P((const char * codep));
126 static const char * getzname P((const char * strp));
127 static const char * getnum P((const char * strp, int * nump, int min,
129 static const char * getsecs P((const char * strp, long * secsp));
130 static const char * getoffset P((const char * strp, long * offsetp));
131 static const char * getrule P((const char * strp, struct rule * rulep));
132 static void gmtload P((struct state * sp));
133 static void gmtsub P((const time_t * timep, long offset,
135 static void localsub P((const time_t * timep, long offset,
137 static int increment_overflow P((int * number, int delta));
138 static int normalize_overflow P((int * tensptr, int * unitsptr,
140 static void settzname P((void));
141 static time_t time1 P((struct tm * tmp,
142 void(*funcp) P((const time_t *,
145 static time_t time2 P((struct tm *tmp,
146 void(*funcp) P((const time_t *,
148 long offset, int * okayp));
149 static void timesub P((const time_t * timep, long offset,
150 const struct state * sp, struct tm * tmp));
151 static int tmcomp P((const struct tm * atmp,
152 const struct tm * btmp));
153 static time_t transtime P((time_t janfirst, int year,
154 const struct rule * rulep, long offset));
155 static int tzload P((const char * name, struct state * sp));
156 static int tzparse P((const char * name, struct state * sp,
160 static struct state * lclptr;
161 static struct state * gmtptr;
162 #endif /* defined ALL_STATE */
165 static struct state lclmem;
166 static struct state gmtmem;
167 #define lclptr (&lclmem)
168 #define gmtptr (&gmtmem)
169 #endif /* State Farm */
171 #ifndef TZ_STRLEN_MAX
172 #define TZ_STRLEN_MAX 255
173 #endif /* !defined TZ_STRLEN_MAX */
175 static char lcl_TZname[TZ_STRLEN_MAX + 1];
176 static int lcl_is_set;
177 static int gmt_is_set;
178 static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
179 static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
187 ** Section 4.12.3 of X3.159-1989 requires that
188 ** Except for the strftime function, these functions [asctime,
189 ** ctime, gmtime, localtime] return values in one of two static
190 ** objects: a broken-down time structure and an array of char.
191 ** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
199 #endif /* defined USG_COMPAT */
203 #endif /* defined ALTZONE */
207 const char * const codep;
212 result = (codep[0] & 0x80) ? ~0L : 0L;
213 for (i = 0; i < 4; ++i)
214 result = (result << 8) | (codep[i] & 0xff);
221 struct state * sp = lclptr;
224 tzname[0] = wildabbr;
225 tzname[1] = wildabbr;
229 #endif /* defined USG_COMPAT */
232 #endif /* defined ALTZONE */
235 tzname[0] = tzname[1] = gmt;
238 #endif /* defined ALL_STATE */
239 for (i = 0; i < sp->typecnt; ++i) {
240 const struct ttinfo * const ttisp = &sp->ttis[i];
242 tzname[ttisp->tt_isdst] =
243 &sp->chars[ttisp->tt_abbrind];
247 if (i == 0 || !ttisp->tt_isdst)
248 timezone = -(ttisp->tt_gmtoff);
249 #endif /* defined USG_COMPAT */
251 if (i == 0 || ttisp->tt_isdst)
252 altzone = -(ttisp->tt_gmtoff);
253 #endif /* defined ALTZONE */
256 ** And to get the latest zone names into tzname. . .
258 for (i = 0; i < sp->timecnt; ++i) {
259 const struct ttinfo * const ttisp =
263 tzname[ttisp->tt_isdst] =
264 &sp->chars[ttisp->tt_abbrind];
271 struct state * const sp;
277 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
278 if (name != NULL && issetugid() != 0)
279 if ((name[0] == ':' && name[1] == '/') ||
280 name[0] == '/' || strchr(name, '.'))
282 if (name == NULL && (name = TZDEFAULT) == NULL)
288 ** Section 4.9.1 of the C standard says that
289 ** "FILENAME_MAX expands to an integral constant expression
290 ** that is the size needed for an array of char large enough
291 ** to hold the longest file name string that the implementation
292 ** guarantees can be opened."
294 char fullname[FILENAME_MAX + 1];
298 doaccess = name[0] == '/';
300 if ((p = TZDIR) == NULL)
302 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
304 (void) strcpy(fullname, p);
305 (void) strcat(fullname, "/");
306 (void) strcat(fullname, name);
308 ** Set doaccess if '.' (as in "../") shows up in name.
310 if (strchr(name, '.') != NULL)
314 if (doaccess && access(name, R_OK) != 0)
316 if ((fid = _open(name, OPEN_MODE)) == -1)
318 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
324 struct tzhead * tzhp;
325 char buf[sizeof *sp + sizeof *tzhp];
329 i = _read(fid, buf, sizeof buf);
330 if (_close(fid) != 0)
333 p += (sizeof tzhp->tzh_magic) + (sizeof tzhp->tzh_reserved);
334 ttisstdcnt = (int) detzcode(p);
336 ttisgmtcnt = (int) detzcode(p);
338 sp->leapcnt = (int) detzcode(p);
340 sp->timecnt = (int) detzcode(p);
342 sp->typecnt = (int) detzcode(p);
344 sp->charcnt = (int) detzcode(p);
346 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
347 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
348 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
349 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
350 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
351 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
353 if (i - (p - buf) < sp->timecnt * 4 + /* ats */
354 sp->timecnt + /* types */
355 sp->typecnt * (4 + 2) + /* ttinfos */
356 sp->charcnt + /* chars */
357 sp->leapcnt * (4 + 4) + /* lsinfos */
358 ttisstdcnt + /* ttisstds */
359 ttisgmtcnt) /* ttisgmts */
361 for (i = 0; i < sp->timecnt; ++i) {
362 sp->ats[i] = detzcode(p);
365 for (i = 0; i < sp->timecnt; ++i) {
366 sp->types[i] = (unsigned char) *p++;
367 if (sp->types[i] >= sp->typecnt)
370 for (i = 0; i < sp->typecnt; ++i) {
371 struct ttinfo * ttisp;
373 ttisp = &sp->ttis[i];
374 ttisp->tt_gmtoff = detzcode(p);
376 ttisp->tt_isdst = (unsigned char) *p++;
377 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
379 ttisp->tt_abbrind = (unsigned char) *p++;
380 if (ttisp->tt_abbrind < 0 ||
381 ttisp->tt_abbrind > sp->charcnt)
384 for (i = 0; i < sp->charcnt; ++i)
386 sp->chars[i] = '\0'; /* ensure '\0' at end */
387 for (i = 0; i < sp->leapcnt; ++i) {
388 struct lsinfo * lsisp;
390 lsisp = &sp->lsis[i];
391 lsisp->ls_trans = detzcode(p);
393 lsisp->ls_corr = detzcode(p);
396 for (i = 0; i < sp->typecnt; ++i) {
397 struct ttinfo * ttisp;
399 ttisp = &sp->ttis[i];
401 ttisp->tt_ttisstd = FALSE;
403 ttisp->tt_ttisstd = *p++;
404 if (ttisp->tt_ttisstd != TRUE &&
405 ttisp->tt_ttisstd != FALSE)
409 for (i = 0; i < sp->typecnt; ++i) {
410 struct ttinfo * ttisp;
412 ttisp = &sp->ttis[i];
414 ttisp->tt_ttisgmt = FALSE;
416 ttisp->tt_ttisgmt = *p++;
417 if (ttisp->tt_ttisgmt != TRUE &&
418 ttisp->tt_ttisgmt != FALSE)
426 static const int mon_lengths[2][MONSPERYEAR] = {
427 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
428 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
431 static const int year_lengths[2] = {
432 DAYSPERNYEAR, DAYSPERLYEAR
436 ** Given a pointer into a time zone string, scan until a character that is not
437 ** a valid character in a zone name is found. Return a pointer to that
447 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
454 ** Given a pointer into a time zone string, extract a number from that string.
455 ** Check that the number is within a specified range; if it is not, return
457 ** Otherwise, return a pointer to the first character not part of the number.
461 getnum(strp, nump, min, max)
470 if (strp == NULL || !is_digit(c = *strp))
474 num = num * 10 + (c - '0');
476 return NULL; /* illegal value */
478 } while (is_digit(c));
480 return NULL; /* illegal value */
486 ** Given a pointer into a time zone string, extract a number of seconds,
487 ** in hh[:mm[:ss]] form, from the string.
488 ** If any error occurs, return NULL.
489 ** Otherwise, return a pointer to the first character not part of the number
501 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
502 ** "M10.4.6/26", which does not conform to Posix,
503 ** but which specifies the equivalent of
504 ** ``02:00 on the first Sunday on or after 23 Oct''.
506 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
509 *secsp = num * (long) SECSPERHOUR;
512 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
515 *secsp += num * SECSPERMIN;
518 /* `SECSPERMIN' allows for leap seconds. */
519 strp = getnum(strp, &num, 0, SECSPERMIN);
529 ** Given a pointer into a time zone string, extract an offset, in
530 ** [+-]hh[:mm[:ss]] form, from the string.
531 ** If any error occurs, return NULL.
532 ** Otherwise, return a pointer to the first character not part of the time.
536 getoffset(strp, offsetp)
538 long * const offsetp;
545 } else if (*strp == '+')
547 strp = getsecs(strp, offsetp);
549 return NULL; /* illegal time */
551 *offsetp = -*offsetp;
556 ** Given a pointer into a time zone string, extract a rule in the form
557 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
558 ** If a valid rule is not found, return NULL.
559 ** Otherwise, return a pointer to the first character not part of the rule.
565 struct rule * const rulep;
571 rulep->r_type = JULIAN_DAY;
573 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
574 } else if (*strp == 'M') {
578 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
580 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
585 strp = getnum(strp, &rulep->r_week, 1, 5);
590 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
591 } else if (is_digit(*strp)) {
595 rulep->r_type = DAY_OF_YEAR;
596 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
597 } else return NULL; /* invalid format */
605 strp = getsecs(strp, &rulep->r_time);
606 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
611 ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
612 ** year, a rule, and the offset from GMT at the time that rule takes effect,
613 ** calculate the Epoch-relative time that rule takes effect.
617 transtime(janfirst, year, rulep, offset)
618 const time_t janfirst;
620 const struct rule * const rulep;
626 int d, m1, yy0, yy1, yy2, dow;
629 leapyear = isleap(year);
630 switch (rulep->r_type) {
634 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
636 ** In non-leap years, or if the day number is 59 or less, just
637 ** add SECSPERDAY times the day number-1 to the time of
638 ** January 1, midnight, to get the day.
640 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
641 if (leapyear && rulep->r_day >= 60)
648 ** Just add SECSPERDAY times the day number to the time of
649 ** January 1, midnight, to get the day.
651 value = janfirst + rulep->r_day * SECSPERDAY;
654 case MONTH_NTH_DAY_OF_WEEK:
656 ** Mm.n.d - nth "dth day" of month m.
659 for (i = 0; i < rulep->r_mon - 1; ++i)
660 value += mon_lengths[leapyear][i] * SECSPERDAY;
663 ** Use Zeller's Congruence to get day-of-week of first day of
666 m1 = (rulep->r_mon + 9) % 12 + 1;
667 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
670 dow = ((26 * m1 - 2) / 10 +
671 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
676 ** "dow" is the day-of-week of the first day of the month. Get
677 ** the day-of-month (zero-origin) of the first "dow" day of the
680 d = rulep->r_day - dow;
683 for (i = 1; i < rulep->r_week; ++i) {
684 if (d + DAYSPERWEEK >=
685 mon_lengths[leapyear][rulep->r_mon - 1])
691 ** "d" is the day-of-month (zero-origin) of the day we want.
693 value += d * SECSPERDAY;
698 ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
699 ** question. To get the Epoch-relative time of the specified local
700 ** time on that day, add the transition time and the current offset
703 return value + rulep->r_time + offset;
707 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
712 tzparse(name, sp, lastditch)
714 struct state * const sp;
717 const char * stdname;
718 const char * dstname;
724 unsigned char * typep;
731 stdlen = strlen(name); /* length of standard zone name */
733 if (stdlen >= sizeof sp->chars)
734 stdlen = (sizeof sp->chars) - 1;
737 name = getzname(name);
738 stdlen = name - stdname;
742 return -1; /* was "stdoffset = 0;" */
744 name = getoffset(name, &stdoffset);
749 load_result = tzload(TZDEFRULES, sp);
750 if (load_result != 0)
751 sp->leapcnt = 0; /* so, we're off a little */
754 name = getzname(name);
755 dstlen = name - dstname; /* length of DST zone name */
758 if (*name != '\0' && *name != ',' && *name != ';') {
759 name = getoffset(name, &dstoffset);
762 } else dstoffset = stdoffset - SECSPERHOUR;
763 if (*name == ',' || *name == ';') {
772 if ((name = getrule(name, &start)) == NULL)
776 if ((name = getrule(name, &end)) == NULL)
780 sp->typecnt = 2; /* standard time and DST */
782 ** Two transitions per year, from EPOCH_YEAR to 2037.
784 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
785 if (sp->timecnt > TZ_MAX_TIMES)
787 sp->ttis[0].tt_gmtoff = -dstoffset;
788 sp->ttis[0].tt_isdst = 1;
789 sp->ttis[0].tt_abbrind = stdlen + 1;
790 sp->ttis[1].tt_gmtoff = -stdoffset;
791 sp->ttis[1].tt_isdst = 0;
792 sp->ttis[1].tt_abbrind = 0;
796 for (year = EPOCH_YEAR; year <= 2037; ++year) {
797 starttime = transtime(janfirst, year, &start,
799 endtime = transtime(janfirst, year, &end,
801 if (starttime > endtime) {
803 *typep++ = 1; /* DST ends */
805 *typep++ = 0; /* DST begins */
808 *typep++ = 0; /* DST begins */
810 *typep++ = 1; /* DST ends */
812 janfirst += year_lengths[isleap(year)] *
825 if (load_result != 0)
828 ** Initial values of theirstdoffset and theirdstoffset.
831 for (i = 0; i < sp->timecnt; ++i) {
833 if (!sp->ttis[j].tt_isdst) {
835 -sp->ttis[j].tt_gmtoff;
840 for (i = 0; i < sp->timecnt; ++i) {
842 if (sp->ttis[j].tt_isdst) {
844 -sp->ttis[j].tt_gmtoff;
849 ** Initially we're assumed to be in standard time.
852 theiroffset = theirstdoffset;
854 ** Now juggle transition times and types
855 ** tracking offsets as you do.
857 for (i = 0; i < sp->timecnt; ++i) {
859 sp->types[i] = sp->ttis[j].tt_isdst;
860 if (sp->ttis[j].tt_ttisgmt) {
861 /* No adjustment to transition time */
864 ** If summer time is in effect, and the
865 ** transition time was not specified as
866 ** standard time, add the summer time
867 ** offset to the transition time;
868 ** otherwise, add the standard time
869 ** offset to the transition time.
872 ** Transitions from DST to DDST
873 ** will effectively disappear since
874 ** POSIX provides for only one DST
877 if (isdst && !sp->ttis[j].tt_ttisstd) {
878 sp->ats[i] += dstoffset -
881 sp->ats[i] += stdoffset -
885 theiroffset = -sp->ttis[j].tt_gmtoff;
886 if (sp->ttis[j].tt_isdst)
887 theirdstoffset = theiroffset;
888 else theirstdoffset = theiroffset;
891 ** Finally, fill in ttis.
892 ** ttisstd and ttisgmt need not be handled.
894 sp->ttis[0].tt_gmtoff = -stdoffset;
895 sp->ttis[0].tt_isdst = FALSE;
896 sp->ttis[0].tt_abbrind = 0;
897 sp->ttis[1].tt_gmtoff = -dstoffset;
898 sp->ttis[1].tt_isdst = TRUE;
899 sp->ttis[1].tt_abbrind = stdlen + 1;
903 sp->typecnt = 1; /* only standard time */
905 sp->ttis[0].tt_gmtoff = -stdoffset;
906 sp->ttis[0].tt_isdst = 0;
907 sp->ttis[0].tt_abbrind = 0;
909 sp->charcnt = stdlen + 1;
911 sp->charcnt += dstlen + 1;
912 if (sp->charcnt > sizeof sp->chars)
915 (void) strncpy(cp, stdname, stdlen);
919 (void) strncpy(cp, dstname, dstlen);
920 *(cp + dstlen) = '\0';
927 struct state * const sp;
929 if (tzload(gmt, sp) != 0)
930 (void) tzparse(gmt, sp, TRUE);
934 tzsetwall_basic(void)
941 if (lclptr == NULL) {
942 lclptr = (struct state *) malloc(sizeof *lclptr);
943 if (lclptr == NULL) {
944 settzname(); /* all we can do */
948 #endif /* defined ALL_STATE */
949 if (tzload((char *) NULL, lclptr) != 0)
957 _MUTEX_LOCK(&lcl_mutex);
959 _MUTEX_UNLOCK(&lcl_mutex);
973 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
975 lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
977 (void) strcpy(lcl_TZname, name);
980 if (lclptr == NULL) {
981 lclptr = (struct state *) malloc(sizeof *lclptr);
982 if (lclptr == NULL) {
983 settzname(); /* all we can do */
987 #endif /* defined ALL_STATE */
990 ** User wants it fast rather than right.
992 lclptr->leapcnt = 0; /* so, we're off a little */
994 lclptr->ttis[0].tt_gmtoff = 0;
995 lclptr->ttis[0].tt_abbrind = 0;
996 (void) strcpy(lclptr->chars, gmt);
997 } else if (tzload(name, lclptr) != 0)
998 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
999 (void) gmtload(lclptr);
1006 _MUTEX_LOCK(&lcl_mutex);
1008 _MUTEX_UNLOCK(&lcl_mutex);
1012 ** The easy way to behave "as if no library function calls" localtime
1013 ** is to not call it--so we drop its guts into "localsub", which can be
1014 ** freely called. (And no, the PANS doesn't require the above behavior--
1015 ** but it *is* desirable.)
1017 ** The unused offset argument is for the benefit of mktime variants.
1022 localsub(timep, offset, tmp)
1023 const time_t * const timep;
1025 struct tm * const tmp;
1028 const struct ttinfo * ttisp;
1030 const time_t t = *timep;
1035 gmtsub(timep, offset, tmp);
1038 #endif /* defined ALL_STATE */
1039 if (sp->timecnt == 0 || t < sp->ats[0]) {
1041 while (sp->ttis[i].tt_isdst)
1042 if (++i >= sp->typecnt) {
1047 for (i = 1; i < sp->timecnt; ++i)
1050 i = sp->types[i - 1];
1052 ttisp = &sp->ttis[i];
1054 ** To get (wrong) behavior that's compatible with System V Release 2.0
1055 ** you'd replace the statement below with
1056 ** t += ttisp->tt_gmtoff;
1057 ** timesub(&t, 0L, sp, tmp);
1059 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1060 tmp->tm_isdst = ttisp->tt_isdst;
1061 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1063 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1064 #endif /* defined TM_ZONE */
1068 localtime_r(timep, p_tm)
1069 const time_t * const timep;
1072 _MUTEX_LOCK(&lcl_mutex);
1074 localsub(timep, 0L, p_tm);
1075 _MUTEX_UNLOCK(&lcl_mutex);
1081 const time_t * const timep;
1083 static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1084 static pthread_key_t localtime_key = -1;
1087 if (__isthreaded != 0) {
1088 _pthread_mutex_lock(&localtime_mutex);
1089 if (localtime_key < 0) {
1090 if (_pthread_key_create(&localtime_key, free) < 0) {
1091 _pthread_mutex_unlock(&localtime_mutex);
1095 _pthread_mutex_unlock(&localtime_mutex);
1096 p_tm = _pthread_getspecific(localtime_key);
1098 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1101 _pthread_setspecific(localtime_key, p_tm);
1103 _pthread_mutex_lock(&lcl_mutex);
1105 localsub(timep, 0L, p_tm);
1106 _pthread_mutex_unlock(&lcl_mutex);
1110 localsub(timep, 0L, &tm);
1116 ** gmtsub is to gmtime as localsub is to localtime.
1120 gmtsub(timep, offset, tmp)
1121 const time_t * const timep;
1123 struct tm * const tmp;
1125 _MUTEX_LOCK(&gmt_mutex);
1129 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1131 #endif /* defined ALL_STATE */
1134 _MUTEX_UNLOCK(&gmt_mutex);
1135 timesub(timep, offset, gmtptr, tmp);
1138 ** Could get fancy here and deliver something such as
1139 ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1140 ** but this is no time for a treasure hunt.
1143 tmp->TM_ZONE = wildabbr;
1148 else tmp->TM_ZONE = gmtptr->chars;
1149 #endif /* defined ALL_STATE */
1151 tmp->TM_ZONE = gmtptr->chars;
1152 #endif /* State Farm */
1154 #endif /* defined TM_ZONE */
1159 const time_t * const timep;
1161 static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1162 static pthread_key_t gmtime_key = -1;
1165 if (__isthreaded != 0) {
1166 _pthread_mutex_lock(&gmtime_mutex);
1167 if (gmtime_key < 0) {
1168 if (_pthread_key_create(&gmtime_key, free) < 0) {
1169 _pthread_mutex_unlock(&gmtime_mutex);
1173 _pthread_mutex_unlock(&gmtime_mutex);
1175 * Changed to follow POSIX.1 threads standard, which
1176 * is what BSD currently has.
1178 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
1179 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1183 _pthread_setspecific(gmtime_key, p_tm);
1185 gmtsub(timep, 0L, p_tm);
1189 gmtsub(timep, 0L, &tm);
1195 gmtime_r(const time_t * timep, struct tm * tm)
1197 gmtsub(timep, 0L, tm);
1204 offtime(timep, offset)
1205 const time_t * const timep;
1208 gmtsub(timep, offset, &tm);
1212 #endif /* defined STD_INSPIRED */
1215 timesub(timep, offset, sp, tmp)
1216 const time_t * const timep;
1218 const struct state * const sp;
1219 struct tm * const tmp;
1221 const struct lsinfo * lp;
1234 i = (sp == NULL) ? 0 : sp->leapcnt;
1235 #endif /* defined ALL_STATE */
1238 #endif /* State Farm */
1241 if (*timep >= lp->ls_trans) {
1242 if (*timep == lp->ls_trans) {
1243 hit = ((i == 0 && lp->ls_corr > 0) ||
1244 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1247 sp->lsis[i].ls_trans ==
1248 sp->lsis[i - 1].ls_trans + 1 &&
1249 sp->lsis[i].ls_corr ==
1250 sp->lsis[i - 1].ls_corr + 1) {
1259 days = *timep / SECSPERDAY;
1260 rem = *timep % SECSPERDAY;
1262 if (*timep == 0x80000000) {
1264 ** A 3B1 muffs the division on the most negative number.
1269 #endif /* defined mc68k */
1270 rem += (offset - corr);
1275 while (rem >= SECSPERDAY) {
1279 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1280 rem = rem % SECSPERHOUR;
1281 tmp->tm_min = (int) (rem / SECSPERMIN);
1283 ** A positive leap second requires a special
1284 ** representation. This uses "... ??:59:60" et seq.
1286 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1287 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1288 if (tmp->tm_wday < 0)
1289 tmp->tm_wday += DAYSPERWEEK;
1291 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1292 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1295 newy = y + days / DAYSPERNYEAR;
1298 days -= (newy - y) * DAYSPERNYEAR +
1299 LEAPS_THRU_END_OF(newy - 1) -
1300 LEAPS_THRU_END_OF(y - 1);
1303 tmp->tm_year = y - TM_YEAR_BASE;
1304 tmp->tm_yday = (int) days;
1305 ip = mon_lengths[yleap];
1306 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1307 days = days - (long) ip[tmp->tm_mon];
1308 tmp->tm_mday = (int) (days + 1);
1311 tmp->TM_GMTOFF = offset;
1312 #endif /* defined TM_GMTOFF */
1317 const time_t * const timep;
1320 ** Section 4.12.3.2 of X3.159-1989 requires that
1321 ** The ctime funciton converts the calendar time pointed to by timer
1322 ** to local time in the form of a string. It is equivalent to
1323 ** asctime(localtime(timer))
1325 return asctime(localtime(timep));
1330 const time_t * const timep;
1334 return asctime_r(localtime_r(timep, &tm), buf);
1338 ** Adapted from code provided by Robert Elz, who writes:
1339 ** The "best" way to do mktime I think is based on an idea of Bob
1340 ** Kridle's (so its said...) from a long time ago.
1341 ** [kridle@xinet.com as of 1996-01-16.]
1342 ** It does a binary search of the time_t space. Since time_t's are
1343 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1344 ** would still be very reasonable).
1349 #endif /* !defined WRONG */
1352 ** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1356 increment_overflow(number, delta)
1364 return (*number < number0) != (delta < 0);
1368 normalize_overflow(tensptr, unitsptr, base)
1369 int * const tensptr;
1370 int * const unitsptr;
1375 tensdelta = (*unitsptr >= 0) ?
1376 (*unitsptr / base) :
1377 (-1 - (-1 - *unitsptr) / base);
1378 *unitsptr -= tensdelta * base;
1379 return increment_overflow(tensptr, tensdelta);
1384 const struct tm * const atmp;
1385 const struct tm * const btmp;
1389 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1390 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1391 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1392 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1393 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1394 result = atmp->tm_sec - btmp->tm_sec;
1399 time2(tmp, funcp, offset, okayp)
1400 struct tm * const tmp;
1401 void (* const funcp) P((const time_t*, long, struct tm*));
1405 const struct state * sp;
1412 struct tm yourtm, mytm;
1416 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1418 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1420 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1423 ** Turn yourtm.tm_year into an actual year number for now.
1424 ** It is converted back to an offset from TM_YEAR_BASE later.
1426 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1428 while (yourtm.tm_mday <= 0) {
1429 if (increment_overflow(&yourtm.tm_year, -1))
1431 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1432 yourtm.tm_mday += year_lengths[isleap(i)];
1434 while (yourtm.tm_mday > DAYSPERLYEAR) {
1435 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1436 yourtm.tm_mday -= year_lengths[isleap(i)];
1437 if (increment_overflow(&yourtm.tm_year, 1))
1441 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1442 if (yourtm.tm_mday <= i)
1444 yourtm.tm_mday -= i;
1445 if (++yourtm.tm_mon >= MONSPERYEAR) {
1447 if (increment_overflow(&yourtm.tm_year, 1))
1451 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1453 if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1455 ** We can't set tm_sec to 0, because that might push the
1456 ** time below the minimum representable time.
1457 ** Set tm_sec to 59 instead.
1458 ** This assumes that the minimum representable time is
1459 ** not in the same minute that a leap second was deleted from,
1460 ** which is a safer assumption than using 58 would be.
1462 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1464 saved_seconds = yourtm.tm_sec;
1465 yourtm.tm_sec = SECSPERMIN - 1;
1467 saved_seconds = yourtm.tm_sec;
1471 ** Divide the search space in half
1472 ** (this works whether time_t is signed or unsigned).
1474 bits = TYPE_BIT(time_t) - 1;
1476 ** If time_t is signed, then 0 is just above the median,
1477 ** assuming two's complement arithmetic.
1478 ** If time_t is unsigned, then (1 << bits) is just above the median.
1480 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1482 (*funcp)(&t, offset, &mytm);
1483 dir = tmcomp(&mytm, &yourtm);
1488 --t; /* may be needed if new t is minimal */
1490 t -= ((time_t) 1) << bits;
1491 else t += ((time_t) 1) << bits;
1494 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1497 ** Right time, wrong type.
1498 ** Hunt for right time, right type.
1499 ** It's okay to guess wrong since the guess
1503 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1505 sp = (const struct state *)
1506 (((void *) funcp == (void *) localsub) ?
1511 #endif /* defined ALL_STATE */
1512 for (i = sp->typecnt - 1; i >= 0; --i) {
1513 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1515 for (j = sp->typecnt - 1; j >= 0; --j) {
1516 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1518 newt = t + sp->ttis[j].tt_gmtoff -
1519 sp->ttis[i].tt_gmtoff;
1520 (*funcp)(&newt, offset, &mytm);
1521 if (tmcomp(&mytm, &yourtm) != 0)
1523 if (mytm.tm_isdst != yourtm.tm_isdst)
1535 newt = t + saved_seconds;
1536 if ((newt < t) != (saved_seconds < 0))
1539 (*funcp)(&t, offset, tmp);
1545 time1(tmp, funcp, offset)
1546 struct tm * const tmp;
1547 void (* const funcp) P((const time_t *, long, struct tm *));
1551 const struct state * sp;
1555 if (tmp->tm_isdst > 1)
1557 t = time2(tmp, funcp, offset, &okay);
1560 ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1564 if (tmp->tm_isdst < 0)
1565 tmp->tm_isdst = 0; /* reset to std and try again */
1566 #endif /* defined PCTS */
1568 if (okay || tmp->tm_isdst < 0)
1570 #endif /* !defined PCTS */
1572 ** We're supposed to assume that somebody took a time of one type
1573 ** and did some math on it that yielded a "struct tm" that's bad.
1574 ** We try to divine the type they started from and adjust to the
1578 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1580 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1585 #endif /* defined ALL_STATE */
1586 for (samei = sp->typecnt - 1; samei >= 0; --samei) {
1587 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1589 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
1590 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1592 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1593 sp->ttis[samei].tt_gmtoff;
1594 tmp->tm_isdst = !tmp->tm_isdst;
1595 t = time2(tmp, funcp, offset, &okay);
1598 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1599 sp->ttis[samei].tt_gmtoff;
1600 tmp->tm_isdst = !tmp->tm_isdst;
1608 struct tm * const tmp;
1610 time_t mktime_return_value;
1611 _MUTEX_LOCK(&lcl_mutex);
1613 mktime_return_value = time1(tmp, localsub, 0L);
1614 _MUTEX_UNLOCK(&lcl_mutex);
1615 return(mktime_return_value);
1622 struct tm * const tmp;
1624 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1630 struct tm * const tmp;
1633 return time1(tmp, gmtsub, 0L);
1637 timeoff(tmp, offset)
1638 struct tm * const tmp;
1642 return time1(tmp, gmtsub, offset);
1645 #endif /* defined STD_INSPIRED */
1650 ** The following is supplied for compatibility with
1651 ** previous versions of the CMUCS runtime library.
1656 struct tm * const tmp;
1658 const time_t t = mktime(tmp);
1665 #endif /* defined CMUCS */
1668 ** XXX--is the below the right way to conditionalize??
1674 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1675 ** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
1676 ** is not the case if we are accounting for leap seconds.
1677 ** So, we provide the following conversion routines for use
1678 ** when exchanging timestamps with POSIX conforming systems.
1693 if (*timep >= lp->ls_trans)
1704 return t - leapcorr(&t);
1716 ** For a positive leap second hit, the result
1717 ** is not unique. For a negative leap second
1718 ** hit, the corresponding time doesn't exist,
1719 ** so we return an adjacent second.
1721 x = t + leapcorr(&t);
1722 y = x - leapcorr(&x);
1726 y = x - leapcorr(&x);
1733 y = x - leapcorr(&x);
1741 #endif /* defined STD_INSPIRED */