import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / localtime.c
blob861a5f81e538b17a4a05d69ef5619595a2d2c4a6
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1988 AT&T */
32 /* All Rights Reserved */
35 * A part of this file comes from public domain source, so
36 * clarified as of June 5, 1996 by Arthur David Olson
37 * (arthur_david_olson@nih.gov).
41 * localtime.c
43 * This file contains routines to convert struct tm to time_t and
44 * back as well as adjust time values based on their timezone, which
45 * is a local offset from GMT (Greenwich Mean Time).
47 * Many timezones actually consist of more than one offset from GMT.
48 * The GMT offset that is considered the normal offset is referred
49 * to as standard time. The other offset is referred to as alternate
50 * time, but is better known as daylight savings time or summer time.
52 * The current timezone for an application is derived from the TZ
53 * environment variable either as defined in the environment or in
54 * /etc/default/init. As defined by IEEE 1003.1-1990 (POSIX), the
55 * TZ variable can either be:
56 * :<characters>
57 * or
58 * <std><offset1>[<dst>[<offset2>]][,<start>[/<time>],<end>[/<time>]
60 * <characters> is an implementation-defined string that somehow describes
61 * a timezone. The implementation-defined description of a timezone used
62 * in Solaris is based on the public domain zoneinfo code available from
63 * elsie.nci.nih.gov and a timezone that is specified in this way is
64 * referred to as a zoneinfo timezone. An example of this is ":US/Pacific".
66 * The precise definition of the second format can be found in POSIX,
67 * but, basically, <std> is the abbreviation for the timezone in standard
68 * (not daylight savings time), <offset1> is the standard offset from GMT,
69 * <dst> is the abbreviation for the timezone in daylight savings time and
70 * <offset2> is the daylight savings time offset from GMT. The remainder
71 * specifies when daylight savings time begins and ends. A timezone
72 * specified in this way is referred to as a POSIX timezone. An example
73 * of this is "PST7PDT".
75 * In Solaris, there is an extension to this. If the timezone is not
76 * preceded by a ":" and it does not parse as a POSIX timezone, then it
77 * will be treated as a zoneinfo timezone. Much usage of zoneinfo
78 * timezones in Solaris is done without the leading ":".
80 * A zoneinfo timezone is a reference to a file that contains a set of
81 * rules that describe the timezone. In Solaris, the file is in
82 * /usr/share/lib/zoneinfo. The file is generated by zic(1M), based
83 * on zoneinfo rules "source" files. This is all described on the zic(1M)
84 * man page.
88 * Functions that are common to ctime(3C) and cftime(3C)
91 #pragma weak _tzset = tzset
93 #include "lint.h"
94 #include "libc.h"
95 #include "tsd.h"
96 #include <stdarg.h>
97 #include <mtlib.h>
98 #include <sys/types.h>
99 #include <ctype.h>
100 #include <stdio.h>
101 #include <limits.h>
102 #include <sys/param.h>
103 #include <time.h>
104 #include <unistd.h>
105 #include <stdlib.h>
106 #include <string.h>
107 #include <tzfile.h>
108 #include <thread.h>
109 #include <synch.h>
110 #include <fcntl.h>
111 #include <errno.h>
112 #include <deflt.h>
113 #include <sys/stat.h>
114 #include <sys/mman.h>
116 /* JAN_01_1902 cast to (int) - negative number of seconds from 1970 */
117 #define JAN_01_1902 (int)0x8017E880
118 #define LEN_TZDIR (sizeof (TZDIR) - 1)
119 #define TIMEZONE "/etc/default/init"
120 #define TZSTRING "TZ="
121 #define HASHTABLE 31
123 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
125 /* Days since 1/1/70 to 12/31/(1900 + Y - 1) */
126 #define DAYS_SINCE_70(Y) (YR((Y)-1L) - YR(70-1))
127 #define YR(X) /* Calc # days since 0 A.D. X = curr. yr - 1900 */ \
128 ((1900L + (X)) * 365L + (1900L + (X)) / 4L - \
129 (1900L + (X)) / 100L + ((1900L + (X)) - 1600L) / 400L)
133 * The following macros are replacements for detzcode(), which has
134 * been in the public domain versions of the localtime.c code for
135 * a long time. The primatives supporting the CVTZCODE macro are
136 * implemented differently for different endianness (ie. little
137 * vs. big endian) out of necessity, to account for the different
138 * byte ordering of the quantities being fetched. Both versions
139 * are substantially faster than the detzcode() macro. The big
140 * endian version is approx. 6.8x faster than detzcode(), the
141 * little endian version is approximately 3x faster, due to the
142 * extra shifting requiring to change byte order. The micro
143 * benchmarks used to compare were based on the SUNWSpro SC6.1
144 * (and later) compilers.
147 #if defined(__sparc) || defined(__sparcv9) /* big endian */
149 #define GET_LONG(p) \
150 *(uint_t *)(p)
152 #define GET_SHORTS(p) \
153 *(ushort_t *)(p) << 16 |\
154 *(ushort_t *)((p) + 2)
156 #define GET_CHARS(p) \
157 *(uchar_t *)(p) << 24 |\
158 *(uchar_t *)((p) + 1) << 16 |\
159 *(uchar_t *)((p) + 2) << 8 |\
160 *(uchar_t *)((p) + 3)
162 #else /* little endian */
164 #define GET_BYTE(x) \
165 ((x) & 0xff)
167 #define SWAP_BYTES(x) ((\
168 GET_BYTE(x) << 8) |\
169 GET_BYTE((x) >> 8))
171 #define SWAP_WORDS(x) ((\
172 SWAP_BYTES(x) << 16) |\
173 SWAP_BYTES((x) >> 16))
175 #define GET_LONG(p) \
176 SWAP_WORDS(*(uint_t *)(p))
178 #define GET_SHORTS(p) \
179 SWAP_BYTES(*(ushort_t *)(p)) << 16 |\
180 SWAP_BYTES(*(ushort_t *)((p) + 2))
182 #define GET_CHARS(p) \
183 GET_BYTE(*(uchar_t *)(p)) << 24 |\
184 GET_BYTE(*(uchar_t *)((p) + 1)) << 16 |\
185 GET_BYTE(*(uchar_t *)((p) + 2)) << 8 |\
186 GET_BYTE(*(uchar_t *)((p) + 3))
188 #endif
191 #define IF_ALIGNED(ptr, byte_alignment) \
192 !((uintptr_t)(ptr) & (byte_alignment - 1))
194 #define CVTZCODE(p) (int)(\
195 IF_ALIGNED(p, 4) ? GET_LONG(p) :\
196 IF_ALIGNED(p, 2) ? GET_SHORTS(p) : GET_CHARS(p));\
197 p += 4;
199 #ifndef FALSE
200 #define FALSE (0)
201 #endif
203 #ifndef TRUE
204 #define TRUE (1)
205 #endif
207 extern mutex_t _time_lock;
209 extern const int __lyday_to_month[];
210 extern const int __yday_to_month[];
211 extern const int __mon_lengths[2][MONS_PER_YEAR];
212 extern const int __year_lengths[2];
214 const char _tz_gmt[4] = "GMT"; /* "GMT" */
215 const char _tz_spaces[4] = " "; /* " " */
216 static const char _posix_gmt0[5] = "GMT0"; /* "GMT0" */
218 typedef struct ttinfo { /* Time type information */
219 long tt_gmtoff; /* GMT offset in seconds */
220 int tt_isdst; /* used to set tm_isdst */
221 int tt_abbrind; /* abbreviation list index */
222 int tt_ttisstd; /* TRUE if trans is std time */
223 int tt_ttisgmt; /* TRUE if transition is GMT */
224 } ttinfo_t;
226 typedef struct lsinfo { /* Leap second information */
227 time_t ls_trans; /* transition time */
228 long ls_corr; /* correction to apply */
229 } lsinfo_t;
231 typedef struct previnfo { /* Info about *prev* trans */
232 ttinfo_t *std; /* Most recent std type */
233 ttinfo_t *alt; /* Most recent alt type */
234 } prev_t;
236 typedef enum {
237 MON_WEEK_DOW, /* Mm.n.d - month, week, day of week */
238 JULIAN_DAY, /* Jn - Julian day */
239 DAY_OF_YEAR /* n - day of year */
240 } posrule_type_t;
242 typedef struct {
243 posrule_type_t r_type; /* type of rule */
244 int r_day; /* day number of rule */
245 int r_week; /* week number of rule */
246 int r_mon; /* month number of rule */
247 long r_time; /* transition time of rule */
248 } rule_t;
250 typedef struct {
251 rule_t *rules[2];
252 long offset[2];
253 long long rtime[2];
254 } posix_daylight_t;
257 * Note: ZONERULES_INVALID used for global curr_zonerules variable, but not
258 * for zonerules field of state_t.
260 typedef enum {
261 ZONERULES_INVALID, POSIX, POSIX_USA, ZONEINFO
262 } zone_rules_t;
265 * The following members are allocated from the libc-internal malloc:
267 * zonename
268 * chars
270 typedef struct state {
271 const char *zonename; /* Timezone */
272 struct state *next; /* next state */
273 zone_rules_t zonerules; /* Type of zone */
274 int daylight; /* daylight global */
275 long default_timezone; /* Def. timezone val */
276 long default_altzone; /* Def. altzone val */
277 const char *default_tzname0; /* Def tz..[0] val */
278 const char *default_tzname1; /* Def tz..[1] val */
279 int leapcnt; /* # leap sec trans */
280 int timecnt; /* # transitions */
281 int typecnt; /* # zone types */
282 int charcnt; /* # zone abbv. chars */
283 char *chars; /* Zone abbv. chars */
284 size_t charsbuf_size; /* malloc'ed buflen */
285 prev_t prev[TZ_MAX_TIMES]; /* Pv. trans info */
286 time_t ats[TZ_MAX_TIMES]; /* Trans. times */
287 uchar_t types[TZ_MAX_TIMES]; /* Type indices */
288 ttinfo_t ttis[TZ_MAX_TYPES]; /* Zone types */
289 lsinfo_t lsis[TZ_MAX_LEAPS]; /* Leap sec trans */
290 int last_ats_idx; /* last ats index */
291 rule_t start_rule; /* For POSIX w/rules */
292 rule_t end_rule; /* For POSIX w/rules */
293 } state_t;
295 typedef struct tznmlist {
296 struct tznmlist *link;
297 char name[1];
298 } tznmlist_t;
300 static const char *systemTZ;
301 static tznmlist_t *systemTZrec;
303 static const char *namecache;
305 static state_t *tzcache[HASHTABLE];
307 #define TZNMC_SZ 43
308 static tznmlist_t *tznmhash[TZNMC_SZ];
309 static const char *last_tzname[2];
311 static state_t *lclzonep;
313 static struct tm tm; /* For non-reentrant use */
314 static int is_in_dst; /* Set if t is in DST */
315 static zone_rules_t curr_zonerules = ZONERULES_INVALID;
316 static int cached_year; /* mktime() perf. enhancement */
317 static long long cached_secs_since_1970; /* mktime() perf. */
318 static int year_is_cached = FALSE; /* mktime() perf. */
320 #define TZSYNC_FILE "/var/run/tzsync"
321 static uint32_t zoneinfo_seqno;
322 static uint32_t zoneinfo_seqno_init = 1;
323 static uint32_t *zoneinfo_seqadr = &zoneinfo_seqno_init;
324 #define RELOAD_INFO() (zoneinfo_seqno != *zoneinfo_seqadr)
326 #define _2AM (2 * SECS_PER_HOUR)
327 #define FIRSTWEEK 1
328 #define LASTWEEK 5
330 enum wks {
331 _1st_week = 1,
332 _2nd_week,
333 _3rd_week,
334 _4th_week,
335 _Last_week
338 enum dwk {
339 Sun,
340 Mon,
341 Tue,
342 Wed,
343 Thu,
344 Fri,
348 enum mth {
349 Jan = 1,
350 Feb,
351 Mar,
352 Apr,
353 May,
354 Jun,
355 Jul,
356 Aug,
357 Sep,
358 Oct,
359 Nov,
364 * The following table defines standard USA DST transitions
365 * as they have been declared throughout history, disregarding
366 * the legally sanctioned local variants.
368 * Note: At some point, this table may be supplanted by
369 * more popular 'posixrules' logic.
371 typedef struct {
372 int s_year;
373 int e_year;
374 rule_t start;
375 rule_t end;
376 } __usa_rules_t;
378 static const __usa_rules_t __usa_rules[] = {
380 2007, 2037,
381 { MON_WEEK_DOW, Sun, _2nd_week, Mar, _2AM },
382 { MON_WEEK_DOW, Sun, _1st_week, Nov, _2AM },
385 1987, 2006,
386 { MON_WEEK_DOW, Sun, _1st_week, Apr, _2AM },
387 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM },
390 1976, 1986,
391 { MON_WEEK_DOW, Sun, _Last_week, Apr, _2AM },
392 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM },
395 1975, 1975,
396 { MON_WEEK_DOW, Sun, _Last_week, Feb, _2AM },
397 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM },
401 1974, 1974,
402 { MON_WEEK_DOW, Sun, _1st_week, Jan, _2AM },
403 { MON_WEEK_DOW, Sun, _Last_week, Nov, _2AM },
406 * The entry below combines two previously separate entries for
407 * 1969-1973 and 1902-1968
410 1902, 1973,
411 { MON_WEEK_DOW, Sun, _Last_week, Apr, _2AM },
412 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM },
415 #define MAX_RULE_TABLE (sizeof (__usa_rules) / sizeof (__usa_rules_t) - 1)
418 * Prototypes for static functions.
420 static const char *getsystemTZ(void);
421 static const char *getzname(const char *, int);
422 static const char *getnum(const char *, int *, int, int);
423 static const char *getsecs(const char *, long *);
424 static const char *getoffset(const char *, long *);
425 static const char *getrule(const char *, rule_t *, int);
426 static int load_posixinfo(const char *, state_t *);
427 static int load_zoneinfo(const char *, state_t *);
428 static void load_posix_transitions(state_t *, long, long, zone_rules_t);
429 static void adjust_posix_default(state_t *, long, long);
430 static void *ltzset_u(time_t);
431 static struct tm *offtime_u(time_t, long, struct tm *);
432 static int posix_check_dst(long long, state_t *);
433 static int posix_daylight(long long *, int, posix_daylight_t *);
434 static void set_zone_context(time_t);
435 static void reload_counter(void);
436 static void purge_zone_cache(void);
437 static void set_tzname(const char **);
440 * definition of difftime
442 * This code assumes time_t is type long. Note the difference of two
443 * longs in absolute value is representable as an unsigned long. So,
444 * compute the absolute value of the difference, cast the result to
445 * double and attach the sign back on.
447 * Note this code assumes 2's complement arithmetic. The subtraction
448 * operation may overflow when using signed operands, but when the
449 * result is cast to unsigned long, it yields the desired value
450 * (ie, the absolute value of the difference). The cast to unsigned
451 * long is done using pointers to avoid undefined behavior if casting
452 * a negative value to unsigned.
454 double
455 difftime(time_t time1, time_t time0)
457 if (time1 < time0) {
458 time0 -= time1;
459 return (-(double)*(unsigned long *) &time0);
460 } else {
461 time1 -= time0;
462 return ((double)*(unsigned long *) &time1);
467 * Accepts a time_t, returns a tm struct based on it, with
468 * no local timezone adjustment.
470 * This routine is the thread-safe variant of gmtime(), and
471 * requires that the call provide the address of their own tm
472 * struct.
474 * Locking is not done here because set_zone_context()
475 * is not called, thus timezone, altzone, and tzname[] are not
476 * accessed, no memory is allocated, and no common dynamic
477 * data is accessed.
479 * See ctime(3C)
481 struct tm *
482 gmtime_r(const time_t *timep, struct tm *p_tm)
484 return (offtime_u((time_t)*timep, 0L, p_tm));
488 * Accepts a time_t, returns a tm struct based on it, with
489 * no local timezone adjustment.
491 * This function is explicitly NOT THREAD-SAFE. The standards
492 * indicate it should provide its results in its own statically
493 * allocated tm struct that gets overwritten. The thread-safe
494 * variant is gmtime_r(). We make it mostly thread-safe by
495 * allocating its buffer in thread-specific data.
497 * See ctime(3C)
499 struct tm *
500 gmtime(const time_t *timep)
502 struct tm *p_tm = tsdalloc(_T_STRUCT_TM, sizeof (struct tm), NULL);
504 if (p_tm == NULL) /* memory allocation failure */
505 p_tm = &tm; /* use static buffer and hope for the best */
506 return (gmtime_r(timep, p_tm));
510 * This is the hashing function, based on the input timezone name.
512 static int
513 get_hashid(const char *id)
515 unsigned char c;
516 unsigned int h;
518 h = *id++;
519 while ((c = *id++) != '\0')
520 h += c;
521 return ((int)(h % HASHTABLE));
525 * find_zone() gets the hashid for zonename, then uses the hashid
526 * to search the hash table for the appropriate timezone entry. If
527 * the entry for zonename is found in the hash table, return a pointer
528 * to the entry.
530 static state_t *
531 find_zone(const char *zonename)
533 int hashid;
534 state_t *cur;
536 hashid = get_hashid(zonename);
537 cur = tzcache[hashid];
538 while (cur) {
539 int res;
540 res = strcmp(cur->zonename, zonename);
541 if (res == 0) {
542 return (cur);
543 } else if (res > 0) {
544 break;
546 cur = cur->next;
548 return (NULL);
552 * Register new state in the cache.
554 static void
555 reg_zone(state_t *new)
557 int hashid, res;
558 state_t *cur, *prv;
560 hashid = get_hashid(new->zonename);
561 cur = tzcache[hashid];
562 prv = NULL;
563 while (cur != NULL) {
564 res = strcmp(cur->zonename, new->zonename);
565 if (res == 0) {
566 /* impossible, but just in case */
567 return;
568 } else if (res > 0) {
569 break;
571 prv = cur;
572 cur = cur->next;
574 if (prv != NULL) {
575 new->next = prv->next;
576 prv->next = new;
577 } else {
578 new->next = tzcache[hashid];
579 tzcache[hashid] = new;
584 * Returns tm struct based on input time_t argument, correcting
585 * for the local timezone, producing documented side-effects
586 * to extern global state, timezone, altzone, daylight and tzname[].
588 * localtime_r() is the thread-safe variant of localtime().
590 * IMPLEMENTATION NOTE:
592 * Locking slows multithreaded access and is probably ultimately
593 * unnecessary here. The POSIX specification is a bit vague
594 * as to whether the extern variables set by tzset() need to
595 * set as a result of a call to localtime_r()
597 * Currently, the spec only mentions that tzname[] doesn't
598 * need to be set. As soon as it becomes unequivocal
599 * that the external zone state doesn't need to be asserted
600 * for this call, and it really doesn't make much sense
601 * to set common state from multi-threaded calls made to this
602 * function, locking can be dispensed with here.
604 * local zone state would still need to be aquired for the
605 * time in question in order for calculations elicited here
606 * to be correct, but that state wouldn't need to be shared,
607 * thus no multi-threaded synchronization would be required.
609 * It would be nice if POSIX would approve an ltzset_r()
610 * function, but if not, it wouldn't stop us from making one
611 * privately.
613 * localtime_r() can now return NULL if overflow is detected.
614 * offtime_u() is the function that detects overflow, and sets
615 * errno appropriately. We unlock before the call to offtime_u(),
616 * so that lmutex_unlock() does not reassign errno. The function
617 * offtime_u() is MT-safe and does not have to be locked. Use
618 * my_is_in_dst to reference local copy of is_in_dst outside locks.
620 * See ctime(3C)
622 struct tm *
623 localtime_r(const time_t *timep, struct tm *p_tm)
625 long offset;
626 struct tm *rt;
627 void *unused;
628 int my_is_in_dst;
630 lmutex_lock(&_time_lock);
631 unused = ltzset_u(*timep);
632 if (lclzonep == NULL) {
633 lmutex_unlock(&_time_lock);
634 free(unused);
635 return (offtime_u(*timep, 0L, p_tm));
637 my_is_in_dst = is_in_dst;
638 offset = (my_is_in_dst) ? -altzone : -timezone;
639 lmutex_unlock(&_time_lock);
640 free(unused);
641 rt = offtime_u(*timep, offset, p_tm);
642 p_tm->tm_isdst = my_is_in_dst;
643 return (rt);
647 * Accepts a time_t, returns a tm struct based on it, correcting
648 * for the local timezone. Produces documented side-effects to
649 * extern global timezone state data.
651 * This function is explicitly NOT THREAD-SAFE. The standards
652 * indicate it should provide its results in its own statically
653 * allocated tm struct that gets overwritten. The thread-safe
654 * variant is localtime_r(). We make it mostly thread-safe by
655 * allocating its buffer in thread-specific data.
657 * localtime() can now return NULL if overflow is detected.
658 * offtime_u() is the function that detects overflow, and sets
659 * errno appropriately.
661 * See ctime(3C)
663 struct tm *
664 localtime(const time_t *timep)
666 struct tm *p_tm = tsdalloc(_T_STRUCT_TM, sizeof (struct tm), NULL);
668 if (p_tm == NULL) /* memory allocation failure */
669 p_tm = &tm; /* use static buffer and hope for the best */
670 return (localtime_r(timep, p_tm));
674 * This function takes a pointer to a tm struct and returns a
675 * normalized time_t, also inducing documented side-effects in
676 * extern global zone state variables. (See mktime(3C)).
678 static time_t
679 mktime1(struct tm *tmptr, int usetz)
681 struct tm _tm;
682 long long t; /* must hold more than 32-bit time_t */
683 int temp;
684 int mketimerrno;
685 int overflow;
686 void *unused = NULL;
688 mketimerrno = errno;
690 /* mktime leaves errno unchanged if no error is encountered */
692 /* Calculate time_t from tm arg. tm may need to be normalized. */
693 t = tmptr->tm_sec + SECSPERMIN * tmptr->tm_min +
694 SECSPERHOUR * tmptr->tm_hour +
695 SECSPERDAY * (tmptr->tm_mday - 1);
697 if (tmptr->tm_mon >= 12) {
698 tmptr->tm_year += tmptr->tm_mon / 12;
699 tmptr->tm_mon %= 12;
700 } else if (tmptr->tm_mon < 0) {
701 temp = -tmptr->tm_mon;
702 tmptr->tm_mon = 0; /* If tm_mon divides by 12. */
703 tmptr->tm_year -= (temp / 12);
704 if (temp %= 12) { /* Remainder... */
705 tmptr->tm_year--;
706 tmptr->tm_mon = 12 - temp;
710 lmutex_lock(&_time_lock);
712 /* Avoid numerous calculations embedded in macro if possible */
713 if (!year_is_cached || (cached_year != tmptr->tm_year)) {
714 cached_year = tmptr->tm_year;
715 year_is_cached = TRUE;
716 /* For boundry values of tm_year, typecasting required */
717 cached_secs_since_1970 =
718 (long long)SECSPERDAY * DAYS_SINCE_70(cached_year);
720 t += cached_secs_since_1970;
722 if (isleap(tmptr->tm_year + TM_YEAR_BASE))
723 t += SECSPERDAY * __lyday_to_month[tmptr->tm_mon];
724 else
725 t += SECSPERDAY * __yday_to_month[tmptr->tm_mon];
728 if (usetz) {
730 * If called from mktime(), then we need to do the TZ
731 * related transformations.
734 unused = ltzset_u((time_t)t);
736 /* Attempt to convert time to GMT based on tm_isdst setting */
737 t += (tmptr->tm_isdst > 0) ? altzone : timezone;
739 #ifdef _ILP32
740 overflow = t > LONG_MAX || t < LONG_MIN ||
741 tmptr->tm_year < 1 || tmptr->tm_year > 138;
742 #else
743 overflow = t > LONG_MAX || t < LONG_MIN;
744 #endif
745 set_zone_context((time_t)t);
746 if (tmptr->tm_isdst < 0) {
747 long dst_delta = timezone - altzone;
748 switch (curr_zonerules) {
749 case ZONEINFO:
750 if (is_in_dst) {
751 t -= dst_delta;
752 set_zone_context((time_t)t);
753 if (is_in_dst) {
754 (void) offtime_u((time_t)t,
755 -altzone, &_tm);
756 _tm.tm_isdst = 1;
757 } else {
758 (void) offtime_u((time_t)t,
759 -timezone, &_tm);
761 } else {
762 (void) offtime_u((time_t)t, -timezone,
763 &_tm);
765 break;
766 case POSIX_USA:
767 case POSIX:
768 if (is_in_dst) {
769 t -= dst_delta;
770 set_zone_context((time_t)t);
771 if (is_in_dst) {
772 (void) offtime_u((time_t)t,
773 -altzone, &_tm);
774 _tm.tm_isdst = 1;
775 } else {
776 (void) offtime_u((time_t)t,
777 -timezone, &_tm);
779 } else {
781 * check for ambiguous
782 * 'fallback' transition
784 set_zone_context((time_t)t - dst_delta);
785 if (is_in_dst) {
786 /* In fallback, force DST */
787 t -= dst_delta;
788 (void) offtime_u((time_t)t,
789 -altzone, &_tm);
790 _tm.tm_isdst = 1;
791 } else {
792 (void) offtime_u((time_t)t,
793 -timezone, &_tm);
796 break;
798 case ZONERULES_INVALID:
799 (void) offtime_u((time_t)t, 0L, &_tm);
800 break;
803 } else if (is_in_dst) {
804 (void) offtime_u((time_t)t, -altzone, &_tm);
805 _tm.tm_isdst = 1;
806 } else {
807 (void) offtime_u((time_t)t, -timezone, &_tm);
810 } else { /* !usetz, i.e. using UTC */
811 overflow = 0;
812 /* Normalize the TM structure */
813 (void) offtime_u((time_t)t, 0, &_tm);
816 if (overflow || t > LONG_MAX || t < LONG_MIN) {
817 mketimerrno = EOVERFLOW;
818 t = -1;
819 } else {
820 *tmptr = _tm;
823 lmutex_unlock(&_time_lock);
824 free(unused);
826 errno = mketimerrno;
827 return ((time_t)t);
830 time_t
831 mktime(struct tm *tmptr)
833 return (mktime1(tmptr, TRUE));
836 time_t
837 timegm(struct tm *tmptr)
839 return (mktime1(tmptr, FALSE));
844 * Sets extern global zone state variables based on the current
845 * time. Specifically, tzname[], timezone, altzone, and daylight
846 * are updated. See ctime(3C) manpage.
848 void
849 tzset(void)
851 void *unused;
853 lmutex_lock(&_time_lock);
854 unused = ltzset_u(time(NULL));
855 lmutex_unlock(&_time_lock);
856 free(unused);
859 void
860 _ltzset(time_t tim)
862 void *unused;
864 lmutex_lock(&_time_lock);
865 unused = ltzset_u(tim);
866 lmutex_unlock(&_time_lock);
867 free(unused);
871 * Loads local zone information if TZ changed since last time zone
872 * information was loaded, or if this is the first time thru.
873 * We already hold _time_lock; no further locking is required.
874 * Return a memory block which can be free'd at safe place.
876 static void *
877 ltzset_u(time_t t)
879 const char *zonename;
880 state_t *entry, *new_entry;
881 const char *newtzname[2];
883 if (RELOAD_INFO()) {
884 reload_counter();
885 purge_zone_cache();
888 if ((zonename = getsystemTZ()) == NULL || *zonename == '\0')
889 zonename = _posix_gmt0;
891 if (namecache != NULL && strcmp(namecache, zonename) == 0) {
892 set_zone_context(t);
893 return (NULL);
896 entry = find_zone(zonename);
897 if (entry == NULL) {
899 * We need to release _time_lock to call out malloc().
900 * We can release _time_lock as far as global variables
901 * can remain consistent. Here, we haven't touch any
902 * variables, so it's okay to release lock.
904 lmutex_unlock(&_time_lock);
905 new_entry = malloc(sizeof (state_t));
906 lmutex_lock(&_time_lock);
909 * check it again, since zone may have been loaded while
910 * time_lock was unlocked.
912 entry = find_zone(zonename);
913 } else {
914 new_entry = NULL;
915 goto out;
919 * We are here because the 1st attemp failed.
920 * new_entry points newly allocated entry. If it was NULL, it
921 * indicates that the memory allocation also failed.
923 if (entry == NULL) {
925 * 2nd attemp also failed.
926 * No timezone entry found in hash table, so load it,
927 * and create a new timezone entry.
929 char *newzonename, *charsbuf;
931 newzonename = libc_strdup(zonename);
932 daylight = 0;
933 entry = new_entry;
935 if (entry == NULL || newzonename == NULL) {
936 /* something wrong happened. */
937 failed:
938 if (newzonename != NULL)
939 libc_free(newzonename);
941 /* Invalidate the current timezone */
942 curr_zonerules = ZONERULES_INVALID;
943 namecache = NULL;
945 timezone = altzone = 0;
946 is_in_dst = 0;
947 newtzname[0] = (char *)_tz_gmt;
948 newtzname[1] = (char *)_tz_spaces;
949 set_tzname(newtzname);
950 return (entry);
954 * Builds transition cache and sets up zone state data for zone
955 * specified in TZ, which can be specified as a POSIX zone or an
956 * Olson zoneinfo file reference.
958 * If local data cannot be parsed or loaded, the local zone
959 * tables are set up for GMT.
961 * Unless a leading ':' is prepended to TZ, TZ is initially
962 * parsed as a POSIX zone; failing that, it reverts to
963 * a zoneinfo check.
964 * However, if a ':' is prepended, the zone will *only* be
965 * parsed as zoneinfo. If any failure occurs parsing or
966 * loading a zoneinfo TZ, GMT data is loaded for the local zone.
968 * Example: There is a zoneinfo file in the standard
969 * distribution called 'PST8PDT'. The only way the user can
970 * specify that file under Solaris is to set TZ to ":PST8PDT".
971 * Otherwise the initial parse of PST8PDT as a POSIX zone will
972 * succeed and be used.
974 if ((charsbuf = libc_malloc(TZ_MAX_CHARS)) == NULL)
975 goto failed;
977 entry->zonerules = ZONERULES_INVALID;
978 entry->charsbuf_size = TZ_MAX_CHARS;
979 entry->chars = charsbuf;
980 entry->default_tzname0 = _tz_gmt;
981 entry->default_tzname1 = _tz_spaces;
982 entry->zonename = newzonename;
984 if (*zonename == ':') {
985 if (load_zoneinfo(zonename + 1, entry) != 0) {
986 (void) load_posixinfo(_posix_gmt0, entry);
988 } else if (load_posixinfo(zonename, entry) != 0) {
989 if (load_zoneinfo(zonename, entry) != 0) {
990 (void) load_posixinfo(_posix_gmt0, entry);
993 entry->last_ats_idx = -1;
996 * The pre-allocated buffer is used; reset the free flag
997 * so the buffer won't be freed.
999 reg_zone(entry);
1000 new_entry = NULL;
1003 out:
1004 curr_zonerules = entry->zonerules;
1005 namecache = entry->zonename;
1006 daylight = entry->daylight;
1007 lclzonep = entry;
1009 set_zone_context(t);
1012 * We shouldn't release lock beyond this point since lclzonep
1013 * can refer to invalid address if cache is invalidated.
1014 * We defer the call to free till it can be done safely.
1016 return (new_entry);
1020 * Sets timezone, altzone, tzname[], extern globals, to represent
1021 * disposition of t with respect to TZ; See ctime(3C). is_in_dst,
1022 * internal global is also set. daylight is set at zone load time.
1024 * Issues:
1026 * In this function, any time_t not located in the cache is handled
1027 * as a miss. To build/update transition cache, load_zoneinfo()
1028 * must be called prior to this routine.
1030 * If POSIX zone, cache miss penalty is slightly degraded
1031 * performance. For zoneinfo, penalty is decreased is_in_dst
1032 * accuracy.
1034 * POSIX, despite its chicken/egg problem, ie. not knowing DST
1035 * until time known, and not knowing time until DST known, at
1036 * least uses the same algorithm for 64-bit time as 32-bit.
1038 * The fact that zoneinfo files only contain transistions for 32-bit
1039 * time space is a well known problem, as yet unresolved.
1040 * Without an official standard for coping with out-of-range
1041 * zoneinfo times, assumptions must be made. For now
1042 * the assumption is: If t exceeds 32-bit boundries and local zone
1043 * is zoneinfo type, is_in_dst is set to to 0 for negative values
1044 * of t, and set to the same DST state as the highest ordered
1045 * transition in cache for positive values of t.
1047 static void
1048 set_zone_default_context(void)
1050 const char *newtzname[2];
1052 /* Retrieve suitable defaults for this zone */
1053 altzone = lclzonep->default_altzone;
1054 timezone = lclzonep->default_timezone;
1055 newtzname[0] = (char *)lclzonep->default_tzname0;
1056 newtzname[1] = (char *)lclzonep->default_tzname1;
1057 is_in_dst = 0;
1059 set_tzname(newtzname);
1062 static void
1063 set_zone_context(time_t t)
1065 prev_t *prevp;
1066 int lo, hi, tidx, lidx;
1067 ttinfo_t *ttisp, *std, *alt;
1068 const char *newtzname[2];
1070 /* If state data not loaded or TZ busted, just use GMT */
1071 if (lclzonep == NULL || curr_zonerules == ZONERULES_INVALID) {
1072 timezone = altzone = 0;
1073 daylight = is_in_dst = 0;
1074 newtzname[0] = (char *)_tz_gmt;
1075 newtzname[1] = (char *)_tz_spaces;
1076 set_tzname(newtzname);
1077 return;
1080 if (lclzonep->timecnt <= 0 || lclzonep->typecnt < 2) {
1081 /* Loaded zone incapable of transitioning. */
1082 set_zone_default_context();
1083 return;
1087 * At least one alt. zone and one transistion exist. Locate
1088 * state for 't' quickly as possible. Use defaults as necessary.
1090 lo = 0;
1091 hi = lclzonep->timecnt - 1;
1093 if (t < lclzonep->ats[0] || t >= lclzonep->ats[hi]) {
1095 * Date which is out of definition.
1096 * Calculate DST as best as possible
1098 if (lclzonep->zonerules == POSIX_USA ||
1099 lclzonep->zonerules == POSIX) {
1100 /* Must invoke calculations to determine DST */
1101 set_zone_default_context();
1102 is_in_dst = (daylight) ?
1103 posix_check_dst(t, lclzonep) : 0;
1104 return;
1105 } else if (t < lclzonep->ats[0]) { /* zoneinfo... */
1106 /* t precedes 1st transition. Use defaults */
1107 set_zone_default_context();
1108 return;
1109 } else { /* zoneinfo */
1110 /* t follows final transistion. Use final */
1111 tidx = hi;
1113 } else {
1114 if ((lidx = lclzonep->last_ats_idx) != -1 &&
1115 lidx != hi &&
1116 t >= lclzonep->ats[lidx] &&
1117 t < lclzonep->ats[lidx + 1]) {
1118 /* CACHE HIT. Nothing needs to be done */
1119 tidx = lidx;
1120 } else {
1122 * CACHE MISS. Locate transition using binary search.
1124 while (lo <= hi) {
1125 tidx = (lo + hi) / 2;
1126 if (t == lclzonep->ats[tidx])
1127 break;
1128 else if (t < lclzonep->ats[tidx])
1129 hi = tidx - 1;
1130 else
1131 lo = tidx + 1;
1133 if (lo > hi)
1134 tidx = hi;
1139 * Set extern globals based on located transition and summary of
1140 * its previous state, which were cached when zone was loaded
1142 ttisp = &lclzonep->ttis[lclzonep->types[tidx]];
1143 prevp = &lclzonep->prev[tidx];
1145 if ((is_in_dst = ttisp->tt_isdst) == 0) { /* std. time */
1146 timezone = -ttisp->tt_gmtoff;
1147 newtzname[0] = &lclzonep->chars[ttisp->tt_abbrind];
1148 if ((alt = prevp->alt) != NULL) {
1149 altzone = -alt->tt_gmtoff;
1150 newtzname[1] = &lclzonep->chars[alt->tt_abbrind];
1151 } else {
1152 altzone = lclzonep->default_altzone;
1153 newtzname[1] = (char *)lclzonep->default_tzname1;
1155 } else { /* alt. time */
1156 altzone = -ttisp->tt_gmtoff;
1157 newtzname[1] = &lclzonep->chars[ttisp->tt_abbrind];
1158 if ((std = prevp->std) != NULL) {
1159 timezone = -std->tt_gmtoff;
1160 newtzname[0] = &lclzonep->chars[std->tt_abbrind];
1161 } else {
1162 timezone = lclzonep->default_timezone;
1163 newtzname[0] = (char *)lclzonep->default_tzname0;
1167 lclzonep->last_ats_idx = tidx;
1168 set_tzname(newtzname);
1172 * This function takes a time_t and gmt offset and produces a
1173 * tm struct based on specified time.
1175 * The the following fields are calculated, based entirely
1176 * on the offset-adjusted value of t:
1178 * tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec
1179 * tm_yday. tm_wday. (tm_isdst is ALWAYS set to 0).
1182 static struct tm *
1183 offtime_u(time_t t, long offset, struct tm *tmptr)
1185 long days;
1186 long rem;
1187 long y;
1188 int yleap;
1189 const int *ip;
1191 days = t / SECSPERDAY;
1192 rem = t % SECSPERDAY;
1193 rem += offset;
1194 while (rem < 0) {
1195 rem += SECSPERDAY;
1196 --days;
1198 while (rem >= SECSPERDAY) {
1199 rem -= SECSPERDAY;
1200 ++days;
1202 tmptr->tm_hour = (int)(rem / SECSPERHOUR);
1203 rem = rem % SECSPERHOUR;
1204 tmptr->tm_min = (int)(rem / SECSPERMIN);
1205 tmptr->tm_sec = (int)(rem % SECSPERMIN);
1207 tmptr->tm_wday = (int)((EPOCH_WDAY + days) % DAYSPERWEEK);
1208 if (tmptr->tm_wday < 0)
1209 tmptr->tm_wday += DAYSPERWEEK;
1210 y = EPOCH_YEAR;
1211 while (days < 0 || days >= (long)__year_lengths[yleap = isleap(y)]) {
1212 long newy;
1214 newy = y + days / DAYSPERNYEAR;
1215 if (days < 0)
1216 --newy;
1217 days -= ((long)newy - (long)y) * DAYSPERNYEAR +
1218 LEAPS_THRU_END_OF(newy > 0 ? newy - 1L : newy) -
1219 LEAPS_THRU_END_OF(y > 0 ? y - 1L : y);
1220 y = newy;
1222 tmptr->tm_year = (int)(y - TM_YEAR_BASE);
1223 tmptr->tm_yday = (int)days;
1224 ip = __mon_lengths[yleap];
1225 for (tmptr->tm_mon = 0; days >=
1226 (long)ip[tmptr->tm_mon]; ++(tmptr->tm_mon)) {
1227 days = days - (long)ip[tmptr->tm_mon];
1229 tmptr->tm_mday = (int)(days + 1);
1230 tmptr->tm_isdst = 0;
1232 #ifdef _LP64
1233 /* do as much as possible before checking for error. */
1234 if ((y > (long)INT_MAX + TM_YEAR_BASE) ||
1235 (y < (long)INT_MIN + TM_YEAR_BASE)) {
1236 errno = EOVERFLOW;
1237 return (NULL);
1239 #endif
1240 return (tmptr);
1244 * Check whether DST is set for time in question. Only applies to
1245 * POSIX timezones. If explicit POSIX transition rules were provided
1246 * for the current zone, use those, otherwise use default USA POSIX
1247 * transitions.
1249 static int
1250 posix_check_dst(long long t, state_t *sp)
1252 struct tm gmttm;
1253 long long jan01;
1254 int year, i, idx, ridx;
1255 posix_daylight_t pdaylight;
1257 (void) offtime_u(t, 0L, &gmttm);
1259 year = gmttm.tm_year + 1900;
1260 jan01 = t - ((gmttm.tm_yday * SECSPERDAY) +
1261 (gmttm.tm_hour * SECSPERHOUR) +
1262 (gmttm.tm_min * SECSPERMIN) + gmttm.tm_sec);
1264 * If transition rules were provided for this zone,
1265 * use them, otherwise, default to USA daylight rules,
1266 * which are historically correct for the continental USA,
1267 * excluding local provisions. (This logic may be replaced
1268 * at some point in the future with "posixrules" to offer
1269 * more flexibility to the system administrator).
1271 if (sp->zonerules == POSIX) { /* POSIX rules */
1272 pdaylight.rules[0] = &sp->start_rule;
1273 pdaylight.rules[1] = &sp->end_rule;
1274 } else { /* POSIX_USA: USA */
1275 i = 0;
1276 while (year < __usa_rules[i].s_year && i < MAX_RULE_TABLE) {
1277 i++;
1279 pdaylight.rules[0] = (rule_t *)&__usa_rules[i].start;
1280 pdaylight.rules[1] = (rule_t *)&__usa_rules[i].end;
1282 pdaylight.offset[0] = timezone;
1283 pdaylight.offset[1] = altzone;
1285 idx = posix_daylight(&jan01, year, &pdaylight);
1286 ridx = !idx;
1289 * Note: t, rtime[0], and rtime[1] are all bounded within 'year'
1290 * beginning on 'jan01'
1292 if (t >= pdaylight.rtime[idx] && t < pdaylight.rtime[ridx]) {
1293 return (ridx);
1294 } else {
1295 return (idx);
1300 * Given January 1, 00:00:00 GMT for a year as an Epoch-relative time,
1301 * along with the integer year #, a posix_daylight_t that is composed
1302 * of two rules, and two GMT offsets (timezone and altzone), calculate
1303 * the two Epoch-relative times the two rules take effect, and return
1304 * them in the two rtime fields of the posix_daylight_t structure.
1305 * Also update janfirst by a year, by adding the appropriate number of
1306 * seconds depending on whether the year is a leap year or not. (We take
1307 * advantage that this routine knows the leap year status.)
1309 static int
1310 posix_daylight(long long *janfirst, int year, posix_daylight_t *pdaylightp)
1312 rule_t *rulep;
1313 long offset;
1314 int idx;
1315 int i, d, m1, yy0, yy1, yy2, dow;
1316 long leapyear;
1317 long long value;
1319 static const int __secs_year_lengths[2] = {
1320 DAYS_PER_NYEAR * SECSPERDAY,
1321 DAYS_PER_LYEAR * SECSPERDAY
1324 leapyear = isleap(year);
1326 for (idx = 0; idx < 2; idx++) {
1327 rulep = pdaylightp->rules[idx];
1328 offset = pdaylightp->offset[idx];
1330 switch (rulep->r_type) {
1332 case MON_WEEK_DOW:
1334 * Mm.n.d - nth "dth day" of month m.
1336 value = *janfirst;
1337 for (i = 0; i < rulep->r_mon - 1; ++i)
1338 value += __mon_lengths[leapyear][i] *
1339 SECSPERDAY;
1342 * Use Zeller's Congruence to get day-of-week of first
1343 * day of month.
1345 m1 = (rulep->r_mon + 9) % 12 + 1;
1346 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
1347 yy1 = yy0 / 100;
1348 yy2 = yy0 % 100;
1349 dow = ((26 * m1 - 2) / 10 +
1350 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
1352 if (dow < 0)
1353 dow += DAYSPERWEEK;
1356 * Following heuristic increases accuracy of USA rules
1357 * for negative years.
1359 if (year < 1 && leapyear)
1360 ++dow;
1362 * "dow" is the day-of-week of the first day of the
1363 * month. Get the day-of-month, zero-origin, of the
1364 * first "dow" day of the month.
1366 d = rulep->r_day - dow;
1367 if (d < 0)
1368 d += DAYSPERWEEK;
1369 for (i = 1; i < rulep->r_week; ++i) {
1370 if (d + DAYSPERWEEK >=
1371 __mon_lengths[leapyear][rulep->r_mon - 1])
1372 break;
1373 d += DAYSPERWEEK;
1376 * "d" is the day-of-month, zero-origin, of the day
1377 * we want.
1379 value += d * SECSPERDAY;
1380 break;
1382 case JULIAN_DAY:
1384 * Jn - Julian day, 1 == Jan 1, 60 == March 1 even
1385 * in leap yrs.
1387 value = *janfirst + (rulep->r_day - 1) * SECSPERDAY;
1388 if (leapyear && rulep->r_day >= 60)
1389 value += SECSPERDAY;
1390 break;
1392 case DAY_OF_YEAR:
1394 * n - day of year.
1396 value = *janfirst + rulep->r_day * SECSPERDAY;
1397 break;
1399 pdaylightp->rtime[idx] = value + rulep->r_time + offset;
1401 *janfirst += __secs_year_lengths[leapyear];
1403 return ((pdaylightp->rtime[0] > pdaylightp->rtime[1]) ? 1 : 0);
1407 * Try to load zoneinfo file into internal transition tables using name
1408 * indicated in TZ, and do validity checks. The format of zic(1M)
1409 * compiled zoneinfo files isdescribed in tzfile.h
1411 static int
1412 load_zoneinfo(const char *name, state_t *sp)
1414 char *cp;
1415 char *cp2;
1416 int i;
1417 long cnt;
1418 int fid;
1419 int ttisstdcnt;
1420 int ttisgmtcnt;
1421 char *fullname;
1422 size_t namelen;
1423 char *bufp;
1424 size_t flen;
1425 prev_t *prevp;
1426 /* LINTED */
1427 struct tzhead *tzhp;
1428 struct stat64 stbuf;
1429 ttinfo_t *most_recent_alt = NULL;
1430 ttinfo_t *most_recent_std = NULL;
1431 ttinfo_t *ttisp;
1434 if (name == NULL && (name = TZDEFAULT) == NULL)
1435 return (-1);
1437 if ((name[0] == '/') || strstr(name, "../"))
1438 return (-1);
1441 * We allocate fullname this way to avoid having
1442 * a PATH_MAX size buffer in our stack frame.
1444 namelen = LEN_TZDIR + 1 + strlen(name) + 1;
1445 if ((fullname = lmalloc(namelen)) == NULL)
1446 return (-1);
1447 (void) strcpy(fullname, TZDIR "/");
1448 (void) strcpy(fullname + LEN_TZDIR + 1, name);
1449 if ((fid = open(fullname, O_RDONLY)) == -1) {
1450 lfree(fullname, namelen);
1451 return (-1);
1453 lfree(fullname, namelen);
1455 if (fstat64(fid, &stbuf) == -1) {
1456 (void) close(fid);
1457 return (-1);
1460 flen = (size_t)stbuf.st_size;
1461 if (flen < sizeof (struct tzhead)) {
1462 (void) close(fid);
1463 return (-1);
1467 * It would be nice to use alloca() to allocate bufp but,
1468 * as above, we wish to avoid allocating a big buffer in
1469 * our stack frame, and also because alloca() gives us no
1470 * opportunity to fail gracefully on allocation failure.
1472 cp = bufp = lmalloc(flen);
1473 if (bufp == NULL) {
1474 (void) close(fid);
1475 return (-1);
1478 if ((cnt = read(fid, bufp, flen)) != flen) {
1479 lfree(bufp, flen);
1480 (void) close(fid);
1481 return (-1);
1484 if (close(fid) != 0) {
1485 lfree(bufp, flen);
1486 return (-1);
1489 cp += (sizeof (tzhp->tzh_magic)) + (sizeof (tzhp->tzh_reserved));
1491 /* LINTED: alignment */
1492 ttisstdcnt = CVTZCODE(cp);
1493 /* LINTED: alignment */
1494 ttisgmtcnt = CVTZCODE(cp);
1495 /* LINTED: alignment */
1496 sp->leapcnt = CVTZCODE(cp);
1497 /* LINTED: alignment */
1498 sp->timecnt = CVTZCODE(cp);
1499 /* LINTED: alignment */
1500 sp->typecnt = CVTZCODE(cp);
1501 /* LINTED: alignment */
1502 sp->charcnt = CVTZCODE(cp);
1504 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
1505 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
1506 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
1507 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
1508 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
1509 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) {
1510 lfree(bufp, flen);
1511 return (-1);
1514 if (cnt - (cp - bufp) < (long)(sp->timecnt * 4 + /* ats */
1515 sp->timecnt + /* types */
1516 sp->typecnt * (4 + 2) + /* ttinfos */
1517 sp->charcnt + /* chars */
1518 sp->leapcnt * (4 + 4) + /* lsinfos */
1519 ttisstdcnt + /* ttisstds */
1520 ttisgmtcnt)) { /* ttisgmts */
1521 lfree(bufp, flen);
1522 return (-1);
1526 for (i = 0; i < sp->timecnt; ++i) {
1527 /* LINTED: alignment */
1528 sp->ats[i] = CVTZCODE(cp);
1532 * Skip over types[] for now and load ttis[] so that when
1533 * types[] are loaded we can check for transitions to STD & DST.
1534 * This allows us to shave cycles in ltzset_u(), including
1535 * eliminating the need to check set 'daylight' later.
1538 cp2 = (char *)((uintptr_t)cp + sp->timecnt);
1540 for (i = 0; i < sp->typecnt; ++i) {
1541 ttisp = &sp->ttis[i];
1542 /* LINTED: alignment */
1543 ttisp->tt_gmtoff = CVTZCODE(cp2);
1544 ttisp->tt_isdst = (uchar_t)*cp2++;
1546 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) {
1547 lfree(bufp, flen);
1548 return (-1);
1551 ttisp->tt_abbrind = (uchar_t)*cp2++;
1552 if (ttisp->tt_abbrind < 0 ||
1553 ttisp->tt_abbrind > sp->charcnt) {
1554 lfree(bufp, flen);
1555 return (-1);
1560 * Since ttis were loaded ahead of types, it is possible to
1561 * detect whether daylight is ever set for this zone now, and
1562 * also preload other information to avoid repeated lookups later.
1563 * This logic facilitates keeping a running tab on the state of
1564 * std zone and alternate zone transitions such that timezone,
1565 * altzone and tzname[] can be determined quickly via an
1566 * index to any transition.
1568 * For transition #0 there are no previous transitions,
1569 * so prev->std and prev->alt will be null, but that's OK,
1570 * because null prev->std/prev->alt effectively
1571 * indicates none existed prior.
1574 prevp = &sp->prev[0];
1576 for (i = 0; i < sp->timecnt; ++i) {
1578 sp->types[i] = (uchar_t)*cp++;
1579 ttisp = &sp->ttis[sp->types[i]];
1581 prevp->std = most_recent_std;
1582 prevp->alt = most_recent_alt;
1584 if (ttisp->tt_isdst == 1) {
1585 most_recent_alt = ttisp;
1586 } else {
1587 most_recent_std = ttisp;
1590 if ((int)sp->types[i] >= sp->typecnt) {
1591 lfree(bufp, flen);
1592 return (-1);
1595 ++prevp;
1597 if (most_recent_alt == NULL)
1598 sp->daylight = 0;
1599 else
1600 sp->daylight = 1;
1603 * Set pointer ahead to where it would have been if we
1604 * had read types[] and ttis[] in the same order they
1605 * occurred in the file.
1607 cp = cp2;
1608 for (i = 0; i < sp->charcnt; ++i)
1609 sp->chars[i] = *cp++;
1611 sp->chars[i] = '\0'; /* ensure '\0' at end */
1613 for (i = 0; i < sp->leapcnt; ++i) {
1614 struct lsinfo *lsisp;
1616 lsisp = &sp->lsis[i];
1617 /* LINTED: alignment */
1618 lsisp->ls_trans = CVTZCODE(cp);
1619 /* LINTED: alignment */
1620 lsisp->ls_corr = CVTZCODE(cp);
1623 for (i = 0; i < sp->typecnt; ++i) {
1624 ttisp = &sp->ttis[i];
1625 if (ttisstdcnt == 0) {
1626 ttisp->tt_ttisstd = FALSE;
1627 } else {
1628 ttisp->tt_ttisstd = *cp++;
1629 if (ttisp->tt_ttisstd != TRUE &&
1630 ttisp->tt_ttisstd != FALSE) {
1631 lfree(bufp, flen);
1632 return (-1);
1637 for (i = 0; i < sp->typecnt; ++i) {
1638 ttisp = &sp->ttis[i];
1639 if (ttisgmtcnt == 0) {
1640 ttisp->tt_ttisgmt = FALSE;
1641 } else {
1642 ttisp->tt_ttisgmt = *cp++;
1643 if (ttisp->tt_ttisgmt != TRUE &&
1644 ttisp->tt_ttisgmt != FALSE) {
1645 lfree(bufp, flen);
1646 return (-1);
1652 * Other defaults set at beginning of this routine
1653 * to cover case where zoneinfo file cannot be loaded
1655 sp->default_timezone = -sp->ttis[0].tt_gmtoff;
1656 sp->default_altzone = 0;
1657 sp->default_tzname0 = &sp->chars[0];
1658 sp->default_tzname1 = _tz_spaces;
1660 lfree(bufp, flen);
1662 sp->zonerules = ZONEINFO;
1664 return (0);
1667 #ifdef _TZ_DEBUG
1668 static void
1669 print_state(state_t *sp)
1671 struct tm tmp;
1672 int i, c;
1674 (void) fprintf(stderr, "=========================================\n");
1675 (void) fprintf(stderr, "zonename: \"%s\"\n", sp->zonename);
1676 (void) fprintf(stderr, "next: 0x%p\n", (void *)sp->next);
1677 (void) fprintf(stderr, "zonerules: %s\n",
1678 sp->zonerules == ZONERULES_INVALID ? "ZONERULES_INVALID" :
1679 sp->zonerules == POSIX ? "POSIX" :
1680 sp->zonerules == POSIX_USA ? "POSIX_USA" :
1681 sp->zonerules == ZONEINFO ? "ZONEINFO" : "UNKNOWN");
1682 (void) fprintf(stderr, "daylight: %d\n", sp->daylight);
1683 (void) fprintf(stderr, "default_timezone: %ld\n", sp->default_timezone);
1684 (void) fprintf(stderr, "default_altzone: %ld\n", sp->default_altzone);
1685 (void) fprintf(stderr, "default_tzname0: \"%s\"\n",
1686 sp->default_tzname0);
1687 (void) fprintf(stderr, "default_tzname1: \"%s\"\n",
1688 sp->default_tzname1);
1689 (void) fprintf(stderr, "leapcnt: %d\n", sp->leapcnt);
1690 (void) fprintf(stderr, "timecnt: %d\n", sp->timecnt);
1691 (void) fprintf(stderr, "typecnt: %d\n", sp->typecnt);
1692 (void) fprintf(stderr, "charcnt: %d\n", sp->charcnt);
1693 (void) fprintf(stderr, "chars: \"%s\"\n", sp->chars);
1694 (void) fprintf(stderr, "charsbuf_size: %u\n", sp->charsbuf_size);
1695 (void) fprintf(stderr, "prev: skipping...\n");
1696 (void) fprintf(stderr, "ats = {\n");
1697 for (c = 0, i = 0; i < sp->timecnt; i++) {
1698 char buf[26];
1699 if (c != 0) {
1700 (void) fprintf(stderr, ", ");
1702 (void) asctime_r(gmtime_r(&sp->ats[i], &tmp), buf);
1703 buf[strcspn(buf, "\n")] = '\0';
1704 (void) fprintf(stderr, "%s", buf);
1705 if (c == 1) {
1706 (void) fprintf(stderr, "\n");
1707 c = 0;
1708 } else {
1709 c++;
1712 (void) fprintf(stderr, "}\n");
1713 (void) fprintf(stderr, "types = {\n");
1714 for (c = 0, i = 0; i < sp->timecnt; i++) {
1715 if (c == 0) {
1716 (void) fprintf(stderr, "\t");
1717 } else {
1718 (void) fprintf(stderr, ", ");
1720 (void) fprintf(stderr, "%d", sp->types[i]);
1721 if (c == 7) {
1722 (void) fprintf(stderr, "\n");
1723 c = 0;
1724 } else {
1725 c++;
1728 (void) fprintf(stderr, "}\n");
1729 (void) fprintf(stderr, "ttis = {\n");
1730 for (i = 0; i < sp->typecnt; i++) {
1731 (void) fprintf(stderr, "\t{\n");
1732 (void) fprintf(stderr, "\t\ttt_gmtoff: %ld\n",
1733 sp->ttis[i].tt_gmtoff);
1734 (void) fprintf(stderr, "\t\ttt_ttisdst: %d\n",
1735 sp->ttis[i].tt_isdst);
1736 (void) fprintf(stderr, "\t\ttt_abbrind: %d\n",
1737 sp->ttis[i].tt_abbrind);
1738 (void) fprintf(stderr, "\t\ttt_tt_isstd: %d\n",
1739 sp->ttis[i].tt_ttisstd);
1740 (void) fprintf(stderr, "\t\ttt_ttisgmt: %d\n",
1741 sp->ttis[i].tt_ttisgmt);
1742 (void) fprintf(stderr, "\t}\n");
1744 (void) fprintf(stderr, "}\n");
1746 #endif
1749 * Given a POSIX section 8-style TZ string, fill in transition tables.
1751 * Examples:
1753 * TZ = PST8 or GMT0
1754 * Timecnt set to 0 and typecnt set to 1, reflecting std time only.
1756 * TZ = PST8PDT or PST8PDT7
1757 * Create transition times by applying USA transitions from
1758 * Jan 1 of each year covering 1902-2038. POSIX offsets
1759 * as specified in the TZ are used to calculate the tt_gmtoff
1760 * for each of the two zones. If ommitted, DST defaults to
1761 * std. time minus one hour.
1763 * TZ = <PST8>8PDT or <PST8>8<PDT9>
1764 * Quoted transition. The values in angled brackets are treated
1765 * as zone name text, not parsed as offsets. The offsets
1766 * occuring following the zonename section. In this way,
1767 * instead of PST being displayed for standard time, it could
1768 * be displayed as PST8 to give an indication of the offset
1769 * of that zone to GMT.
1771 * TZ = GMT0BST, M3.5.0/1, M10.5.0/2 or GMT0BST, J23953, J23989
1772 * Create transition times based on the application new-year
1773 * relative POSIX transitions, parsed from TZ, from Jan 1
1774 * for each year covering 1902-2038. POSIX offsets specified
1775 * in TZ are used to calculate tt_gmtoff for each of the two
1776 * zones.
1779 static int
1780 load_posixinfo(const char *name, state_t *sp)
1782 const char *stdname;
1783 const char *dstname = 0;
1784 size_t stdlen;
1785 size_t dstlen;
1786 long stdoff = 0;
1787 long dstoff = 0;
1788 char *cp;
1789 int i;
1790 ttinfo_t *dst;
1791 ttinfo_t *std;
1792 int quoted;
1793 zone_rules_t zonetype;
1796 zonetype = POSIX_USA;
1797 stdname = name;
1799 if ((quoted = (*stdname == '<')) != 0)
1800 ++stdname;
1802 /* Parse/extract STD zone name, len and GMT offset */
1803 if (*name != '\0') {
1804 if ((name = getzname(name, quoted)) == NULL)
1805 return (-1);
1806 stdlen = name - stdname;
1807 if (*name == '>')
1808 ++name;
1809 if (*name == '\0' || stdlen < 1) {
1810 return (-1);
1811 } else {
1812 if ((name = getoffset(name, &stdoff)) == NULL)
1813 return (-1);
1817 /* If DST specified in TZ, extract DST zone details */
1818 if (*name != '\0') {
1820 dstname = name;
1821 if ((quoted = (*dstname == '<')) != 0)
1822 ++dstname;
1823 if ((name = getzname(name, quoted)) == NULL)
1824 return (-1);
1825 dstlen = name - dstname;
1826 if (dstlen < 1)
1827 return (-1);
1828 if (*name == '>')
1829 ++name;
1830 if (*name != '\0' && *name != ',' && *name != ';') {
1831 if ((name = getoffset(name, &dstoff)) == NULL)
1832 return (-1);
1833 } else {
1834 dstoff = stdoff - SECSPERHOUR;
1837 if (*name != ',' && *name != ';') {
1838 /* no transtition specified; using default rule */
1839 if (load_zoneinfo(TZDEFRULES, sp) == 0 &&
1840 sp->daylight == 1) {
1841 /* loading TZDEFRULES zoneinfo succeeded */
1842 adjust_posix_default(sp, stdoff, dstoff);
1843 } else {
1844 /* loading TZDEFRULES zoneinfo failed */
1845 load_posix_transitions(sp, stdoff, dstoff,
1846 zonetype);
1848 } else {
1849 /* extract POSIX transitions from TZ */
1850 /* Backward compatibility using ';' separator */
1851 int compat_flag = (*name == ';');
1852 ++name;
1853 if ((name = getrule(name, &sp->start_rule, compat_flag))
1854 == NULL)
1855 return (-1);
1856 if (*name++ != ',')
1857 return (-1);
1858 if ((name = getrule(name, &sp->end_rule, compat_flag))
1859 == NULL)
1860 return (-1);
1861 if (*name != '\0')
1862 return (-1);
1863 zonetype = POSIX;
1864 load_posix_transitions(sp, stdoff, dstoff, zonetype);
1866 dst = &sp->ttis[0];
1867 std = &sp->ttis[1];
1868 } else { /* DST wasn't specified in POSIX TZ */
1870 /* Since we only have STD time, there are no transitions */
1871 dstlen = 0;
1872 sp->daylight = 0;
1873 sp->typecnt = 1;
1874 sp->timecnt = 0;
1875 std = &sp->ttis[0];
1876 std->tt_gmtoff = -stdoff;
1877 std->tt_isdst = 0;
1880 /* Setup zone name character data for state table */
1881 sp->charcnt = (int)(stdlen + 1);
1882 if (dstlen != 0)
1883 sp->charcnt += dstlen + 1;
1885 /* If bigger than zone name abbv. buffer, grow it */
1886 if ((size_t)sp->charcnt > sp->charsbuf_size) {
1887 if ((cp = libc_realloc(sp->chars, sp->charcnt)) == NULL)
1888 return (-1);
1889 sp->chars = cp;
1890 sp->charsbuf_size = sp->charcnt;
1894 * Copy zone name text null-terminatedly into state table.
1895 * By doing the copy once during zone loading, setting
1896 * tzname[] subsequently merely involves setting pointer
1898 * If either or both std. or alt. zone name < 3 chars,
1899 * space pad the deficient name(s) to right.
1902 std->tt_abbrind = 0;
1903 cp = sp->chars;
1904 (void) strncpy(cp, stdname, stdlen);
1905 while (stdlen < 3)
1906 cp[stdlen++] = ' ';
1907 cp[stdlen] = '\0';
1909 i = (int)(stdlen + 1);
1910 if (dstlen != 0) {
1911 dst->tt_abbrind = i;
1912 cp += i;
1913 (void) strncpy(cp, dstname, dstlen);
1914 while (dstlen < 3)
1915 cp[dstlen++] = ' ';
1916 cp[dstlen] = '\0';
1919 /* Save default values */
1920 if (sp->typecnt == 1) {
1921 sp->default_timezone = stdoff;
1922 sp->default_altzone = stdoff;
1923 sp->default_tzname0 = &sp->chars[0];
1924 sp->default_tzname1 = _tz_spaces;
1925 } else {
1926 sp->default_timezone = -std->tt_gmtoff;
1927 sp->default_altzone = -dst->tt_gmtoff;
1928 sp->default_tzname0 = &sp->chars[std->tt_abbrind];
1929 sp->default_tzname1 = &sp->chars[dst->tt_abbrind];
1932 sp->zonerules = zonetype;
1934 return (0);
1938 * We loaded the TZDEFAULT which usually the one in US zones. We
1939 * adjust the GMT offset for the zone which has stdoff/dstoff
1940 * offset.
1942 static void
1943 adjust_posix_default(state_t *sp, long stdoff, long dstoff)
1945 long zone_stdoff = 0;
1946 long zone_dstoff = 0;
1947 int zone_stdoff_flag = 0;
1948 int zone_dstoff_flag = 0;
1949 int isdst;
1950 int i;
1953 * Initial values of zone_stdoff and zone_dstoff
1955 for (i = 0; (zone_stdoff_flag == 0 || zone_dstoff_flag == 0) &&
1956 i < sp->timecnt; i++) {
1957 ttinfo_t *zone;
1959 zone = &sp->ttis[sp->types[i]];
1961 if (zone_stdoff_flag == 0 && zone->tt_isdst == 0) {
1962 zone_stdoff = -zone->tt_gmtoff;
1963 zone_stdoff_flag = 1;
1964 } else if (zone_dstoff_flag == 0 && zone->tt_isdst != 0) {
1965 zone_dstoff = -zone->tt_gmtoff;
1966 zone_dstoff_flag = 1;
1969 if (zone_dstoff_flag == 0)
1970 zone_dstoff = zone_stdoff;
1973 * Initially we're assumed to be in standard time.
1975 isdst = 0;
1977 for (i = 0; i < sp->timecnt; i++) {
1978 ttinfo_t *zone;
1979 int next_isdst;
1981 zone = &sp->ttis[sp->types[i]];
1982 next_isdst = zone->tt_isdst;
1984 sp->types[i] = next_isdst ? 0 : 1;
1986 if (zone->tt_ttisgmt == 0) {
1988 * If summer time is in effect, and the transition time
1989 * was not specified as standard time, add the summer
1990 * time offset to the transition time;
1991 * otherwise, add the standard time offset to the
1992 * transition time.
1995 * Transitions from DST to DDST will effectively
1996 * disappear since POSIX provides for only one DST
1997 * offset.
1999 if (isdst != 0 && zone->tt_ttisstd == 0)
2000 sp->ats[i] += dstoff - zone_dstoff;
2001 else
2002 sp->ats[i] += stdoff - zone_stdoff;
2004 if (next_isdst != 0)
2005 zone_dstoff = -zone->tt_gmtoff;
2006 else
2007 zone_stdoff = -zone->tt_gmtoff;
2008 isdst = next_isdst;
2011 * Finally, fill in ttis.
2012 * ttisstd and ttisgmt need not be handled.
2014 sp->ttis[0].tt_gmtoff = -dstoff;
2015 sp->ttis[0].tt_isdst = 1;
2016 sp->ttis[1].tt_gmtoff = -stdoff;
2017 sp->ttis[1].tt_isdst = 0;
2018 sp->typecnt = 2;
2019 sp->daylight = 1;
2025 static void
2026 load_posix_transitions(state_t *sp, long stdoff, long dstoff,
2027 zone_rules_t zonetype)
2029 ttinfo_t *std, *dst;
2030 time_t *tranp;
2031 uchar_t *typep;
2032 prev_t *prevp;
2033 int year;
2034 int i;
2035 long long janfirst;
2036 posix_daylight_t pdaylight;
2039 * We know STD and DST zones are specified with this timezone
2040 * therefore the cache will be set up with 2 transitions per
2041 * year transitioning to their respective std and dst zones.
2043 sp->daylight = 1;
2044 sp->typecnt = 2;
2045 sp->timecnt = 272;
2048 * Insert zone data from POSIX TZ into state table
2049 * The Olson public domain POSIX code sets up ttis[0] to be DST,
2050 * as we are doing here. It seems to be the correct behavior.
2051 * The US/Pacific zoneinfo file also lists DST as first type.
2054 dst = &sp->ttis[0];
2055 dst->tt_gmtoff = -dstoff;
2056 dst->tt_isdst = 1;
2058 std = &sp->ttis[1];
2059 std->tt_gmtoff = -stdoff;
2060 std->tt_isdst = 0;
2062 sp->prev[0].std = NULL;
2063 sp->prev[0].alt = NULL;
2065 /* Create transition data based on POSIX TZ */
2066 tranp = sp->ats;
2067 prevp = &sp->prev[1];
2068 typep = sp->types;
2071 * We only cache from 1902 to 2037 to avoid transistions
2072 * that wrap at the 32-bit boundries, since 1901 and 2038
2073 * are not full years in 32-bit time. The rough edges
2074 * will be handled as transition cache misses.
2077 janfirst = JAN_01_1902;
2079 pdaylight.rules[0] = &sp->start_rule;
2080 pdaylight.rules[1] = &sp->end_rule;
2081 pdaylight.offset[0] = stdoff;
2082 pdaylight.offset[1] = dstoff;
2084 for (i = MAX_RULE_TABLE; i >= 0; i--) {
2085 if (zonetype == POSIX_USA) {
2086 pdaylight.rules[0] = (rule_t *)&__usa_rules[i].start;
2087 pdaylight.rules[1] = (rule_t *)&__usa_rules[i].end;
2089 for (year = __usa_rules[i].s_year;
2090 year <= __usa_rules[i].e_year; year++) {
2091 int idx, ridx;
2092 idx = posix_daylight(&janfirst, year, &pdaylight);
2093 ridx = !idx;
2096 * Two transitions per year. Since there are
2097 * only two zone types for this POSIX zone,
2098 * previous std and alt are always set to
2099 * &ttis[0] and &ttis[1].
2101 *tranp++ = (time_t)pdaylight.rtime[idx];
2102 *typep++ = idx;
2103 prevp->std = std;
2104 prevp->alt = dst;
2105 ++prevp;
2107 *tranp++ = (time_t)pdaylight.rtime[ridx];
2108 *typep++ = ridx;
2109 prevp->std = std;
2110 prevp->alt = dst;
2111 ++prevp;
2117 * Given a pointer into a time zone string, scan until a character that is not
2118 * a valid character in a zone name is found. Return ptr to that character.
2119 * Return NULL if error (ie. non-printable character located in name)
2121 static const char *
2122 getzname(const char *strp, int quoted)
2124 char c;
2126 if (quoted) {
2127 while ((c = *strp) != '\0' && c != '>' &&
2128 isgraph((unsigned char)c)) {
2129 ++strp;
2131 } else {
2132 while ((c = *strp) != '\0' && isgraph((unsigned char)c) &&
2133 !isdigit((unsigned char)c) && c != ',' && c != '-' &&
2134 c != '+') {
2135 ++strp;
2139 /* Found an excessively invalid character. Discredit whole name */
2140 if (c != '\0' && !isgraph((unsigned char)c))
2141 return (NULL);
2143 return (strp);
2147 * Given pointer into time zone string, extract first
2148 * number pointed to. Validate number within range specified,
2149 * Return ptr to first char following valid numeric sequence.
2151 static const char *
2152 getnum(const char *strp, int *nump, int min, int max)
2154 char c;
2155 int num;
2157 if (strp == NULL || !isdigit((unsigned char)(c = *strp)))
2158 return (NULL);
2159 num = 0;
2160 do {
2161 num = num * 10 + (c - '0');
2162 if (num > max)
2163 return (NULL); /* illegal value */
2164 c = *++strp;
2165 } while (isdigit((unsigned char)c));
2166 if (num < min)
2167 return (NULL); /* illegal value */
2168 *nump = num;
2169 return (strp);
2173 * Given a pointer into a time zone string, extract a number of seconds,
2174 * in hh[:mm[:ss]] form, from the string. If an error occurs, return NULL,
2175 * otherwise, return a pointer to the first character not part of the number
2176 * of seconds.
2178 static const char *
2179 getsecs(const char *strp, long *secsp)
2181 int num;
2184 * `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
2185 * "M10.4.6/26", which does not conform to Posix,
2186 * but which specifies the equivalent of
2187 * ``02:00 on the first Sunday on or after 23 Oct''.
2189 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
2190 if (strp == NULL)
2191 return (NULL);
2192 *secsp = num * (long)SECSPERHOUR;
2193 if (*strp == ':') {
2194 ++strp;
2195 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
2196 if (strp == NULL)
2197 return (NULL);
2198 *secsp += num * SECSPERMIN;
2199 if (*strp == ':') {
2200 ++strp;
2201 /* `SECSPERMIN' allows for leap seconds. */
2202 strp = getnum(strp, &num, 0, SECSPERMIN);
2203 if (strp == NULL)
2204 return (NULL);
2205 *secsp += num;
2208 return (strp);
2212 * Given a pointer into a time zone string, extract an offset, in
2213 * [+-]hh[:mm[:ss]] form, from the string.
2214 * If any error occurs, return NULL.
2215 * Otherwise, return a pointer to the first character not part of the time.
2217 static const char *
2218 getoffset(const char *strp, long *offsetp)
2220 int neg = 0;
2222 if (*strp == '-') {
2223 neg = 1;
2224 ++strp;
2225 } else if (*strp == '+') {
2226 ++strp;
2228 strp = getsecs(strp, offsetp);
2229 if (strp == NULL)
2230 return (NULL); /* illegal time */
2231 if (neg)
2232 *offsetp = -*offsetp;
2233 return (strp);
2237 * Given a pointer into a time zone string, extract a rule in the form
2238 * date[/time]. See POSIX section 8 for the format of "date" and "time".
2239 * If a valid rule is not found, return NULL.
2240 * Otherwise, return a pointer to the first character not part of the rule.
2242 * If compat_flag is set, support old 1-based day of year values.
2244 static const char *
2245 getrule(const char *strp, rule_t *rulep, int compat_flag)
2247 if (compat_flag == 0 && *strp == 'M') {
2249 * Month, week, day.
2251 rulep->r_type = MON_WEEK_DOW;
2252 ++strp;
2253 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
2254 if (strp == NULL)
2255 return (NULL);
2256 if (*strp++ != '.')
2257 return (NULL);
2258 strp = getnum(strp, &rulep->r_week, 1, 5);
2259 if (strp == NULL)
2260 return (NULL);
2261 if (*strp++ != '.')
2262 return (NULL);
2263 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
2264 } else if (compat_flag == 0 && *strp == 'J') {
2266 * Julian day.
2268 rulep->r_type = JULIAN_DAY;
2269 ++strp;
2270 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
2272 } else if (isdigit((unsigned char)*strp)) {
2274 * Day of year.
2276 rulep->r_type = DAY_OF_YEAR;
2277 if (compat_flag == 0) {
2278 /* zero-based day of year */
2279 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
2280 } else {
2281 /* one-based day of year */
2282 strp = getnum(strp, &rulep->r_day, 1, DAYSPERLYEAR);
2283 rulep->r_day--;
2285 } else {
2286 return (NULL); /* ZONERULES_INVALID format */
2288 if (strp == NULL)
2289 return (NULL);
2290 if (*strp == '/') {
2292 * Time specified.
2294 ++strp;
2295 strp = getsecs(strp, &rulep->r_time);
2296 } else {
2297 rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
2299 return (strp);
2303 * Returns default value for TZ as specified in /etc/default/init file, if
2304 * a default value for TZ is provided there.
2306 static char *
2307 get_default_tz(void)
2309 char *tz = NULL;
2310 uchar_t *tzp, *tzq;
2311 int flags;
2312 void *defp;
2314 assert_no_libc_locks_held();
2316 if ((defp = defopen_r(TIMEZONE)) != NULL) {
2317 flags = defcntl_r(DC_GETFLAGS, 0, defp);
2318 TURNON(flags, DC_STRIP_QUOTES);
2319 (void) defcntl_r(DC_SETFLAGS, flags, defp);
2321 if ((tzp = (uchar_t *)defread_r(TZSTRING, defp)) != NULL) {
2322 while (isspace(*tzp))
2323 tzp++;
2324 tzq = tzp;
2325 while (!isspace(*tzq) &&
2326 *tzq != ';' &&
2327 *tzq != '#' &&
2328 *tzq != '\0')
2329 tzq++;
2330 *tzq = '\0';
2331 if (*tzp != '\0')
2332 tz = libc_strdup((char *)tzp);
2335 defclose_r(defp);
2337 return (tz);
2341 * Purge all cache'd state_t
2343 static void
2344 purge_zone_cache(void)
2346 int hashid;
2347 state_t *p, *n, *r;
2350 * Create a single list of caches which are detached
2351 * from hash table.
2353 r = NULL;
2354 for (hashid = 0; hashid < HASHTABLE; hashid++) {
2355 for (p = tzcache[hashid]; p != NULL; p = n) {
2356 n = p->next;
2357 p->next = r;
2358 r = p;
2360 tzcache[hashid] = NULL;
2362 namecache = NULL;
2364 /* last_tzname[] may point cache being freed */
2365 last_tzname[0] = NULL;
2366 last_tzname[1] = NULL;
2368 /* We'll reload system TZ as well */
2369 systemTZ = NULL;
2372 * Hash table has been cleared, and all elements are detached from
2373 * the hash table. Now we are safe to release _time_lock.
2374 * We need to unlock _time_lock because we need to call out to
2375 * free().
2377 lmutex_unlock(&_time_lock);
2379 assert_no_libc_locks_held();
2381 while (r != NULL) {
2382 n = r->next;
2383 libc_free((char *)r->zonename);
2384 libc_free((char *)r->chars);
2385 free(r);
2386 r = n;
2389 lmutex_lock(&_time_lock);
2393 * When called first time, open the counter device and load
2394 * the initial value. If counter is updated, copy value to
2395 * private memory.
2397 static void
2398 reload_counter(void)
2400 int fd;
2401 caddr_t addr;
2403 if (zoneinfo_seqadr != &zoneinfo_seqno_init) {
2404 zoneinfo_seqno = *zoneinfo_seqadr;
2405 return;
2408 if ((fd = open(TZSYNC_FILE, O_RDONLY)) < 0)
2409 return;
2411 addr = mmap(NULL, sizeof (uint32_t), PROT_READ, MAP_SHARED, fd, 0);
2412 (void) close(fd);
2414 if (addr == MAP_FAILED)
2415 return;
2416 /*LINTED*/
2417 zoneinfo_seqadr = (uint32_t *)addr;
2418 zoneinfo_seqno = *zoneinfo_seqadr;
2422 * getsystemTZ() returns the TZ value if it is set in the environment, or
2423 * it returns the system TZ; if the systemTZ has not yet been set, or
2424 * cleared by tzreload, get_default_tz() is called to read the
2425 * /etc/default/init file to get the value.
2427 static const char *
2428 getsystemTZ()
2430 tznmlist_t *tzn;
2431 char *tz;
2433 tz = getenv("TZ");
2434 if (tz != NULL && *tz != '\0')
2435 return ((const char *)tz);
2437 if (systemTZ != NULL)
2438 return (systemTZ);
2441 * get_default_tz calls out stdio functions via defread.
2443 lmutex_unlock(&_time_lock);
2444 tz = get_default_tz();
2445 lmutex_lock(&_time_lock);
2447 if (tz == NULL) {
2448 /* no TZ entry in the file */
2449 systemTZ = _posix_gmt0;
2450 return (systemTZ);
2454 * look up timezone used previously. We will not free the
2455 * old timezone name, because ltzset_u() can release _time_lock
2456 * while it has references to systemTZ (via zonename). If we
2457 * free the systemTZ, the reference via zonename can access
2458 * invalid memory when systemTZ is reset.
2460 for (tzn = systemTZrec; tzn != NULL; tzn = tzn->link) {
2461 if (strcmp(tz, tzn->name) == 0)
2462 break;
2464 if (tzn == NULL) {
2465 /* This is new timezone name */
2466 tzn = lmalloc(sizeof (tznmlist_t *) + strlen(tz) + 1);
2467 (void) strcpy(tzn->name, tz);
2468 tzn->link = systemTZrec;
2469 systemTZrec = tzn;
2472 libc_free(tz);
2474 return (systemTZ = tzn->name);
2478 * tzname[] is the user visible string which applications may have
2479 * references. Even though TZ was changed, references to the old tzname
2480 * may continue to remain in the application, and those references need
2481 * to be valid. They were valid by our implementation because strings being
2482 * pointed by tzname were never be freed nor altered by the change of TZ.
2483 * However, this will no longer be the case.
2485 * state_t is now freed when cache is purged. Therefore, reading string
2486 * from old tzname[] addr may end up with accessing a stale data(freed area).
2487 * To avoid this, we maintain a copy of all timezone name strings which will
2488 * never be freed, and tzname[] will point those copies.
2491 static int
2492 set_one_tzname(const char *name, int idx)
2494 const unsigned char *nm;
2495 int hashid, i;
2496 char *s;
2497 tznmlist_t *tzn;
2499 if (name == _tz_gmt || name == _tz_spaces) {
2500 tzname[idx] = (char *)name;
2501 return (0);
2504 nm = (const unsigned char *)name;
2505 hashid = (nm[0] * 29 + nm[1] * 3) % TZNMC_SZ;
2506 for (tzn = tznmhash[hashid]; tzn != NULL; tzn = tzn->link) {
2507 s = tzn->name;
2508 /* do the strcmp() */
2509 for (i = 0; s[i] == name[i]; i++) {
2510 if (s[i] == '\0') {
2511 tzname[idx] = tzn->name;
2512 return (0);
2517 * allocate new entry. This entry is never freed, so use lmalloc
2519 tzn = lmalloc(sizeof (tznmlist_t *) + strlen(name) + 1);
2520 if (tzn == NULL)
2521 return (1);
2523 (void) strcpy(tzn->name, name);
2525 /* link it */
2526 tzn->link = tznmhash[hashid];
2527 tznmhash[hashid] = tzn;
2529 tzname[idx] = tzn->name;
2530 return (0);
2534 * Set tzname[] after testing parameter to see if we are setting
2535 * same zone name. If we got same address, it should be same zone
2536 * name as tzname[], unless cache have been purged.
2537 * Note, purge_zone_cache() resets last_tzname[].
2539 static void
2540 set_tzname(const char **namep)
2542 if (namep[0] != last_tzname[0]) {
2543 if (set_one_tzname(namep[0], 0)) {
2544 tzname[0] = (char *)_tz_gmt;
2545 last_tzname[0] = NULL;
2546 } else {
2547 last_tzname[0] = namep[0];
2551 if (namep[1] != last_tzname[1]) {
2552 if (set_one_tzname(namep[1], 1)) {
2553 tzname[1] = (char *)_tz_spaces;
2554 last_tzname[1] = NULL;
2555 } else {
2556 last_tzname[1] = namep[1];