2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov).
9 static char elsieid
[] __unused
= "@(#)localtime.c 7.78";
10 #endif /* !defined NOID */
11 #endif /* !defined lint */
12 __FBSDID("$FreeBSD: src/lib/libc/stdtime/localtime.c,v 1.40 2004/08/24 00:15:37 peter Exp $");
15 ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
16 ** POSIX-style TZ environment variable handling from Guy Harris
22 #include "namespace.h"
23 #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";
74 * In June 2004 it was decided UTC was a more appropriate default time
78 static const char gmt
[] = "UTC";
81 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
82 ** We default to US rules as of 1999-08-17.
83 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
84 ** implementation dependent; for historical reasons, US rules are a
87 #ifndef TZDEFRULESTRING
88 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
89 #endif /* !defined TZDEFDST */
91 struct ttinfo
{ /* time type information */
92 long tt_gmtoff
; /* UTC offset in seconds */
93 int tt_isdst
; /* used to set tm_isdst */
94 int tt_abbrind
; /* abbreviation list index */
95 int tt_ttisstd
; /* TRUE if transition is std time */
96 int tt_ttisgmt
; /* TRUE if transition is UTC */
99 struct lsinfo
{ /* leap second information */
100 time_t ls_trans
; /* transition time */
101 long ls_corr
; /* correction to apply */
104 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
107 #define MY_TZNAME_MAX TZNAME_MAX
108 #endif /* defined TZNAME_MAX */
110 #define MY_TZNAME_MAX 255
111 #endif /* !defined TZNAME_MAX */
118 time_t ats
[TZ_MAX_TIMES
];
119 unsigned char types
[TZ_MAX_TIMES
];
120 struct ttinfo ttis
[TZ_MAX_TYPES
];
121 char chars
[BIGGEST(BIGGEST(TZ_MAX_CHARS
+ 1, sizeof gmt
),
122 (2 * (MY_TZNAME_MAX
+ 1)))];
123 struct lsinfo lsis
[TZ_MAX_LEAPS
];
127 int r_type
; /* type of rule--see below */
128 int r_day
; /* day number of rule */
129 int r_week
; /* week number of rule */
130 int r_mon
; /* month number of rule */
131 long r_time
; /* transition time of rule */
134 #define JULIAN_DAY 0 /* Jn - Julian day */
135 #define DAY_OF_YEAR 1 /* n - day of year */
136 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
139 ** Prototypes for static functions.
142 static long detzcode(const char * codep
);
143 static const char * getzname(const char * strp
);
144 static const char * getnum(const char * strp
, int * nump
, int min
,
146 static const char * getsecs(const char * strp
, long * secsp
);
147 static const char * getoffset(const char * strp
, long * offsetp
);
148 static const char * getrule(const char * strp
, struct rule
* rulep
);
149 static void gmtload(struct state
* sp
);
150 static void gmtsub(const time_t * timep
, long offset
,
152 static void localsub(const time_t * timep
, long offset
,
154 static int increment_overflow(int * number
, int delta
);
155 static int normalize_overflow(int * tensptr
, int * unitsptr
,
157 static void settzname(void);
158 static time_t time1(struct tm
* tmp
,
159 void(*funcp
) (const time_t *,
162 static time_t time2(struct tm
*tmp
,
163 void(*funcp
) (const time_t *,
165 long offset
, int * okayp
);
166 static time_t time2sub(struct tm
*tmp
,
167 void(*funcp
) (const time_t *,
169 long offset
, int * okayp
, int do_norm_secs
);
170 static void timesub(const time_t * timep
, long offset
,
171 const struct state
* sp
, struct tm
* tmp
);
172 static int tmcomp(const struct tm
* atmp
,
173 const struct tm
* btmp
);
174 static time_t transtime(time_t janfirst
, int year
,
175 const struct rule
* rulep
, long offset
);
176 static int tzload(const char * name
, struct state
* sp
);
177 static int tzparse(const char * name
, struct state
* sp
,
181 static struct state
* lclptr
;
182 static struct state
* gmtptr
;
183 #endif /* defined ALL_STATE */
186 static struct state lclmem
;
187 static struct state gmtmem
;
188 #define lclptr (&lclmem)
189 #define gmtptr (&gmtmem)
190 #endif /* State Farm */
192 #ifndef TZ_STRLEN_MAX
193 #define TZ_STRLEN_MAX 255
194 #endif /* !defined TZ_STRLEN_MAX */
196 static char lcl_TZname
[TZ_STRLEN_MAX
+ 1];
197 static int lcl_is_set
;
198 static int gmt_is_set
;
199 static pthread_mutex_t lcl_mutex
= PTHREAD_MUTEX_INITIALIZER
;
200 static pthread_mutex_t gmt_mutex
= PTHREAD_MUTEX_INITIALIZER
;
208 ** Section 4.12.3 of X3.159-1989 requires that
209 ** Except for the strftime function, these functions [asctime,
210 ** ctime, gmtime, localtime] return values in one of two static
211 ** objects: a broken-down time structure and an array of char.
212 ** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
220 #endif /* defined USG_COMPAT */
224 #endif /* defined ALTZONE */
228 const char * const codep
;
233 result
= (codep
[0] & 0x80) ? ~0L : 0L;
234 for (i
= 0; i
< 4; ++i
)
235 result
= (result
<< 8) | (codep
[i
] & 0xff);
242 struct state
* sp
= lclptr
;
245 tzname
[0] = wildabbr
;
246 tzname
[1] = wildabbr
;
250 #endif /* defined USG_COMPAT */
253 #endif /* defined ALTZONE */
256 tzname
[0] = tzname
[1] = gmt
;
259 #endif /* defined ALL_STATE */
260 for (i
= 0; i
< sp
->typecnt
; ++i
) {
261 const struct ttinfo
* const ttisp
= &sp
->ttis
[i
];
263 tzname
[ttisp
->tt_isdst
] =
264 &sp
->chars
[ttisp
->tt_abbrind
];
268 if (i
== 0 || !ttisp
->tt_isdst
)
269 timezone
= -(ttisp
->tt_gmtoff
);
270 #endif /* defined USG_COMPAT */
272 if (i
== 0 || ttisp
->tt_isdst
)
273 altzone
= -(ttisp
->tt_gmtoff
);
274 #endif /* defined ALTZONE */
277 ** And to get the latest zone names into tzname. . .
279 for (i
= 0; i
< sp
->timecnt
; ++i
) {
280 const struct ttinfo
* const ttisp
=
284 tzname
[ttisp
->tt_isdst
] =
285 &sp
->chars
[ttisp
->tt_abbrind
];
292 struct state
* const sp
;
298 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
299 if (name
!= NULL
&& issetugid() != 0)
300 if ((name
[0] == ':' && name
[1] == '/') ||
301 name
[0] == '/' || strchr(name
, '.'))
303 if (name
== NULL
&& (name
= TZDEFAULT
) == NULL
)
309 ** Section 4.9.1 of the C standard says that
310 ** "FILENAME_MAX expands to an integral constant expression
311 ** that is the size needed for an array of char large enough
312 ** to hold the longest file name string that the implementation
313 ** guarantees can be opened."
315 char fullname
[FILENAME_MAX
+ 1];
319 doaccess
= name
[0] == '/';
321 if ((p
= TZDIR
) == NULL
)
323 if ((strlen(p
) + 1 + strlen(name
) + 1) >= sizeof fullname
)
325 (void) strcpy(fullname
, p
);
326 (void) strcat(fullname
, "/");
327 (void) strcat(fullname
, name
);
329 ** Set doaccess if '.' (as in "../") shows up in name.
331 if (strchr(name
, '.') != NULL
)
335 if (doaccess
&& access(name
, R_OK
) != 0)
337 if ((fid
= _open(name
, OPEN_MODE
)) == -1)
339 if ((_fstat(fid
, &stab
) < 0) || !S_ISREG(stab
.st_mode
)) {
345 struct tzhead
* tzhp
;
347 struct tzhead tzhead
;
348 char buf
[sizeof *sp
+ sizeof *tzhp
];
353 i
= _read(fid
, u
.buf
, sizeof u
.buf
);
354 if (_close(fid
) != 0)
356 ttisstdcnt
= (int) detzcode(u
.tzhead
.tzh_ttisstdcnt
);
357 ttisgmtcnt
= (int) detzcode(u
.tzhead
.tzh_ttisgmtcnt
);
358 sp
->leapcnt
= (int) detzcode(u
.tzhead
.tzh_leapcnt
);
359 sp
->timecnt
= (int) detzcode(u
.tzhead
.tzh_timecnt
);
360 sp
->typecnt
= (int) detzcode(u
.tzhead
.tzh_typecnt
);
361 sp
->charcnt
= (int) detzcode(u
.tzhead
.tzh_charcnt
);
362 p
= u
.tzhead
.tzh_charcnt
+ sizeof u
.tzhead
.tzh_charcnt
;
363 if (sp
->leapcnt
< 0 || sp
->leapcnt
> TZ_MAX_LEAPS
||
364 sp
->typecnt
<= 0 || sp
->typecnt
> TZ_MAX_TYPES
||
365 sp
->timecnt
< 0 || sp
->timecnt
> TZ_MAX_TIMES
||
366 sp
->charcnt
< 0 || sp
->charcnt
> TZ_MAX_CHARS
||
367 (ttisstdcnt
!= sp
->typecnt
&& ttisstdcnt
!= 0) ||
368 (ttisgmtcnt
!= sp
->typecnt
&& ttisgmtcnt
!= 0))
370 if (i
- (p
- u
.buf
) < sp
->timecnt
* 4 + /* ats */
371 sp
->timecnt
+ /* types */
372 sp
->typecnt
* (4 + 2) + /* ttinfos */
373 sp
->charcnt
+ /* chars */
374 sp
->leapcnt
* (4 + 4) + /* lsinfos */
375 ttisstdcnt
+ /* ttisstds */
376 ttisgmtcnt
) /* ttisgmts */
378 for (i
= 0; i
< sp
->timecnt
; ++i
) {
379 sp
->ats
[i
] = detzcode(p
);
382 for (i
= 0; i
< sp
->timecnt
; ++i
) {
383 sp
->types
[i
] = (unsigned char) *p
++;
384 if (sp
->types
[i
] >= sp
->typecnt
)
387 for (i
= 0; i
< sp
->typecnt
; ++i
) {
388 struct ttinfo
* ttisp
;
390 ttisp
= &sp
->ttis
[i
];
391 ttisp
->tt_gmtoff
= detzcode(p
);
393 ttisp
->tt_isdst
= (unsigned char) *p
++;
394 if (ttisp
->tt_isdst
!= 0 && ttisp
->tt_isdst
!= 1)
396 ttisp
->tt_abbrind
= (unsigned char) *p
++;
397 if (ttisp
->tt_abbrind
< 0 ||
398 ttisp
->tt_abbrind
> sp
->charcnt
)
401 for (i
= 0; i
< sp
->charcnt
; ++i
)
403 sp
->chars
[i
] = '\0'; /* ensure '\0' at end */
404 for (i
= 0; i
< sp
->leapcnt
; ++i
) {
405 struct lsinfo
* lsisp
;
407 lsisp
= &sp
->lsis
[i
];
408 lsisp
->ls_trans
= detzcode(p
);
410 lsisp
->ls_corr
= detzcode(p
);
413 for (i
= 0; i
< sp
->typecnt
; ++i
) {
414 struct ttinfo
* ttisp
;
416 ttisp
= &sp
->ttis
[i
];
418 ttisp
->tt_ttisstd
= FALSE
;
420 ttisp
->tt_ttisstd
= *p
++;
421 if (ttisp
->tt_ttisstd
!= TRUE
&&
422 ttisp
->tt_ttisstd
!= FALSE
)
426 for (i
= 0; i
< sp
->typecnt
; ++i
) {
427 struct ttinfo
* ttisp
;
429 ttisp
= &sp
->ttis
[i
];
431 ttisp
->tt_ttisgmt
= FALSE
;
433 ttisp
->tt_ttisgmt
= *p
++;
434 if (ttisp
->tt_ttisgmt
!= TRUE
&&
435 ttisp
->tt_ttisgmt
!= FALSE
)
443 static const int mon_lengths
[2][MONSPERYEAR
] = {
444 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
445 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
448 static const int year_lengths
[2] = {
449 DAYSPERNYEAR
, DAYSPERLYEAR
453 ** Given a pointer into a time zone string, scan until a character that is not
454 ** a valid character in a zone name is found. Return a pointer to that
464 while ((c
= *strp
) != '\0' && !is_digit(c
) && c
!= ',' && c
!= '-' &&
471 ** Given a pointer into a time zone string, extract a number from that string.
472 ** Check that the number is within a specified range; if it is not, return
474 ** Otherwise, return a pointer to the first character not part of the number.
478 getnum(strp
, nump
, min
, max
)
487 if (strp
== NULL
|| !is_digit(c
= *strp
))
491 num
= num
* 10 + (c
- '0');
493 return NULL
; /* illegal value */
495 } while (is_digit(c
));
497 return NULL
; /* illegal value */
503 ** Given a pointer into a time zone string, extract a number of seconds,
504 ** in hh[:mm[:ss]] form, from the string.
505 ** If any error occurs, return NULL.
506 ** Otherwise, return a pointer to the first character not part of the number
518 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
519 ** "M10.4.6/26", which does not conform to Posix,
520 ** but which specifies the equivalent of
521 ** ``02:00 on the first Sunday on or after 23 Oct''.
523 strp
= getnum(strp
, &num
, 0, HOURSPERDAY
* DAYSPERWEEK
- 1);
526 *secsp
= num
* (long) SECSPERHOUR
;
529 strp
= getnum(strp
, &num
, 0, MINSPERHOUR
- 1);
532 *secsp
+= num
* SECSPERMIN
;
535 /* `SECSPERMIN' allows for leap seconds. */
536 strp
= getnum(strp
, &num
, 0, SECSPERMIN
);
546 ** Given a pointer into a time zone string, extract an offset, in
547 ** [+-]hh[:mm[:ss]] form, from the string.
548 ** If any error occurs, return NULL.
549 ** Otherwise, return a pointer to the first character not part of the time.
553 getoffset(strp
, offsetp
)
555 long * const offsetp
;
562 } else if (*strp
== '+')
564 strp
= getsecs(strp
, offsetp
);
566 return NULL
; /* illegal time */
568 *offsetp
= -*offsetp
;
573 ** Given a pointer into a time zone string, extract a rule in the form
574 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
575 ** If a valid rule is not found, return NULL.
576 ** Otherwise, return a pointer to the first character not part of the rule.
582 struct rule
* const rulep
;
588 rulep
->r_type
= JULIAN_DAY
;
590 strp
= getnum(strp
, &rulep
->r_day
, 1, DAYSPERNYEAR
);
591 } else if (*strp
== 'M') {
595 rulep
->r_type
= MONTH_NTH_DAY_OF_WEEK
;
597 strp
= getnum(strp
, &rulep
->r_mon
, 1, MONSPERYEAR
);
602 strp
= getnum(strp
, &rulep
->r_week
, 1, 5);
607 strp
= getnum(strp
, &rulep
->r_day
, 0, DAYSPERWEEK
- 1);
608 } else if (is_digit(*strp
)) {
612 rulep
->r_type
= DAY_OF_YEAR
;
613 strp
= getnum(strp
, &rulep
->r_day
, 0, DAYSPERLYEAR
- 1);
614 } else return NULL
; /* invalid format */
622 strp
= getsecs(strp
, &rulep
->r_time
);
623 } else rulep
->r_time
= 2 * SECSPERHOUR
; /* default = 2:00:00 */
628 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
629 ** year, a rule, and the offset from UTC at the time that rule takes effect,
630 ** calculate the Epoch-relative time that rule takes effect.
634 transtime(janfirst
, year
, rulep
, offset
)
635 const time_t janfirst
;
637 const struct rule
* const rulep
;
643 int d
, m1
, yy0
, yy1
, yy2
, dow
;
646 leapyear
= isleap(year
);
647 switch (rulep
->r_type
) {
651 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
653 ** In non-leap years, or if the day number is 59 or less, just
654 ** add SECSPERDAY times the day number-1 to the time of
655 ** January 1, midnight, to get the day.
657 value
= janfirst
+ (rulep
->r_day
- 1) * SECSPERDAY
;
658 if (leapyear
&& rulep
->r_day
>= 60)
665 ** Just add SECSPERDAY times the day number to the time of
666 ** January 1, midnight, to get the day.
668 value
= janfirst
+ rulep
->r_day
* SECSPERDAY
;
671 case MONTH_NTH_DAY_OF_WEEK
:
673 ** Mm.n.d - nth "dth day" of month m.
676 for (i
= 0; i
< rulep
->r_mon
- 1; ++i
)
677 value
+= mon_lengths
[leapyear
][i
] * SECSPERDAY
;
680 ** Use Zeller's Congruence to get day-of-week of first day of
683 m1
= (rulep
->r_mon
+ 9) % 12 + 1;
684 yy0
= (rulep
->r_mon
<= 2) ? (year
- 1) : year
;
687 dow
= ((26 * m1
- 2) / 10 +
688 1 + yy2
+ yy2
/ 4 + yy1
/ 4 - 2 * yy1
) % 7;
693 ** "dow" is the day-of-week of the first day of the month. Get
694 ** the day-of-month (zero-origin) of the first "dow" day of the
697 d
= rulep
->r_day
- dow
;
700 for (i
= 1; i
< rulep
->r_week
; ++i
) {
701 if (d
+ DAYSPERWEEK
>=
702 mon_lengths
[leapyear
][rulep
->r_mon
- 1])
708 ** "d" is the day-of-month (zero-origin) of the day we want.
710 value
+= d
* SECSPERDAY
;
715 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
716 ** question. To get the Epoch-relative time of the specified local
717 ** time on that day, add the transition time and the current offset
720 return value
+ rulep
->r_time
+ offset
;
724 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
729 tzparse(name
, sp
, lastditch
)
731 struct state
* const sp
;
734 const char * stdname
;
735 const char * dstname
;
741 unsigned char * typep
;
748 stdlen
= strlen(name
); /* length of standard zone name */
750 if (stdlen
>= sizeof sp
->chars
)
751 stdlen
= (sizeof sp
->chars
) - 1;
754 name
= getzname(name
);
755 stdlen
= name
- stdname
;
759 return -1; /* was "stdoffset = 0;" */
761 name
= getoffset(name
, &stdoffset
);
766 load_result
= tzload(TZDEFRULES
, sp
);
767 if (load_result
!= 0)
768 sp
->leapcnt
= 0; /* so, we're off a little */
771 name
= getzname(name
);
772 dstlen
= name
- dstname
; /* length of DST zone name */
775 if (*name
!= '\0' && *name
!= ',' && *name
!= ';') {
776 name
= getoffset(name
, &dstoffset
);
779 } else dstoffset
= stdoffset
- SECSPERHOUR
;
780 if (*name
== '\0' && load_result
!= 0)
781 name
= TZDEFRULESTRING
;
782 if (*name
== ',' || *name
== ';') {
791 if ((name
= getrule(name
, &start
)) == NULL
)
795 if ((name
= getrule(name
, &end
)) == NULL
)
799 sp
->typecnt
= 2; /* standard time and DST */
801 ** Two transitions per year, from EPOCH_YEAR to 2037.
803 sp
->timecnt
= 2 * (2037 - EPOCH_YEAR
+ 1);
804 if (sp
->timecnt
> TZ_MAX_TIMES
)
806 sp
->ttis
[0].tt_gmtoff
= -dstoffset
;
807 sp
->ttis
[0].tt_isdst
= 1;
808 sp
->ttis
[0].tt_abbrind
= stdlen
+ 1;
809 sp
->ttis
[1].tt_gmtoff
= -stdoffset
;
810 sp
->ttis
[1].tt_isdst
= 0;
811 sp
->ttis
[1].tt_abbrind
= 0;
815 for (year
= EPOCH_YEAR
; year
<= 2037; ++year
) {
816 starttime
= transtime(janfirst
, year
, &start
,
818 endtime
= transtime(janfirst
, year
, &end
,
820 if (starttime
> endtime
) {
822 *typep
++ = 1; /* DST ends */
824 *typep
++ = 0; /* DST begins */
827 *typep
++ = 0; /* DST begins */
829 *typep
++ = 1; /* DST ends */
831 janfirst
+= year_lengths
[isleap(year
)] *
845 ** Initial values of theirstdoffset and theirdstoffset.
848 for (i
= 0; i
< sp
->timecnt
; ++i
) {
850 if (!sp
->ttis
[j
].tt_isdst
) {
852 -sp
->ttis
[j
].tt_gmtoff
;
857 for (i
= 0; i
< sp
->timecnt
; ++i
) {
859 if (sp
->ttis
[j
].tt_isdst
) {
861 -sp
->ttis
[j
].tt_gmtoff
;
866 ** Initially we're assumed to be in standard time.
869 theiroffset
= theirstdoffset
;
871 ** Now juggle transition times and types
872 ** tracking offsets as you do.
874 for (i
= 0; i
< sp
->timecnt
; ++i
) {
876 sp
->types
[i
] = sp
->ttis
[j
].tt_isdst
;
877 if (sp
->ttis
[j
].tt_ttisgmt
) {
878 /* No adjustment to transition time */
881 ** If summer time is in effect, and the
882 ** transition time was not specified as
883 ** standard time, add the summer time
884 ** offset to the transition time;
885 ** otherwise, add the standard time
886 ** offset to the transition time.
889 ** Transitions from DST to DDST
890 ** will effectively disappear since
891 ** POSIX provides for only one DST
894 if (isdst
&& !sp
->ttis
[j
].tt_ttisstd
) {
895 sp
->ats
[i
] += dstoffset
-
898 sp
->ats
[i
] += stdoffset
-
902 theiroffset
= -sp
->ttis
[j
].tt_gmtoff
;
903 if (sp
->ttis
[j
].tt_isdst
)
904 theirdstoffset
= theiroffset
;
905 else theirstdoffset
= theiroffset
;
908 ** Finally, fill in ttis.
909 ** ttisstd and ttisgmt need not be handled.
911 sp
->ttis
[0].tt_gmtoff
= -stdoffset
;
912 sp
->ttis
[0].tt_isdst
= FALSE
;
913 sp
->ttis
[0].tt_abbrind
= 0;
914 sp
->ttis
[1].tt_gmtoff
= -dstoffset
;
915 sp
->ttis
[1].tt_isdst
= TRUE
;
916 sp
->ttis
[1].tt_abbrind
= stdlen
+ 1;
921 sp
->typecnt
= 1; /* only standard time */
923 sp
->ttis
[0].tt_gmtoff
= -stdoffset
;
924 sp
->ttis
[0].tt_isdst
= 0;
925 sp
->ttis
[0].tt_abbrind
= 0;
927 sp
->charcnt
= stdlen
+ 1;
929 sp
->charcnt
+= dstlen
+ 1;
930 if ((size_t) sp
->charcnt
> sizeof sp
->chars
)
933 (void) strncpy(cp
, stdname
, stdlen
);
937 (void) strncpy(cp
, dstname
, dstlen
);
938 *(cp
+ dstlen
) = '\0';
945 struct state
* const sp
;
947 if (tzload(gmt
, sp
) != 0)
948 (void) tzparse(gmt
, sp
, TRUE
);
952 tzsetwall_basic(void)
959 if (lclptr
== NULL
) {
960 lclptr
= (struct state
*) malloc(sizeof *lclptr
);
961 if (lclptr
== NULL
) {
962 settzname(); /* all we can do */
966 #endif /* defined ALL_STATE */
967 if (tzload((char *) NULL
, lclptr
) != 0)
975 _MUTEX_LOCK(&lcl_mutex
);
977 _MUTEX_UNLOCK(&lcl_mutex
);
991 if (lcl_is_set
> 0 && strcmp(lcl_TZname
, name
) == 0)
993 lcl_is_set
= strlen(name
) < sizeof lcl_TZname
;
995 (void) strcpy(lcl_TZname
, name
);
998 if (lclptr
== NULL
) {
999 lclptr
= (struct state
*) malloc(sizeof *lclptr
);
1000 if (lclptr
== NULL
) {
1001 settzname(); /* all we can do */
1005 #endif /* defined ALL_STATE */
1006 if (*name
== '\0') {
1008 ** User wants it fast rather than right.
1010 lclptr
->leapcnt
= 0; /* so, we're off a little */
1011 lclptr
->timecnt
= 0;
1012 lclptr
->typecnt
= 0;
1013 lclptr
->ttis
[0].tt_isdst
= 0;
1014 lclptr
->ttis
[0].tt_gmtoff
= 0;
1015 lclptr
->ttis
[0].tt_abbrind
= 0;
1016 (void) strcpy(lclptr
->chars
, gmt
);
1017 } else if (tzload(name
, lclptr
) != 0)
1018 if (name
[0] == ':' || tzparse(name
, lclptr
, FALSE
) != 0)
1019 (void) gmtload(lclptr
);
1026 _MUTEX_LOCK(&lcl_mutex
);
1028 _MUTEX_UNLOCK(&lcl_mutex
);
1032 ** The easy way to behave "as if no library function calls" localtime
1033 ** is to not call it--so we drop its guts into "localsub", which can be
1034 ** freely called. (And no, the PANS doesn't require the above behavior--
1035 ** but it *is* desirable.)
1037 ** The unused offset argument is for the benefit of mktime variants.
1042 localsub(timep
, offset
, tmp
)
1043 const time_t * const timep
;
1045 struct tm
* const tmp
;
1048 const struct ttinfo
* ttisp
;
1050 const time_t t
= *timep
;
1055 gmtsub(timep
, offset
, tmp
);
1058 #endif /* defined ALL_STATE */
1059 if (sp
->timecnt
== 0 || t
< sp
->ats
[0]) {
1061 while (sp
->ttis
[i
].tt_isdst
)
1062 if (++i
>= sp
->typecnt
) {
1067 for (i
= 1; i
< sp
->timecnt
; ++i
)
1070 i
= sp
->types
[i
- 1];
1072 ttisp
= &sp
->ttis
[i
];
1074 ** To get (wrong) behavior that's compatible with System V Release 2.0
1075 ** you'd replace the statement below with
1076 ** t += ttisp->tt_gmtoff;
1077 ** timesub(&t, 0L, sp, tmp);
1079 timesub(&t
, ttisp
->tt_gmtoff
, sp
, tmp
);
1080 tmp
->tm_isdst
= ttisp
->tt_isdst
;
1081 tzname
[tmp
->tm_isdst
] = &sp
->chars
[ttisp
->tt_abbrind
];
1083 tmp
->TM_ZONE
= &sp
->chars
[ttisp
->tt_abbrind
];
1084 #endif /* defined TM_ZONE */
1089 const time_t * const timep
;
1091 static pthread_mutex_t localtime_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1092 static pthread_key_t localtime_key
= -1;
1095 if (__isthreaded
!= 0) {
1096 _pthread_mutex_lock(&localtime_mutex
);
1097 if (localtime_key
< 0) {
1098 if (_pthread_key_create(&localtime_key
, free
) < 0) {
1099 _pthread_mutex_unlock(&localtime_mutex
);
1103 _pthread_mutex_unlock(&localtime_mutex
);
1104 p_tm
= _pthread_getspecific(localtime_key
);
1106 if ((p_tm
= (struct tm
*)malloc(sizeof(struct tm
)))
1109 _pthread_setspecific(localtime_key
, p_tm
);
1111 _pthread_mutex_lock(&lcl_mutex
);
1113 localsub(timep
, 0L, p_tm
);
1114 _pthread_mutex_unlock(&lcl_mutex
);
1118 localsub(timep
, 0L, &tm
);
1124 ** Re-entrant version of localtime.
1128 localtime_r(timep
, tm
)
1129 const time_t * const timep
;
1132 _MUTEX_LOCK(&lcl_mutex
);
1134 localsub(timep
, 0L, tm
);
1135 _MUTEX_UNLOCK(&lcl_mutex
);
1140 ** gmtsub is to gmtime as localsub is to localtime.
1144 gmtsub(timep
, offset
, tmp
)
1145 const time_t * const timep
;
1147 struct tm
* const tmp
;
1149 _MUTEX_LOCK(&gmt_mutex
);
1153 gmtptr
= (struct state
*) malloc(sizeof *gmtptr
);
1155 #endif /* defined ALL_STATE */
1158 _MUTEX_UNLOCK(&gmt_mutex
);
1159 timesub(timep
, offset
, gmtptr
, tmp
);
1162 ** Could get fancy here and deliver something such as
1163 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1164 ** but this is no time for a treasure hunt.
1167 tmp
->TM_ZONE
= wildabbr
;
1172 else tmp
->TM_ZONE
= gmtptr
->chars
;
1173 #endif /* defined ALL_STATE */
1175 tmp
->TM_ZONE
= gmtptr
->chars
;
1176 #endif /* State Farm */
1178 #endif /* defined TM_ZONE */
1183 const time_t * const timep
;
1185 static pthread_mutex_t gmtime_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1186 static pthread_key_t gmtime_key
= -1;
1189 if (__isthreaded
!= 0) {
1190 _pthread_mutex_lock(&gmtime_mutex
);
1191 if (gmtime_key
< 0) {
1192 if (_pthread_key_create(&gmtime_key
, free
) < 0) {
1193 _pthread_mutex_unlock(&gmtime_mutex
);
1197 _pthread_mutex_unlock(&gmtime_mutex
);
1199 * Changed to follow POSIX.1 threads standard, which
1200 * is what BSD currently has.
1202 if ((p_tm
= _pthread_getspecific(gmtime_key
)) == NULL
) {
1203 if ((p_tm
= (struct tm
*)malloc(sizeof(struct tm
)))
1207 _pthread_setspecific(gmtime_key
, p_tm
);
1209 gmtsub(timep
, 0L, p_tm
);
1213 gmtsub(timep
, 0L, &tm
);
1219 * Re-entrant version of gmtime.
1224 const time_t * const timep
;
1227 gmtsub(timep
, 0L, tm
);
1234 offtime(timep
, offset
)
1235 const time_t * const timep
;
1238 gmtsub(timep
, offset
, &tm
);
1242 #endif /* defined STD_INSPIRED */
1245 timesub(timep
, offset
, sp
, tmp
)
1246 const time_t * const timep
;
1248 const struct state
* const sp
;
1249 struct tm
* const tmp
;
1251 const struct lsinfo
* lp
;
1264 i
= (sp
== NULL
) ? 0 : sp
->leapcnt
;
1265 #endif /* defined ALL_STATE */
1268 #endif /* State Farm */
1271 if (*timep
>= lp
->ls_trans
) {
1272 if (*timep
== lp
->ls_trans
) {
1273 hit
= ((i
== 0 && lp
->ls_corr
> 0) ||
1274 lp
->ls_corr
> sp
->lsis
[i
- 1].ls_corr
);
1277 sp
->lsis
[i
].ls_trans
==
1278 sp
->lsis
[i
- 1].ls_trans
+ 1 &&
1279 sp
->lsis
[i
].ls_corr
==
1280 sp
->lsis
[i
- 1].ls_corr
+ 1) {
1289 days
= *timep
/ SECSPERDAY
;
1290 rem
= *timep
% SECSPERDAY
;
1292 if (*timep
== 0x80000000) {
1294 ** A 3B1 muffs the division on the most negative number.
1299 #endif /* defined mc68k */
1300 rem
+= (offset
- corr
);
1305 while (rem
>= SECSPERDAY
) {
1309 tmp
->tm_hour
= (int) (rem
/ SECSPERHOUR
);
1310 rem
= rem
% SECSPERHOUR
;
1311 tmp
->tm_min
= (int) (rem
/ SECSPERMIN
);
1313 ** A positive leap second requires a special
1314 ** representation. This uses "... ??:59:60" et seq.
1316 tmp
->tm_sec
= (int) (rem
% SECSPERMIN
) + hit
;
1317 tmp
->tm_wday
= (int) ((EPOCH_WDAY
+ days
) % DAYSPERWEEK
);
1318 if (tmp
->tm_wday
< 0)
1319 tmp
->tm_wday
+= DAYSPERWEEK
;
1321 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1322 while (days
< 0 || days
>= (long) year_lengths
[yleap
= isleap(y
)]) {
1325 newy
= y
+ days
/ DAYSPERNYEAR
;
1328 days
-= (newy
- y
) * DAYSPERNYEAR
+
1329 LEAPS_THRU_END_OF(newy
- 1) -
1330 LEAPS_THRU_END_OF(y
- 1);
1333 tmp
->tm_year
= y
- TM_YEAR_BASE
;
1334 tmp
->tm_yday
= (int) days
;
1335 ip
= mon_lengths
[yleap
];
1336 for (tmp
->tm_mon
= 0; days
>= (long) ip
[tmp
->tm_mon
]; ++(tmp
->tm_mon
))
1337 days
= days
- (long) ip
[tmp
->tm_mon
];
1338 tmp
->tm_mday
= (int) (days
+ 1);
1341 tmp
->TM_GMTOFF
= offset
;
1342 #endif /* defined TM_GMTOFF */
1347 const time_t * const timep
;
1350 ** Section 4.12.3.2 of X3.159-1989 requires that
1351 ** The ctime function converts the calendar time pointed to by timer
1352 ** to local time in the form of a string. It is equivalent to
1353 ** asctime(localtime(timer))
1355 return asctime(localtime(timep
));
1360 const time_t * const timep
;
1365 return asctime_r(localtime_r(timep
, &tm
), buf
);
1369 ** Adapted from code provided by Robert Elz, who writes:
1370 ** The "best" way to do mktime I think is based on an idea of Bob
1371 ** Kridle's (so its said...) from a long time ago.
1372 ** [kridle@xinet.com as of 1996-01-16.]
1373 ** It does a binary search of the time_t space. Since time_t's are
1374 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1375 ** would still be very reasonable).
1380 #endif /* !defined WRONG */
1383 ** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1387 increment_overflow(number
, delta
)
1395 return (*number
< number0
) != (delta
< 0);
1399 normalize_overflow(tensptr
, unitsptr
, base
)
1400 int * const tensptr
;
1401 int * const unitsptr
;
1406 tensdelta
= (*unitsptr
>= 0) ?
1407 (*unitsptr
/ base
) :
1408 (-1 - (-1 - *unitsptr
) / base
);
1409 *unitsptr
-= tensdelta
* base
;
1410 return increment_overflow(tensptr
, tensdelta
);
1415 const struct tm
* const atmp
;
1416 const struct tm
* const btmp
;
1420 if ((result
= (atmp
->tm_year
- btmp
->tm_year
)) == 0 &&
1421 (result
= (atmp
->tm_mon
- btmp
->tm_mon
)) == 0 &&
1422 (result
= (atmp
->tm_mday
- btmp
->tm_mday
)) == 0 &&
1423 (result
= (atmp
->tm_hour
- btmp
->tm_hour
)) == 0 &&
1424 (result
= (atmp
->tm_min
- btmp
->tm_min
)) == 0)
1425 result
= atmp
->tm_sec
- btmp
->tm_sec
;
1430 time2sub(tmp
, funcp
, offset
, okayp
, do_norm_secs
)
1431 struct tm
* const tmp
;
1432 void (* const funcp
)(const time_t*, long, struct tm
*);
1435 const int do_norm_secs
;
1437 const struct state
* sp
;
1444 struct tm yourtm
, mytm
;
1449 if (normalize_overflow(&yourtm
.tm_min
, &yourtm
.tm_sec
,
1453 if (normalize_overflow(&yourtm
.tm_hour
, &yourtm
.tm_min
, MINSPERHOUR
))
1455 if (normalize_overflow(&yourtm
.tm_mday
, &yourtm
.tm_hour
, HOURSPERDAY
))
1457 if (normalize_overflow(&yourtm
.tm_year
, &yourtm
.tm_mon
, MONSPERYEAR
))
1460 ** Turn yourtm.tm_year into an actual year number for now.
1461 ** It is converted back to an offset from TM_YEAR_BASE later.
1463 if (increment_overflow(&yourtm
.tm_year
, TM_YEAR_BASE
))
1465 while (yourtm
.tm_mday
<= 0) {
1466 if (increment_overflow(&yourtm
.tm_year
, -1))
1468 i
= yourtm
.tm_year
+ (1 < yourtm
.tm_mon
);
1469 yourtm
.tm_mday
+= year_lengths
[isleap(i
)];
1471 while (yourtm
.tm_mday
> DAYSPERLYEAR
) {
1472 i
= yourtm
.tm_year
+ (1 < yourtm
.tm_mon
);
1473 yourtm
.tm_mday
-= year_lengths
[isleap(i
)];
1474 if (increment_overflow(&yourtm
.tm_year
, 1))
1478 i
= mon_lengths
[isleap(yourtm
.tm_year
)][yourtm
.tm_mon
];
1479 if (yourtm
.tm_mday
<= i
)
1481 yourtm
.tm_mday
-= i
;
1482 if (++yourtm
.tm_mon
>= MONSPERYEAR
) {
1484 if (increment_overflow(&yourtm
.tm_year
, 1))
1488 if (increment_overflow(&yourtm
.tm_year
, -TM_YEAR_BASE
))
1490 /* Don't go below 1900 for POLA */
1491 if (yourtm
.tm_year
< 0)
1493 if (yourtm
.tm_sec
>= 0 && yourtm
.tm_sec
< SECSPERMIN
)
1495 else if (yourtm
.tm_year
+ TM_YEAR_BASE
< EPOCH_YEAR
) {
1497 ** We can't set tm_sec to 0, because that might push the
1498 ** time below the minimum representable time.
1499 ** Set tm_sec to 59 instead.
1500 ** This assumes that the minimum representable time is
1501 ** not in the same minute that a leap second was deleted from,
1502 ** which is a safer assumption than using 58 would be.
1504 if (increment_overflow(&yourtm
.tm_sec
, 1 - SECSPERMIN
))
1506 saved_seconds
= yourtm
.tm_sec
;
1507 yourtm
.tm_sec
= SECSPERMIN
- 1;
1509 saved_seconds
= yourtm
.tm_sec
;
1513 ** Divide the search space in half
1514 ** (this works whether time_t is signed or unsigned).
1516 bits
= TYPE_BIT(time_t) - 1;
1518 ** If we have more than this, we will overflow tm_year for tmcomp().
1519 ** We should really return an error if we cannot represent it.
1524 ** If time_t is signed, then 0 is just above the median,
1525 ** assuming two's complement arithmetic.
1526 ** If time_t is unsigned, then (1 << bits) is just above the median.
1528 t
= TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits
);
1530 (*funcp
)(&t
, offset
, &mytm
);
1531 dir
= tmcomp(&mytm
, &yourtm
);
1536 --t
; /* may be needed if new t is minimal */
1538 t
-= ((time_t) 1) << bits
;
1539 else t
+= ((time_t) 1) << bits
;
1542 if (yourtm
.tm_isdst
< 0 || mytm
.tm_isdst
== yourtm
.tm_isdst
)
1545 ** Right time, wrong type.
1546 ** Hunt for right time, right type.
1547 ** It's okay to guess wrong since the guess
1550 sp
= (funcp
== localsub
) ? lclptr
: gmtptr
;
1554 #endif /* defined ALL_STATE */
1555 for (i
= sp
->typecnt
- 1; i
>= 0; --i
) {
1556 if (sp
->ttis
[i
].tt_isdst
!= yourtm
.tm_isdst
)
1558 for (j
= sp
->typecnt
- 1; j
>= 0; --j
) {
1559 if (sp
->ttis
[j
].tt_isdst
== yourtm
.tm_isdst
)
1561 newt
= t
+ sp
->ttis
[j
].tt_gmtoff
-
1562 sp
->ttis
[i
].tt_gmtoff
;
1563 (*funcp
)(&newt
, offset
, &mytm
);
1564 if (tmcomp(&mytm
, &yourtm
) != 0)
1566 if (mytm
.tm_isdst
!= yourtm
.tm_isdst
)
1578 newt
= t
+ saved_seconds
;
1579 if ((newt
< t
) != (saved_seconds
< 0))
1582 (*funcp
)(&t
, offset
, tmp
);
1588 time2(tmp
, funcp
, offset
, okayp
)
1589 struct tm
* const tmp
;
1590 void (* const funcp
)(const time_t*, long, struct tm
*);
1597 ** First try without normalization of seconds
1598 ** (in case tm_sec contains a value associated with a leap second).
1599 ** If that fails, try with normalization of seconds.
1601 t
= time2sub(tmp
, funcp
, offset
, okayp
, FALSE
);
1602 return *okayp
? t
: time2sub(tmp
, funcp
, offset
, okayp
, TRUE
);
1606 time1(tmp
, funcp
, offset
)
1607 struct tm
* const tmp
;
1608 void (* const funcp
)(const time_t *, long, struct tm
*);
1612 const struct state
* sp
;
1614 int sameind
, otherind
;
1617 int seen
[TZ_MAX_TYPES
];
1618 int types
[TZ_MAX_TYPES
];
1621 if (tmp
->tm_isdst
> 1)
1623 t
= time2(tmp
, funcp
, offset
, &okay
);
1626 ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1630 if (tmp
->tm_isdst
< 0)
1631 tmp
->tm_isdst
= 0; /* reset to std and try again */
1632 #endif /* defined PCTS */
1634 if (okay
|| tmp
->tm_isdst
< 0)
1636 #endif /* !defined PCTS */
1638 ** We're supposed to assume that somebody took a time of one type
1639 ** and did some math on it that yielded a "struct tm" that's bad.
1640 ** We try to divine the type they started from and adjust to the
1643 sp
= (funcp
== localsub
) ? lclptr
: gmtptr
;
1647 #endif /* defined ALL_STATE */
1648 for (i
= 0; i
< sp
->typecnt
; ++i
)
1651 for (i
= sp
->timecnt
- 1; i
>= 0; --i
)
1652 if (!seen
[sp
->types
[i
]]) {
1653 seen
[sp
->types
[i
]] = TRUE
;
1654 types
[nseen
++] = sp
->types
[i
];
1656 for (sameind
= 0; sameind
< nseen
; ++sameind
) {
1657 samei
= types
[sameind
];
1658 if (sp
->ttis
[samei
].tt_isdst
!= tmp
->tm_isdst
)
1660 for (otherind
= 0; otherind
< nseen
; ++otherind
) {
1661 otheri
= types
[otherind
];
1662 if (sp
->ttis
[otheri
].tt_isdst
== tmp
->tm_isdst
)
1664 tmp
->tm_sec
+= sp
->ttis
[otheri
].tt_gmtoff
-
1665 sp
->ttis
[samei
].tt_gmtoff
;
1666 tmp
->tm_isdst
= !tmp
->tm_isdst
;
1667 t
= time2(tmp
, funcp
, offset
, &okay
);
1670 tmp
->tm_sec
-= sp
->ttis
[otheri
].tt_gmtoff
-
1671 sp
->ttis
[samei
].tt_gmtoff
;
1672 tmp
->tm_isdst
= !tmp
->tm_isdst
;
1680 struct tm
* const tmp
;
1682 time_t mktime_return_value
;
1683 _MUTEX_LOCK(&lcl_mutex
);
1685 mktime_return_value
= time1(tmp
, localsub
, 0L);
1686 _MUTEX_UNLOCK(&lcl_mutex
);
1687 return(mktime_return_value
);
1694 struct tm
* const tmp
;
1696 tmp
->tm_isdst
= -1; /* in case it wasn't initialized */
1702 struct tm
* const tmp
;
1705 return time1(tmp
, gmtsub
, 0L);
1709 timeoff(tmp
, offset
)
1710 struct tm
* const tmp
;
1714 return time1(tmp
, gmtsub
, offset
);
1717 #endif /* defined STD_INSPIRED */
1722 ** The following is supplied for compatibility with
1723 ** previous versions of the CMUCS runtime library.
1728 struct tm
* const tmp
;
1730 const time_t t
= mktime(tmp
);
1737 #endif /* defined CMUCS */
1740 ** XXX--is the below the right way to conditionalize??
1746 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1747 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
1748 ** is not the case if we are accounting for leap seconds.
1749 ** So, we provide the following conversion routines for use
1750 ** when exchanging timestamps with POSIX conforming systems.
1765 if (*timep
>= lp
->ls_trans
)
1776 return t
- leapcorr(&t
);
1788 ** For a positive leap second hit, the result
1789 ** is not unique. For a negative leap second
1790 ** hit, the corresponding time doesn't exist,
1791 ** so we return an adjacent second.
1793 x
= t
+ leapcorr(&t
);
1794 y
= x
- leapcorr(&x
);
1798 y
= x
- leapcorr(&x
);
1805 y
= x
- leapcorr(&x
);
1813 #endif /* defined STD_INSPIRED */