2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
6 ** $FreeBSD: src/lib/libc/stdtime/localtime.c,v 1.25.2.2 2002/08/13 16:08:07 bmilekic Exp $
7 ** $DragonFly: src/lib/libc/stdtime/localtime.c,v 1.7 2008/10/19 20:15:58 swildner Exp $
11 ** Leap second handling from Bradley White.
12 ** POSIX-style TZ environment variable handling from Guy Harris.
17 #include "namespace.h"
18 #include <sys/types.h>
22 #include <float.h> /* for FLT_MAX and DBL_MAX */
26 #include <un-namespace.h>
30 #include "libc_private.h"
32 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
33 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
35 #ifndef TZ_ABBR_MAX_LEN
36 #define TZ_ABBR_MAX_LEN 16
37 #endif /* !defined TZ_ABBR_MAX_LEN */
39 #ifndef TZ_ABBR_CHAR_SET
40 #define TZ_ABBR_CHAR_SET \
41 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
42 #endif /* !defined TZ_ABBR_CHAR_SET */
44 #ifndef TZ_ABBR_ERR_CHAR
45 #define TZ_ABBR_ERR_CHAR '_'
46 #endif /* !defined TZ_ABBR_ERR_CHAR */
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).
69 static char wildabbr
[] = WILDABBR
;
71 static const char gmt
[] = "UTC";
74 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
75 ** We default to US rules as of 1999-08-17.
76 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
77 ** implementation dependent; for historical reasons, US rules are a
80 #ifndef TZDEFRULESTRING
81 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
82 #endif /* !defined TZDEFDST */
84 struct ttinfo
{ /* time type information */
85 long tt_gmtoff
; /* UTC offset in seconds */
86 int tt_isdst
; /* used to set tm_isdst */
87 int tt_abbrind
; /* abbreviation list index */
88 int tt_ttisstd
; /* TRUE if transition is std time */
89 int tt_ttisgmt
; /* TRUE if transition is UTC */
92 struct lsinfo
{ /* leap second information */
93 time_t ls_trans
; /* transition time */
94 long ls_corr
; /* correction to apply */
97 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
100 #define MY_TZNAME_MAX TZNAME_MAX
101 #endif /* defined TZNAME_MAX */
103 #define MY_TZNAME_MAX 255
104 #endif /* !defined TZNAME_MAX */
113 time_t ats
[TZ_MAX_TIMES
];
114 unsigned char types
[TZ_MAX_TIMES
];
115 struct ttinfo ttis
[TZ_MAX_TYPES
];
116 char chars
[BIGGEST(BIGGEST(TZ_MAX_CHARS
+ 1, sizeof gmt
),
117 (2 * (MY_TZNAME_MAX
+ 1)))];
118 struct lsinfo lsis
[TZ_MAX_LEAPS
];
122 int r_type
; /* type of rule--see below */
123 int r_day
; /* day number of rule */
124 int r_week
; /* week number of rule */
125 int r_mon
; /* month number of rule */
126 long r_time
; /* transition time of rule */
129 #define JULIAN_DAY 0 /* Jn - Julian day */
130 #define DAY_OF_YEAR 1 /* n - day of year */
131 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
134 ** Prototypes for static functions.
137 static long detzcode(const char * codep
);
138 static time_t detzcode64(const char * codep
);
139 static int differ_by_repeat(time_t t1
, time_t t0
);
140 static const char * getzname(const char * strp
);
141 static const char * getqzname(const char * strp
, const int delim
);
142 static const char * getnum(const char * strp
, int * nump
, int min
,
144 static const char * getsecs(const char * strp
, long * secsp
);
145 static const char * getoffset(const char * strp
, long * offsetp
);
146 static const char * getrule(const char * strp
, struct rule
* rulep
);
147 static void gmtload(struct state
* sp
);
148 static struct tm
* gmtsub(const time_t * timep
, long offset
,
150 static struct tm
* localsub(const time_t * timep
, long offset
,
152 static int increment_overflow(int * number
, int delta
);
153 static int leaps_thru_end_of(int y
);
154 static int long_increment_overflow(long * number
, int delta
);
155 static int long_normalize_overflow(long * tensptr
,
156 int * unitsptr
, int base
);
157 static int normalize_overflow(int * tensptr
, int * unitsptr
,
159 static void settzname(void);
160 static time_t time1(struct tm
* tmp
,
161 struct tm
* (*funcp
)(const time_t *,
164 static time_t time2(struct tm
*tmp
,
165 struct tm
* (*funcp
)(const time_t *,
167 long offset
, int * okayp
);
168 static time_t time2sub(struct tm
*tmp
,
169 struct tm
* (*funcp
)(const time_t *,
171 long offset
, int * okayp
, int do_norm_secs
);
172 static struct tm
* timesub(const time_t * timep
, long offset
,
173 const struct state
* sp
, struct tm
* tmp
);
174 static int tmcomp(const struct tm
* atmp
,
175 const struct tm
* btmp
);
176 static time_t transtime(time_t janfirst
, int year
,
177 const struct rule
* rulep
, long offset
);
178 static int typesequiv(const struct state
* sp
, int a
, int b
);
179 static int tzload(const char * name
, struct state
* sp
,
181 static int tzparse(const char * name
, struct state
* sp
,
184 static struct state lclmem
;
185 static struct state gmtmem
;
186 #define lclptr (&lclmem)
187 #define gmtptr (&gmtmem)
189 #ifndef TZ_STRLEN_MAX
190 #define TZ_STRLEN_MAX 255
191 #endif /* !defined TZ_STRLEN_MAX */
193 static char lcl_TZname
[TZ_STRLEN_MAX
+ 1];
194 static int lcl_is_set
;
195 static int gmt_is_set
;
196 static pthread_mutex_t lcl_mutex
= PTHREAD_MUTEX_INITIALIZER
;
197 static pthread_mutex_t gmt_mutex
= PTHREAD_MUTEX_INITIALIZER
;
205 ** Section 4.12.3 of X3.159-1989 requires that
206 ** Except for the strftime function, these functions [asctime,
207 ** ctime, gmtime, localtime] return values in one of two static
208 ** objects: a broken-down time structure and an array of char.
209 ** Thanks to Paul Eggert for noting this.
215 detzcode(const char * const codep
)
220 result
= (codep
[0] & 0x80) ? ~0L : 0;
221 for (i
= 0; i
< 4; ++i
)
222 result
= (result
<< 8) | (codep
[i
] & 0xff);
227 detzcode64(const char * const codep
)
232 result
= (codep
[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
233 for (i
= 0; i
< 8; ++i
)
234 result
= result
* 256 + (codep
[i
] & 0xff);
241 struct state
* const sp
= lclptr
;
244 tzname
[0] = wildabbr
;
245 tzname
[1] = wildabbr
;
247 for (i
= 0; i
< sp
->typecnt
; ++i
) {
248 const struct ttinfo
* const ttisp
= &sp
->ttis
[i
];
250 tzname
[ttisp
->tt_isdst
] =
251 &sp
->chars
[ttisp
->tt_abbrind
];
254 ** And to get the latest zone names into tzname. . .
256 for (i
= 0; i
< sp
->timecnt
; ++i
) {
257 const struct ttinfo
* const ttisp
=
261 tzname
[ttisp
->tt_isdst
] =
262 &sp
->chars
[ttisp
->tt_abbrind
];
265 ** Finally, scrub the abbreviations.
266 ** First, replace bogus characters.
268 for (i
= 0; i
< sp
->charcnt
; ++i
)
269 if (strchr(TZ_ABBR_CHAR_SET
, sp
->chars
[i
]) == NULL
)
270 sp
->chars
[i
] = TZ_ABBR_ERR_CHAR
;
272 ** Second, truncate long abbreviations.
274 for (i
= 0; i
< sp
->typecnt
; ++i
) {
275 const struct ttinfo
* const ttisp
= &sp
->ttis
[i
];
276 char * cp
= &sp
->chars
[ttisp
->tt_abbrind
];
278 if (strlen(cp
) > TZ_ABBR_MAX_LEN
&&
279 strcmp(cp
, GRANDPARENTED
) != 0)
280 *(cp
+ TZ_ABBR_MAX_LEN
) = '\0';
285 differ_by_repeat(const time_t t1
, const time_t t0
)
287 if (TYPE_INTEGRAL(time_t) &&
288 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS
)
290 return t1
- t0
== SECSPERREPEAT
;
294 tzload(const char *name
, struct state
* const sp
, const int doextend
)
302 struct tzhead tzhead
;
303 char buf
[2 * sizeof(struct tzhead
) +
308 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
309 if (name
!= NULL
&& issetugid() != 0)
310 if ((name
[0] == ':' && name
[1] == '/') ||
311 name
[0] == '/' || strchr(name
, '.'))
313 if (name
== NULL
&& (name
= TZDEFAULT
) == NULL
)
319 ** Section 4.9.1 of the C standard says that
320 ** "FILENAME_MAX expands to an integral constant expression
321 ** that is the size needed for an array of char large enough
322 ** to hold the longest file name string that the implementation
323 ** guarantees can be opened."
325 char fullname
[FILENAME_MAX
+ 1];
329 doaccess
= name
[0] == '/';
331 if ((p
= TZDIR
) == NULL
)
333 if ((strlen(p
) + 1 + strlen(name
) + 1) >= sizeof fullname
)
336 strcat(fullname
, "/");
337 strcat(fullname
, name
);
339 ** Set doaccess if '.' (as in "../") shows up in name.
341 if (strchr(name
, '.') != NULL
)
345 if (doaccess
&& access(name
, R_OK
) != 0)
347 if ((fid
= _open(name
, O_RDONLY
)) == -1)
349 if ((_fstat(fid
, &stab
) < 0) || !S_ISREG(stab
.st_mode
)) {
354 nread
= read(fid
, u
.buf
, sizeof u
.buf
);
355 if (close(fid
) < 0 || nread
<= 0)
357 for (stored
= 4; stored
<= 8; stored
*= 2) {
361 ttisstdcnt
= (int) detzcode(u
.tzhead
.tzh_ttisstdcnt
);
362 ttisgmtcnt
= (int) detzcode(u
.tzhead
.tzh_ttisgmtcnt
);
363 sp
->leapcnt
= (int) detzcode(u
.tzhead
.tzh_leapcnt
);
364 sp
->timecnt
= (int) detzcode(u
.tzhead
.tzh_timecnt
);
365 sp
->typecnt
= (int) detzcode(u
.tzhead
.tzh_typecnt
);
366 sp
->charcnt
= (int) detzcode(u
.tzhead
.tzh_charcnt
);
367 p
= u
.tzhead
.tzh_charcnt
+ sizeof u
.tzhead
.tzh_charcnt
;
368 if (sp
->leapcnt
< 0 || sp
->leapcnt
> TZ_MAX_LEAPS
||
369 sp
->typecnt
<= 0 || sp
->typecnt
> TZ_MAX_TYPES
||
370 sp
->timecnt
< 0 || sp
->timecnt
> TZ_MAX_TIMES
||
371 sp
->charcnt
< 0 || sp
->charcnt
> TZ_MAX_CHARS
||
372 (ttisstdcnt
!= sp
->typecnt
&& ttisstdcnt
!= 0) ||
373 (ttisgmtcnt
!= sp
->typecnt
&& ttisgmtcnt
!= 0))
375 if (nread
- (p
- u
.buf
) <
376 sp
->timecnt
* stored
+ /* ats */
377 sp
->timecnt
+ /* types */
378 sp
->typecnt
* 6 + /* ttinfos */
379 sp
->charcnt
+ /* chars */
380 sp
->leapcnt
* (stored
+ 4) + /* lsinfos */
381 ttisstdcnt
+ /* ttisstds */
382 ttisgmtcnt
) /* ttisgmts */
384 for (i
= 0; i
< sp
->timecnt
; ++i
) {
385 sp
->ats
[i
] = (stored
== 4) ?
386 detzcode(p
) : detzcode64(p
);
389 for (i
= 0; i
< sp
->timecnt
; ++i
) {
390 sp
->types
[i
] = (unsigned char) *p
++;
391 if (sp
->types
[i
] >= sp
->typecnt
)
394 for (i
= 0; i
< sp
->typecnt
; ++i
) {
395 struct ttinfo
* ttisp
;
397 ttisp
= &sp
->ttis
[i
];
398 ttisp
->tt_gmtoff
= detzcode(p
);
400 ttisp
->tt_isdst
= (unsigned char) *p
++;
401 if (ttisp
->tt_isdst
!= 0 && ttisp
->tt_isdst
!= 1)
403 ttisp
->tt_abbrind
= (unsigned char) *p
++;
404 if (ttisp
->tt_abbrind
< 0 ||
405 ttisp
->tt_abbrind
> sp
->charcnt
)
408 for (i
= 0; i
< sp
->charcnt
; ++i
)
410 sp
->chars
[i
] = '\0'; /* ensure '\0' at end */
411 for (i
= 0; i
< sp
->leapcnt
; ++i
) {
412 struct lsinfo
* lsisp
;
414 lsisp
= &sp
->lsis
[i
];
415 lsisp
->ls_trans
= (stored
== 4) ?
416 detzcode(p
) : detzcode64(p
);
418 lsisp
->ls_corr
= detzcode(p
);
421 for (i
= 0; i
< sp
->typecnt
; ++i
) {
422 struct ttinfo
* ttisp
;
424 ttisp
= &sp
->ttis
[i
];
426 ttisp
->tt_ttisstd
= FALSE
;
428 ttisp
->tt_ttisstd
= *p
++;
429 if (ttisp
->tt_ttisstd
!= TRUE
&&
430 ttisp
->tt_ttisstd
!= FALSE
)
434 for (i
= 0; i
< sp
->typecnt
; ++i
) {
435 struct ttinfo
* ttisp
;
437 ttisp
= &sp
->ttis
[i
];
439 ttisp
->tt_ttisgmt
= FALSE
;
441 ttisp
->tt_ttisgmt
= *p
++;
442 if (ttisp
->tt_ttisgmt
!= TRUE
&&
443 ttisp
->tt_ttisgmt
!= FALSE
)
448 ** Out-of-sort ats should mean we're running on a
449 ** signed time_t system but using a data file with
450 ** unsigned values (or vice versa).
452 for (i
= 0; i
< sp
->timecnt
- 2; ++i
)
453 if (sp
->ats
[i
] > sp
->ats
[i
+ 1]) {
455 if (TYPE_SIGNED(time_t)) {
457 ** Ignore the end (easy).
462 ** Ignore the beginning (harder).
466 for (j
= 0; j
+ i
< sp
->timecnt
; ++j
) {
467 sp
->ats
[j
] = sp
->ats
[j
+ i
];
468 sp
->types
[j
] = sp
->types
[j
+ i
];
475 ** If this is an old file, we're done.
477 if (u
.tzhead
.tzh_version
[0] == '\0')
480 for (i
= 0; i
< nread
; ++i
)
483 ** If this is a narrow integer time_t system, we're done.
485 if (stored
>= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
488 if (doextend
&& nread
> 2 &&
489 u
.buf
[0] == '\n' && u
.buf
[nread
- 1] == '\n' &&
490 sp
->typecnt
+ 2 <= TZ_MAX_TYPES
) {
494 u
.buf
[nread
- 1] = '\0';
495 result
= tzparse(&u
.buf
[1], &ts
, FALSE
);
496 if (result
== 0 && ts
.typecnt
== 2 &&
497 sp
->charcnt
+ ts
.charcnt
<= TZ_MAX_CHARS
) {
498 for (i
= 0; i
< 2; ++i
)
499 ts
.ttis
[i
].tt_abbrind
+=
501 for (i
= 0; i
< ts
.charcnt
; ++i
)
502 sp
->chars
[sp
->charcnt
++] =
505 while (i
< ts
.timecnt
&&
507 sp
->ats
[sp
->timecnt
- 1])
509 while (i
< ts
.timecnt
&&
510 sp
->timecnt
< TZ_MAX_TIMES
) {
511 sp
->ats
[sp
->timecnt
] =
513 sp
->types
[sp
->timecnt
] =
519 sp
->ttis
[sp
->typecnt
++] = ts
.ttis
[0];
520 sp
->ttis
[sp
->typecnt
++] = ts
.ttis
[1];
523 sp
->goback
= sp
->goahead
= FALSE
;
524 if (sp
->timecnt
> 1) {
525 for (i
= 1; i
< sp
->timecnt
; ++i
)
526 if (typesequiv(sp
, sp
->types
[i
], sp
->types
[0]) &&
527 differ_by_repeat(sp
->ats
[i
], sp
->ats
[0])) {
531 for (i
= sp
->timecnt
- 2; i
>= 0; --i
)
532 if (typesequiv(sp
, sp
->types
[sp
->timecnt
- 1],
534 differ_by_repeat(sp
->ats
[sp
->timecnt
- 1],
544 typesequiv(const struct state
* const sp
, const int a
, const int b
)
549 a
< 0 || a
>= sp
->typecnt
||
550 b
< 0 || b
>= sp
->typecnt
)
553 const struct ttinfo
* ap
= &sp
->ttis
[a
];
554 const struct ttinfo
* bp
= &sp
->ttis
[b
];
555 result
= ap
->tt_gmtoff
== bp
->tt_gmtoff
&&
556 ap
->tt_isdst
== bp
->tt_isdst
&&
557 ap
->tt_ttisstd
== bp
->tt_ttisstd
&&
558 ap
->tt_ttisgmt
== bp
->tt_ttisgmt
&&
559 strcmp(&sp
->chars
[ap
->tt_abbrind
],
560 &sp
->chars
[bp
->tt_abbrind
]) == 0;
565 static const int mon_lengths
[2][MONSPERYEAR
] = {
566 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
567 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
570 static const int year_lengths
[2] = {
571 DAYSPERNYEAR
, DAYSPERLYEAR
575 ** Given a pointer into a time zone string, scan until a character that is not
576 ** a valid character in a zone name is found. Return a pointer to that
581 getzname(const char *strp
)
585 while ((c
= *strp
) != '\0' && !is_digit(c
) && c
!= ',' && c
!= '-' &&
592 ** Given a pointer into an extended time zone string, scan until the ending
593 ** delimiter of the zone name is located. Return a pointer to the delimiter.
595 ** As with getzname above, the legal character set is actually quite
596 ** restricted, with other characters producing undefined results.
597 ** We don't do any checking here; checking is done later in common-case code.
601 getqzname(const char *strp
, const int delim
)
605 while ((c
= *strp
) != '\0' && c
!= delim
)
611 ** Given a pointer into a time zone string, extract a number from that string.
612 ** Check that the number is within a specified range; if it is not, return
614 ** Otherwise, return a pointer to the first character not part of the number.
618 getnum(const char *strp
, int * const nump
, const int min
, const int max
)
623 if (strp
== NULL
|| !is_digit(c
= *strp
))
627 num
= num
* 10 + (c
- '0');
629 return NULL
; /* illegal value */
631 } while (is_digit(c
));
633 return NULL
; /* illegal value */
639 ** Given a pointer into a time zone string, extract a number of seconds,
640 ** in hh[:mm[:ss]] form, from the string.
641 ** If any error occurs, return NULL.
642 ** Otherwise, return a pointer to the first character not part of the number
647 getsecs(const char *strp
, long * const secsp
)
652 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
653 ** "M10.4.6/26", which does not conform to Posix,
654 ** but which specifies the equivalent of
655 ** ``02:00 on the first Sunday on or after 23 Oct''.
657 strp
= getnum(strp
, &num
, 0, HOURSPERDAY
* DAYSPERWEEK
- 1);
660 *secsp
= num
* (long) SECSPERHOUR
;
663 strp
= getnum(strp
, &num
, 0, MINSPERHOUR
- 1);
666 *secsp
+= num
* SECSPERMIN
;
669 /* `SECSPERMIN' allows for leap seconds. */
670 strp
= getnum(strp
, &num
, 0, SECSPERMIN
);
680 ** Given a pointer into a time zone string, extract an offset, in
681 ** [+-]hh[:mm[:ss]] form, from the string.
682 ** If any error occurs, return NULL.
683 ** Otherwise, return a pointer to the first character not part of the time.
687 getoffset(const char *strp
, long * const offsetp
)
694 } else if (*strp
== '+')
696 strp
= getsecs(strp
, offsetp
);
698 return NULL
; /* illegal time */
700 *offsetp
= -*offsetp
;
705 ** Given a pointer into a time zone string, extract a rule in the form
706 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
707 ** If a valid rule is not found, return NULL.
708 ** Otherwise, return a pointer to the first character not part of the rule.
712 getrule(const char *strp
, struct rule
* const rulep
)
718 rulep
->r_type
= JULIAN_DAY
;
720 strp
= getnum(strp
, &rulep
->r_day
, 1, DAYSPERNYEAR
);
721 } else if (*strp
== 'M') {
725 rulep
->r_type
= MONTH_NTH_DAY_OF_WEEK
;
727 strp
= getnum(strp
, &rulep
->r_mon
, 1, MONSPERYEAR
);
732 strp
= getnum(strp
, &rulep
->r_week
, 1, 5);
737 strp
= getnum(strp
, &rulep
->r_day
, 0, DAYSPERWEEK
- 1);
738 } else if (is_digit(*strp
)) {
742 rulep
->r_type
= DAY_OF_YEAR
;
743 strp
= getnum(strp
, &rulep
->r_day
, 0, DAYSPERLYEAR
- 1);
744 } else return NULL
; /* invalid format */
752 strp
= getsecs(strp
, &rulep
->r_time
);
753 } else rulep
->r_time
= 2 * SECSPERHOUR
; /* default = 2:00:00 */
758 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
759 ** year, a rule, and the offset from UTC at the time that rule takes effect,
760 ** calculate the Epoch-relative time that rule takes effect.
764 transtime(const time_t janfirst
, const int year
,
765 const struct rule
* const rulep
, const long offset
)
770 int d
, m1
, yy0
, yy1
, yy2
, dow
;
773 leapyear
= isleap(year
);
774 switch (rulep
->r_type
) {
778 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
780 ** In non-leap years, or if the day number is 59 or less, just
781 ** add SECSPERDAY times the day number-1 to the time of
782 ** January 1, midnight, to get the day.
784 value
= janfirst
+ (rulep
->r_day
- 1) * SECSPERDAY
;
785 if (leapyear
&& rulep
->r_day
>= 60)
792 ** Just add SECSPERDAY times the day number to the time of
793 ** January 1, midnight, to get the day.
795 value
= janfirst
+ rulep
->r_day
* SECSPERDAY
;
798 case MONTH_NTH_DAY_OF_WEEK
:
800 ** Mm.n.d - nth "dth day" of month m.
803 for (i
= 0; i
< rulep
->r_mon
- 1; ++i
)
804 value
+= mon_lengths
[leapyear
][i
] * SECSPERDAY
;
807 ** Use Zeller's Congruence to get day-of-week of first day of
810 m1
= (rulep
->r_mon
+ 9) % 12 + 1;
811 yy0
= (rulep
->r_mon
<= 2) ? (year
- 1) : year
;
814 dow
= ((26 * m1
- 2) / 10 +
815 1 + yy2
+ yy2
/ 4 + yy1
/ 4 - 2 * yy1
) % 7;
820 ** "dow" is the day-of-week of the first day of the month. Get
821 ** the day-of-month (zero-origin) of the first "dow" day of the
824 d
= rulep
->r_day
- dow
;
827 for (i
= 1; i
< rulep
->r_week
; ++i
) {
828 if (d
+ DAYSPERWEEK
>=
829 mon_lengths
[leapyear
][rulep
->r_mon
- 1])
835 ** "d" is the day-of-month (zero-origin) of the day we want.
837 value
+= d
* SECSPERDAY
;
842 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
843 ** question. To get the Epoch-relative time of the specified local
844 ** time on that day, add the transition time and the current offset
847 return value
+ rulep
->r_time
+ offset
;
851 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
856 tzparse(const char *name
, struct state
* const sp
, const int lastditch
)
858 const char * stdname
;
859 const char * dstname
;
865 unsigned char * typep
;
872 stdlen
= strlen(name
); /* length of standard zone name */
874 if (stdlen
>= sizeof sp
->chars
)
875 stdlen
= (sizeof sp
->chars
) - 1;
881 name
= getqzname(name
, '>');
884 stdlen
= name
- stdname
;
887 name
= getzname(name
);
888 stdlen
= name
- stdname
;
892 name
= getoffset(name
, &stdoffset
);
896 load_result
= tzload(TZDEFRULES
, sp
, FALSE
);
897 if (load_result
!= 0)
898 sp
->leapcnt
= 0; /* so, we're off a little */
902 name
= getqzname(name
, '>');
905 dstlen
= name
- dstname
;
909 name
= getzname(name
);
910 dstlen
= name
- dstname
; /* length of DST zone name */
912 if (*name
!= '\0' && *name
!= ',' && *name
!= ';') {
913 name
= getoffset(name
, &dstoffset
);
916 } else dstoffset
= stdoffset
- SECSPERHOUR
;
917 if (*name
== '\0' && load_result
!= 0)
918 name
= TZDEFRULESTRING
;
919 if (*name
== ',' || *name
== ';') {
928 if ((name
= getrule(name
, &start
)) == NULL
)
932 if ((name
= getrule(name
, &end
)) == NULL
)
936 sp
->typecnt
= 2; /* standard time and DST */
938 ** Two transitions per year, from EPOCH_YEAR forward.
940 sp
->ttis
[0].tt_gmtoff
= -dstoffset
;
941 sp
->ttis
[0].tt_isdst
= 1;
942 sp
->ttis
[0].tt_abbrind
= stdlen
+ 1;
943 sp
->ttis
[1].tt_gmtoff
= -stdoffset
;
944 sp
->ttis
[1].tt_isdst
= 0;
945 sp
->ttis
[1].tt_abbrind
= 0;
950 for (year
= EPOCH_YEAR
;
951 sp
->timecnt
+ 2 <= TZ_MAX_TIMES
;
955 starttime
= transtime(janfirst
, year
, &start
,
957 endtime
= transtime(janfirst
, year
, &end
,
959 if (starttime
> endtime
) {
961 *typep
++ = 1; /* DST ends */
963 *typep
++ = 0; /* DST begins */
966 *typep
++ = 0; /* DST begins */
968 *typep
++ = 1; /* DST ends */
972 newfirst
+= year_lengths
[isleap(year
)] *
974 if (newfirst
<= janfirst
)
989 ** Initial values of theirstdoffset and theirdstoffset.
992 for (i
= 0; i
< sp
->timecnt
; ++i
) {
994 if (!sp
->ttis
[j
].tt_isdst
) {
996 -sp
->ttis
[j
].tt_gmtoff
;
1001 for (i
= 0; i
< sp
->timecnt
; ++i
) {
1003 if (sp
->ttis
[j
].tt_isdst
) {
1005 -sp
->ttis
[j
].tt_gmtoff
;
1010 ** Initially we're assumed to be in standard time.
1013 theiroffset
= theirstdoffset
;
1015 ** Now juggle transition times and types
1016 ** tracking offsets as you do.
1018 for (i
= 0; i
< sp
->timecnt
; ++i
) {
1020 sp
->types
[i
] = sp
->ttis
[j
].tt_isdst
;
1021 if (sp
->ttis
[j
].tt_ttisgmt
) {
1022 /* No adjustment to transition time */
1025 ** If summer time is in effect, and the
1026 ** transition time was not specified as
1027 ** standard time, add the summer time
1028 ** offset to the transition time;
1029 ** otherwise, add the standard time
1030 ** offset to the transition time.
1033 ** Transitions from DST to DDST
1034 ** will effectively disappear since
1035 ** POSIX provides for only one DST
1038 if (isdst
&& !sp
->ttis
[j
].tt_ttisstd
) {
1039 sp
->ats
[i
] += dstoffset
-
1042 sp
->ats
[i
] += stdoffset
-
1046 theiroffset
= -sp
->ttis
[j
].tt_gmtoff
;
1047 if (sp
->ttis
[j
].tt_isdst
)
1048 theirdstoffset
= theiroffset
;
1049 else theirstdoffset
= theiroffset
;
1052 ** Finally, fill in ttis.
1053 ** ttisstd and ttisgmt need not be handled.
1055 sp
->ttis
[0].tt_gmtoff
= -stdoffset
;
1056 sp
->ttis
[0].tt_isdst
= FALSE
;
1057 sp
->ttis
[0].tt_abbrind
= 0;
1058 sp
->ttis
[1].tt_gmtoff
= -dstoffset
;
1059 sp
->ttis
[1].tt_isdst
= TRUE
;
1060 sp
->ttis
[1].tt_abbrind
= stdlen
+ 1;
1065 sp
->typecnt
= 1; /* only standard time */
1067 sp
->ttis
[0].tt_gmtoff
= -stdoffset
;
1068 sp
->ttis
[0].tt_isdst
= 0;
1069 sp
->ttis
[0].tt_abbrind
= 0;
1071 sp
->charcnt
= stdlen
+ 1;
1073 sp
->charcnt
+= dstlen
+ 1;
1074 if ((size_t) sp
->charcnt
> sizeof sp
->chars
)
1077 strncpy(cp
, stdname
, stdlen
);
1081 strncpy(cp
, dstname
, dstlen
);
1082 *(cp
+ dstlen
) = '\0';
1088 gmtload(struct state
* const sp
)
1090 if (tzload(gmt
, sp
, TRUE
) != 0)
1091 tzparse(gmt
, sp
, TRUE
);
1095 tzsetwall_basic(void)
1101 if (tzload((char *) NULL
, lclptr
, TRUE
) != 0)
1109 _MUTEX_LOCK(&lcl_mutex
);
1111 _MUTEX_UNLOCK(&lcl_mutex
);
1119 name
= getenv("TZ");
1125 if (lcl_is_set
> 0 && strcmp(lcl_TZname
, name
) == 0)
1127 lcl_is_set
= strlen(name
) < sizeof lcl_TZname
;
1129 strcpy(lcl_TZname
, name
);
1131 if (*name
== '\0') {
1133 ** User wants it fast rather than right.
1135 lclptr
->leapcnt
= 0; /* so, we're off a little */
1136 lclptr
->timecnt
= 0;
1137 lclptr
->typecnt
= 0;
1138 lclptr
->ttis
[0].tt_isdst
= 0;
1139 lclptr
->ttis
[0].tt_gmtoff
= 0;
1140 lclptr
->ttis
[0].tt_abbrind
= 0;
1141 strcpy(lclptr
->chars
, gmt
);
1142 } else if (tzload(name
, lclptr
, TRUE
) != 0)
1143 if (name
[0] == ':' || tzparse(name
, lclptr
, FALSE
) != 0)
1151 _MUTEX_LOCK(&lcl_mutex
);
1153 _MUTEX_UNLOCK(&lcl_mutex
);
1157 ** The easy way to behave "as if no library function calls" localtime
1158 ** is to not call it--so we drop its guts into "localsub", which can be
1159 ** freely called. (And no, the PANS doesn't require the above behavior--
1160 ** but it *is* desirable.)
1162 ** The unused offset argument is for the benefit of mktime variants.
1167 localsub(const time_t * const timep
, const long offset __unused
,
1168 struct tm
* const tmp
)
1171 const struct ttinfo
* ttisp
;
1174 const time_t t
= *timep
;
1178 if ((sp
->goback
&& t
< sp
->ats
[0]) ||
1179 (sp
->goahead
&& t
> sp
->ats
[sp
->timecnt
- 1])) {
1183 int_fast64_t icycles
;
1186 seconds
= sp
->ats
[0] - t
;
1187 else seconds
= t
- sp
->ats
[sp
->timecnt
- 1];
1189 tcycles
= seconds
/ YEARSPERREPEAT
/ AVGSECSPERYEAR
;
1192 if (tcycles
- icycles
>= 1 || icycles
- tcycles
>= 1)
1195 seconds
*= YEARSPERREPEAT
;
1196 seconds
*= AVGSECSPERYEAR
;
1199 else newt
-= seconds
;
1200 if (newt
< sp
->ats
[0] ||
1201 newt
> sp
->ats
[sp
->timecnt
- 1])
1202 return NULL
; /* "cannot happen" */
1203 result
= localsub(&newt
, offset
, tmp
);
1204 if (result
== tmp
) {
1207 newy
= tmp
->tm_year
;
1209 newy
-= icycles
* YEARSPERREPEAT
;
1210 else newy
+= icycles
* YEARSPERREPEAT
;
1211 tmp
->tm_year
= newy
;
1212 if (tmp
->tm_year
!= newy
)
1217 if (sp
->timecnt
== 0 || t
< sp
->ats
[0]) {
1219 while (sp
->ttis
[i
].tt_isdst
)
1220 if (++i
>= sp
->typecnt
) {
1226 int hi
= sp
->timecnt
;
1229 int mid
= (lo
+ hi
) >> 1;
1231 if (t
< sp
->ats
[mid
])
1235 i
= (int) sp
->types
[lo
- 1];
1237 ttisp
= &sp
->ttis
[i
];
1239 ** To get (wrong) behavior that's compatible with System V Release 2.0
1240 ** you'd replace the statement below with
1241 ** t += ttisp->tt_gmtoff;
1242 ** timesub(&t, 0L, sp, tmp);
1244 result
= timesub(&t
, ttisp
->tt_gmtoff
, sp
, tmp
);
1245 tmp
->tm_isdst
= ttisp
->tt_isdst
;
1246 tzname
[tmp
->tm_isdst
] = &sp
->chars
[ttisp
->tt_abbrind
];
1248 tmp
->TM_ZONE
= &sp
->chars
[ttisp
->tt_abbrind
];
1249 #endif /* defined TM_ZONE */
1254 localtime_r(const time_t * const timep
, struct tm
*p_tm
)
1256 _MUTEX_LOCK(&lcl_mutex
);
1258 localsub(timep
, 0L, p_tm
);
1259 _MUTEX_UNLOCK(&lcl_mutex
);
1264 localtime(const time_t * const timep
)
1266 static pthread_mutex_t localtime_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1267 static pthread_key_t localtime_key
= -1;
1270 if (__isthreaded
!= 0) {
1271 _pthread_mutex_lock(&localtime_mutex
);
1272 if (localtime_key
< 0) {
1273 if (_pthread_key_create(&localtime_key
, free
) < 0) {
1274 _pthread_mutex_unlock(&localtime_mutex
);
1278 _pthread_mutex_unlock(&localtime_mutex
);
1279 p_tm
= _pthread_getspecific(localtime_key
);
1281 if ((p_tm
= (struct tm
*)malloc(sizeof(struct tm
)))
1284 _pthread_setspecific(localtime_key
, p_tm
);
1286 _pthread_mutex_lock(&lcl_mutex
);
1288 localsub(timep
, 0L, p_tm
);
1289 _pthread_mutex_unlock(&lcl_mutex
);
1293 localsub(timep
, 0L, &tm
);
1299 ** gmtsub is to gmtime as localsub is to localtime.
1303 gmtsub(const time_t * const timep
, const long offset
, struct tm
* const tmp
)
1307 _MUTEX_LOCK(&gmt_mutex
);
1312 _MUTEX_UNLOCK(&gmt_mutex
);
1313 result
= timesub(timep
, offset
, gmtptr
, tmp
);
1316 ** Could get fancy here and deliver something such as
1317 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1318 ** but this is no time for a treasure hunt.
1321 tmp
->TM_ZONE
= wildabbr
;
1323 tmp
->TM_ZONE
= gmtptr
->chars
;
1324 #endif /* defined TM_ZONE */
1329 gmtime(const time_t * const timep
)
1331 static pthread_mutex_t gmtime_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1332 static pthread_key_t gmtime_key
= -1;
1335 if (__isthreaded
!= 0) {
1336 _pthread_mutex_lock(&gmtime_mutex
);
1337 if (gmtime_key
< 0) {
1338 if (_pthread_key_create(&gmtime_key
, free
) < 0) {
1339 _pthread_mutex_unlock(&gmtime_mutex
);
1343 _pthread_mutex_unlock(&gmtime_mutex
);
1345 * Changed to follow POSIX.1 threads standard, which
1346 * is what BSD currently has.
1348 if ((p_tm
= _pthread_getspecific(gmtime_key
)) == NULL
) {
1349 if ((p_tm
= (struct tm
*)malloc(sizeof(struct tm
)))
1353 _pthread_setspecific(gmtime_key
, p_tm
);
1355 return gmtsub(timep
, 0L, p_tm
);
1357 return gmtsub(timep
, 0L, &tm
);
1362 gmtime_r(const time_t * timep
, struct tm
* tmp
)
1364 return gmtsub(timep
, 0L, tmp
);
1368 offtime(const time_t * const timep
, const long offset
)
1370 return gmtsub(timep
, offset
, &tm
);
1374 ** Return the number of leap years through the end of the given year
1375 ** where, to make the math easy, the answer for year zero is defined as zero.
1379 leaps_thru_end_of(const int y
)
1381 return (y
>= 0) ? (y
/ 4 - y
/ 100 + y
/ 400) :
1382 -(leaps_thru_end_of(-(y
+ 1)) + 1);
1386 timesub(const time_t * const timep
, const long offset
,
1387 const struct state
* const sp
, struct tm
* const tmp
)
1389 const struct lsinfo
* lp
;
1391 int idays
; /* unsigned would be so 2003 */
1406 if (*timep
>= lp
->ls_trans
) {
1407 if (*timep
== lp
->ls_trans
) {
1408 hit
= ((i
== 0 && lp
->ls_corr
> 0) ||
1409 lp
->ls_corr
> sp
->lsis
[i
- 1].ls_corr
);
1412 sp
->lsis
[i
].ls_trans
==
1413 sp
->lsis
[i
- 1].ls_trans
+ 1 &&
1414 sp
->lsis
[i
].ls_corr
==
1415 sp
->lsis
[i
- 1].ls_corr
+ 1) {
1425 tdays
= *timep
/ SECSPERDAY
;
1426 rem
= *timep
- tdays
* SECSPERDAY
;
1427 while (tdays
< 0 || tdays
>= year_lengths
[isleap(y
)]) {
1433 tdelta
= tdays
/ DAYSPERLYEAR
;
1435 if (tdelta
- idelta
>= 1 || idelta
- tdelta
>= 1)
1438 idelta
= (tdays
< 0) ? -1 : 1;
1440 if (increment_overflow(&newy
, idelta
))
1442 leapdays
= leaps_thru_end_of(newy
- 1) -
1443 leaps_thru_end_of(y
- 1);
1444 tdays
-= ((time_t) newy
- y
) * DAYSPERNYEAR
;
1451 seconds
= tdays
* SECSPERDAY
+ 0.5;
1452 tdays
= seconds
/ SECSPERDAY
;
1453 rem
+= seconds
- tdays
* SECSPERDAY
;
1456 ** Given the range, we can now fearlessly cast...
1459 rem
+= offset
- corr
;
1464 while (rem
>= SECSPERDAY
) {
1469 if (increment_overflow(&y
, -1))
1471 idays
+= year_lengths
[isleap(y
)];
1473 while (idays
>= year_lengths
[isleap(y
)]) {
1474 idays
-= year_lengths
[isleap(y
)];
1475 if (increment_overflow(&y
, 1))
1479 if (increment_overflow(&tmp
->tm_year
, -TM_YEAR_BASE
))
1481 tmp
->tm_yday
= idays
;
1483 ** The "extra" mods below avoid overflow problems.
1485 tmp
->tm_wday
= EPOCH_WDAY
+
1486 ((y
- EPOCH_YEAR
) % DAYSPERWEEK
) *
1487 (DAYSPERNYEAR
% DAYSPERWEEK
) +
1488 leaps_thru_end_of(y
- 1) -
1489 leaps_thru_end_of(EPOCH_YEAR
- 1) +
1491 tmp
->tm_wday
%= DAYSPERWEEK
;
1492 if (tmp
->tm_wday
< 0)
1493 tmp
->tm_wday
+= DAYSPERWEEK
;
1494 tmp
->tm_hour
= (int) (rem
/ SECSPERHOUR
);
1496 tmp
->tm_min
= (int) (rem
/ SECSPERMIN
);
1498 ** A positive leap second requires a special
1499 ** representation. This uses "... ??:59:60" et seq.
1501 tmp
->tm_sec
= (int) (rem
% SECSPERMIN
) + hit
;
1502 ip
= mon_lengths
[isleap(y
)];
1503 for (tmp
->tm_mon
= 0; idays
>= ip
[tmp
->tm_mon
]; ++(tmp
->tm_mon
))
1504 idays
-= ip
[tmp
->tm_mon
];
1505 tmp
->tm_mday
= (int) (idays
+ 1);
1508 tmp
->TM_GMTOFF
= offset
;
1509 #endif /* defined TM_GMTOFF */
1514 ctime(const time_t * const timep
)
1517 ** Section 4.12.3.2 of X3.159-1989 requires that
1518 ** The ctime function converts the calendar time pointed to by timer
1519 ** to local time in the form of a string. It is equivalent to
1520 ** asctime(localtime(timer))
1522 return asctime(localtime(timep
));
1526 ctime_r(const time_t * const timep
, char *buf
)
1529 return asctime_r(localtime_r(timep
, &mytm
), buf
);
1533 ** Adapted from code provided by Robert Elz, who writes:
1534 ** The "best" way to do mktime I think is based on an idea of Bob
1535 ** Kridle's (so its said...) from a long time ago.
1536 ** It does a binary search of the time_t space. Since time_t's are
1537 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1538 ** would still be very reasonable).
1543 #endif /* !defined WRONG */
1546 ** Simplified normalize logic courtesy Paul Eggert.
1550 increment_overflow(int *number
, int delta
)
1556 return (*number
< number0
) != (delta
< 0);
1560 long_increment_overflow(long *number
, int delta
)
1566 return (*number
< number0
) != (delta
< 0);
1570 normalize_overflow(int * const tensptr
, int * const unitsptr
, const int base
)
1574 tensdelta
= (*unitsptr
>= 0) ?
1575 (*unitsptr
/ base
) :
1576 (-1 - (-1 - *unitsptr
) / base
);
1577 *unitsptr
-= tensdelta
* base
;
1578 return increment_overflow(tensptr
, tensdelta
);
1582 long_normalize_overflow(long * const tensptr
, int * const unitsptr
,
1587 tensdelta
= (*unitsptr
>= 0) ?
1588 (*unitsptr
/ base
) :
1589 (-1 - (-1 - *unitsptr
) / base
);
1590 *unitsptr
-= tensdelta
* base
;
1591 return long_increment_overflow(tensptr
, tensdelta
);
1595 tmcomp(const struct tm
* const atmp
, const struct tm
* const btmp
)
1599 if ((result
= (atmp
->tm_year
- btmp
->tm_year
)) == 0 &&
1600 (result
= (atmp
->tm_mon
- btmp
->tm_mon
)) == 0 &&
1601 (result
= (atmp
->tm_mday
- btmp
->tm_mday
)) == 0 &&
1602 (result
= (atmp
->tm_hour
- btmp
->tm_hour
)) == 0 &&
1603 (result
= (atmp
->tm_min
- btmp
->tm_min
)) == 0)
1604 result
= atmp
->tm_sec
- btmp
->tm_sec
;
1609 time2sub(struct tm
* const tmp
,
1610 struct tm
* (* const funcp
)(const time_t *, long, struct tm
*),
1611 const long offset
, int * const okayp
, const int do_norm_secs
)
1613 const struct state
* sp
;
1623 struct tm yourtm
, mytm
;
1628 if (normalize_overflow(&yourtm
.tm_min
, &yourtm
.tm_sec
,
1632 if (normalize_overflow(&yourtm
.tm_hour
, &yourtm
.tm_min
, MINSPERHOUR
))
1634 if (normalize_overflow(&yourtm
.tm_mday
, &yourtm
.tm_hour
, HOURSPERDAY
))
1637 if (long_normalize_overflow(&y
, &yourtm
.tm_mon
, MONSPERYEAR
))
1640 ** Turn y into an actual year number for now.
1641 ** It is converted back to an offset from TM_YEAR_BASE later.
1643 if (long_increment_overflow(&y
, TM_YEAR_BASE
))
1645 while (yourtm
.tm_mday
<= 0) {
1646 if (long_increment_overflow(&y
, -1))
1648 li
= y
+ (1 < yourtm
.tm_mon
);
1649 yourtm
.tm_mday
+= year_lengths
[isleap(li
)];
1651 while (yourtm
.tm_mday
> DAYSPERLYEAR
) {
1652 li
= y
+ (1 < yourtm
.tm_mon
);
1653 yourtm
.tm_mday
-= year_lengths
[isleap(li
)];
1654 if (long_increment_overflow(&y
, 1))
1658 i
= mon_lengths
[isleap(y
)][yourtm
.tm_mon
];
1659 if (yourtm
.tm_mday
<= i
)
1661 yourtm
.tm_mday
-= i
;
1662 if (++yourtm
.tm_mon
>= MONSPERYEAR
) {
1664 if (long_increment_overflow(&y
, 1))
1668 if (long_increment_overflow(&y
, -TM_YEAR_BASE
))
1671 if (yourtm
.tm_year
!= y
)
1673 if (yourtm
.tm_sec
>= 0 && yourtm
.tm_sec
< SECSPERMIN
)
1675 else if (y
+ TM_YEAR_BASE
< EPOCH_YEAR
) {
1677 ** We can't set tm_sec to 0, because that might push the
1678 ** time below the minimum representable time.
1679 ** Set tm_sec to 59 instead.
1680 ** This assumes that the minimum representable time is
1681 ** not in the same minute that a leap second was deleted from,
1682 ** which is a safer assumption than using 58 would be.
1684 if (increment_overflow(&yourtm
.tm_sec
, 1 - SECSPERMIN
))
1686 saved_seconds
= yourtm
.tm_sec
;
1687 yourtm
.tm_sec
= SECSPERMIN
- 1;
1689 saved_seconds
= yourtm
.tm_sec
;
1693 ** Do a binary search (this works whatever time_t's type is).
1695 if (!TYPE_SIGNED(time_t)) {
1698 } else if (!TYPE_INTEGRAL(time_t)) {
1699 if (sizeof(time_t) > sizeof(float))
1700 hi
= (time_t) DBL_MAX
;
1701 else hi
= (time_t) FLT_MAX
;
1705 for (i
= 0; i
< (int) TYPE_BIT(time_t) - 1; ++i
)
1710 t
= lo
/ 2 + hi
/ 2;
1715 if ((*funcp
)(&t
, offset
, &mytm
) == NULL
) {
1717 ** Assume that t is too extreme to be represented in
1718 ** a struct tm; arrange things so that it is less
1719 ** extreme on the next pass.
1721 dir
= (t
> 0) ? 1 : -1;
1722 } else dir
= tmcomp(&mytm
, &yourtm
);
1729 } else if (t
== hi
) {
1742 if (yourtm
.tm_isdst
< 0 || mytm
.tm_isdst
== yourtm
.tm_isdst
)
1745 ** Right time, wrong type.
1746 ** Hunt for right time, right type.
1747 ** It's okay to guess wrong since the guess
1750 sp
= (const struct state
*)
1751 ((funcp
== localsub
) ? lclptr
: gmtptr
);
1753 for (i
= sp
->typecnt
- 1; i
>= 0; --i
) {
1754 if (sp
->ttis
[i
].tt_isdst
!= yourtm
.tm_isdst
)
1756 for (j
= sp
->typecnt
- 1; j
>= 0; --j
) {
1757 if (sp
->ttis
[j
].tt_isdst
== yourtm
.tm_isdst
)
1759 newt
= t
+ sp
->ttis
[j
].tt_gmtoff
-
1760 sp
->ttis
[i
].tt_gmtoff
;
1761 if ((*funcp
)(&newt
, offset
, &mytm
) == NULL
)
1763 if (tmcomp(&mytm
, &yourtm
) != 0)
1765 if (mytm
.tm_isdst
!= yourtm
.tm_isdst
)
1777 newt
= t
+ saved_seconds
;
1778 if ((newt
< t
) != (saved_seconds
< 0))
1781 if ((*funcp
)(&t
, offset
, tmp
))
1787 time2(struct tm
* const tmp
,
1788 struct tm
* (* const funcp
)(const time_t *, long, struct tm
*),
1789 const long offset
, int * const okayp
)
1794 ** First try without normalization of seconds
1795 ** (in case tm_sec contains a value associated with a leap second).
1796 ** If that fails, try with normalization of seconds.
1798 t
= time2sub(tmp
, funcp
, offset
, okayp
, FALSE
);
1799 return *okayp
? t
: time2sub(tmp
, funcp
, offset
, okayp
, TRUE
);
1803 time1(struct tm
* const tmp
,
1804 struct tm
* (* const funcp
)(const time_t *, long, struct tm
*),
1808 const struct state
* sp
;
1810 int sameind
, otherind
;
1813 int seen
[TZ_MAX_TYPES
];
1814 int types
[TZ_MAX_TYPES
];
1817 if (tmp
->tm_isdst
> 1)
1819 t
= time2(tmp
, funcp
, offset
, &okay
);
1822 ** PCTS code courtesy Grant Sullivan.
1826 if (tmp
->tm_isdst
< 0)
1827 tmp
->tm_isdst
= 0; /* reset to std and try again */
1830 ** We're supposed to assume that somebody took a time of one type
1831 ** and did some math on it that yielded a "struct tm" that's bad.
1832 ** We try to divine the type they started from and adjust to the
1835 sp
= (const struct state
*) ((funcp
== localsub
) ? lclptr
: gmtptr
);
1837 for (i
= 0; i
< sp
->typecnt
; ++i
)
1840 for (i
= sp
->timecnt
- 1; i
>= 0; --i
)
1841 if (!seen
[sp
->types
[i
]]) {
1842 seen
[sp
->types
[i
]] = TRUE
;
1843 types
[nseen
++] = sp
->types
[i
];
1845 for (sameind
= 0; sameind
< nseen
; ++sameind
) {
1846 samei
= types
[sameind
];
1847 if (sp
->ttis
[samei
].tt_isdst
!= tmp
->tm_isdst
)
1849 for (otherind
= 0; otherind
< nseen
; ++otherind
) {
1850 otheri
= types
[otherind
];
1851 if (sp
->ttis
[otheri
].tt_isdst
== tmp
->tm_isdst
)
1853 tmp
->tm_sec
+= sp
->ttis
[otheri
].tt_gmtoff
-
1854 sp
->ttis
[samei
].tt_gmtoff
;
1855 tmp
->tm_isdst
= !tmp
->tm_isdst
;
1856 t
= time2(tmp
, funcp
, offset
, &okay
);
1859 tmp
->tm_sec
-= sp
->ttis
[otheri
].tt_gmtoff
-
1860 sp
->ttis
[samei
].tt_gmtoff
;
1861 tmp
->tm_isdst
= !tmp
->tm_isdst
;
1868 mktime(struct tm
* const tmp
)
1870 time_t mktime_return_value
;
1871 _MUTEX_LOCK(&lcl_mutex
);
1873 mktime_return_value
= time1(tmp
, localsub
, 0L);
1874 _MUTEX_UNLOCK(&lcl_mutex
);
1875 return(mktime_return_value
);
1879 timelocal(struct tm
* const tmp
)
1881 tmp
->tm_isdst
= -1; /* in case it wasn't initialized */
1886 timegm(struct tm
* const tmp
)
1889 return time1(tmp
, gmtsub
, 0L);
1893 timeoff(struct tm
* const tmp
, const long offset
)
1896 return time1(tmp
, gmtsub
, offset
);
1902 ** The following is supplied for compatibility with
1903 ** previous versions of the CMUCS runtime library.
1907 gtime(struct tm
* const tmp
)
1909 const time_t t
= mktime(tmp
);
1916 #endif /* defined CMUCS */
1919 ** XXX--is the below the right way to conditionalize??
1923 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1924 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
1925 ** is not the case if we are accounting for leap seconds.
1926 ** So, we provide the following conversion routines for use
1927 ** when exchanging timestamps with POSIX conforming systems.
1931 leapcorr(time_t *timep
)
1941 if (*timep
>= lp
->ls_trans
)
1948 time2posix(time_t t
)
1951 return t
- leapcorr(&t
);
1955 posix2time(time_t t
)
1962 ** For a positive leap second hit, the result
1963 ** is not unique. For a negative leap second
1964 ** hit, the corresponding time doesn't exist,
1965 ** so we return an adjacent second.
1967 x
= t
+ leapcorr(&t
);
1968 y
= x
- leapcorr(&x
);
1972 y
= x
- leapcorr(&x
);
1979 y
= x
- leapcorr(&x
);