1 --- localtime.c.orig 2011-05-02 23:14:13.000000000 -0700
2 +++ localtime.c 2011-05-03 17:12:02.000000000 -0700
3 @@ -22,8 +22,22 @@ __FBSDID("$FreeBSD: src/lib/libc/stdtime
12 +//#define NOTIFY_TZ_DEBUG
13 +//#define NOTIFY_TZ_DEBUG_FILE "/var/log/localtime.debug"
14 +//#define NOTIFY_TZ_LOG "/var/log/localtime.log"
15 +/* force ALL_STATE if NOTIFY_TZ is set */
18 +#endif /* ALL_STATE */
19 +#include <mach/mach_init.h>
22 +#endif /* NOTIFY_TZ */
24 #include "un-namespace.h"
26 @@ -150,40 +164,94 @@ struct rule {
27 #define DAY_OF_YEAR 1 /* n - day of year */
28 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
37 +#define NOTIFY_TZ_NAME "com.apple.system.timezone"
38 +#endif /* NOTIFY_TZ */
41 ** Prototypes for static functions.
43 +#define localsub _st_localsub
44 +#define time1 _st_time1
45 +#define tzset_basic _st_tzset_basic
48 +struct tm * localsub(const time_t * timep, long offset,
50 +#else /* !__LP64__ */
51 +void localsub(const time_t * timep, long offset,
53 +#endif /* __LP64__ */
55 +time_t time1(struct tm * tmp,
57 + struct tm *(*funcp) (const time_t *,
59 +#else /* !__LP64__ */
60 + void(*funcp) (const time_t *,
62 +#endif /* __LP64__ */
66 +void tzset_basic(int);
68 +#if !BUILDING_VARIANT
69 static long detzcode(const char * codep);
70 -static const char * getzname(const char * strp);
71 +static const char * getzname(const char * strp, char **name, size_t *len);
72 static const char * getnum(const char * strp, int * nump, int min,
74 static const char * getsecs(const char * strp, long * secsp);
75 static const char * getoffset(const char * strp, long * offsetp);
76 static const char * getrule(const char * strp, struct rule * rulep);
77 static void gmtload(struct state * sp);
78 -static void gmtsub(const time_t * timep, long offset,
80 +static struct tm * gmtsub(const time_t * timep, long offset,
82 -static void localsub(const time_t * timep, long offset,
83 +#else /* !__LP64__ */
84 +static void gmtsub(const time_t * timep, long offset,
86 +#endif /* __LP64__ */
87 static int increment_overflow(int * number, int delta);
88 static int normalize_overflow(int * tensptr, int * unitsptr,
91 +static void notify_check_tz(notify_tz_t *p);
92 +static void notify_register_tz(char *file, notify_tz_t *p);
93 +#endif /* NOTIFY_TZ */
94 static void settzname(void);
95 -static time_t time1(struct tm * tmp,
96 - void(*funcp) (const time_t *,
99 static time_t time2(struct tm *tmp,
101 + struct tm *(*funcp) (const time_t *,
103 +#else /* !__LP64__ */
104 void(*funcp) (const time_t *,
106 - long offset, int * okayp);
107 +#endif /* __LP64__ */
108 + long offset, int * okayp, int unix03);
109 static time_t time2sub(struct tm *tmp,
111 + struct tm *(*funcp) (const time_t *,
113 +#else /* !__LP64__ */
114 void(*funcp) (const time_t *,
116 - long offset, int * okayp, int do_norm_secs);
117 +#endif /* __LP64__ */
118 + long offset, int * okayp, int do_norm_secs,
121 +static struct tm * timesub(const time_t * timep, long offset,
122 + const struct state * sp, struct tm * tmp);
123 +#else /* !__LP64__ */
124 static void timesub(const time_t * timep, long offset,
125 const struct state * sp, struct tm * tmp);
126 +#endif /* __LP64__ */
127 static int tmcomp(const struct tm * atmp,
128 const struct tm * btmp);
129 static time_t transtime(time_t janfirst, int year,
130 @@ -209,9 +277,14 @@ static struct state gmtmem;
131 #endif /* !defined TZ_STRLEN_MAX */
133 static char lcl_TZname[TZ_STRLEN_MAX + 1];
135 +#define lcl_is_set (lcl_notify.is_set)
136 +#define gmt_is_set (gmt_notify.is_set)
137 +#else /* ! NOTIFY_TZ */
138 static int lcl_is_set;
139 static int gmt_is_set;
140 -static pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
141 +#endif /* NOTIFY_TZ */
142 +__private_extern__ pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
143 static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
146 @@ -229,15 +302,62 @@ char * tzname[2] = {
153 -time_t timezone = 0;
155 +__private_extern__ void _st_set_timezone(long);
156 #endif /* defined USG_COMPAT */
160 +__private_extern__ long __darwin_altzone = 0;
161 +#define altzone __darwin_altzone
162 #endif /* defined ALTZONE */
165 +#ifdef NOTIFY_TZ_DEBUG
166 +#ifdef NOTIFY_TZ_DEBUG_FILE
167 +#define NOTIFY_TZ_PRINTF(fmt, args...) \
169 + FILE *_notify_tz_fp_; \
170 + if((_notify_tz_fp_ = fopen(NOTIFY_TZ_DEBUG_FILE, "a")) != NULL) { \
171 + fprintf(_notify_tz_fp_, "%d: " fmt, getpid(), ## args); \
172 + fclose(_notify_tz_fp_); \
175 +#else /* ! NOTIFY_TZ_DEBUG_FILE */
176 +#define NOTIFY_TZ_PRINTF(args...) fprintf(stdout, ## args)
177 +#endif /* NOTIFY_TZ_DEBUG_FILE */
178 +#endif /* NOTIFY_TZ_DEBUG */
179 +#ifdef NOTIFY_TZ_LOG
180 +#define NOTIFY_LOG(fmt, args...) \
182 + FILE *_notify_log_fp_; \
183 + if((_notify_log_fp_ = fopen(NOTIFY_TZ_LOG, "a")) != NULL) { \
184 + fprintf(_notify_log_fp_, "%d: " fmt, getpid(), ## args); \
185 + fclose(_notify_log_fp_); \
188 +#endif /* NOTIFY_TZ_LOG */
189 +/*--------------------------------------------------------------------
190 + * __notify_78945668_info__ is a global variable (defined in Libnotify)
191 + * that can be used to disable the notify mechanism. Set to a negative
192 + * value to disable. It can then be set back to zero to re-enable.
193 + *-------------------------------------------------------------------- */
194 +extern int __notify_78945668_info__;
196 +/*--------------------------------------------------------------------
197 + * fullname is used to pass the actual path of the timezone file to the
198 + * notify routines. If it is a nil string, that means no timezone file
200 + *-------------------------------------------------------------------- */
201 +static char * fullname = NULL;
203 +static notify_tz_t gmt_notify = {-1, 0, 0};
204 +static notify_tz_t lcl_notify = {-1, 0, 0};
205 +static char notify_tz_name[] = NOTIFY_TZ_NAME;
206 +#endif /* NOTIFY_TZ */
210 const char * const codep;
211 @@ -255,51 +375,203 @@ static void
214 struct state * sp = lclptr;
217 + unsigned char * types;
220 +#define NEED_DAYLIGHT 4
221 +#define NEED_ALL (NEED_STD | NEED_DST | NEED_DAYLIGHT)
223 tzname[0] = wildabbr;
224 tzname[1] = wildabbr;
228 + _st_set_timezone(0);
229 #endif /* defined USG_COMPAT */
232 #endif /* defined ALTZONE */
235 - tzname[0] = tzname[1] = gmt;
236 + tzname[0] = tzname[1] = (char *)gmt;
239 #endif /* defined ALL_STATE */
240 - for (i = 0; i < sp->typecnt; ++i) {
241 - const struct ttinfo * const ttisp = &sp->ttis[i];
243 + * PR-3765457: The original settzname went sequentially through the ttis
244 + * array, rather than correctly indexing via the types array, to get
245 + * the real order of the timezone changes. In addition, as a speed up,
246 + * we start at the end of the changes, and work back, so that most of
247 + * the time, we don't have to look through the entire array.
249 + if (sp->timecnt == 0 && sp->typecnt == 1) {
251 + * Unfortunately, there is an edge case when typecnt == 1 and
252 + * timecnt == 0, which would cause the loop to never run. So
253 + * in that case, we fudge things up so that it is as if
257 + types = (unsigned char *)""; /* we use the null as index */
259 + /* the usual case */
260 + i = sp->timecnt - 1;
264 + for (; i >= 0 && need; --i) {
265 + const struct ttinfo * const ttisp = &sp->ttis[types[i]];
267 - tzname[ttisp->tt_isdst] =
268 - &sp->chars[ttisp->tt_abbrind];
270 - if (ttisp->tt_isdst)
271 + if ((need & NEED_DAYLIGHT) && ttisp->tt_isdst) {
272 + need &= ~NEED_DAYLIGHT;
274 - if (i == 0 || !ttisp->tt_isdst)
275 - timezone = -(ttisp->tt_gmtoff);
277 #endif /* defined USG_COMPAT */
278 + if (ttisp->tt_isdst) {
279 + if (need & NEED_DST) {
281 + tzname[1] = &sp->chars[ttisp->tt_abbrind];
283 - if (i == 0 || ttisp->tt_isdst)
284 - altzone = -(ttisp->tt_gmtoff);
285 + altzone = -(ttisp->tt_gmtoff);
286 #endif /* defined ALTZONE */
288 + } else if (need & NEED_STD) {
290 + tzname[0] = &sp->chars[ttisp->tt_abbrind];
292 + _st_set_timezone(-(ttisp->tt_gmtoff));
293 +#endif /* defined USG_COMPAT */
295 +#if defined(ALTZONE) || defined(USG_COMPAT)
297 +#endif /* defined(ALTZONE) || defined(USG_COMPAT) */
299 + if (need & NEED_DST)
300 + altzone = -(ttisp->tt_gmtoff);
301 +#endif /* defined ALTZONE */
303 + if (need & NEED_STD)
304 + _st_set_timezone(-(ttisp->tt_gmtoff));
305 +#endif /* defined USG_COMPAT */
306 +#if defined(ALTZONE) || defined(USG_COMPAT)
308 +#endif /* defined(ALTZONE) || defined(USG_COMPAT) */
311 - ** And to get the latest zone names into tzname. . .
313 - for (i = 0; i < sp->timecnt; ++i) {
314 - const struct ttinfo * const ttisp =
321 +notify_check_tz(notify_tz_t *p)
323 + unsigned int nstat;
326 + if (__notify_78945668_info__ < 0) {
327 +#ifdef NOTIFY_TZ_DEBUG
328 + if(!p->notify_was_off) NOTIFY_TZ_PRINTF("notify_check_tz: setting %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
329 +#endif /* NOTIFY_TZ_DEBUG */
330 + p->notify_was_off = 1;
333 + /* force rereading the timezone file if notify was off */
334 + if (p->notify_was_off) {
335 +#ifdef NOTIFY_TZ_DEBUG
336 + NOTIFY_TZ_PRINTF("notify_check_tz: saw %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
337 +#endif /* NOTIFY_TZ_DEBUG */
339 + p->notify_was_off = 0;
344 + nstat = notify_check(p->token, &ncheck);
345 + if (nstat || ncheck) {
347 +#ifdef NOTIFY_TZ_DEBUG
348 + NOTIFY_TZ_PRINTF("notify_check_tz: %s changed\n", (p == &lcl_notify) ? "lcl" : "gmt");
349 +#endif /* NOTIFY_TZ_DEBUG */
351 +#ifdef NOTIFY_TZ_DEBUG
352 + NOTIFY_TZ_PRINTF("notify_check_tz: %s unchanged\n", (p == &lcl_notify) ? "lcl" : "gmt");
353 +#endif /* NOTIFY_TZ_DEBUG */
356 +extern uint32_t notify_monitor_file(int token, char *path, int flags);
359 +notify_register_tz(char *file, notify_tz_t *p)
362 + unsigned int nstat;
365 - tzname[ttisp->tt_isdst] =
366 - &sp->chars[ttisp->tt_abbrind];
367 + if (__notify_78945668_info__ < 0)
369 + /*----------------------------------------------------------------
370 + * Since we don't record the last time zone filename, just cancel
371 + * (which should remove the file monitor) and setup from scratch
372 + *----------------------------------------------------------------*/
374 + notify_cancel(p->token);
375 + if (!file || *file == 0) {
376 + /* no time zone file to monitor */
380 + /*----------------------------------------------------------------
381 + * Just use com.apple.system.timezone if the path is /etc/localtime.
382 + * Otherwise use com.apple.system.timezone.<fullpath>
383 + *----------------------------------------------------------------*/
384 + if (TZDEFAULT && strcmp(file, TZDEFAULT) == 0)
385 + name = notify_tz_name;
387 + name = alloca(sizeof(notify_tz_name) + strlen(file) + 1);
388 + if (name == NULL) {
392 + strcpy(name, notify_tz_name);
394 + strcat(name, file);
396 +#ifdef NOTIFY_TZ_DEBUG
397 + NOTIFY_TZ_PRINTF("notify_register_tz: file=%s name=%s\n", file, name);
398 +#endif /* NOTIFY_TZ_DEBUG */
399 + nstat = notify_register_check(name, &p->token);
403 +#ifdef NOTIFY_TZ_DEBUG
404 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_register_check failed: %u\n", nstat);
405 +#endif /* NOTIFY_TZ_DEBUG */
406 +#ifdef NOTIFY_TZ_LOG
407 + NOTIFY_LOG("notify_register_check(%s) failed: %u\n", name, nstat);
408 +#endif /* NOTIFY_TZ_LOG */
411 + /* don't need to request monitoring /etc/localtime */
412 + if (name != notify_tz_name) {
413 +#ifdef NOTIFY_TZ_DEBUG
414 + NOTIFY_TZ_PRINTF("notify_register_tz: monitor %s\n", file);
415 +#endif /* NOTIFY_TZ_DEBUG */
416 + nstat = notify_monitor_file(p->token, file, 0);
418 + notify_cancel(p->token);
421 +#ifdef NOTIFY_TZ_DEBUG
422 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_monitor_file failed: %u\n", nstat);
423 +#endif /* NOTIFY_TZ_DEBUG */
424 +#ifdef NOTIFY_TZ_LOG
425 + NOTIFY_LOG("notify_monitor_file(%s) failed: %u\n", file, nstat);
426 +#endif /* NOTIFY_TZ_LOG */
430 + notify_check(p->token, &ncheck); /* this always returns true */
432 +#endif /* NOTIFY_TZ */
436 @@ -310,6 +582,9 @@ struct state * const sp;
440 +#ifdef NOTIFY_TZ_DEBUG
441 + NOTIFY_TZ_PRINTF("tzload: name=%s\n", name);
442 +#endif /* NOTIFY_TZ_DEBUG */
443 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
444 if (name != NULL && issetugid() != 0)
445 if ((name[0] == ':' && name[1] == '/') ||
446 @@ -327,7 +602,15 @@ struct state * const sp;
447 ** to hold the longest file name string that the implementation
448 ** guarantees can be opened."
452 + fullname = malloc(FILENAME_MAX + 1);
456 +#else /* ! NOTIFY_TZ */
457 char fullname[FILENAME_MAX + 1];
458 +#endif /* NOTIFY_TZ */
462 @@ -335,7 +618,11 @@ struct state * const sp;
464 if ((p = TZDIR) == NULL)
467 + if ((strlen(p) + 1 + strlen(name) + 1) >= (FILENAME_MAX + 1))
468 +#else /* ! NOTIFY_TZ */
469 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
470 +#endif /* NOTIFY_TZ */
472 (void) strcpy(fullname, p);
473 (void) strcat(fullname, "/");
474 @@ -347,6 +634,10 @@ struct state * const sp;
480 + strcpy(fullname, name);
481 +#endif /* NOTIFY_TZ */
482 if (doaccess && access(name, R_OK) != 0)
484 if ((fid = _open(name, OPEN_MODE)) == -1)
485 @@ -365,6 +656,9 @@ struct state * const sp;
489 +#ifdef NOTIFY_TZ_DEBUG
490 + NOTIFY_TZ_PRINTF("tzload: reading %s\n", name);
491 +#endif /* NOTIFY_TZ_DEBUG */
492 i = _read(fid, u.buf, sizeof u.buf);
493 if (_close(fid) != 0)
495 @@ -471,14 +765,24 @@ static const int year_lengths[2] = {
500 +getzname(strp, name, len)
508 + if (*strp == '<' && (ket = strchr(strp, '>')) != NULL) {
509 + *name = (char *)(strp + 1);
510 + *len = ket - strp - 1;
513 + *name = (char *)strp;
514 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
517 + *len = strp - *name;
521 @@ -758,16 +1062,15 @@ const int lastditch;
528 stdlen = strlen(name); /* length of standard zone name */
530 if (stdlen >= sizeof sp->chars)
531 stdlen = (sizeof sp->chars) - 1;
534 - name = getzname(name);
535 - stdlen = name - stdname;
536 + name = getzname(name, (char **)&stdname, &stdlen);
540 @@ -779,12 +1082,14 @@ const int lastditch;
543 load_result = tzload(TZDEFRULES, sp);
545 + *fullname = 0; /* mark fullname as invalid */
546 +#endif /* NOTIFY_TZ */
547 if (load_result != 0)
548 sp->leapcnt = 0; /* so, we're off a little */
551 - name = getzname(name);
552 - dstlen = name - dstname; /* length of DST zone name */
553 + name = getzname(name, (char **)&dstname, &dstlen);
556 if (*name != '\0' && *name != ',' && *name != ';') {
557 @@ -966,13 +1271,37 @@ struct state * const sp;
559 tzsetwall_basic(int rdlocked)
562 + notify_check_tz(&lcl_notify);
565 + static struct timespec last_mtimespec = {0, 0};
566 + struct stat statbuf;
568 + if (lstat(TZDEFAULT, &statbuf) == 0) {
569 + if (statbuf.st_mtimespec.tv_sec > last_mtimespec.tv_sec ||
570 + (statbuf.st_mtimespec.tv_sec == last_mtimespec.tv_sec &&
571 + statbuf.st_mtimespec.tv_nsec > last_mtimespec.tv_nsec)) {
572 + /* Trigger resetting the local TZ */
575 + last_mtimespec = statbuf.st_mtimespec;
578 +#endif /* NOTIFY_TZ */
580 _RWLOCK_RDLOCK(&lcl_rwlock);
581 if (lcl_is_set < 0) {
582 +#ifdef NOTIFY_TZ_DEBUG
583 + NOTIFY_TZ_PRINTF("tzsetwall_basic lcl_is_set < 0\n");
586 _RWLOCK_UNLOCK(&lcl_rwlock);
589 +#ifdef NOTIFY_TZ_DEBUG
590 + NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
592 _RWLOCK_UNLOCK(&lcl_rwlock);
594 _RWLOCK_WRLOCK(&lcl_rwlock);
595 @@ -992,6 +1321,9 @@ tzsetwall_basic(int rdlocked)
596 #endif /* defined ALL_STATE */
597 if (tzload((char *) NULL, lclptr) != 0)
600 + notify_register_tz(fullname, &lcl_notify);
601 +#endif /* NOTIFY_TZ */
603 _RWLOCK_UNLOCK(&lcl_rwlock);
605 @@ -1002,10 +1334,13 @@ tzsetwall_basic(int rdlocked)
609 +#ifdef NOTIFY_TZ_DEBUG
610 + NOTIFY_TZ_PRINTF("tzsetwall called\n");
611 +#endif /* NOTIFY_TZ_DEBUG */
616 +__private_extern__ void
617 tzset_basic(int rdlocked)
620 @@ -1016,11 +1351,17 @@ tzset_basic(int rdlocked)
625 + notify_check_tz(&lcl_notify);
626 +#endif /* NOTIFY_TZ */
628 _RWLOCK_RDLOCK(&lcl_rwlock);
629 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
631 _RWLOCK_UNLOCK(&lcl_rwlock);
632 +#ifdef NOTIFY_TZ_DEBUG
633 + NOTIFY_TZ_PRINTF("tzset_basic matched %s\n", lcl_TZname);
637 _RWLOCK_UNLOCK(&lcl_rwlock);
638 @@ -1053,9 +1394,16 @@ tzset_basic(int rdlocked)
639 lclptr->ttis[0].tt_gmtoff = 0;
640 lclptr->ttis[0].tt_abbrind = 0;
641 (void) strcpy(lclptr->chars, gmt);
645 +#endif /* NOTIFY_TZ */
646 } else if (tzload(name, lclptr) != 0)
647 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
648 (void) gmtload(lclptr);
650 + notify_register_tz(fullname, &lcl_notify);
651 +#endif /* NOTIFY_TZ */
653 _RWLOCK_UNLOCK(&lcl_rwlock);
655 @@ -1066,6 +1414,9 @@ tzset_basic(int rdlocked)
659 +#ifdef NOTIFY_TZ_DEBUG
660 + NOTIFY_TZ_PRINTF("tzset called TZ=%s\n", getenv("TZ"));
661 +#endif /* NOTIFY_TZ_DEBUG */
665 @@ -1079,7 +1430,11 @@ tzset(void)
671 +__private_extern__ struct tm *
672 +#else /* !__LP64__ */
673 +__private_extern__ void
674 +#endif /* __LP64__ */
675 localsub(timep, offset, tmp)
676 const time_t * const timep;
678 @@ -1090,11 +1445,18 @@ struct tm * const tmp;
680 const time_t t = *timep;
682 +#ifdef NOTIFY_TZ_DEBUG
683 + NOTIFY_TZ_PRINTF("localsub called\n");
684 +#endif /* NOTIFY_TZ_DEBUG */
689 + return gmtsub(timep, offset, tmp);
690 +#else /* !__LP64__ */
691 gmtsub(timep, offset, tmp);
693 +#endif /* __LP64__ */
695 #endif /* defined ALL_STATE */
696 if (sp->timecnt == 0 || t < sp->ats[0]) {
697 @@ -1117,12 +1479,20 @@ struct tm * const tmp;
698 ** t += ttisp->tt_gmtoff;
699 ** timesub(&t, 0L, sp, tmp);
702 + if (timesub(&t, ttisp->tt_gmtoff, sp, tmp) == NULL)
704 +#else /* !__LP64__ */
705 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
706 +#endif /* __LP64__ */
707 tmp->tm_isdst = ttisp->tt_isdst;
708 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
710 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
711 #endif /* defined TM_ZONE */
714 +#endif /* __LP64__ */
718 @@ -1134,10 +1504,11 @@ const time_t * const timep;
721 if (__isthreaded != 0) {
722 - if (localtime_key < 0) {
723 + if (localtime_key == (pthread_key_t)-1) {
724 _pthread_mutex_lock(&localtime_mutex);
725 - if (localtime_key < 0) {
726 - if (_pthread_key_create(&localtime_key, free) < 0) {
727 + if (localtime_key == (pthread_key_t)-1) {
728 + localtime_key = __LIBC_PTHREAD_KEY_LOCALTIME;
729 + if (pthread_key_init_np(localtime_key, free) < 0) {
730 _pthread_mutex_unlock(&localtime_mutex);
733 @@ -1153,13 +1524,21 @@ const time_t * const timep;
735 _RWLOCK_RDLOCK(&lcl_rwlock);
738 + p_tm = localsub(timep, 0L, p_tm);
739 +#else /* !__LP64__ */
740 localsub(timep, 0L, p_tm);
741 +#endif /* __LP64__ */
742 _RWLOCK_UNLOCK(&lcl_rwlock);
747 + return localsub(timep, 0L, &tm);
748 +#else /* !__LP64__ */
749 localsub(timep, 0L, &tm);
751 +#endif /* __LP64__ */
755 @@ -1168,13 +1547,15 @@ const time_t * const timep;
759 -localtime_r(timep, tm)
760 -const time_t * const timep;
762 +localtime_r(const time_t * const __restrict timep, struct tm * __restrict tm)
764 _RWLOCK_RDLOCK(&lcl_rwlock);
767 + tm = localsub(timep, 0L, tm);
768 +#else /* !__LP64__ */
769 localsub(timep, 0L, tm);
770 +#endif /* __LP64__ */
771 _RWLOCK_UNLOCK(&lcl_rwlock);
774 @@ -1183,25 +1564,52 @@ struct tm * tm;
775 ** gmtsub is to gmtime as localsub is to localtime.
780 +#else /* !__LP64__ */
782 +#endif /* __LP64__ */
783 gmtsub(timep, offset, tmp)
784 const time_t * const timep;
786 struct tm * const tmp;
788 +#ifdef NOTIFY_TZ_DEBUG
789 + NOTIFY_TZ_PRINTF("gmtsub called\n");
790 +#endif /* NOTIFY_TZ_DEBUG */
792 + notify_check_tz(&gmt_notify);
793 +#endif /* NOTIFY_TZ */
795 _MUTEX_LOCK(&gmt_mutex);
798 - gmtptr = (struct state *) malloc(sizeof *gmtptr);
800 + if (gmtptr == NULL)
801 +#endif /* NOTIFY_TZ */
802 + gmtptr = (struct state *) malloc(sizeof *gmtptr);
806 +#endif /* NOTIFY_TZ */
807 #endif /* defined ALL_STATE */
810 + notify_register_tz(fullname, &gmt_notify);
814 +#endif /* NOTIFY_TZ */
817 _MUTEX_UNLOCK(&gmt_mutex);
820 + if(timesub(timep, offset, gmtptr, tmp) == NULL)
822 +#else /* !__LP64__ */
823 timesub(timep, offset, gmtptr, tmp);
824 +#endif /* __LP64__ */
827 ** Could get fancy here and deliver something such as
828 @@ -1213,7 +1621,7 @@ struct tm * const tmp;
832 - tmp->TM_ZONE = gmt;
833 + tmp->TM_ZONE = (char *)gmt;
834 else tmp->TM_ZONE = gmtptr->chars;
835 #endif /* defined ALL_STATE */
837 @@ -1221,6 +1629,9 @@ struct tm * const tmp;
838 #endif /* State Farm */
840 #endif /* defined TM_ZONE */
843 +#endif /* __LP64__ */
847 @@ -1232,10 +1643,11 @@ const time_t * const timep;
850 if (__isthreaded != 0) {
851 - if (gmtime_key < 0) {
852 + if (gmtime_key == (pthread_key_t)-1) {
853 _pthread_mutex_lock(&gmtime_mutex);
854 - if (gmtime_key < 0) {
855 - if (_pthread_key_create(&gmtime_key, free) < 0) {
856 + if (gmtime_key == (pthread_key_t)-1) {
857 + gmtime_key = __LIBC_PTHREAD_KEY_GMTIME;
858 + if (pthread_key_init_np(gmtime_key, free) < 0) {
859 _pthread_mutex_unlock(&gmtime_mutex);
862 @@ -1253,12 +1665,20 @@ const time_t * const timep;
864 _pthread_setspecific(gmtime_key, p_tm);
867 + return gmtsub(timep, 0L, p_tm);
868 +#else /* !__LP64__ */
869 gmtsub(timep, 0L, p_tm);
871 +#endif /* __LP64__ */
875 + return gmtsub(timep, 0L, &tm);
876 +#else /* !__LP64__ */
877 gmtsub(timep, 0L, &tm);
879 +#endif /* __LP64__ */
883 @@ -1271,8 +1691,13 @@ gmtime_r(timep, tm)
884 const time_t * const timep;
889 + return gmtsub(timep, 0L, tm);
890 +#else /* !__LP64__ */
891 gmtsub(timep, 0L, tm);
893 +#endif /* __LP64__ */
897 @@ -1282,13 +1707,21 @@ offtime(timep, offset)
898 const time_t * const timep;
902 + return gmtsub(timep, offset, &tm);
903 +#else /* !__LP64__ */
904 gmtsub(timep, offset, &tm);
906 +#endif /* __LP64__ */
909 #endif /* defined STD_INSPIRED */
913 +#else /* !__LP64__ */
915 +#endif /* __LP64__ */
916 timesub(timep, offset, sp, tmp)
917 const time_t * const timep;
919 @@ -1365,7 +1798,12 @@ struct tm * const tmp;
920 if (tmp->tm_wday < 0)
921 tmp->tm_wday += DAYSPERWEEK;
923 -#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
924 +#define _LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
926 +#define LEAPS_THRU_END_OF(y) ((y) >= 0 ? _LEAPS_THRU_END_OF(y) : _LEAPS_THRU_END_OF((y) + 1) - 1)
927 +#else /* !__LP64__ */
928 +#define LEAPS_THRU_END_OF(y) _LEAPS_THRU_END_OF(y)
929 +#endif /* __LP64__ */
930 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
933 @@ -1377,7 +1815,16 @@ struct tm * const tmp;
934 LEAPS_THRU_END_OF(y - 1);
939 + if (y < INT_MIN || y > INT_MAX) {
944 +#else /* !__LP64__ */
945 tmp->tm_year = y - TM_YEAR_BASE;
946 +#endif /* __LP64__ */
947 tmp->tm_yday = (int) days;
948 ip = mon_lengths[yleap];
949 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
950 @@ -1387,6 +1834,9 @@ struct tm * const tmp;
952 tmp->TM_GMTOFF = offset;
953 #endif /* defined TM_GMTOFF */
956 +#endif /* __LP64__ */
960 @@ -1399,7 +1849,20 @@ const time_t * const timep;
961 ** to local time in the form of a string. It is equivalent to
962 ** asctime(localtime(timer))
966 + * In 64-bit, the timep value may produce a time value with a year
967 + * that exceeds 32-bits in size (won't fit in struct tm), so localtime
968 + * will return NULL.
970 + struct tm *tm = localtime(timep);
974 + return asctime(tm);
975 +#else /* !__LP64__ */
976 return asctime(localtime(timep));
977 +#endif /* __LP64__ */
981 @@ -1409,7 +1872,18 @@ char * buf;
987 + * In 64-bit, the timep value may produce a time value with a year
988 + * that exceeds 32-bits in size (won't fit in struct tm), so localtime_r
989 + * will return NULL.
991 + if (localtime_r(timep, &tm) == NULL)
993 + return asctime_r(&tm, buf);
994 +#else /* !__LP64__ */
995 return asctime_r(localtime_r(timep, &tm), buf);
996 +#endif /* __LP64__ */
1000 @@ -1464,8 +1938,14 @@ const struct tm * const btmp;
1004 - if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1005 - (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1007 + * Assume that atmp and btmp point to normalized tm strutures.
1008 + * So only arithmetic with tm_year could overflow in 64-bit.
1010 + if (atmp->tm_year != btmp->tm_year) {
1011 + return (atmp->tm_year > btmp->tm_year ? 1 : -1);
1013 + if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1014 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1015 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1016 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1017 @@ -1474,12 +1954,17 @@ const struct tm * const btmp;
1021 -time2sub(tmp, funcp, offset, okayp, do_norm_secs)
1022 +time2sub(tmp, funcp, offset, okayp, do_norm_secs, unix03)
1023 struct tm * const tmp;
1025 +struct tm *(* const funcp)(const time_t*, long, struct tm*);
1026 +#else /* !__LP64__ */
1027 void (* const funcp)(const time_t*, long, struct tm*);
1028 +#endif /* __LP64__ */
1031 const int do_norm_secs;
1034 const struct state * sp;
1036 @@ -1489,6 +1974,9 @@ const int do_norm_secs;
1039 struct tm yourtm, mytm;
1042 +#endif /* __LP64__ */
1046 @@ -1507,33 +1995,64 @@ const int do_norm_secs;
1047 ** Turn yourtm.tm_year into an actual year number for now.
1048 ** It is converted back to an offset from TM_YEAR_BASE later.
1051 + year = (long)yourtm.tm_year + TM_YEAR_BASE;
1052 +#else /* !__LP64__ */
1053 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1055 +#endif /* __LP64__ */
1056 while (yourtm.tm_mday <= 0) {
1059 + il = year + (1 < yourtm.tm_mon);
1060 + yourtm.tm_mday += year_lengths[isleap(il)];
1061 +#else /* !__LP64__ */
1062 if (increment_overflow(&yourtm.tm_year, -1))
1064 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1065 yourtm.tm_mday += year_lengths[isleap(i)];
1066 +#endif /* __LP64__ */
1068 while (yourtm.tm_mday > DAYSPERLYEAR) {
1070 + il = year + (1 < yourtm.tm_mon);
1071 + yourtm.tm_mday -= year_lengths[isleap(il)];
1073 +#else /* !__LP64__ */
1074 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1075 yourtm.tm_mday -= year_lengths[isleap(i)];
1076 if (increment_overflow(&yourtm.tm_year, 1))
1078 +#endif /* __LP64__ */
1082 + i = mon_lengths[isleap(year)][yourtm.tm_mon];
1083 +#else /* !__LP64__ */
1084 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1085 +#endif /* __LP64__ */
1086 if (yourtm.tm_mday <= i)
1088 yourtm.tm_mday -= i;
1089 if (++yourtm.tm_mon >= MONSPERYEAR) {
1093 +#else /* !__LP64__ */
1094 if (increment_overflow(&yourtm.tm_year, 1))
1096 +#endif /* __LP64__ */
1100 + year -= TM_YEAR_BASE;
1101 + if (year > INT_MAX || year < INT_MIN)
1103 + yourtm.tm_year = year;
1104 +#else /* !__LP64__ */
1105 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1107 +#endif /* __LP64__ */
1108 /* Don't go below 1900 for POLA */
1109 if (yourtm.tm_year < 0)
1111 @@ -1560,13 +2079,21 @@ const int do_norm_secs;
1112 ** Divide the search space in half
1113 ** (this works whether time_t is signed or unsigned).
1116 + /* optimization: see if the value is 31-bit (signed) */
1117 + t = (((time_t) 1) << (TYPE_BIT(int) - 1)) - 1;
1118 + bits = ((*funcp)(&t, offset, &mytm) == NULL || tmcomp(&mytm, &yourtm) < 0) ? TYPE_BIT(time_t) - 1 : TYPE_BIT(int) - 1;
1119 +#else /* !__LP64__ */
1120 bits = TYPE_BIT(time_t) - 1;
1121 +#endif /* __LP64__ */
1123 - ** If we have more than this, we will overflow tm_year for tmcomp().
1124 - ** We should really return an error if we cannot represent it.
1125 + ** In 64-bit, we now return an error if we cannot represent the
1126 + ** struct tm value in a time_t. And tmcomp() is fixed to avoid
1127 + ** overflow in tm_year. So we only put a cap on bits because time_t
1128 + ** can't be larger that 56 bit (when tm_year == INT_MAX).
1135 ** If time_t is signed, then 0 is just above the median,
1136 ** assuming two's complement arithmetic.
1137 @@ -1574,8 +2101,19 @@ const int do_norm_secs;
1139 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1142 + if ((*funcp)(&t, offset, &mytm) == NULL) {
1143 + /* we overflowed, so t is too big */
1147 +#else /* !__LP64__ */
1148 (*funcp)(&t, offset, &mytm);
1149 +#endif /* __LP64__ */
1150 dir = tmcomp(&mytm, &yourtm);
1153 +#endif /* __LP64__ */
1157 @@ -1586,6 +2124,9 @@ const int do_norm_secs;
1158 else t += ((time_t) 1) << bits;
1161 + sp = (funcp == localsub) ? lclptr : gmtptr;
1162 + if (unix03 && sp->typecnt == 1 && yourtm.tm_isdst > 0)
1163 + yourtm.tm_isdst = 0; /* alternative time does not apply */
1164 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1167 @@ -1594,7 +2135,6 @@ const int do_norm_secs;
1168 ** It's okay to guess wrong since the guess
1171 - sp = (funcp == localsub) ? lclptr : gmtptr;
1175 @@ -1607,7 +2147,12 @@ const int do_norm_secs;
1177 newt = t + sp->ttis[j].tt_gmtoff -
1178 sp->ttis[i].tt_gmtoff;
1180 + if ((*funcp)(&newt, offset, &mytm) == NULL)
1182 +#else /* !__LP64__ */
1183 (*funcp)(&newt, offset, &mytm);
1184 +#endif /* __LP64__ */
1185 if (tmcomp(&mytm, &yourtm) != 0)
1187 if (mytm.tm_isdst != yourtm.tm_isdst)
1188 @@ -1626,17 +2171,27 @@ label:
1189 if ((newt < t) != (saved_seconds < 0))
1193 + if ((*funcp)(&t, offset, tmp) == NULL)
1195 +#else /* !__LP64__ */
1196 (*funcp)(&t, offset, tmp);
1197 +#endif /* __LP64__ */
1203 -time2(tmp, funcp, offset, okayp)
1204 +time2(tmp, funcp, offset, okayp, unix03)
1205 struct tm * const tmp;
1207 +struct tm *(* const funcp)(const time_t*, long, struct tm*);
1208 +#else /* !__LP64__ */
1209 void (* const funcp)(const time_t*, long, struct tm*);
1210 +#endif /* __LP64__ */
1217 @@ -1645,15 +2200,20 @@ int * const okayp;
1218 ** (in case tm_sec contains a value associated with a leap second).
1219 ** If that fails, try with normalization of seconds.
1221 - t = time2sub(tmp, funcp, offset, okayp, FALSE);
1222 - return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1223 + t = time2sub(tmp, funcp, offset, okayp, FALSE, unix03);
1224 + return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, unix03);
1228 -time1(tmp, funcp, offset)
1229 +__private_extern__ time_t
1230 +time1(tmp, funcp, offset, unix03)
1231 struct tm * const tmp;
1233 +struct tm *(* const funcp)(const time_t *, long, struct tm *);
1234 +#else /* !__LP64__ */
1235 void (* const funcp)(const time_t *, long, struct tm *);
1236 +#endif /* __LP64__ */
1241 const struct state * sp;
1242 @@ -1667,7 +2227,7 @@ const long offset;
1244 if (tmp->tm_isdst > 1)
1246 - t = time2(tmp, funcp, offset, &okay);
1247 + t = time2(tmp, funcp, offset, &okay, unix03);
1250 ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1251 @@ -1711,7 +2271,7 @@ const long offset;
1252 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1253 sp->ttis[samei].tt_gmtoff;
1254 tmp->tm_isdst = !tmp->tm_isdst;
1255 - t = time2(tmp, funcp, offset, &okay);
1256 + t = time2(tmp, funcp, offset, &okay, unix03);
1259 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1260 @@ -1721,19 +2281,25 @@ const long offset;
1264 +#else /* BUILDING_VARIANT */
1265 +__private_extern__ pthread_rwlock_t lcl_rwlock;
1266 +#endif /* BUILDING_VARIANT */
1270 struct tm * const tmp;
1272 time_t mktime_return_value;
1273 + int serrno = errno;
1274 _RWLOCK_RDLOCK(&lcl_rwlock);
1276 - mktime_return_value = time1(tmp, localsub, 0L);
1277 + mktime_return_value = time1(tmp, localsub, 0L, __DARWIN_UNIX03);
1278 _RWLOCK_UNLOCK(&lcl_rwlock);
1280 return(mktime_return_value);
1283 +#if !BUILDING_VARIANT
1287 @@ -1749,7 +2315,7 @@ timegm(tmp)
1288 struct tm * const tmp;
1291 - return time1(tmp, gmtsub, 0L);
1292 + return time1(tmp, gmtsub, 0L, __DARWIN_UNIX03);
1296 @@ -1758,7 +2324,7 @@ struct tm * const tmp;
1300 - return time1(tmp, gmtsub, offset);
1301 + return time1(tmp, gmtsub, offset, __DARWIN_UNIX03);
1304 #endif /* defined STD_INSPIRED */
1305 @@ -1858,3 +2424,4 @@ time_t t;
1308 #endif /* defined STD_INSPIRED */
1309 +#endif /* !BUILDING_VARIANT */