2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
9 static char elsieid
[] __unused
= "@(#)localtime.c 8.14";
10 #endif /* !defined NOID */
11 #endif /* !defined lint */
12 __FBSDID("$FreeBSD: head/contrib/tzcode/stdtime/localtime.c 289027 2015-10-08 11:42:15Z rodrigc $");
15 ** Leap second handling from Bradley White.
16 ** POSIX-style TZ environment variable handling from Guy Harris.
21 #include "namespace.h"
22 #include <sys/types.h>
29 //#define NOTIFY_TZ_DEBUG
30 //#define NOTIFY_TZ_DEBUG_FILE "/var/log/localtime.debug"
31 //#define NOTIFY_TZ_LOG "/var/log/localtime.log"
32 /* force ALL_STATE if NOTIFY_TZ is set */
35 #endif /* ALL_STATE */
36 #include <mach/mach_init.h>
39 #endif /* NOTIFY_TZ */
41 #include "un-namespace.h"
44 #include "float.h" /* for FLT_MAX and DBL_MAX */
46 #ifndef TZ_ABBR_MAX_LEN
47 /* UNIX03 requires this to be the same as sysconf(_SC_TZNAME_MAX) */
48 #define TZ_ABBR_MAX_LEN 255
49 #endif /* !defined TZ_ABBR_MAX_LEN */
51 #ifndef TZ_ABBR_CHAR_SET
52 #define TZ_ABBR_CHAR_SET \
53 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
54 #endif /* !defined TZ_ABBR_CHAR_SET */
56 #ifndef TZ_ABBR_ERR_CHAR
57 #define TZ_ABBR_ERR_CHAR '_'
58 #endif /* !defined TZ_ABBR_ERR_CHAR */
60 #include "libc_private.h"
62 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
63 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
65 #define _RWLOCK_RDLOCK(x) \
67 if (__isthreaded) _pthread_rwlock_rdlock(x); \
70 #define _RWLOCK_WRLOCK(x) \
72 if (__isthreaded) _pthread_rwlock_wrlock(x); \
75 #define _RWLOCK_UNLOCK(x) \
77 if (__isthreaded) _pthread_rwlock_unlock(x); \
81 ** SunOS 4.1.1 headers lack O_BINARY.
85 #define OPEN_MODE (O_RDONLY | O_BINARY)
86 #endif /* defined O_BINARY */
88 #define OPEN_MODE O_RDONLY
89 #endif /* !defined O_BINARY */
93 ** Someone might make incorrect use of a time zone abbreviation:
94 ** 1. They might reference tzname[0] before calling tzset (explicitly
96 ** 2. They might reference tzname[1] before calling tzset (explicitly
98 ** 3. They might reference tzname[1] after setting to a time zone
99 ** in which Daylight Saving Time is never observed.
100 ** 4. They might reference tzname[0] after setting to a time zone
101 ** in which Standard Time is never observed.
102 ** 5. They might reference tm.TM_ZONE after calling offtime.
103 ** What's best to do in the above cases is open to debate;
104 ** for now, we just set things up so that in any of the five cases
105 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
106 ** string "tzname[0] used before set", and similarly for the other cases.
107 ** And another: initialize tzname[0] to "ERA", with an explanation in the
108 ** manual page of what this "time zone abbreviation" means (doing this so
109 ** that tzname[0] has the "normal" length of three characters).
112 #endif /* !defined WILDABBR */
114 __used
static const char wildabbr
[] = WILDABBR
;
117 * In June 2004 it was decided UTC was a more appropriate default time
121 __used
static const char gmt
[] = "UTC";
124 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
125 ** We default to US rules as of 1999-08-17.
126 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
127 ** implementation dependent; for historical reasons, US rules are a
130 #ifndef TZDEFRULESTRING
131 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
132 #endif /* !defined TZDEFDST */
134 struct ttinfo
{ /* time type information */
135 long tt_gmtoff
; /* UTC offset in seconds */
136 int tt_isdst
; /* used to set tm_isdst */
137 int tt_abbrind
; /* abbreviation list index */
138 int tt_ttisstd
; /* TRUE if transition is std time */
139 int tt_ttisgmt
; /* TRUE if transition is UTC */
142 struct lsinfo
{ /* leap second information */
143 time_t ls_trans
; /* transition time */
144 long ls_corr
; /* correction to apply */
147 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
150 #define MY_TZNAME_MAX TZNAME_MAX
151 #endif /* defined TZNAME_MAX */
153 #define MY_TZNAME_MAX 255
154 #endif /* !defined TZNAME_MAX */
163 time_t ats
[TZ_MAX_TIMES
];
164 unsigned char types
[TZ_MAX_TIMES
];
165 struct ttinfo ttis
[TZ_MAX_TYPES
];
166 char chars
[BIGGEST(BIGGEST(TZ_MAX_CHARS
+ 1, sizeof gmt
),
167 (2 * (MY_TZNAME_MAX
+ 1)))];
168 struct lsinfo lsis
[TZ_MAX_LEAPS
];
172 int r_type
; /* type of rule--see below */
173 int r_day
; /* day number of rule */
174 int r_week
; /* week number of rule */
175 int r_mon
; /* month number of rule */
176 long r_time
; /* transition time of rule */
179 #define JULIAN_DAY 0 /* Jn - Julian day */
180 #define DAY_OF_YEAR 1 /* n - day of year */
181 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
189 #define NOTIFY_TZ_NAME "com.apple.system.timezone"
190 #endif /* NOTIFY_TZ */
193 ** Prototypes for static functions.
195 #define localsub _st_localsub
196 #define time1 _st_time1
197 #define tzset_basic _st_tzset_basic
200 struct tm
* localsub(const time_t * timep
, long offset
,
202 #else /* !__LP64__ */
203 void localsub(const time_t * timep
, long offset
,
205 #endif /* __LP64__ */
207 time_t time1(struct tm
* tmp
,
209 struct tm
*(*funcp
) (const time_t *,
211 #else /* !__LP64__ */
212 void(*funcp
) (const time_t *,
214 #endif /* __LP64__ */
218 void tzset_basic(int);
220 #if !BUILDING_VARIANT
221 static long detzcode(const char * codep
);
222 static time_t detzcode64(const char * codep
);
223 static int differ_by_repeat(time_t t1
, time_t t0
);
224 static const char * getzname(const char * strp
, char **name
, size_t *len
);
225 static const char * getqzname(const char * strp
, const int delim
)
227 static const char * getnum(const char * strp
, int * nump
, int min
,
229 static const char * getsecs(const char * strp
, long * secsp
);
230 static const char * getoffset(const char * strp
, long * offsetp
);
231 static const char * getrule(const char * strp
, struct rule
* rulep
);
233 static void gmtload(struct state
* sp
, char *path
);
234 #else /* ! NOTIFY_TZ */
235 static void gmtload(struct state
* sp
);
236 #endif /* NOTIFY_TZ */
238 static struct tm
* gmtsub(const time_t * timep
, long offset
,
240 #else /* !__LP64__ */
241 static void gmtsub(const time_t * timep
, long offset
,
243 #endif /* __LP64__ */
244 static int increment_overflow(int * number
, int delta
);
245 static int leaps_thru_end_of(int y
) ATTRIBUTE_PURE
;
246 static int long_increment_overflow(long * number
, int delta
);
247 static int long_normalize_overflow(long * tensptr
,
248 int * unitsptr
, int base
);
249 static int normalize_overflow(int * tensptr
, int * unitsptr
,
252 static void notify_check_tz(notify_tz_t
*p
);
253 static void notify_register_tz(char *file
, notify_tz_t
*p
);
254 #endif /* NOTIFY_TZ */
255 static void settzname(void);
256 static time_t time2(struct tm
*tmp
,
258 struct tm
*(*funcp
) (const time_t *,
260 #else /* !__LP64__ */
261 void(*funcp
) (const time_t *,
263 #endif /* __LP64__ */
264 long offset
, int * okayp
, int unix03
);
265 static time_t time2sub(struct tm
*tmp
,
267 struct tm
*(*funcp
) (const time_t *,
269 #else /* !__LP64__ */
270 void(*funcp
) (const time_t *,
272 #endif /* __LP64__ */
273 long offset
, int * okayp
, int do_norm_secs
,
276 static struct tm
* timesub(const time_t * timep
, long offset
,
277 const struct state
* sp
, struct tm
* tmp
);
278 #else /* !__LP64__ */
279 static void timesub(const time_t * timep
, long offset
,
280 const struct state
* sp
, struct tm
* tmp
);
281 #endif /* __LP64__ */
282 static int tmcomp(const struct tm
* atmp
,
283 const struct tm
* btmp
);
284 static time_t transtime(time_t janfirst
, int year
,
285 const struct rule
* rulep
, long offset
)
287 static int typesequiv(const struct state
* sp
, int a
, int b
);
289 static int tzload(const char * name
, struct state
* sp
, char *path
, int doextend
);
290 #else /* ! NOTIFY_TZ */
291 static int tzload(const char * name
, struct state
* sp
, int doextend
);
292 #endif /* NOTIFY_TZ */
293 static int tzparse(const char * name
, struct state
* sp
,
297 static struct state
* lclptr
;
298 static struct state
* gmtptr
;
299 #endif /* defined ALL_STATE */
302 static struct state lclmem
;
303 static struct state gmtmem
;
304 #define lclptr (&lclmem)
305 #define gmtptr (&gmtmem)
306 #endif /* State Farm */
308 #ifndef TZ_STRLEN_MAX
309 #define TZ_STRLEN_MAX 255
310 #endif /* !defined TZ_STRLEN_MAX */
312 static char lcl_TZname
[TZ_STRLEN_MAX
+ 1];
314 #define lcl_is_set (lcl_notify.is_set)
315 #define gmt_is_set (gmt_notify.is_set)
316 #else /* ! NOTIFY_TZ */
317 static int lcl_is_set
;
318 #endif /* NOTIFY_TZ */
319 static pthread_once_t gmt_once
= PTHREAD_ONCE_INIT
;
320 __private_extern__ pthread_rwlock_t lcl_rwlock
= PTHREAD_RWLOCK_INITIALIZER
;
321 static pthread_once_t gmtime_once
= PTHREAD_ONCE_INIT
;
322 static pthread_key_t gmtime_key
;
323 static int gmtime_key_error
;
324 static pthread_once_t localtime_once
= PTHREAD_ONCE_INIT
;
325 static pthread_key_t localtime_key
;
326 static int localtime_key_error
;
334 ** Section 4.12.3 of X3.159-1989 requires that
335 ** Except for the strftime function, these functions [asctime,
336 ** ctime, gmtime, localtime] return values in one of two static
337 ** objects: a broken-down time structure and an array of char.
338 ** Thanks to Paul Eggert for noting this.
347 __private_extern__
void _st_set_timezone(long);
348 #endif /* defined USG_COMPAT */
351 __private_extern__
long __darwin_altzone
= 0;
352 #define altzone __darwin_altzone
353 #endif /* defined ALTZONE */
356 #ifdef NOTIFY_TZ_DEBUG
357 #ifdef NOTIFY_TZ_DEBUG_FILE
358 #define NOTIFY_TZ_PRINTF(fmt, args...) \
360 FILE *_notify_tz_fp_; \
361 if((_notify_tz_fp_ = fopen(NOTIFY_TZ_DEBUG_FILE, "a")) != NULL) { \
362 fprintf(_notify_tz_fp_, "%d: " fmt, getpid(), ## args); \
363 fclose(_notify_tz_fp_); \
366 #else /* ! NOTIFY_TZ_DEBUG_FILE */
367 #define NOTIFY_TZ_PRINTF(args...) fprintf(stdout, ## args)
368 #endif /* NOTIFY_TZ_DEBUG_FILE */
369 #endif /* NOTIFY_TZ_DEBUG */
371 #define NOTIFY_LOG(fmt, args...) \
373 FILE *_notify_log_fp_; \
374 if((_notify_log_fp_ = fopen(NOTIFY_TZ_LOG, "a")) != NULL) { \
375 fprintf(_notify_log_fp_, "%d: " fmt, getpid(), ## args); \
376 fclose(_notify_log_fp_); \
379 #endif /* NOTIFY_TZ_LOG */
381 static notify_tz_t gmt_notify
= {-1, 0};
382 static notify_tz_t lcl_notify
= {-1, 0};
383 static const char notify_tz_name
[] = NOTIFY_TZ_NAME
;
384 #endif /* NOTIFY_TZ */
387 detzcode(const char *const codep
)
392 result
= (codep
[0] & 0x80) ? ~0L : 0;
393 for (i
= 0; i
< 4; ++i
)
394 result
= (result
<< 8) | (codep
[i
] & 0xff);
399 detzcode64(const char *const codep
)
401 register time_t result
;
404 result
= (codep
[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
405 for (i
= 0; i
< 8; ++i
)
406 result
= result
* 256 + (codep
[i
] & 0xff);
413 struct state
* sp
= lclptr
;
415 unsigned char * types
;
418 #define NEED_DAYLIGHT 4
419 #define NEED_ALL (NEED_STD | NEED_DST | NEED_DAYLIGHT)
421 tzname
[0] = (char *)wildabbr
;
422 tzname
[1] = (char *)wildabbr
;
426 #endif /* defined USG_COMPAT */
429 #endif /* defined ALTZONE */
432 tzname
[0] = tzname
[1] = (char *)gmt
;
435 #endif /* defined ALL_STATE */
437 * PR-3765457: The original settzname went sequentially through the ttis
438 * array, rather than correctly indexing via the types array, to get
439 * the real order of the timezone changes. In addition, as a speed up,
440 * we start at the end of the changes, and work back, so that most of
441 * the time, we don't have to look through the entire array.
443 if (sp
->timecnt
== 0 && sp
->typecnt
== 1) {
445 * Unfortunately, there is an edge case when typecnt == 1 and
446 * timecnt == 0, which would cause the loop to never run. So
447 * in that case, we fudge things up so that it is as if
451 types
= (unsigned char *)""; /* we use the null as index */
458 for (; i
>= 0 && need
; --i
) {
459 const struct ttinfo
* const ttisp
= &sp
->ttis
[types
[i
]];
462 if ((need
& NEED_DAYLIGHT
) && ttisp
->tt_isdst
) {
463 need
&= ~NEED_DAYLIGHT
;
466 #endif /* defined USG_COMPAT */
467 if (ttisp
->tt_isdst
) {
468 if (need
& NEED_DST
) {
470 tzname
[1] = &sp
->chars
[ttisp
->tt_abbrind
];
472 altzone
= -(ttisp
->tt_gmtoff
);
473 #endif /* defined ALTZONE */
475 } else if (need
& NEED_STD
) {
477 tzname
[0] = &sp
->chars
[ttisp
->tt_abbrind
];
479 _st_set_timezone(-(ttisp
->tt_gmtoff
));
480 #endif /* defined USG_COMPAT */
482 #if defined(ALTZONE) || defined(USG_COMPAT)
484 #endif /* defined(ALTZONE) || defined(USG_COMPAT) */
487 altzone
= -(ttisp
->tt_gmtoff
);
488 #endif /* defined ALTZONE */
491 _st_set_timezone(-(ttisp
->tt_gmtoff
));
492 #endif /* defined USG_COMPAT */
493 #if defined(ALTZONE) || defined(USG_COMPAT)
495 #endif /* defined(ALTZONE) || defined(USG_COMPAT) */
498 ** Finally, scrub the abbreviations.
499 ** First, replace bogus characters.
501 for (i
= 0; i
< sp
->charcnt
; ++i
)
502 if (strchr(TZ_ABBR_CHAR_SET
, sp
->chars
[i
]) == NULL
)
503 sp
->chars
[i
] = TZ_ABBR_ERR_CHAR
;
505 ** Second, truncate long abbreviations.
507 for (i
= 0; i
< sp
->typecnt
; ++i
) {
508 register const struct ttinfo
* const ttisp
= &sp
->ttis
[i
];
509 register char * cp
= &sp
->chars
[ttisp
->tt_abbrind
];
511 if (strlen(cp
) > TZ_ABBR_MAX_LEN
&&
512 strcmp(cp
, GRANDPARENTED
) != 0)
513 *(cp
+ TZ_ABBR_MAX_LEN
) = '\0';
519 notify_check_tz(notify_tz_t
*p
)
526 nstat
= notify_check(p
->token
, &ncheck
);
527 if (nstat
|| ncheck
) {
529 #ifdef NOTIFY_TZ_DEBUG
530 NOTIFY_TZ_PRINTF("notify_check_tz: %s changed\n", (p
== &lcl_notify
) ? "lcl" : "gmt");
531 #endif /* NOTIFY_TZ_DEBUG */
533 #ifdef NOTIFY_TZ_DEBUG
534 NOTIFY_TZ_PRINTF("notify_check_tz: %s unchanged\n", (p
== &lcl_notify
) ? "lcl" : "gmt");
535 #endif /* NOTIFY_TZ_DEBUG */
538 extern uint32_t notify_monitor_file(int token
, char *path
, int flags
);
541 notify_register_tz(char *file
, notify_tz_t
*p
)
547 /*----------------------------------------------------------------
548 * Since we don't record the last time zone filename, just cancel
549 * (which should remove the file monitor) and setup from scratch
550 *----------------------------------------------------------------*/
552 notify_cancel(p
->token
);
553 if (!file
|| *file
== 0) {
554 /* no time zone file to monitor */
558 /*----------------------------------------------------------------
559 * Just use com.apple.system.timezone if the path is /etc/localtime.
560 * Otherwise use com.apple.system.timezone.<fullpath>
561 *----------------------------------------------------------------*/
562 if (TZDEFAULT
&& strcmp(file
, TZDEFAULT
) == 0)
563 name
= (char *)notify_tz_name
;
565 name
= alloca(sizeof(notify_tz_name
) + strlen(file
) + 1);
570 strcpy(name
, notify_tz_name
);
574 #ifdef NOTIFY_TZ_DEBUG
575 NOTIFY_TZ_PRINTF("notify_register_tz: file=%s name=%s\n", file
, name
);
576 #endif /* NOTIFY_TZ_DEBUG */
577 nstat
= notify_register_check(name
, &p
->token
);
581 #ifdef NOTIFY_TZ_DEBUG
582 NOTIFY_TZ_PRINTF("***notify_register_tz: notify_register_check failed: %u\n", nstat
);
583 #endif /* NOTIFY_TZ_DEBUG */
585 NOTIFY_LOG("notify_register_check(%s) failed: %u\n", name
, nstat
);
586 #endif /* NOTIFY_TZ_LOG */
589 /* don't need to request monitoring /etc/localtime */
590 if (name
!= notify_tz_name
) {
591 #ifdef NOTIFY_TZ_DEBUG
592 NOTIFY_TZ_PRINTF("notify_register_tz: monitor %s\n", file
);
593 #endif /* NOTIFY_TZ_DEBUG */
594 nstat
= notify_monitor_file(p
->token
, file
, 0);
596 notify_cancel(p
->token
);
599 #ifdef NOTIFY_TZ_DEBUG
600 NOTIFY_TZ_PRINTF("***notify_register_tz: notify_monitor_file failed: %u\n", nstat
);
601 #endif /* NOTIFY_TZ_DEBUG */
603 NOTIFY_LOG("notify_monitor_file(%s) failed: %u\n", file
, nstat
);
604 #endif /* NOTIFY_TZ_LOG */
608 notify_check(p
->token
, &ncheck
); /* this always returns true */
610 #endif /* NOTIFY_TZ */
613 differ_by_repeat(const time_t t1
, const time_t t0
)
615 int_fast64_t _t0
= t0
;
616 int_fast64_t _t1
= t1
;
618 if (TYPE_INTEGRAL(time_t) &&
619 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS
)
621 //turn ((int_fast64_t)(t1 - t0) == SECSPERREPEAT);
622 return _t1
- _t0
== SECSPERREPEAT
;
627 tzload(name
, sp
, path
, doextend
)
628 #else /* ! NOTIFY_TZ */
629 tzload(name
, sp
, doextend
)
630 #endif /* NOTIFY_TZ */
632 struct state
* const sp
;
634 char * path
; /* copy full path if non-NULL */
635 #endif /* NOTIFY_TZ */
636 register const int doextend
;
645 struct tzhead tzhead
;
646 char buf
[2 * sizeof(struct tzhead
) +
653 sp
->goback
= sp
->goahead
= FALSE
;
655 #ifdef NOTIFY_TZ_DEBUG
656 NOTIFY_TZ_PRINTF("tzload: name=%s\n", name
);
657 #endif /* NOTIFY_TZ_DEBUG */
658 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
659 if (name
!= NULL
&& issetugid() != 0)
660 if ((name
[0] == ':' && name
[1] == '/') ||
661 name
[0] == '/' || strchr(name
, '.'))
665 *path
= 0; /* default to empty string on error */
666 #endif /* NOTIFY_TZ */
667 if (name
== NULL
&& (name
= TZDEFAULT
) == NULL
)
673 ** Section 4.9.1 of the C standard says that
674 ** "FILENAME_MAX expands to an integral constant expression
675 ** that is the size needed for an array of char large enough
676 ** to hold the longest file name string that the implementation
677 ** guarantees can be opened."
681 fullname
= malloc(FILENAME_MAX
+ 1);
682 if (fullname
== NULL
)
687 doaccess
= name
[0] == '/';
689 if ((p
= TZDIR
) == NULL
) {
693 if (strlen(p
) + 1 + strlen(name
) >= FILENAME_MAX
) {
697 (void) strcpy(fullname
, p
);
698 (void) strcat(fullname
, "/");
699 (void) strcat(fullname
, name
);
701 ** Set doaccess if '.' (as in "../") shows up in name.
703 if (strchr(name
, '.') != NULL
)
709 if (strlen(name
) > FILENAME_MAX
)
713 #endif /* NOTIFY_TZ */
714 if (doaccess
&& access(name
, R_OK
) != 0) {
718 if ((fid
= _open(name
, OPEN_MODE
)) == -1) {
722 if ((_fstat(fid
, &stab
) < 0) || !S_ISREG(stab
.st_mode
)) {
729 u
= malloc(sizeof(*u
));
732 #ifdef NOTIFY_TZ_DEBUG
733 NOTIFY_TZ_PRINTF("tzload: reading %s\n", name
);
734 #endif /* NOTIFY_TZ_DEBUG */
735 nread
= _read(fid
, u
->buf
, sizeof u
->buf
);
736 if (_close(fid
) < 0 || nread
<= 0)
738 for (stored
= 4; stored
<= 8; stored
*= 2) {
742 ttisstdcnt
= (int) detzcode(u
->tzhead
.tzh_ttisstdcnt
);
743 ttisgmtcnt
= (int) detzcode(u
->tzhead
.tzh_ttisgmtcnt
);
744 sp
->leapcnt
= (int) detzcode(u
->tzhead
.tzh_leapcnt
);
745 sp
->timecnt
= (int) detzcode(u
->tzhead
.tzh_timecnt
);
746 sp
->typecnt
= (int) detzcode(u
->tzhead
.tzh_typecnt
);
747 sp
->charcnt
= (int) detzcode(u
->tzhead
.tzh_charcnt
);
748 p
= u
->tzhead
.tzh_charcnt
+ sizeof u
->tzhead
.tzh_charcnt
;
749 if (sp
->leapcnt
< 0 || sp
->leapcnt
> TZ_MAX_LEAPS
||
750 sp
->typecnt
<= 0 || sp
->typecnt
> TZ_MAX_TYPES
||
751 sp
->timecnt
< 0 || sp
->timecnt
> TZ_MAX_TIMES
||
752 sp
->charcnt
< 0 || sp
->charcnt
> TZ_MAX_CHARS
||
753 (ttisstdcnt
!= sp
->typecnt
&& ttisstdcnt
!= 0) ||
754 (ttisgmtcnt
!= sp
->typecnt
&& ttisgmtcnt
!= 0))
756 if (nread
- (p
- u
->buf
) <
757 sp
->timecnt
* stored
+ /* ats */
758 sp
->timecnt
+ /* types */
759 sp
->typecnt
* 6 + /* ttinfos */
760 sp
->charcnt
+ /* chars */
761 sp
->leapcnt
* (stored
+ 4) + /* lsinfos */
762 ttisstdcnt
+ /* ttisstds */
763 ttisgmtcnt
) /* ttisgmts */
765 for (i
= 0; i
< sp
->timecnt
; ++i
) {
766 sp
->ats
[i
] = (stored
== 4) ?
767 detzcode(p
) : detzcode64(p
);
770 for (i
= 0; i
< sp
->timecnt
; ++i
) {
771 sp
->types
[i
] = (unsigned char) *p
++;
772 if (sp
->types
[i
] >= sp
->typecnt
)
775 for (i
= 0; i
< sp
->typecnt
; ++i
) {
776 struct ttinfo
* ttisp
;
778 ttisp
= &sp
->ttis
[i
];
779 ttisp
->tt_gmtoff
= detzcode(p
);
781 ttisp
->tt_isdst
= (unsigned char) *p
++;
782 if (ttisp
->tt_isdst
!= 0 && ttisp
->tt_isdst
!= 1)
784 ttisp
->tt_abbrind
= (unsigned char) *p
++;
785 if (ttisp
->tt_abbrind
< 0 ||
786 ttisp
->tt_abbrind
> sp
->charcnt
)
789 for (i
= 0; i
< sp
->charcnt
; ++i
)
791 sp
->chars
[i
] = '\0'; /* ensure '\0' at end */
792 for (i
= 0; i
< sp
->leapcnt
; ++i
) {
793 struct lsinfo
* lsisp
;
795 lsisp
= &sp
->lsis
[i
];
796 lsisp
->ls_trans
= (stored
== 4) ?
797 detzcode(p
) : detzcode64(p
);
799 lsisp
->ls_corr
= detzcode(p
);
802 for (i
= 0; i
< sp
->typecnt
; ++i
) {
803 struct ttinfo
* ttisp
;
805 ttisp
= &sp
->ttis
[i
];
807 ttisp
->tt_ttisstd
= FALSE
;
809 ttisp
->tt_ttisstd
= *p
++;
810 if (ttisp
->tt_ttisstd
!= TRUE
&&
811 ttisp
->tt_ttisstd
!= FALSE
)
815 for (i
= 0; i
< sp
->typecnt
; ++i
) {
816 struct ttinfo
* ttisp
;
818 ttisp
= &sp
->ttis
[i
];
820 ttisp
->tt_ttisgmt
= FALSE
;
822 ttisp
->tt_ttisgmt
= *p
++;
823 if (ttisp
->tt_ttisgmt
!= TRUE
&&
824 ttisp
->tt_ttisgmt
!= FALSE
)
829 ** Out-of-sort ats should mean we're running on a
830 ** signed time_t system but using a data file with
831 ** unsigned values (or vice versa).
833 for (i
= 0; i
< sp
->timecnt
- 2; ++i
)
834 if (sp
->ats
[i
] > sp
->ats
[i
+ 1]) {
836 if (TYPE_SIGNED(time_t)) {
838 ** Ignore the end (easy).
843 ** Ignore the beginning (harder).
847 for (j
= 0; j
+ i
< sp
->timecnt
; ++j
) {
848 sp
->ats
[j
] = sp
->ats
[j
+ i
];
849 sp
->types
[j
] = sp
->types
[j
+ i
];
856 ** If this is an old file, we're done.
858 if (u
->tzhead
.tzh_version
[0] == '\0')
861 for (i
= 0; i
< nread
; ++i
)
864 ** If this is a narrow integer time_t system, we're done.
866 if (stored
>= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
869 if (doextend
&& nread
> 2 &&
870 u
->buf
[0] == '\n' && u
->buf
[nread
- 1] == '\n' &&
871 sp
->typecnt
+ 2 <= TZ_MAX_TYPES
) {
875 ts
= malloc(sizeof(*ts
));
878 u
->buf
[nread
- 1] = '\0';
879 result
= tzparse(&u
->buf
[1], ts
, FALSE
);
880 if (result
== 0 && ts
->typecnt
== 2 &&
881 sp
->charcnt
+ ts
->charcnt
<= TZ_MAX_CHARS
) {
882 for (i
= 0; i
< 2; ++i
)
883 ts
->ttis
[i
].tt_abbrind
+=
885 for (i
= 0; i
< ts
->charcnt
; ++i
)
886 sp
->chars
[sp
->charcnt
++] =
889 while (i
< ts
->timecnt
&&
891 sp
->ats
[sp
->timecnt
- 1])
893 while (i
< ts
->timecnt
&&
894 sp
->timecnt
< TZ_MAX_TIMES
) {
895 sp
->ats
[sp
->timecnt
] =
897 sp
->types
[sp
->timecnt
] =
903 sp
->ttis
[sp
->typecnt
++] = ts
->ttis
[0];
904 sp
->ttis
[sp
->typecnt
++] = ts
->ttis
[1];
908 if (sp
->timecnt
> 1) {
909 for (i
= 1; i
< sp
->timecnt
; ++i
)
910 if (typesequiv(sp
, sp
->types
[i
], sp
->types
[0]) &&
911 differ_by_repeat(sp
->ats
[i
], sp
->ats
[0])) {
915 for (i
= sp
->timecnt
- 2; i
>= 0; --i
)
916 if (typesequiv(sp
, sp
->types
[sp
->timecnt
- 1],
918 differ_by_repeat(sp
->ats
[sp
->timecnt
- 1],
932 const struct state
* const sp
;
939 a
< 0 || a
>= sp
->typecnt
||
940 b
< 0 || b
>= sp
->typecnt
)
943 register const struct ttinfo
* ap
= &sp
->ttis
[a
];
944 register const struct ttinfo
* bp
= &sp
->ttis
[b
];
945 result
= ap
->tt_gmtoff
== bp
->tt_gmtoff
&&
946 ap
->tt_isdst
== bp
->tt_isdst
&&
947 ap
->tt_ttisstd
== bp
->tt_ttisstd
&&
948 ap
->tt_ttisgmt
== bp
->tt_ttisgmt
&&
949 strcmp(&sp
->chars
[ap
->tt_abbrind
],
950 &sp
->chars
[bp
->tt_abbrind
]) == 0;
955 static const int mon_lengths
[2][MONSPERYEAR
] = {
956 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
957 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
960 static const int year_lengths
[2] = {
961 DAYSPERNYEAR
, DAYSPERLYEAR
965 ** Given a pointer into a time zone string, scan until a character that is not
966 ** a valid character in a zone name is found. Return a pointer to that
971 getzname(strp
, name
, len
)
979 if (*strp
== '<' && (ket
= strchr(strp
, '>')) != NULL
) {
980 *name
= (char *)(strp
+ 1);
981 *len
= ket
- strp
- 1;
984 *name
= (char *)strp
;
985 while ((c
= *strp
) != '\0' && !is_digit(c
) && c
!= ',' && c
!= '-' &&
993 ** Given a pointer into an extended time zone string, scan until the ending
994 ** delimiter of the zone name is located. Return a pointer to the delimiter.
996 ** As with getzname above, the legal character set is actually quite
997 ** restricted, with other characters producing undefined results.
998 ** We don't do any checking here; checking is done later in common-case code.
1002 getqzname(register const char *strp
, const int delim
)
1006 while ((c
= *strp
) != '\0' && c
!= delim
)
1012 ** Given a pointer into a time zone string, extract a number from that string.
1013 ** Check that the number is within a specified range; if it is not, return
1015 ** Otherwise, return a pointer to the first character not part of the number.
1019 getnum(strp
, nump
, min
, max
)
1028 if (strp
== NULL
|| !is_digit(c
= *strp
))
1032 num
= num
* 10 + (c
- '0');
1034 return NULL
; /* illegal value */
1036 } while (is_digit(c
));
1038 return NULL
; /* illegal value */
1044 ** Given a pointer into a time zone string, extract a number of seconds,
1045 ** in hh[:mm[:ss]] form, from the string.
1046 ** If any error occurs, return NULL.
1047 ** Otherwise, return a pointer to the first character not part of the number
1052 getsecs(strp
, secsp
)
1059 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
1060 ** "M10.4.6/26", which does not conform to Posix,
1061 ** but which specifies the equivalent of
1062 ** ``02:00 on the first Sunday on or after 23 Oct''.
1064 strp
= getnum(strp
, &num
, 0, HOURSPERDAY
* DAYSPERWEEK
- 1);
1067 *secsp
= num
* (long) SECSPERHOUR
;
1070 strp
= getnum(strp
, &num
, 0, MINSPERHOUR
- 1);
1073 *secsp
+= num
* SECSPERMIN
;
1076 /* `SECSPERMIN' allows for leap seconds. */
1077 strp
= getnum(strp
, &num
, 0, SECSPERMIN
);
1087 ** Given a pointer into a time zone string, extract an offset, in
1088 ** [+-]hh[:mm[:ss]] form, from the string.
1089 ** If any error occurs, return NULL.
1090 ** Otherwise, return a pointer to the first character not part of the time.
1094 getoffset(strp
, offsetp
)
1096 long * const offsetp
;
1103 } else if (*strp
== '+')
1105 strp
= getsecs(strp
, offsetp
);
1107 return NULL
; /* illegal time */
1109 *offsetp
= -*offsetp
;
1114 ** Given a pointer into a time zone string, extract a rule in the form
1115 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
1116 ** If a valid rule is not found, return NULL.
1117 ** Otherwise, return a pointer to the first character not part of the rule.
1121 getrule(strp
, rulep
)
1123 struct rule
* const rulep
;
1129 rulep
->r_type
= JULIAN_DAY
;
1131 strp
= getnum(strp
, &rulep
->r_day
, 1, DAYSPERNYEAR
);
1132 } else if (*strp
== 'M') {
1134 ** Month, week, day.
1136 rulep
->r_type
= MONTH_NTH_DAY_OF_WEEK
;
1138 strp
= getnum(strp
, &rulep
->r_mon
, 1, MONSPERYEAR
);
1143 strp
= getnum(strp
, &rulep
->r_week
, 1, 5);
1148 strp
= getnum(strp
, &rulep
->r_day
, 0, DAYSPERWEEK
- 1);
1149 } else if (is_digit(*strp
)) {
1153 rulep
->r_type
= DAY_OF_YEAR
;
1154 strp
= getnum(strp
, &rulep
->r_day
, 0, DAYSPERLYEAR
- 1);
1155 } else return NULL
; /* invalid format */
1163 strp
= getsecs(strp
, &rulep
->r_time
);
1164 } else rulep
->r_time
= 2 * SECSPERHOUR
; /* default = 2:00:00 */
1169 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
1170 ** year, a rule, and the offset from UTC at the time that rule takes effect,
1171 ** calculate the Epoch-relative time that rule takes effect.
1175 transtime(janfirst
, year
, rulep
, offset
)
1176 const time_t janfirst
;
1178 const struct rule
* const rulep
;
1184 int d
, m1
, yy0
, yy1
, yy2
, dow
;
1187 leapyear
= isleap(year
);
1188 switch (rulep
->r_type
) {
1192 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
1194 ** In non-leap years, or if the day number is 59 or less, just
1195 ** add SECSPERDAY times the day number-1 to the time of
1196 ** January 1, midnight, to get the day.
1198 value
= janfirst
+ (rulep
->r_day
- 1) * SECSPERDAY
;
1199 if (leapyear
&& rulep
->r_day
>= 60)
1200 value
+= SECSPERDAY
;
1206 ** Just add SECSPERDAY times the day number to the time of
1207 ** January 1, midnight, to get the day.
1209 value
= janfirst
+ rulep
->r_day
* SECSPERDAY
;
1212 case MONTH_NTH_DAY_OF_WEEK
:
1214 ** Mm.n.d - nth "dth day" of month m.
1217 for (i
= 0; i
< rulep
->r_mon
- 1; ++i
)
1218 value
+= mon_lengths
[leapyear
][i
] * SECSPERDAY
;
1221 ** Use Zeller's Congruence to get day-of-week of first day of
1224 m1
= (rulep
->r_mon
+ 9) % 12 + 1;
1225 yy0
= (rulep
->r_mon
<= 2) ? (year
- 1) : year
;
1228 dow
= ((26 * m1
- 2) / 10 +
1229 1 + yy2
+ yy2
/ 4 + yy1
/ 4 - 2 * yy1
) % 7;
1234 ** "dow" is the day-of-week of the first day of the month. Get
1235 ** the day-of-month (zero-origin) of the first "dow" day of the
1238 d
= rulep
->r_day
- dow
;
1241 for (i
= 1; i
< rulep
->r_week
; ++i
) {
1242 if (d
+ DAYSPERWEEK
>=
1243 mon_lengths
[leapyear
][rulep
->r_mon
- 1])
1249 ** "d" is the day-of-month (zero-origin) of the day we want.
1251 value
+= d
* SECSPERDAY
;
1256 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
1257 ** question. To get the Epoch-relative time of the specified local
1258 ** time on that day, add the transition time and the current offset
1261 return value
+ rulep
->r_time
+ offset
;
1265 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
1270 tzparse(name
, sp
, lastditch
)
1272 struct state
* const sp
;
1273 const int lastditch
;
1275 const char * stdname
;
1276 const char * dstname
;
1282 unsigned char * typep
;
1286 INITIALIZE(dstname
);
1289 stdlen
= strlen(name
); /* length of standard zone name */
1291 if (stdlen
>= sizeof sp
->chars
)
1292 stdlen
= (sizeof sp
->chars
) - 1;
1295 name
= getzname(name
, (char **)&stdname
, &stdlen
);
1297 return -1; /* was "stdoffset = 0;" */
1299 name
= getoffset(name
, &stdoffset
);
1305 load_result
= tzload(TZDEFRULES
, sp
, NULL
, FALSE
);
1306 #else /* !NOTIFY_TZ */
1307 load_result
= tzload(TZDEFRULES
, sp
, FALSE
);
1308 #endif /* NOTIFY_TZ */
1309 if (load_result
!= 0)
1310 sp
->leapcnt
= 0; /* so, we're off a little */
1311 if (*name
!= '\0') {
1314 name
= getqzname(name
, '>');
1317 dstlen
= name
- dstname
;
1321 name
= getzname(name
, (char **)&dstname
, &dstlen
);
1323 if (*name
!= '\0' && *name
!= ',' && *name
!= ';') {
1324 name
= getoffset(name
, &dstoffset
);
1327 } else dstoffset
= stdoffset
- SECSPERHOUR
;
1328 if (*name
== '\0' && load_result
!= 0)
1329 name
= TZDEFRULESTRING
;
1330 if (*name
== ',' || *name
== ';') {
1339 if ((name
= getrule(name
, &start
)) == NULL
)
1343 if ((name
= getrule(name
, &end
)) == NULL
)
1347 sp
->typecnt
= 2; /* standard time and DST */
1349 ** Two transitions per year, from EPOCH_YEAR forward.
1351 sp
->ttis
[0].tt_gmtoff
= -dstoffset
;
1352 sp
->ttis
[0].tt_isdst
= 1;
1353 sp
->ttis
[0].tt_abbrind
= stdlen
+ 1;
1354 sp
->ttis
[1].tt_gmtoff
= -stdoffset
;
1355 sp
->ttis
[1].tt_isdst
= 0;
1356 sp
->ttis
[1].tt_abbrind
= 0;
1361 for (year
= EPOCH_YEAR
;
1362 sp
->timecnt
+ 2 <= TZ_MAX_TIMES
;
1366 starttime
= transtime(janfirst
, year
, &start
,
1368 endtime
= transtime(janfirst
, year
, &end
,
1370 if (starttime
> endtime
) {
1372 *typep
++ = 1; /* DST ends */
1374 *typep
++ = 0; /* DST begins */
1377 *typep
++ = 0; /* DST begins */
1379 *typep
++ = 1; /* DST ends */
1382 newfirst
= janfirst
;
1383 newfirst
+= year_lengths
[isleap(year
)] *
1385 if (newfirst
<= janfirst
)
1387 janfirst
= newfirst
;
1390 long theirstdoffset
;
1391 long theirdstoffset
;
1400 ** Initial values of theirstdoffset and theirdstoffset.
1403 for (i
= 0; i
< sp
->timecnt
; ++i
) {
1405 if (!sp
->ttis
[j
].tt_isdst
) {
1407 -sp
->ttis
[j
].tt_gmtoff
;
1412 for (i
= 0; i
< sp
->timecnt
; ++i
) {
1414 if (sp
->ttis
[j
].tt_isdst
) {
1416 -sp
->ttis
[j
].tt_gmtoff
;
1421 ** Initially we're assumed to be in standard time.
1424 theiroffset
= theirstdoffset
;
1426 ** Now juggle transition times and types
1427 ** tracking offsets as you do.
1429 for (i
= 0; i
< sp
->timecnt
; ++i
) {
1431 sp
->types
[i
] = sp
->ttis
[j
].tt_isdst
;
1432 if (sp
->ttis
[j
].tt_ttisgmt
) {
1433 /* No adjustment to transition time */
1436 ** If summer time is in effect, and the
1437 ** transition time was not specified as
1438 ** standard time, add the summer time
1439 ** offset to the transition time;
1440 ** otherwise, add the standard time
1441 ** offset to the transition time.
1444 ** Transitions from DST to DDST
1445 ** will effectively disappear since
1446 ** POSIX provides for only one DST
1449 if (isdst
&& !sp
->ttis
[j
].tt_ttisstd
) {
1450 sp
->ats
[i
] += dstoffset
-
1453 sp
->ats
[i
] += stdoffset
-
1457 theiroffset
= -sp
->ttis
[j
].tt_gmtoff
;
1458 if (sp
->ttis
[j
].tt_isdst
)
1459 theirdstoffset
= theiroffset
;
1460 else theirstdoffset
= theiroffset
;
1463 ** Finally, fill in ttis.
1464 ** ttisstd and ttisgmt need not be handled.
1466 sp
->ttis
[0].tt_gmtoff
= -stdoffset
;
1467 sp
->ttis
[0].tt_isdst
= FALSE
;
1468 sp
->ttis
[0].tt_abbrind
= 0;
1469 sp
->ttis
[1].tt_gmtoff
= -dstoffset
;
1470 sp
->ttis
[1].tt_isdst
= TRUE
;
1471 sp
->ttis
[1].tt_abbrind
= stdlen
+ 1;
1476 sp
->typecnt
= 1; /* only standard time */
1478 sp
->ttis
[0].tt_gmtoff
= -stdoffset
;
1479 sp
->ttis
[0].tt_isdst
= 0;
1480 sp
->ttis
[0].tt_abbrind
= 0;
1482 sp
->charcnt
= stdlen
+ 1;
1484 sp
->charcnt
+= dstlen
+ 1;
1485 if ((size_t) sp
->charcnt
> sizeof sp
->chars
)
1488 (void) strncpy(cp
, stdname
, stdlen
);
1492 (void) strncpy(cp
, dstname
, dstlen
);
1493 *(cp
+ dstlen
) = '\0';
1501 #else /* ! NOTIFY_TZ */
1503 #endif /* NOTIFY_TZ */
1504 struct state
* const sp
;
1507 #endif /* NOTIFY_TZ */
1510 if (tzload(gmt
, sp
, path
, TRUE
) != 0)
1511 #else /* ! NOTIFY_TZ */
1512 if (tzload(gmt
, sp
, TRUE
) != 0)
1513 #endif /* NOTIFY_TZ */
1514 (void) tzparse(gmt
, sp
, TRUE
);
1518 tzsetwall_basic(int rdlocked
)
1521 notify_check_tz(&lcl_notify
);
1524 static struct timespec last_mtimespec
= {0, 0};
1525 struct stat statbuf
;
1527 if (lstat(TZDEFAULT
, &statbuf
) == 0) {
1528 if (statbuf
.st_mtimespec
.tv_sec
> last_mtimespec
.tv_sec
||
1529 (statbuf
.st_mtimespec
.tv_sec
== last_mtimespec
.tv_sec
&&
1530 statbuf
.st_mtimespec
.tv_nsec
> last_mtimespec
.tv_nsec
)) {
1531 /* Trigger resetting the local TZ */
1534 last_mtimespec
= statbuf
.st_mtimespec
;
1537 #endif /* NOTIFY_TZ */
1539 _RWLOCK_RDLOCK(&lcl_rwlock
);
1540 if (lcl_is_set
< 0) {
1541 #ifdef NOTIFY_TZ_DEBUG
1542 NOTIFY_TZ_PRINTF("tzsetwall_basic lcl_is_set < 0\n");
1545 _RWLOCK_UNLOCK(&lcl_rwlock
);
1548 #ifdef NOTIFY_TZ_DEBUG
1549 NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
1551 _RWLOCK_UNLOCK(&lcl_rwlock
);
1553 _RWLOCK_WRLOCK(&lcl_rwlock
);
1557 if (lclptr
== NULL
) {
1558 lclptr
= calloc(1, sizeof *lclptr
);
1559 if (lclptr
== NULL
) {
1560 settzname(); /* all we can do */
1561 _RWLOCK_UNLOCK(&lcl_rwlock
);
1563 _RWLOCK_RDLOCK(&lcl_rwlock
);
1567 #endif /* defined ALL_STATE */
1570 char fullname
[FILENAME_MAX
+ 1];
1571 if (tzload((char *) NULL
, lclptr
, fullname
, TRUE
) != 0)
1573 * If fullname is empty (an error occurred) then
1574 * default to the UTC path
1576 gmtload(lclptr
, *fullname
? NULL
: fullname
);
1577 notify_register_tz(fullname
, &lcl_notify
);
1579 #else /* ! NOTIFY_TZ */
1580 if (tzload((char *) NULL
, lclptr
, TRUE
) != 0)
1582 #endif /* NOTIFY_TZ */
1584 _RWLOCK_UNLOCK(&lcl_rwlock
);
1587 _RWLOCK_RDLOCK(&lcl_rwlock
);
1593 #ifdef NOTIFY_TZ_DEBUG
1594 NOTIFY_TZ_PRINTF("tzsetwall called\n");
1595 #endif /* NOTIFY_TZ_DEBUG */
1599 __private_extern__
void
1600 tzset_basic(int rdlocked
)
1604 name
= getenv("TZ");
1606 tzsetwall_basic(rdlocked
);
1611 notify_check_tz(&lcl_notify
);
1612 #endif /* NOTIFY_TZ */
1614 _RWLOCK_RDLOCK(&lcl_rwlock
);
1615 if (lcl_is_set
> 0 && strcmp(lcl_TZname
, name
) == 0) {
1617 _RWLOCK_UNLOCK(&lcl_rwlock
);
1618 #ifdef NOTIFY_TZ_DEBUG
1619 NOTIFY_TZ_PRINTF("tzset_basic matched %s\n", lcl_TZname
);
1623 _RWLOCK_UNLOCK(&lcl_rwlock
);
1625 _RWLOCK_WRLOCK(&lcl_rwlock
);
1626 lcl_is_set
= strlen(name
) < sizeof lcl_TZname
;
1628 (void) strcpy(lcl_TZname
, name
);
1631 if (lclptr
== NULL
) {
1632 lclptr
= (struct state
*) calloc(1, sizeof *lclptr
);
1633 if (lclptr
== NULL
) {
1634 settzname(); /* all we can do */
1635 _RWLOCK_UNLOCK(&lcl_rwlock
);
1637 _RWLOCK_RDLOCK(&lcl_rwlock
);
1641 #endif /* defined ALL_STATE */
1642 if (*name
== '\0') {
1644 ** User wants it fast rather than right.
1646 lclptr
->leapcnt
= 0; /* so, we're off a little */
1647 lclptr
->timecnt
= 0;
1648 lclptr
->typecnt
= 0;
1649 lclptr
->ttis
[0].tt_isdst
= 0;
1650 lclptr
->ttis
[0].tt_gmtoff
= 0;
1651 lclptr
->ttis
[0].tt_abbrind
= 0;
1652 (void) strcpy(lclptr
->chars
, gmt
);
1654 notify_register_tz(NULL
, &lcl_notify
);
1655 #endif /* NOTIFY_TZ */
1659 char fullname
[FILENAME_MAX
+ 1];
1661 * parsedOK indicates whether tzparse() was called and
1662 * succeeded. This means that TZ is a time conversion
1663 * specification, so we don't need to register for
1666 int parsedOK
= FALSE
;
1667 if (tzload(name
, lclptr
, fullname
, TRUE
) != 0)
1668 if (name
[0] == ':' || !(parsedOK
= tzparse(name
, lclptr
, FALSE
) == 0))
1670 * If fullname is empty (an error occurred) then
1671 * default to the UTC path
1673 (void) gmtload(lclptr
, *fullname
? NULL
: fullname
);
1674 notify_register_tz(parsedOK
? NULL
: fullname
, &lcl_notify
);
1676 #else /* ! NOTIFY_TZ */
1677 if (tzload(name
, lclptr
, TRUE
) != 0)
1678 if (name
[0] == ':' || tzparse(name
, lclptr
, FALSE
) != 0)
1679 (void) gmtload(lclptr
);
1680 #endif /* NOTIFY_TZ */
1682 _RWLOCK_UNLOCK(&lcl_rwlock
);
1685 _RWLOCK_RDLOCK(&lcl_rwlock
);
1691 #ifdef NOTIFY_TZ_DEBUG
1692 NOTIFY_TZ_PRINTF("tzset called TZ=%s\n", getenv("TZ"));
1693 #endif /* NOTIFY_TZ_DEBUG */
1698 ** The easy way to behave "as if no library function calls" localtime
1699 ** is to not call it--so we drop its guts into "localsub", which can be
1700 ** freely called. (And no, the PANS doesn't require the above behavior--
1701 ** but it *is* desirable.)
1703 ** The unused offset argument is for the benefit of mktime variants.
1708 __private_extern__
struct tm
*
1709 #else /* !__LP64__ */
1710 __private_extern__
void
1711 #endif /* __LP64__ */
1712 localsub(const time_t *const timep
, const long offset
, struct tm
*const tmp
)
1715 const struct ttinfo
* ttisp
;
1719 #endif /* __LP64__ */
1720 const time_t t
= *timep
;
1722 #ifdef NOTIFY_TZ_DEBUG
1723 NOTIFY_TZ_PRINTF("localsub called\n");
1724 #endif /* NOTIFY_TZ_DEBUG */
1729 return gmtsub(timep
, offset
, tmp
);
1730 #else /* !__LP64__ */
1731 gmtsub(timep
, offset
, tmp
);
1733 #endif /* __LP64__ */
1735 #endif /* defined ALL_STATE */
1736 if ((sp
->goback
&& t
< sp
->ats
[0]) ||
1737 (sp
->goahead
&& t
> sp
->ats
[sp
->timecnt
- 1])) {
1739 register time_t seconds
;
1740 register time_t tcycles
;
1741 register int_fast64_t icycles
;
1744 seconds
= sp
->ats
[0] - t
;
1745 else seconds
= t
- sp
->ats
[sp
->timecnt
- 1];
1747 tcycles
= seconds
/ YEARSPERREPEAT
/ AVGSECSPERYEAR
;
1750 if (tcycles
- icycles
>= 1 || icycles
- tcycles
>= 1) {
1753 #else /* !__LP64__ */
1755 #endif /* __LP64__ */
1758 seconds
*= YEARSPERREPEAT
;
1759 seconds
*= AVGSECSPERYEAR
;
1762 else newt
-= seconds
;
1763 if (newt
< sp
->ats
[0] ||
1764 newt
> sp
->ats
[sp
->timecnt
- 1])
1766 return NULL
; /* "cannot happen" */
1767 result
= localsub(&newt
, offset
, tmp
);
1768 if (result
== tmp
) {
1769 #else /* !__LP64__ */
1771 localsub(&newt
, offset
, tmp
);
1773 #endif /* __LP64__ */
1774 register time_t newy
;
1776 newy
= tmp
->tm_year
;
1778 newy
-= icycles
* YEARSPERREPEAT
;
1779 else newy
+= icycles
* YEARSPERREPEAT
;
1780 tmp
->tm_year
= newy
;
1781 if (tmp
->tm_year
!= newy
)
1786 #else /* !__LP64__ */
1790 #endif /* __LP64__ */
1792 if (sp
->timecnt
== 0 || t
< sp
->ats
[0]) {
1794 while (sp
->ttis
[i
].tt_isdst
)
1795 if (++i
>= sp
->typecnt
) {
1800 register int lo
= 1;
1801 register int hi
= sp
->timecnt
;
1804 register int mid
= (lo
+ hi
) >> 1;
1806 if (t
< sp
->ats
[mid
])
1810 i
= (int) sp
->types
[lo
- 1];
1812 ttisp
= &sp
->ttis
[i
];
1814 ** To get (wrong) behavior that's compatible with System V Release 2.0
1815 ** you'd replace the statement below with
1816 ** t += ttisp->tt_gmtoff;
1817 ** timesub(&t, 0L, sp, tmp);
1820 result
= timesub(&t
, ttisp
->tt_gmtoff
, sp
, tmp
);
1823 #else /* !__LP64__ */
1824 timesub(&t
, ttisp
->tt_gmtoff
, sp
, tmp
);
1825 #endif /* __LP64__ */
1826 tmp
->tm_isdst
= ttisp
->tt_isdst
;
1827 tzname
[tmp
->tm_isdst
] = &sp
->chars
[ttisp
->tt_abbrind
];
1829 tmp
->TM_ZONE
= &sp
->chars
[ttisp
->tt_abbrind
];
1830 #endif /* defined TM_ZONE */
1833 #endif /* __LP64__ */
1837 localtime_key_init(void)
1840 localtime_key
= __LIBC_PTHREAD_KEY_LOCALTIME
;
1841 localtime_key_error
= pthread_key_init_np(localtime_key
, free
);
1845 localtime(const time_t *const timep
)
1849 if (__isthreaded
!= 0) {
1850 _pthread_once(&localtime_once
, localtime_key_init
);
1851 if (localtime_key_error
!= 0) {
1852 errno
= localtime_key_error
;
1855 p_tm
= _pthread_getspecific(localtime_key
);
1857 if ((p_tm
= (struct tm
*)malloc(sizeof(struct tm
)))
1860 _pthread_setspecific(localtime_key
, p_tm
);
1862 _RWLOCK_RDLOCK(&lcl_rwlock
);
1865 p_tm
= localsub(timep
, 0L, p_tm
);
1866 #else /* !__LP64__ */
1867 localsub(timep
, 0L, p_tm
);
1868 #endif /* __LP64__ */
1869 _RWLOCK_UNLOCK(&lcl_rwlock
);
1874 return localsub(timep
, 0L, &tm
);
1875 #else /* !__LP64__ */
1876 localsub(timep
, 0L, &tm
);
1878 #endif /* __LP64__ */
1883 ** Re-entrant version of localtime.
1887 localtime_r(const time_t *const __restrict timep
, struct tm
* __restrict tmp
)
1889 _RWLOCK_RDLOCK(&lcl_rwlock
);
1892 tmp
= localsub(timep
, 0L, tmp
);
1893 #else /* !__LP64__ */
1894 localsub(timep
, 0L, tmp
);
1895 #endif /* __LP64__ */
1896 _RWLOCK_UNLOCK(&lcl_rwlock
);
1907 #endif /* NOTIFY_TZ */
1908 gmtptr
= (struct state
*) calloc(1, sizeof *gmtptr
);
1910 #endif /* defined ALL_STATE */
1913 char fullname
[FILENAME_MAX
+ 1];
1914 gmtload(gmtptr
, fullname
);
1915 notify_register_tz(fullname
, &gmt_notify
);
1917 #else /* ! NOTIFY_TZ */
1919 #endif /* NOTIFY_TZ */
1923 ** gmtsub is to gmtime as localsub is to localtime.
1928 #else /* !__LP64__ */
1930 #endif /* __LP64__ */
1931 gmtsub(timep
, offset
, tmp
)
1932 const time_t * const timep
;
1934 struct tm
* const tmp
;
1937 register struct tm
* result
;
1938 #endif /* __LP64__ */
1940 #ifdef NOTIFY_TZ_DEBUG
1941 NOTIFY_TZ_PRINTF("gmtsub called\n");
1942 #endif /* NOTIFY_TZ_DEBUG */
1944 notify_check_tz(&gmt_notify
);
1945 #endif /* NOTIFY_TZ */
1946 pthread_once(&gmt_once
, gmt_init
);
1948 result
= timesub(timep
, offset
, gmtptr
, tmp
);
1951 #else /* !__LP64__ */
1952 timesub(timep
, offset
, gmtptr
, tmp
);
1953 #endif /* __LP64__ */
1956 ** Could get fancy here and deliver something such as
1957 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1958 ** but this is no time for a treasure hunt.
1961 tmp
->TM_ZONE
= (char*)wildabbr
;
1965 tmp
->TM_ZONE
= (char *)gmt
;
1966 else tmp
->TM_ZONE
= gmtptr
->chars
;
1967 #endif /* defined ALL_STATE */
1969 tmp
->TM_ZONE
= gmtptr
->chars
;
1970 #endif /* State Farm */
1972 #endif /* defined TM_ZONE */
1975 #endif /* __LP64__ */
1979 gmtime_key_init(void)
1982 gmtime_key
= __LIBC_PTHREAD_KEY_GMTIME
;
1983 gmtime_key_error
= pthread_key_init_np(gmtime_key
, free
);
1987 gmtime(const time_t *const timep
)
1991 if (__isthreaded
!= 0) {
1992 _pthread_once(&gmtime_once
, gmtime_key_init
);
1993 if (gmtime_key_error
!= 0) {
1994 errno
= gmtime_key_error
;
1998 * Changed to follow POSIX.1 threads standard, which
1999 * is what BSD currently has.
2001 if ((p_tm
= _pthread_getspecific(gmtime_key
)) == NULL
) {
2002 if ((p_tm
= (struct tm
*)malloc(sizeof(struct tm
)))
2006 _pthread_setspecific(gmtime_key
, p_tm
);
2009 return gmtsub(timep
, 0L, p_tm
);
2010 #else /* !__LP64__ */
2011 gmtsub(timep
, 0L, p_tm
);
2013 #endif /* __LP64__ */
2017 return gmtsub(timep
, 0L, &tm
);
2018 #else /* !__LP64__ */
2019 gmtsub(timep
, 0L, &tm
);
2021 #endif /* __LP64__ */
2026 * Re-entrant version of gmtime.
2030 gmtime_r(const time_t *const timep
, struct tm
*tmp
)
2034 return gmtsub(timep
, 0L, tmp
);
2035 #else /* !__LP64__ */
2036 gmtsub(timep
, 0L, tmp
);
2038 #endif /* __LP64__ */
2044 offtime(const time_t *const timep
, const long offset
)
2047 return gmtsub(timep
, offset
, &tm
);
2048 #else /* !__LP64__ */
2049 gmtsub(timep
, offset
, &tm
);
2051 #endif /* __LP64__ */
2054 #endif /* defined STD_INSPIRED */
2057 ** Return the number of leap years through the end of the given year
2058 ** where, to make the math easy, the answer for year zero is defined as zero.
2062 leaps_thru_end_of(y
)
2063 register const int y
;
2066 return (y
>= 0) ? (y
/ 4 - y
/ 100 + y
/ 400) :
2067 -(leaps_thru_end_of(-(y
+ 1)) + 1);
2068 #else /* !__LP64__ */
2069 return (y
/ 4 - y
/ 100 + y
/ 400);
2070 #endif /* __LP64__ */
2075 #else /* !__LP64__ */
2077 #endif /* __LP64__ */
2078 timesub(timep
, offset
, sp
, tmp
)
2079 const time_t * const timep
;
2081 const struct state
* const sp
;
2082 struct tm
* const tmp
;
2084 const struct lsinfo
* lp
;
2097 i
= (sp
== NULL
) ? 0 : sp
->leapcnt
;
2098 #endif /* defined ALL_STATE */
2101 #endif /* State Farm */
2104 if (*timep
>= lp
->ls_trans
) {
2105 if (*timep
== lp
->ls_trans
) {
2106 hit
= ((i
== 0 && lp
->ls_corr
> 0) ||
2107 lp
->ls_corr
> sp
->lsis
[i
- 1].ls_corr
);
2110 sp
->lsis
[i
].ls_trans
==
2111 sp
->lsis
[i
- 1].ls_trans
+ 1 &&
2112 sp
->lsis
[i
].ls_corr
==
2113 sp
->lsis
[i
- 1].ls_corr
+ 1) {
2122 days
= *timep
/ SECSPERDAY
;
2123 rem
= *timep
% SECSPERDAY
;
2125 if (*timep
== 0x80000000) {
2127 ** A 3B1 muffs the division on the most negative number.
2132 #endif /* defined mc68k */
2133 rem
+= (offset
- corr
);
2138 while (rem
>= SECSPERDAY
) {
2142 tmp
->tm_hour
= (int) (rem
/ SECSPERHOUR
);
2143 rem
= rem
% SECSPERHOUR
;
2144 tmp
->tm_min
= (int) (rem
/ SECSPERMIN
);
2146 ** A positive leap second requires a special
2147 ** representation. This uses "... ??:59:60" et seq.
2149 tmp
->tm_sec
= (int) (rem
% SECSPERMIN
) + hit
;
2150 tmp
->tm_wday
= (int) ((EPOCH_WDAY
+ days
) % DAYSPERWEEK
);
2151 if (tmp
->tm_wday
< 0)
2152 tmp
->tm_wday
+= DAYSPERWEEK
;
2154 #define _LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
2156 #define LEAPS_THRU_END_OF(y) ((y) >= 0 ? _LEAPS_THRU_END_OF(y) : _LEAPS_THRU_END_OF((y) + 1) - 1)
2157 #else /* !__LP64__ */
2158 #define LEAPS_THRU_END_OF(y) _LEAPS_THRU_END_OF(y)
2159 #endif /* __LP64__ */
2160 while (days
< 0 || days
>= (long) year_lengths
[yleap
= isleap(y
)]) {
2163 newy
= y
+ days
/ DAYSPERNYEAR
;
2166 days
-= (newy
- y
) * DAYSPERNYEAR
+
2167 LEAPS_THRU_END_OF(newy
- 1) -
2168 LEAPS_THRU_END_OF(y
- 1);
2173 if (y
< INT_MIN
|| y
> INT_MAX
) {
2178 #else /* !__LP64__ */
2179 tmp
->tm_year
= y
- TM_YEAR_BASE
;
2180 #endif /* __LP64__ */
2181 tmp
->tm_yday
= (int) days
;
2182 ip
= mon_lengths
[yleap
];
2183 for (tmp
->tm_mon
= 0; days
>= (long) ip
[tmp
->tm_mon
]; ++(tmp
->tm_mon
))
2184 days
= days
- (long) ip
[tmp
->tm_mon
];
2185 tmp
->tm_mday
= (int) (days
+ 1);
2188 tmp
->TM_GMTOFF
= offset
;
2189 #endif /* defined TM_GMTOFF */
2192 #endif /* __LP64__ */
2196 ctime(const time_t *const timep
)
2199 ** Section 4.12.3.2 of X3.159-1989 requires that
2200 ** The ctime function converts the calendar time pointed to by timer
2201 ** to local time in the form of a string. It is equivalent to
2202 ** asctime(localtime(timer))
2206 * In 64-bit, the timep value may produce a time value with a year
2207 * that exceeds 32-bits in size (won't fit in struct tm), so localtime
2210 struct tm
*tm
= localtime(timep
);
2215 #else /* !__LP64__ */
2216 return asctime(localtime(timep
));
2217 #endif /* __LP64__ */
2221 ctime_r(const time_t *const timep
, char *buf
)
2227 * In 64-bit, the timep value may produce a time value with a year
2228 * that exceeds 32-bits in size (won't fit in struct tm), so localtime_r
2231 if (localtime_r(timep
, &mytm
) == NULL
)
2233 return asctime_r(&mytm
, buf
);
2234 #else /* !__LP64__ */
2235 return asctime_r(localtime_r(timep
, &mytm
), buf
);
2236 #endif /* __LP64__ */
2240 ** Adapted from code provided by Robert Elz, who writes:
2241 ** The "best" way to do mktime I think is based on an idea of Bob
2242 ** Kridle's (so its said...) from a long time ago.
2243 ** It does a binary search of the time_t space. Since time_t's are
2244 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
2245 ** would still be very reasonable).
2250 #endif /* !defined WRONG */
2253 ** Simplified normalize logic courtesy Paul Eggert.
2257 increment_overflow(number
, delta
)
2265 return (*number
< number0
) != (delta
< 0);
2269 long_increment_overflow(number
, delta
)
2277 return (*number
< number0
) != (delta
< 0);
2281 normalize_overflow(int *const tensptr
, int *const unitsptr
, const int base
)
2285 tensdelta
= (*unitsptr
>= 0) ?
2286 (*unitsptr
/ base
) :
2287 (-1 - (-1 - *unitsptr
) / base
);
2288 *unitsptr
-= tensdelta
* base
;
2289 return increment_overflow(tensptr
, tensdelta
);
2293 long_normalize_overflow(long *const tensptr
, int *const unitsptr
, const int base
)
2295 register int tensdelta
;
2297 tensdelta
= (*unitsptr
>= 0) ?
2298 (*unitsptr
/ base
) :
2299 (-1 - (-1 - *unitsptr
) / base
);
2300 *unitsptr
-= tensdelta
* base
;
2301 return long_increment_overflow(tensptr
, tensdelta
);
2306 const struct tm
* const atmp
;
2307 const struct tm
* const btmp
;
2312 * Assume that atmp and btmp point to normalized tm strutures.
2313 * So only arithmetic with tm_year could overflow in 64-bit.
2315 if (atmp
->tm_year
!= btmp
->tm_year
) {
2316 return (atmp
->tm_year
> btmp
->tm_year
? 1 : -1);
2318 if ((result
= (atmp
->tm_mon
- btmp
->tm_mon
)) == 0 &&
2319 (result
= (atmp
->tm_mday
- btmp
->tm_mday
)) == 0 &&
2320 (result
= (atmp
->tm_hour
- btmp
->tm_hour
)) == 0 &&
2321 (result
= (atmp
->tm_min
- btmp
->tm_min
)) == 0)
2322 result
= atmp
->tm_sec
- btmp
->tm_sec
;
2327 time2sub(struct tm
*const tmp
,
2329 struct tm
*(*const funcp
)(const time_t *, long, struct tm
*),
2330 #else /* !__LP64__ */
2331 void(*funcp
) (const time_t *, long, struct tm
*),
2332 #endif /* __LP64__ */
2335 const int do_norm_secs
,
2338 const struct state
* sp
;
2348 struct tm yourtm
, mytm
;
2353 if (normalize_overflow(&yourtm
.tm_min
, &yourtm
.tm_sec
,
2357 if (normalize_overflow(&yourtm
.tm_hour
, &yourtm
.tm_min
, MINSPERHOUR
))
2359 if (normalize_overflow(&yourtm
.tm_mday
, &yourtm
.tm_hour
, HOURSPERDAY
))
2362 if (long_normalize_overflow(&y
, &yourtm
.tm_mon
, MONSPERYEAR
))
2365 ** Turn y into an actual year number for now.
2366 ** It is converted back to an offset from TM_YEAR_BASE later.
2368 if (long_increment_overflow(&y
, TM_YEAR_BASE
))
2370 while (yourtm
.tm_mday
<= 0) {
2371 if (long_increment_overflow(&y
, -1))
2373 li
= y
+ (1 < yourtm
.tm_mon
);
2374 yourtm
.tm_mday
+= year_lengths
[isleap(li
)];
2376 while (yourtm
.tm_mday
> DAYSPERLYEAR
) {
2377 li
= y
+ (1 < yourtm
.tm_mon
);
2378 yourtm
.tm_mday
-= year_lengths
[isleap(li
)];
2379 if (long_increment_overflow(&y
, 1))
2383 i
= mon_lengths
[isleap(y
)][yourtm
.tm_mon
];
2384 if (yourtm
.tm_mday
<= i
)
2386 yourtm
.tm_mday
-= i
;
2387 if (++yourtm
.tm_mon
>= MONSPERYEAR
) {
2389 if (long_increment_overflow(&y
, 1))
2393 if (long_increment_overflow(&y
, -TM_YEAR_BASE
))
2396 if (yourtm
.tm_year
!= y
)
2398 /* Don't go below 1900 for POLA */
2399 if (yourtm
.tm_year
< 0)
2401 if (yourtm
.tm_sec
>= 0 && yourtm
.tm_sec
< SECSPERMIN
)
2403 else if (y
+ TM_YEAR_BASE
< EPOCH_YEAR
) {
2405 ** We can't set tm_sec to 0, because that might push the
2406 ** time below the minimum representable time.
2407 ** Set tm_sec to 59 instead.
2408 ** This assumes that the minimum representable time is
2409 ** not in the same minute that a leap second was deleted from,
2410 ** which is a safer assumption than using 58 would be.
2412 if (increment_overflow(&yourtm
.tm_sec
, 1 - SECSPERMIN
))
2414 saved_seconds
= yourtm
.tm_sec
;
2415 yourtm
.tm_sec
= SECSPERMIN
- 1;
2417 saved_seconds
= yourtm
.tm_sec
;
2421 ** Do a binary search (this works whatever time_t's type is).
2423 if (!TYPE_SIGNED(time_t)) {
2426 } else if (!TYPE_INTEGRAL(time_t)) {
2427 if (sizeof(time_t) > sizeof(float))
2428 hi
= (time_t) DBL_MAX
;
2429 else hi
= (time_t) FLT_MAX
;
2433 for (i
= 0; i
< (int) TYPE_BIT(time_t) - 1; ++i
)
2438 t
= lo
/ 2 + hi
/ 2;
2444 if ((*funcp
)(&t
, offset
, &mytm
) == NULL
) {
2446 ** Assume that t is too extreme to be represented in
2447 ** a struct tm; arrange things so that it is less
2448 ** extreme on the next pass.
2450 dir
= (t
> 0) ? 1 : -1;
2451 } else dir
= tmcomp(&mytm
, &yourtm
);
2452 #else /* !__LP64__ */
2453 (*funcp
)(&t
, offset
, &mytm
);
2454 dir
= tmcomp(&mytm
, &yourtm
);
2455 // If we have searched the entire space without a match, exit
2456 if (dir
!= 0 && t
== lo
&& t
== hi
)
2458 #endif /* __LP64__ */
2465 } else if (t
== hi
) {
2478 sp
= (funcp
== localsub
) ? lclptr
: gmtptr
;
2479 if (unix03
&& sp
->typecnt
== 1 && yourtm
.tm_isdst
> 0)
2480 yourtm
.tm_isdst
= 0; /* alternative time does not apply */
2481 if (yourtm
.tm_isdst
< 0 || mytm
.tm_isdst
== yourtm
.tm_isdst
)
2484 ** Right time, wrong type.
2485 ** Hunt for right time, right type.
2486 ** It's okay to guess wrong since the guess
2492 #endif /* defined ALL_STATE */
2493 for (i
= sp
->typecnt
- 1; i
>= 0; --i
) {
2494 if (sp
->ttis
[i
].tt_isdst
!= yourtm
.tm_isdst
)
2496 for (j
= sp
->typecnt
- 1; j
>= 0; --j
) {
2497 if (sp
->ttis
[j
].tt_isdst
== yourtm
.tm_isdst
)
2499 newt
= t
+ sp
->ttis
[j
].tt_gmtoff
-
2500 sp
->ttis
[i
].tt_gmtoff
;
2502 if ((*funcp
)(&newt
, offset
, &mytm
) == NULL
)
2504 #else /* !__LP64__ */
2505 (*funcp
)(&newt
, offset
, &mytm
);
2506 #endif /* __LP64__ */
2507 if (tmcomp(&mytm
, &yourtm
) != 0)
2509 if (mytm
.tm_isdst
!= yourtm
.tm_isdst
)
2521 newt
= t
+ saved_seconds
;
2522 if ((newt
< t
) != (saved_seconds
< 0))
2526 if ((*funcp
)(&t
, offset
, tmp
) == NULL
)
2528 #else /* !__LP64__ */
2529 (*funcp
)(&t
, offset
, tmp
);
2530 #endif /* __LP64__ */
2536 time2(struct tm
* const tmp
,
2538 struct tm
* (*const funcp
)(const time_t *, long, struct tm
*),
2539 #else /* !__LP64__ */
2540 void (*const funcp
)(const time_t *, long, struct tm
*),
2541 #endif /* __LP64__ */
2549 ** First try without normalization of seconds
2550 ** (in case tm_sec contains a value associated with a leap second).
2551 ** If that fails, try with normalization of seconds.
2553 t
= time2sub(tmp
, funcp
, offset
, okayp
, FALSE
, unix03
);
2554 return *okayp
? t
: time2sub(tmp
, funcp
, offset
, okayp
, TRUE
, unix03
);
2557 __private_extern__
time_t
2558 time1(tmp
, funcp
, offset
, unix03
)
2559 struct tm
* const tmp
;
2561 struct tm
* (* const funcp
)(const time_t *, long, struct tm
*);
2562 #else /* !__LP64__ */
2563 void (* const funcp
)(const time_t *, long, struct tm
*);
2564 #endif /* __LP64__ */
2569 const struct state
* sp
;
2571 int sameind
, otherind
;
2574 int seen
[TZ_MAX_TYPES
];
2575 int types
[TZ_MAX_TYPES
];
2583 if (tmp
->tm_isdst
> 1)
2585 t
= time2(tmp
, funcp
, offset
, &okay
, unix03
);
2588 ** PCTS code courtesy Grant Sullivan.
2592 if (tmp
->tm_isdst
< 0)
2593 tmp
->tm_isdst
= 0; /* reset to std and try again */
2594 #endif /* defined PCTS */
2596 if (okay
|| tmp
->tm_isdst
< 0)
2598 #endif /* !defined PCTS */
2600 ** We're supposed to assume that somebody took a time of one type
2601 ** and did some math on it that yielded a "struct tm" that's bad.
2602 ** We try to divine the type they started from and adjust to the
2605 sp
= (const struct state
*) ((funcp
== localsub
) ? lclptr
: gmtptr
);
2609 #endif /* defined ALL_STATE */
2610 for (i
= 0; i
< sp
->typecnt
; ++i
)
2613 for (i
= sp
->timecnt
- 1; i
>= 0; --i
)
2614 if (!seen
[sp
->types
[i
]]) {
2615 seen
[sp
->types
[i
]] = TRUE
;
2616 types
[nseen
++] = sp
->types
[i
];
2618 for (sameind
= 0; sameind
< nseen
; ++sameind
) {
2619 samei
= types
[sameind
];
2620 if (sp
->ttis
[samei
].tt_isdst
!= tmp
->tm_isdst
)
2622 for (otherind
= 0; otherind
< nseen
; ++otherind
) {
2623 otheri
= types
[otherind
];
2624 if (sp
->ttis
[otheri
].tt_isdst
== tmp
->tm_isdst
)
2626 tmp
->tm_sec
+= sp
->ttis
[otheri
].tt_gmtoff
-
2627 sp
->ttis
[samei
].tt_gmtoff
;
2628 tmp
->tm_isdst
= !tmp
->tm_isdst
;
2629 t
= time2(tmp
, funcp
, offset
, &okay
, unix03
);
2632 tmp
->tm_sec
-= sp
->ttis
[otheri
].tt_gmtoff
-
2633 sp
->ttis
[samei
].tt_gmtoff
;
2634 tmp
->tm_isdst
= !tmp
->tm_isdst
;
2639 #else /* BUILDING_VARIANT */
2640 extern pthread_rwlock_t lcl_rwlock
;
2641 #endif /* BUILDING_VARIANT */
2644 mktime(struct tm
*const tmp
)
2646 time_t mktime_return_value
;
2648 _RWLOCK_RDLOCK(&lcl_rwlock
);
2650 mktime_return_value
= time1(tmp
, localsub
, 0L, __DARWIN_UNIX03
);
2651 _RWLOCK_UNLOCK(&lcl_rwlock
);
2653 return(mktime_return_value
);
2656 #if !BUILDING_VARIANT
2660 timelocal(struct tm
*const tmp
)
2663 tmp
->tm_isdst
= -1; /* in case it wasn't initialized */
2668 timegm(struct tm
*const tmp
)
2672 return time1(tmp
, gmtsub
, 0L, __DARWIN_UNIX03
);
2676 timeoff(struct tm
*const tmp
, const long offset
)
2680 return time1(tmp
, gmtsub
, offset
, __DARWIN_UNIX03
);
2683 #endif /* defined STD_INSPIRED */
2688 ** The following is supplied for compatibility with
2689 ** previous versions of the CMUCS runtime library.
2693 gtime(struct tm
*const tmp
)
2695 const time_t t
= mktime(tmp
);
2702 #endif /* defined CMUCS */
2705 ** XXX--is the below the right way to conditionalize??
2711 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2712 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2713 ** is not the case if we are accounting for leap seconds.
2714 ** So, we provide the following conversion routines for use
2715 ** when exchanging timestamps with POSIX conforming systems.
2719 leapcorr(time_t *timep
)
2729 if (*timep
>= lp
->ls_trans
)
2736 time2posix(time_t t
)
2739 return t
- leapcorr(&t
);
2743 posix2time(time_t t
)
2750 ** For a positive leap second hit, the result
2751 ** is not unique. For a negative leap second
2752 ** hit, the corresponding time doesn't exist,
2753 ** so we return an adjacent second.
2755 x
= t
+ leapcorr(&t
);
2756 y
= x
- leapcorr(&x
);
2760 y
= x
- leapcorr(&x
);
2767 y
= x
- leapcorr(&x
);
2775 #endif /* defined STD_INSPIRED */
2776 #endif /* !BUILDING_VARIANT */