xref: /minix3/lib/libc/time/localtime.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: localtime.c,v 1.98 2015/10/09 17:21:45 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras ** This file is in the public domain, so clarified as of
52fe8fb19SBen Gras ** 1996-06-05 by Arthur David Olson.
62fe8fb19SBen Gras */
72fe8fb19SBen Gras 
82fe8fb19SBen Gras #include <sys/cdefs.h>
92fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
102fe8fb19SBen Gras #if 0
11f14fb602SLionel Sambuc static char	elsieid[] = "@(#)localtime.c	8.17";
122fe8fb19SBen Gras #else
13*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: localtime.c,v 1.98 2015/10/09 17:21:45 christos Exp $");
142fe8fb19SBen Gras #endif
152fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
162fe8fb19SBen Gras 
172fe8fb19SBen Gras /*
182fe8fb19SBen Gras ** Leap second handling from Bradley White.
192fe8fb19SBen Gras ** POSIX-style TZ environment variable handling from Guy Harris.
202fe8fb19SBen Gras */
212fe8fb19SBen Gras 
222fe8fb19SBen Gras /*LINTLIBRARY*/
232fe8fb19SBen Gras 
242fe8fb19SBen Gras #include "namespace.h"
2584d9c625SLionel Sambuc #include <assert.h>
26*0a6a1f1dSLionel Sambuc #define LOCALTIME_IMPLEMENTATION
272fe8fb19SBen Gras #include "private.h"
28*0a6a1f1dSLionel Sambuc 
292fe8fb19SBen Gras #include "tzfile.h"
302fe8fb19SBen Gras #include "fcntl.h"
312fe8fb19SBen Gras #include "reentrant.h"
322fe8fb19SBen Gras 
33*0a6a1f1dSLionel Sambuc #if NETBSD_INSPIRED
34*0a6a1f1dSLionel Sambuc # define NETBSD_INSPIRED_EXTERN
35*0a6a1f1dSLionel Sambuc #else
36*0a6a1f1dSLionel Sambuc # define NETBSD_INSPIRED_EXTERN static
37*0a6a1f1dSLionel Sambuc #endif
38*0a6a1f1dSLionel Sambuc 
392fe8fb19SBen Gras #if defined(__weak_alias)
402fe8fb19SBen Gras __weak_alias(daylight,_daylight)
412fe8fb19SBen Gras __weak_alias(tzname,_tzname)
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #ifndef TZ_ABBR_MAX_LEN
452fe8fb19SBen Gras #define TZ_ABBR_MAX_LEN	16
462fe8fb19SBen Gras #endif /* !defined TZ_ABBR_MAX_LEN */
472fe8fb19SBen Gras 
482fe8fb19SBen Gras #ifndef TZ_ABBR_CHAR_SET
492fe8fb19SBen Gras #define TZ_ABBR_CHAR_SET \
502fe8fb19SBen Gras 	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
512fe8fb19SBen Gras #endif /* !defined TZ_ABBR_CHAR_SET */
522fe8fb19SBen Gras 
532fe8fb19SBen Gras #ifndef TZ_ABBR_ERR_CHAR
542fe8fb19SBen Gras #define TZ_ABBR_ERR_CHAR	'_'
552fe8fb19SBen Gras #endif /* !defined TZ_ABBR_ERR_CHAR */
562fe8fb19SBen Gras 
572fe8fb19SBen Gras /*
582fe8fb19SBen Gras ** SunOS 4.1.1 headers lack O_BINARY.
592fe8fb19SBen Gras */
602fe8fb19SBen Gras 
612fe8fb19SBen Gras #ifdef O_BINARY
62*0a6a1f1dSLionel Sambuc #define OPEN_MODE	(O_RDONLY | O_BINARY | O_CLOEXEC)
632fe8fb19SBen Gras #endif /* defined O_BINARY */
642fe8fb19SBen Gras #ifndef O_BINARY
65*0a6a1f1dSLionel Sambuc #define OPEN_MODE	(O_RDONLY | O_CLOEXEC)
662fe8fb19SBen Gras #endif /* !defined O_BINARY */
672fe8fb19SBen Gras 
682fe8fb19SBen Gras #ifndef WILDABBR
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras ** Someone might make incorrect use of a time zone abbreviation:
712fe8fb19SBen Gras **	1.	They might reference tzname[0] before calling tzset (explicitly
722fe8fb19SBen Gras **		or implicitly).
732fe8fb19SBen Gras **	2.	They might reference tzname[1] before calling tzset (explicitly
742fe8fb19SBen Gras **		or implicitly).
752fe8fb19SBen Gras **	3.	They might reference tzname[1] after setting to a time zone
762fe8fb19SBen Gras **		in which Daylight Saving Time is never observed.
772fe8fb19SBen Gras **	4.	They might reference tzname[0] after setting to a time zone
782fe8fb19SBen Gras **		in which Standard Time is never observed.
792fe8fb19SBen Gras **	5.	They might reference tm.TM_ZONE after calling offtime.
802fe8fb19SBen Gras ** What's best to do in the above cases is open to debate;
812fe8fb19SBen Gras ** for now, we just set things up so that in any of the five cases
822fe8fb19SBen Gras ** WILDABBR is used. Another possibility: initialize tzname[0] to the
832fe8fb19SBen Gras ** string "tzname[0] used before set", and similarly for the other cases.
842fe8fb19SBen Gras ** And another: initialize tzname[0] to "ERA", with an explanation in the
852fe8fb19SBen Gras ** manual page of what this "time zone abbreviation" means (doing this so
862fe8fb19SBen Gras ** that tzname[0] has the "normal" length of three characters).
872fe8fb19SBen Gras */
882fe8fb19SBen Gras #define WILDABBR	"   "
892fe8fb19SBen Gras #endif /* !defined WILDABBR */
902fe8fb19SBen Gras 
912fe8fb19SBen Gras static const char	wildabbr[] = WILDABBR;
922fe8fb19SBen Gras 
9384d9c625SLionel Sambuc static const char	gmt[] = "GMT";
942fe8fb19SBen Gras 
952fe8fb19SBen Gras /*
962fe8fb19SBen Gras ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
972fe8fb19SBen Gras ** We default to US rules as of 1999-08-17.
982fe8fb19SBen Gras ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
992fe8fb19SBen Gras ** implementation dependent; for historical reasons, US rules are a
1002fe8fb19SBen Gras ** common default.
1012fe8fb19SBen Gras */
1022fe8fb19SBen Gras #ifndef TZDEFRULESTRING
1032fe8fb19SBen Gras #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
1042fe8fb19SBen Gras #endif /* !defined TZDEFDST */
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras struct ttinfo {				/* time type information */
10784d9c625SLionel Sambuc 	int_fast32_t	tt_gmtoff;	/* UT offset in seconds */
108*0a6a1f1dSLionel Sambuc 	bool		tt_isdst;	/* used to set tm_isdst */
1092fe8fb19SBen Gras 	int		tt_abbrind;	/* abbreviation list index */
110*0a6a1f1dSLionel Sambuc 	bool		tt_ttisstd;	/* transition is std time */
111*0a6a1f1dSLionel Sambuc 	bool		tt_ttisgmt;	/* transition is UT */
1122fe8fb19SBen Gras };
1132fe8fb19SBen Gras 
1142fe8fb19SBen Gras struct lsinfo {				/* leap second information */
1152fe8fb19SBen Gras 	time_t		ls_trans;	/* transition time */
11684d9c625SLionel Sambuc 	int_fast64_t	ls_corr;	/* correction to apply */
1172fe8fb19SBen Gras };
1182fe8fb19SBen Gras 
119*0a6a1f1dSLionel Sambuc #define SMALLEST(a, b)	(((a) < (b)) ? (a) : (b))
1202fe8fb19SBen Gras #define BIGGEST(a, b)	(((a) > (b)) ? (a) : (b))
1212fe8fb19SBen Gras 
1222fe8fb19SBen Gras #ifdef TZNAME_MAX
1232fe8fb19SBen Gras #define MY_TZNAME_MAX	TZNAME_MAX
1242fe8fb19SBen Gras #endif /* defined TZNAME_MAX */
1252fe8fb19SBen Gras #ifndef TZNAME_MAX
1262fe8fb19SBen Gras #define MY_TZNAME_MAX	255
1272fe8fb19SBen Gras #endif /* !defined TZNAME_MAX */
1282fe8fb19SBen Gras 
129*0a6a1f1dSLionel Sambuc #define state __state
130*0a6a1f1dSLionel Sambuc struct state {
1312fe8fb19SBen Gras 	int		leapcnt;
1322fe8fb19SBen Gras 	int		timecnt;
1332fe8fb19SBen Gras 	int		typecnt;
1342fe8fb19SBen Gras 	int		charcnt;
135*0a6a1f1dSLionel Sambuc 	bool		goback;
136*0a6a1f1dSLionel Sambuc 	bool		goahead;
1372fe8fb19SBen Gras 	time_t		ats[TZ_MAX_TIMES];
1382fe8fb19SBen Gras 	unsigned char	types[TZ_MAX_TIMES];
1392fe8fb19SBen Gras 	struct ttinfo	ttis[TZ_MAX_TYPES];
14084d9c625SLionel Sambuc 	char		chars[/*CONSTCOND*/BIGGEST(BIGGEST(TZ_MAX_CHARS + 1,
14184d9c625SLionel Sambuc 				sizeof gmt), (2 * (MY_TZNAME_MAX + 1)))];
1422fe8fb19SBen Gras 	struct lsinfo	lsis[TZ_MAX_LEAPS];
14384d9c625SLionel Sambuc 	int		defaulttype; /* for early times or if no transitions */
1442fe8fb19SBen Gras };
1452fe8fb19SBen Gras 
146*0a6a1f1dSLionel Sambuc enum r_type {
147*0a6a1f1dSLionel Sambuc   JULIAN_DAY,		/* Jn = Julian day */
148*0a6a1f1dSLionel Sambuc   DAY_OF_YEAR,		/* n = day of year */
149*0a6a1f1dSLionel Sambuc   MONTH_NTH_DAY_OF_WEEK	/* Mm.n.d = month, week, day of week */
150*0a6a1f1dSLionel Sambuc };
151*0a6a1f1dSLionel Sambuc 
1522fe8fb19SBen Gras struct rule {
153*0a6a1f1dSLionel Sambuc 	enum r_type	r_type;		/* type of rule */
1542fe8fb19SBen Gras 	int		r_day;		/* day number of rule */
1552fe8fb19SBen Gras 	int		r_week;		/* week number of rule */
1562fe8fb19SBen Gras 	int		r_mon;		/* month number of rule */
15784d9c625SLionel Sambuc 	int_fast32_t	r_time;		/* transition time of rule */
1582fe8fb19SBen Gras };
1592fe8fb19SBen Gras 
160*0a6a1f1dSLionel Sambuc static struct tm *gmtsub(struct state const *, time_t const *, int_fast32_t,
161*0a6a1f1dSLionel Sambuc 			 struct tm *);
162*0a6a1f1dSLionel Sambuc static bool increment_overflow(int *, int);
163*0a6a1f1dSLionel Sambuc static bool increment_overflow_time(time_t *, int_fast32_t);
164*0a6a1f1dSLionel Sambuc static bool normalize_overflow32(int_fast32_t *, int *, int);
165*0a6a1f1dSLionel Sambuc static struct tm *timesub(time_t const *, int_fast32_t, struct state const *,
166*0a6a1f1dSLionel Sambuc 			  struct tm *);
167*0a6a1f1dSLionel Sambuc static bool typesequiv(struct state const *, int, int);
168*0a6a1f1dSLionel Sambuc static bool tzparse(char const *, struct state *, bool);
1692fe8fb19SBen Gras 
1702fe8fb19SBen Gras static timezone_t lclptr;
1712fe8fb19SBen Gras static timezone_t gmtptr;
1722fe8fb19SBen Gras 
1732fe8fb19SBen Gras #ifndef TZ_STRLEN_MAX
1742fe8fb19SBen Gras #define TZ_STRLEN_MAX 255
1752fe8fb19SBen Gras #endif /* !defined TZ_STRLEN_MAX */
1762fe8fb19SBen Gras 
1772fe8fb19SBen Gras static char		lcl_TZname[TZ_STRLEN_MAX + 1];
1782fe8fb19SBen Gras static int		lcl_is_set;
1792fe8fb19SBen Gras 
1802fe8fb19SBen Gras #if !defined(__LIBC12_SOURCE__)
1812fe8fb19SBen Gras 
1822fe8fb19SBen Gras __aconst char *		tzname[2] = {
1832fe8fb19SBen Gras 	(__aconst char *)__UNCONST(wildabbr),
1842fe8fb19SBen Gras 	(__aconst char *)__UNCONST(wildabbr)
1852fe8fb19SBen Gras };
1862fe8fb19SBen Gras 
1872fe8fb19SBen Gras #else
1882fe8fb19SBen Gras 
1892fe8fb19SBen Gras extern __aconst char *	tzname[2];
1902fe8fb19SBen Gras 
1912fe8fb19SBen Gras #endif
1922fe8fb19SBen Gras 
1932fe8fb19SBen Gras #ifdef _REENTRANT
1942fe8fb19SBen Gras static rwlock_t lcl_lock = RWLOCK_INITIALIZER;
1952fe8fb19SBen Gras #endif
1962fe8fb19SBen Gras 
1972fe8fb19SBen Gras /*
1982fe8fb19SBen Gras ** Section 4.12.3 of X3.159-1989 requires that
1992fe8fb19SBen Gras **	Except for the strftime function, these functions [asctime,
2002fe8fb19SBen Gras **	ctime, gmtime, localtime] return values in one of two static
2012fe8fb19SBen Gras **	objects: a broken-down time structure and an array of char.
2022fe8fb19SBen Gras ** Thanks to Paul Eggert for noting this.
2032fe8fb19SBen Gras */
2042fe8fb19SBen Gras 
2052fe8fb19SBen Gras static struct tm	tm;
2062fe8fb19SBen Gras 
2072fe8fb19SBen Gras #ifdef USG_COMPAT
2082fe8fb19SBen Gras #if !defined(__LIBC12_SOURCE__)
2092fe8fb19SBen Gras long 			timezone = 0;
2102fe8fb19SBen Gras int			daylight = 0;
2112fe8fb19SBen Gras #else
2122fe8fb19SBen Gras extern int		daylight;
2132fe8fb19SBen Gras extern long		timezone __RENAME(__timezone13);
2142fe8fb19SBen Gras #endif
2152fe8fb19SBen Gras #endif /* defined USG_COMPAT */
2162fe8fb19SBen Gras 
2172fe8fb19SBen Gras #ifdef ALTZONE
218*0a6a1f1dSLionel Sambuc long			altzone = 0;
2192fe8fb19SBen Gras #endif /* defined ALTZONE */
2202fe8fb19SBen Gras 
221*0a6a1f1dSLionel Sambuc /* Initialize *S to a value based on GMTOFF, ISDST, and ABBRIND.  */
222*0a6a1f1dSLionel Sambuc static void
init_ttinfo(struct ttinfo * s,int_fast32_t gmtoff,bool isdst,int abbrind)223*0a6a1f1dSLionel Sambuc init_ttinfo(struct ttinfo *s, int_fast32_t gmtoff, bool isdst, int abbrind)
224*0a6a1f1dSLionel Sambuc {
225*0a6a1f1dSLionel Sambuc 	s->tt_gmtoff = gmtoff;
226*0a6a1f1dSLionel Sambuc 	s->tt_isdst = isdst;
227*0a6a1f1dSLionel Sambuc 	s->tt_abbrind = abbrind;
228*0a6a1f1dSLionel Sambuc 	s->tt_ttisstd = false;
229*0a6a1f1dSLionel Sambuc 	s->tt_ttisgmt = false;
230*0a6a1f1dSLionel Sambuc }
231*0a6a1f1dSLionel Sambuc 
23284d9c625SLionel Sambuc static int_fast32_t
detzcode(const char * const codep)2332fe8fb19SBen Gras detzcode(const char *const codep)
2342fe8fb19SBen Gras {
23584d9c625SLionel Sambuc 	int_fast32_t result;
2362fe8fb19SBen Gras 	int	i;
237*0a6a1f1dSLionel Sambuc 	int_fast32_t one = 1;
238*0a6a1f1dSLionel Sambuc 	int_fast32_t halfmaxval = one << (32 - 2);
239*0a6a1f1dSLionel Sambuc 	int_fast32_t maxval = halfmaxval - 1 + halfmaxval;
240*0a6a1f1dSLionel Sambuc 	int_fast32_t minval = -1 - maxval;
2412fe8fb19SBen Gras 
242*0a6a1f1dSLionel Sambuc 	result = codep[0] & 0x7f;
243*0a6a1f1dSLionel Sambuc 	for (i = 1; i < 4; ++i)
2442fe8fb19SBen Gras 		result = (result << 8) | (codep[i] & 0xff);
245*0a6a1f1dSLionel Sambuc 
246*0a6a1f1dSLionel Sambuc 	if (codep[0] & 0x80) {
247*0a6a1f1dSLionel Sambuc 	  /* Do two's-complement negation even on non-two's-complement machines.
248*0a6a1f1dSLionel Sambuc 	     If the result would be minval - 1, return minval.  */
249*0a6a1f1dSLionel Sambuc 	    result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0;
250*0a6a1f1dSLionel Sambuc 	    result += minval;
251*0a6a1f1dSLionel Sambuc 	}
2522fe8fb19SBen Gras  	return result;
2532fe8fb19SBen Gras }
2542fe8fb19SBen Gras 
255*0a6a1f1dSLionel Sambuc static int_fast64_t
detzcode64(const char * const codep)2562fe8fb19SBen Gras detzcode64(const char *const codep)
2572fe8fb19SBen Gras {
258*0a6a1f1dSLionel Sambuc 	int_fast64_t result;
2592fe8fb19SBen Gras 	int	i;
260*0a6a1f1dSLionel Sambuc 	int_fast64_t one = 1;
261*0a6a1f1dSLionel Sambuc 	int_fast64_t halfmaxval = one << (64 - 2);
262*0a6a1f1dSLionel Sambuc 	int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
263*0a6a1f1dSLionel Sambuc 	int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
2642fe8fb19SBen Gras 
265*0a6a1f1dSLionel Sambuc 	result = codep[0] & 0x7f;
266*0a6a1f1dSLionel Sambuc 	for (i = 1; i < 8; ++i)
267*0a6a1f1dSLionel Sambuc 		result = (result << 8) | (codep[i] & 0xff);
268*0a6a1f1dSLionel Sambuc 
269*0a6a1f1dSLionel Sambuc 	if (codep[0] & 0x80) {
270*0a6a1f1dSLionel Sambuc 	  /* Do two's-complement negation even on non-two's-complement machines.
271*0a6a1f1dSLionel Sambuc 	     If the result would be minval - 1, return minval.  */
272*0a6a1f1dSLionel Sambuc 	  result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
273*0a6a1f1dSLionel Sambuc 	  result += minval;
274*0a6a1f1dSLionel Sambuc 	}
2752fe8fb19SBen Gras  	return result;
2762fe8fb19SBen Gras }
2772fe8fb19SBen Gras 
2782fe8fb19SBen Gras const char *
tzgetname(const timezone_t sp,int isdst)2792fe8fb19SBen Gras tzgetname(const timezone_t sp, int isdst)
2802fe8fb19SBen Gras {
2812fe8fb19SBen Gras 	int i;
2822fe8fb19SBen Gras 	for (i = 0; i < sp->timecnt; ++i) {
2832fe8fb19SBen Gras 		const struct ttinfo *const ttisp = &sp->ttis[sp->types[i]];
2842fe8fb19SBen Gras 
2852fe8fb19SBen Gras 		if (ttisp->tt_isdst == isdst)
2862fe8fb19SBen Gras 			return &sp->chars[ttisp->tt_abbrind];
2872fe8fb19SBen Gras 	}
288*0a6a1f1dSLionel Sambuc 	errno = ESRCH;
2892fe8fb19SBen Gras 	return NULL;
2902fe8fb19SBen Gras }
2912fe8fb19SBen Gras 
2922fe8fb19SBen Gras static void
scrub_abbrs(struct state * sp)293*0a6a1f1dSLionel Sambuc scrub_abbrs(struct state *sp)
2942fe8fb19SBen Gras {
2952fe8fb19SBen Gras 	int i;
2962fe8fb19SBen Gras 
2972fe8fb19SBen Gras 	/*
2982fe8fb19SBen Gras 	** First, replace bogus characters.
2992fe8fb19SBen Gras 	*/
3002fe8fb19SBen Gras 	for (i = 0; i < sp->charcnt; ++i)
3012fe8fb19SBen Gras 		if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
3022fe8fb19SBen Gras 			sp->chars[i] = TZ_ABBR_ERR_CHAR;
3032fe8fb19SBen Gras 	/*
3042fe8fb19SBen Gras 	** Second, truncate long abbreviations.
3052fe8fb19SBen Gras 	*/
3062fe8fb19SBen Gras 	for (i = 0; i < sp->typecnt; ++i) {
3072fe8fb19SBen Gras 		const struct ttinfo * const	ttisp = &sp->ttis[i];
3082fe8fb19SBen Gras 		char *				cp = &sp->chars[ttisp->tt_abbrind];
3092fe8fb19SBen Gras 
3102fe8fb19SBen Gras 		if (strlen(cp) > TZ_ABBR_MAX_LEN &&
3112fe8fb19SBen Gras 			strcmp(cp, GRANDPARENTED) != 0)
3122fe8fb19SBen Gras 				*(cp + TZ_ABBR_MAX_LEN) = '\0';
3132fe8fb19SBen Gras 	}
3142fe8fb19SBen Gras }
3152fe8fb19SBen Gras 
3162fe8fb19SBen Gras static void
update_tzname_etc(const struct state * sp,const struct ttinfo * ttisp)317*0a6a1f1dSLionel Sambuc update_tzname_etc(const struct state *sp, const struct ttinfo *ttisp)
318*0a6a1f1dSLionel Sambuc {
319*0a6a1f1dSLionel Sambuc 	tzname[ttisp->tt_isdst] = __UNCONST(&sp->chars[ttisp->tt_abbrind]);
320*0a6a1f1dSLionel Sambuc #ifdef USG_COMPAT
321*0a6a1f1dSLionel Sambuc 	if (!ttisp->tt_isdst)
322*0a6a1f1dSLionel Sambuc 		timezone = - ttisp->tt_gmtoff;
323*0a6a1f1dSLionel Sambuc #endif
324*0a6a1f1dSLionel Sambuc #ifdef ALTZONE
325*0a6a1f1dSLionel Sambuc 	if (ttisp->tt_isdst)
326*0a6a1f1dSLionel Sambuc 	    altzone = - ttisp->tt_gmtoff;
327*0a6a1f1dSLionel Sambuc #endif /* defined ALTZONE */
328*0a6a1f1dSLionel Sambuc }
329*0a6a1f1dSLionel Sambuc 
330*0a6a1f1dSLionel Sambuc static void
settzname(void)3312fe8fb19SBen Gras settzname(void)
3322fe8fb19SBen Gras {
3332fe8fb19SBen Gras 	timezone_t const	sp = lclptr;
3342fe8fb19SBen Gras 	int			i;
3352fe8fb19SBen Gras 
3362fe8fb19SBen Gras 	tzname[0] = (__aconst char *)__UNCONST(wildabbr);
3372fe8fb19SBen Gras 	tzname[1] = (__aconst char *)__UNCONST(wildabbr);
3382fe8fb19SBen Gras #ifdef USG_COMPAT
3392fe8fb19SBen Gras 	daylight = 0;
3402fe8fb19SBen Gras 	timezone = 0;
3412fe8fb19SBen Gras #endif /* defined USG_COMPAT */
3422fe8fb19SBen Gras #ifdef ALTZONE
3432fe8fb19SBen Gras 	altzone = 0;
3442fe8fb19SBen Gras #endif /* defined ALTZONE */
3452fe8fb19SBen Gras 	if (sp == NULL) {
3462fe8fb19SBen Gras 		tzname[0] = tzname[1] = (__aconst char *)__UNCONST(gmt);
3472fe8fb19SBen Gras 		return;
3482fe8fb19SBen Gras 	}
349f14fb602SLionel Sambuc 	/*
350f14fb602SLionel Sambuc 	** And to get the latest zone names into tzname. . .
351f14fb602SLionel Sambuc 	*/
352*0a6a1f1dSLionel Sambuc 	for (i = 0; i < sp->typecnt; ++i)
353*0a6a1f1dSLionel Sambuc 		update_tzname_etc(sp, &sp->ttis[i]);
3542fe8fb19SBen Gras 
355*0a6a1f1dSLionel Sambuc 	for (i = 0; i < sp->timecnt; ++i) {
356*0a6a1f1dSLionel Sambuc 		const struct ttinfo * const ttisp = &sp->ttis[sp->types[i]];
357*0a6a1f1dSLionel Sambuc 		update_tzname_etc(sp, ttisp);
3582fe8fb19SBen Gras #ifdef USG_COMPAT
3592fe8fb19SBen Gras 		if (ttisp->tt_isdst)
3602fe8fb19SBen Gras 			daylight = 1;
3612fe8fb19SBen Gras #endif /* defined USG_COMPAT */
3622fe8fb19SBen Gras 	}
3632fe8fb19SBen Gras }
3642fe8fb19SBen Gras 
365*0a6a1f1dSLionel Sambuc static bool
differ_by_repeat(const time_t t1,const time_t t0)3662fe8fb19SBen Gras differ_by_repeat(const time_t t1, const time_t t0)
3672fe8fb19SBen Gras {
36884d9c625SLionel Sambuc 	if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
3692fe8fb19SBen Gras 		return 0;
3702fe8fb19SBen Gras 	return (int_fast64_t)t1 - (int_fast64_t)t0 == SECSPERREPEAT;
3712fe8fb19SBen Gras }
3722fe8fb19SBen Gras 
373*0a6a1f1dSLionel Sambuc union input_buffer {
374*0a6a1f1dSLionel Sambuc 	/* The first part of the buffer, interpreted as a header.  */
375*0a6a1f1dSLionel Sambuc 	struct tzhead tzhead;
376*0a6a1f1dSLionel Sambuc 
377*0a6a1f1dSLionel Sambuc 	/* The entire buffer.  */
378*0a6a1f1dSLionel Sambuc 	char buf[2 * sizeof(struct tzhead) + 2 * sizeof (struct state)
379*0a6a1f1dSLionel Sambuc 	  + 4 * TZ_MAX_TIMES];
380*0a6a1f1dSLionel Sambuc };
381*0a6a1f1dSLionel Sambuc 
382*0a6a1f1dSLionel Sambuc /* Local storage needed for 'tzloadbody'.  */
383*0a6a1f1dSLionel Sambuc union local_storage {
384*0a6a1f1dSLionel Sambuc 	/* The file name to be opened.  */
385*0a6a1f1dSLionel Sambuc 	char fullname[FILENAME_MAX + 1];
386*0a6a1f1dSLionel Sambuc 
387*0a6a1f1dSLionel Sambuc 	/* The results of analyzing the file's contents after it is opened.  */
388*0a6a1f1dSLionel Sambuc 	struct {
389*0a6a1f1dSLionel Sambuc 		/* The input buffer.  */
390*0a6a1f1dSLionel Sambuc 		union input_buffer u;
391*0a6a1f1dSLionel Sambuc 
392*0a6a1f1dSLionel Sambuc 		/* A temporary state used for parsing a TZ string in the file.  */
393*0a6a1f1dSLionel Sambuc 		struct state st;
394*0a6a1f1dSLionel Sambuc 	} u;
395*0a6a1f1dSLionel Sambuc };
396*0a6a1f1dSLionel Sambuc 
397*0a6a1f1dSLionel Sambuc /* Load tz data from the file named NAME into *SP.  Read extended
398*0a6a1f1dSLionel Sambuc    format if DOEXTEND.  Use *LSP for temporary storage.  Return 0 on
399*0a6a1f1dSLionel Sambuc    success, an errno value on failure.  */
4002fe8fb19SBen Gras static int
tzloadbody(char const * name,struct state * sp,bool doextend,union local_storage * lsp)401*0a6a1f1dSLionel Sambuc tzloadbody(char const *name, struct state *sp, bool doextend,
402*0a6a1f1dSLionel Sambuc   union local_storage *lsp)
4032fe8fb19SBen Gras {
4042fe8fb19SBen Gras 	int			i;
4052fe8fb19SBen Gras 	int			fid;
4062fe8fb19SBen Gras 	int			stored;
407f14fb602SLionel Sambuc 	ssize_t			nread;
408*0a6a1f1dSLionel Sambuc 	bool			doaccess;
409*0a6a1f1dSLionel Sambuc 	char			*fullname = lsp->fullname;
410*0a6a1f1dSLionel Sambuc 	union input_buffer	*up = &lsp->u.u;
411*0a6a1f1dSLionel Sambuc 	size_t			tzheadsize = sizeof(struct tzhead);
412f14fb602SLionel Sambuc 
413*0a6a1f1dSLionel Sambuc 	sp->goback = sp->goahead = false;
4142fe8fb19SBen Gras 
415*0a6a1f1dSLionel Sambuc 	if (! name) {
416*0a6a1f1dSLionel Sambuc 		name = TZDEFAULT;
417*0a6a1f1dSLionel Sambuc 		if (! name)
418*0a6a1f1dSLionel Sambuc 			return EINVAL;
419*0a6a1f1dSLionel Sambuc 	}
4202fe8fb19SBen Gras 
4212fe8fb19SBen Gras 	if (name[0] == ':')
4222fe8fb19SBen Gras 		++name;
4232fe8fb19SBen Gras 	doaccess = name[0] == '/';
4242fe8fb19SBen Gras 	if (!doaccess) {
425*0a6a1f1dSLionel Sambuc 		char const *p = TZDIR;
426*0a6a1f1dSLionel Sambuc 		if (! p)
427*0a6a1f1dSLionel Sambuc 			return EINVAL;
428*0a6a1f1dSLionel Sambuc 		if (sizeof lsp->fullname - 1 <= strlen(p) + strlen(name))
429*0a6a1f1dSLionel Sambuc 			return ENAMETOOLONG;
430*0a6a1f1dSLionel Sambuc 		strcpy(fullname, p);
431*0a6a1f1dSLionel Sambuc 		strcat(fullname, "/");
432*0a6a1f1dSLionel Sambuc 		strcat(fullname, name);
433*0a6a1f1dSLionel Sambuc 		/* Set doaccess if '.' (as in "../") shows up in name.  */
434*0a6a1f1dSLionel Sambuc 		if (strchr(name, '.'))
435*0a6a1f1dSLionel Sambuc 			doaccess = true;
4362fe8fb19SBen Gras 		name = fullname;
4372fe8fb19SBen Gras 	}
4382fe8fb19SBen Gras 	if (doaccess && access(name, R_OK) != 0)
439*0a6a1f1dSLionel Sambuc 		return errno;
4402fe8fb19SBen Gras 
441*0a6a1f1dSLionel Sambuc 	fid = open(name, OPEN_MODE);
442*0a6a1f1dSLionel Sambuc 	if (fid < 0)
443*0a6a1f1dSLionel Sambuc 		return errno;
444*0a6a1f1dSLionel Sambuc 	nread = read(fid, up->buf, sizeof up->buf);
445*0a6a1f1dSLionel Sambuc 	if (nread < (ssize_t)tzheadsize) {
446*0a6a1f1dSLionel Sambuc 		int err = nread < 0 ? errno : EINVAL;
447*0a6a1f1dSLionel Sambuc 		close(fid);
448*0a6a1f1dSLionel Sambuc 		return err;
449*0a6a1f1dSLionel Sambuc 	}
450*0a6a1f1dSLionel Sambuc 	if (close(fid) < 0)
451*0a6a1f1dSLionel Sambuc 		return errno;
452*0a6a1f1dSLionel Sambuc 	for (stored = 4; stored <= 8; stored *= 2) {
453*0a6a1f1dSLionel Sambuc 		int_fast32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
454*0a6a1f1dSLionel Sambuc 		int_fast32_t ttisgmtcnt = detzcode(up->tzhead.tzh_ttisgmtcnt);
455*0a6a1f1dSLionel Sambuc 		int_fast32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
456*0a6a1f1dSLionel Sambuc 		int_fast32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
457*0a6a1f1dSLionel Sambuc 		int_fast32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
458*0a6a1f1dSLionel Sambuc 		int_fast32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
459*0a6a1f1dSLionel Sambuc 		char const *p = up->buf + tzheadsize;
460*0a6a1f1dSLionel Sambuc 		if (! (0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
461*0a6a1f1dSLionel Sambuc 		       && 0 < typecnt && typecnt < TZ_MAX_TYPES
462*0a6a1f1dSLionel Sambuc 		       && 0 <= timecnt && timecnt < TZ_MAX_TIMES
463*0a6a1f1dSLionel Sambuc 		       && 0 <= charcnt && charcnt < TZ_MAX_CHARS
464*0a6a1f1dSLionel Sambuc 		       && (ttisstdcnt == typecnt || ttisstdcnt == 0)
465*0a6a1f1dSLionel Sambuc 		       && (ttisgmtcnt == typecnt || ttisgmtcnt == 0)))
466*0a6a1f1dSLionel Sambuc 		  return EINVAL;
467*0a6a1f1dSLionel Sambuc 		if ((size_t)nread
468*0a6a1f1dSLionel Sambuc 		    < (tzheadsize		/* struct tzhead */
469*0a6a1f1dSLionel Sambuc 		       + timecnt * stored	/* ats */
470*0a6a1f1dSLionel Sambuc 		       + timecnt		/* types */
471*0a6a1f1dSLionel Sambuc 		       + typecnt * 6		/* ttinfos */
472*0a6a1f1dSLionel Sambuc 		       + charcnt		/* chars */
473*0a6a1f1dSLionel Sambuc 		       + leapcnt * (stored + 4)	/* lsinfos */
474*0a6a1f1dSLionel Sambuc 		       + ttisstdcnt		/* ttisstds */
475*0a6a1f1dSLionel Sambuc 		       + ttisgmtcnt))		/* ttisgmts */
476*0a6a1f1dSLionel Sambuc 		  return EINVAL;
477*0a6a1f1dSLionel Sambuc 		sp->leapcnt = leapcnt;
478*0a6a1f1dSLionel Sambuc 		sp->timecnt = timecnt;
479*0a6a1f1dSLionel Sambuc 		sp->typecnt = typecnt;
480*0a6a1f1dSLionel Sambuc 		sp->charcnt = charcnt;
481*0a6a1f1dSLionel Sambuc 
482*0a6a1f1dSLionel Sambuc 		/* Read transitions, discarding those out of time_t range.
483*0a6a1f1dSLionel Sambuc 		   But pretend the last transition before time_t_min
484*0a6a1f1dSLionel Sambuc 		   occurred at time_t_min.  */
485*0a6a1f1dSLionel Sambuc 		timecnt = 0;
4862fe8fb19SBen Gras 		for (i = 0; i < sp->timecnt; ++i) {
487*0a6a1f1dSLionel Sambuc 			int_fast64_t at
488*0a6a1f1dSLionel Sambuc 			  = stored == 4 ? detzcode(p) : detzcode64(p);
489*0a6a1f1dSLionel Sambuc 			sp->types[i] = at <= time_t_max;
490*0a6a1f1dSLionel Sambuc 			if (sp->types[i]) {
491*0a6a1f1dSLionel Sambuc 				time_t attime
492*0a6a1f1dSLionel Sambuc 				    = ((TYPE_SIGNED(time_t) ?
493*0a6a1f1dSLionel Sambuc 				    at < time_t_min : at < 0)
494*0a6a1f1dSLionel Sambuc 				    ? time_t_min : (time_t)at);
495*0a6a1f1dSLionel Sambuc 				if (timecnt && attime <= sp->ats[timecnt - 1]) {
496*0a6a1f1dSLionel Sambuc 					if (attime < sp->ats[timecnt - 1])
497*0a6a1f1dSLionel Sambuc 						return EINVAL;
498*0a6a1f1dSLionel Sambuc 					sp->types[i - 1] = 0;
499*0a6a1f1dSLionel Sambuc 					timecnt--;
500*0a6a1f1dSLionel Sambuc 				}
501*0a6a1f1dSLionel Sambuc 				sp->ats[timecnt++] = attime;
502*0a6a1f1dSLionel Sambuc 			}
5032fe8fb19SBen Gras 			p += stored;
5042fe8fb19SBen Gras 		}
505*0a6a1f1dSLionel Sambuc 
506*0a6a1f1dSLionel Sambuc 		timecnt = 0;
5072fe8fb19SBen Gras 		for (i = 0; i < sp->timecnt; ++i) {
508*0a6a1f1dSLionel Sambuc 			unsigned char typ = *p++;
509*0a6a1f1dSLionel Sambuc 			if (sp->typecnt <= typ)
510*0a6a1f1dSLionel Sambuc 			  return EINVAL;
511*0a6a1f1dSLionel Sambuc 			if (sp->types[i])
512*0a6a1f1dSLionel Sambuc 				sp->types[timecnt++] = typ;
5132fe8fb19SBen Gras 		}
514*0a6a1f1dSLionel Sambuc 		sp->timecnt = timecnt;
5152fe8fb19SBen Gras 		for (i = 0; i < sp->typecnt; ++i) {
5162fe8fb19SBen Gras 			struct ttinfo *	ttisp;
517*0a6a1f1dSLionel Sambuc 			unsigned char isdst, abbrind;
5182fe8fb19SBen Gras 
5192fe8fb19SBen Gras 			ttisp = &sp->ttis[i];
5202fe8fb19SBen Gras 			ttisp->tt_gmtoff = detzcode(p);
5212fe8fb19SBen Gras 			p += 4;
522*0a6a1f1dSLionel Sambuc 			isdst = *p++;
523*0a6a1f1dSLionel Sambuc 			if (! (isdst < 2))
524*0a6a1f1dSLionel Sambuc 				return EINVAL;
525*0a6a1f1dSLionel Sambuc 			ttisp->tt_isdst = isdst;
526*0a6a1f1dSLionel Sambuc 			abbrind = *p++;
527*0a6a1f1dSLionel Sambuc 			if (! (abbrind < sp->charcnt))
528*0a6a1f1dSLionel Sambuc 				return EINVAL;
529*0a6a1f1dSLionel Sambuc 			ttisp->tt_abbrind = abbrind;
5302fe8fb19SBen Gras 		}
5312fe8fb19SBen Gras 		for (i = 0; i < sp->charcnt; ++i)
5322fe8fb19SBen Gras 			sp->chars[i] = *p++;
5332fe8fb19SBen Gras 		sp->chars[i] = '\0';	/* ensure '\0' at end */
5342fe8fb19SBen Gras 
535*0a6a1f1dSLionel Sambuc 		/* Read leap seconds, discarding those out of time_t range.  */
536*0a6a1f1dSLionel Sambuc 		leapcnt = 0;
537*0a6a1f1dSLionel Sambuc 		for (i = 0; i < sp->leapcnt; ++i) {
538*0a6a1f1dSLionel Sambuc 			int_fast64_t tr = stored == 4 ? detzcode(p) :
539*0a6a1f1dSLionel Sambuc 			    detzcode64(p);
540*0a6a1f1dSLionel Sambuc 			int_fast32_t corr = detzcode(p + stored);
541*0a6a1f1dSLionel Sambuc 			p += stored + 4;
542*0a6a1f1dSLionel Sambuc 			if (tr <= time_t_max) {
543*0a6a1f1dSLionel Sambuc 				time_t trans = ((TYPE_SIGNED(time_t) ?
544*0a6a1f1dSLionel Sambuc 				    tr < time_t_min : tr < 0)
545*0a6a1f1dSLionel Sambuc 				    ? time_t_min : (time_t)tr);
546*0a6a1f1dSLionel Sambuc 				if (leapcnt && trans <=
547*0a6a1f1dSLionel Sambuc 				    sp->lsis[leapcnt - 1].ls_trans) {
548*0a6a1f1dSLionel Sambuc 					if (trans <
549*0a6a1f1dSLionel Sambuc 					    sp->lsis[leapcnt - 1].ls_trans)
550*0a6a1f1dSLionel Sambuc 						return EINVAL;
551*0a6a1f1dSLionel Sambuc 					leapcnt--;
5522fe8fb19SBen Gras 				}
553*0a6a1f1dSLionel Sambuc 				sp->lsis[leapcnt].ls_trans = trans;
554*0a6a1f1dSLionel Sambuc 				sp->lsis[leapcnt].ls_corr = corr;
555*0a6a1f1dSLionel Sambuc 				leapcnt++;
556*0a6a1f1dSLionel Sambuc 			}
557*0a6a1f1dSLionel Sambuc 		}
558*0a6a1f1dSLionel Sambuc 		sp->leapcnt = leapcnt;
559*0a6a1f1dSLionel Sambuc 
5602fe8fb19SBen Gras 		for (i = 0; i < sp->typecnt; ++i) {
5612fe8fb19SBen Gras 			struct ttinfo *	ttisp;
5622fe8fb19SBen Gras 
5632fe8fb19SBen Gras 			ttisp = &sp->ttis[i];
5642fe8fb19SBen Gras 			if (ttisstdcnt == 0)
565*0a6a1f1dSLionel Sambuc 				ttisp->tt_ttisstd = false;
5662fe8fb19SBen Gras 			else {
567*0a6a1f1dSLionel Sambuc 				if (*p != true && *p != false)
568*0a6a1f1dSLionel Sambuc 				  return EINVAL;
5692fe8fb19SBen Gras 				ttisp->tt_ttisstd = *p++;
5702fe8fb19SBen Gras 			}
5712fe8fb19SBen Gras 		}
5722fe8fb19SBen Gras 		for (i = 0; i < sp->typecnt; ++i) {
5732fe8fb19SBen Gras 			struct ttinfo *	ttisp;
5742fe8fb19SBen Gras 
5752fe8fb19SBen Gras 			ttisp = &sp->ttis[i];
5762fe8fb19SBen Gras 			if (ttisgmtcnt == 0)
577*0a6a1f1dSLionel Sambuc 				ttisp->tt_ttisgmt = false;
5782fe8fb19SBen Gras 			else {
579*0a6a1f1dSLionel Sambuc 				if (*p != true && *p != false)
580*0a6a1f1dSLionel Sambuc 						return EINVAL;
5812fe8fb19SBen Gras 				ttisp->tt_ttisgmt = *p++;
5822fe8fb19SBen Gras 			}
5832fe8fb19SBen Gras 		}
5842fe8fb19SBen Gras 		/*
5852fe8fb19SBen Gras 		** If this is an old file, we're done.
5862fe8fb19SBen Gras 		*/
587f14fb602SLionel Sambuc 		if (up->tzhead.tzh_version[0] == '\0')
5882fe8fb19SBen Gras 			break;
589f14fb602SLionel Sambuc 		nread -= p - up->buf;
590*0a6a1f1dSLionel Sambuc 		memmove(up->buf, p, (size_t)nread);
5912fe8fb19SBen Gras 	}
5922fe8fb19SBen Gras 	if (doextend && nread > 2 &&
593f14fb602SLionel Sambuc 		up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
5942fe8fb19SBen Gras 		sp->typecnt + 2 <= TZ_MAX_TYPES) {
595*0a6a1f1dSLionel Sambuc 			struct state *ts = &lsp->u.st;
5962fe8fb19SBen Gras 
597f14fb602SLionel Sambuc 			up->buf[nread - 1] = '\0';
598*0a6a1f1dSLionel Sambuc 			if (tzparse(&up->buf[1], ts, false)
599*0a6a1f1dSLionel Sambuc 			    && ts->typecnt == 2) {
600*0a6a1f1dSLionel Sambuc 
601*0a6a1f1dSLionel Sambuc 			  /* Attempt to reuse existing abbreviations.
602*0a6a1f1dSLionel Sambuc 			     Without this, America/Anchorage would stop
603*0a6a1f1dSLionel Sambuc 			     working after 2037 when TZ_MAX_CHARS is 50, as
604*0a6a1f1dSLionel Sambuc 			     sp->charcnt equals 42 (for LMT CAT CAWT CAPT AHST
605*0a6a1f1dSLionel Sambuc 			     AHDT YST AKDT AKST) and ts->charcnt equals 10
606*0a6a1f1dSLionel Sambuc 			     (for AKST AKDT).  Reusing means sp->charcnt can
607*0a6a1f1dSLionel Sambuc 			     stay 42 in this example.  */
608*0a6a1f1dSLionel Sambuc 			  int gotabbr = 0;
609*0a6a1f1dSLionel Sambuc 			  int charcnt = sp->charcnt;
610*0a6a1f1dSLionel Sambuc 			  for (i = 0; i < 2; i++) {
611*0a6a1f1dSLionel Sambuc 			    char *tsabbr = ts->chars + ts->ttis[i].tt_abbrind;
612*0a6a1f1dSLionel Sambuc 			    int j;
613*0a6a1f1dSLionel Sambuc 			    for (j = 0; j < charcnt; j++)
614*0a6a1f1dSLionel Sambuc 			      if (strcmp(sp->chars + j, tsabbr) == 0) {
615*0a6a1f1dSLionel Sambuc 				ts->ttis[i].tt_abbrind = j;
616*0a6a1f1dSLionel Sambuc 				gotabbr++;
617*0a6a1f1dSLionel Sambuc 				break;
6182fe8fb19SBen Gras 			      }
619*0a6a1f1dSLionel Sambuc 			    if (! (j < charcnt)) {
620*0a6a1f1dSLionel Sambuc 			      int tsabbrlen = strlen(tsabbr);
621*0a6a1f1dSLionel Sambuc 			      if (j + tsabbrlen < TZ_MAX_CHARS) {
622*0a6a1f1dSLionel Sambuc 				strcpy(sp->chars + j, tsabbr);
623*0a6a1f1dSLionel Sambuc 				charcnt = j + tsabbrlen + 1;
624*0a6a1f1dSLionel Sambuc 				ts->ttis[i].tt_abbrind = j;
625*0a6a1f1dSLionel Sambuc 				gotabbr++;
626*0a6a1f1dSLionel Sambuc 			      }
627*0a6a1f1dSLionel Sambuc 			    }
628*0a6a1f1dSLionel Sambuc 			  }
629*0a6a1f1dSLionel Sambuc 			  if (gotabbr == 2) {
630*0a6a1f1dSLionel Sambuc 			    sp->charcnt = charcnt;
631*0a6a1f1dSLionel Sambuc 			    for (i = 0; i < ts->timecnt; i++)
632*0a6a1f1dSLionel Sambuc 			      if (sp->ats[sp->timecnt - 1] < ts->ats[i])
633*0a6a1f1dSLionel Sambuc 				break;
634*0a6a1f1dSLionel Sambuc 			    while (i < ts->timecnt
635*0a6a1f1dSLionel Sambuc 				   && sp->timecnt < TZ_MAX_TIMES) {
636*0a6a1f1dSLionel Sambuc 			      sp->ats[sp->timecnt] = ts->ats[i];
637*0a6a1f1dSLionel Sambuc 			      sp->types[sp->timecnt] = (sp->typecnt
638*0a6a1f1dSLionel Sambuc 							+ ts->types[i]);
639*0a6a1f1dSLionel Sambuc 			      sp->timecnt++;
640*0a6a1f1dSLionel Sambuc 			      i++;
641*0a6a1f1dSLionel Sambuc 			    }
642*0a6a1f1dSLionel Sambuc 			    sp->ttis[sp->typecnt++] = ts->ttis[0];
643*0a6a1f1dSLionel Sambuc 			    sp->ttis[sp->typecnt++] = ts->ttis[1];
644*0a6a1f1dSLionel Sambuc 			  }
6452fe8fb19SBen Gras 			}
6462fe8fb19SBen Gras 	}
6472fe8fb19SBen Gras 	if (sp->timecnt > 1) {
6482fe8fb19SBen Gras 		for (i = 1; i < sp->timecnt; ++i)
6492fe8fb19SBen Gras 			if (typesequiv(sp, sp->types[i], sp->types[0]) &&
6502fe8fb19SBen Gras 				differ_by_repeat(sp->ats[i], sp->ats[0])) {
651*0a6a1f1dSLionel Sambuc 					sp->goback = true;
6522fe8fb19SBen Gras 					break;
6532fe8fb19SBen Gras 				}
6542fe8fb19SBen Gras 		for (i = sp->timecnt - 2; i >= 0; --i)
6552fe8fb19SBen Gras 			if (typesequiv(sp, sp->types[sp->timecnt - 1],
6562fe8fb19SBen Gras 				sp->types[i]) &&
6572fe8fb19SBen Gras 				differ_by_repeat(sp->ats[sp->timecnt - 1],
6582fe8fb19SBen Gras 				sp->ats[i])) {
659*0a6a1f1dSLionel Sambuc 					sp->goahead = true;
6602fe8fb19SBen Gras 					break;
6612fe8fb19SBen Gras 		}
6622fe8fb19SBen Gras 	}
66384d9c625SLionel Sambuc 	/*
66484d9c625SLionel Sambuc 	** If type 0 is is unused in transitions,
66584d9c625SLionel Sambuc 	** it's the type to use for early times.
66684d9c625SLionel Sambuc 	*/
667*0a6a1f1dSLionel Sambuc 	for (i = 0; i < sp->timecnt; ++i)
66884d9c625SLionel Sambuc 		if (sp->types[i] == 0)
66984d9c625SLionel Sambuc 			break;
670*0a6a1f1dSLionel Sambuc 	i = i < sp->timecnt ? -1 : 0;
67184d9c625SLionel Sambuc 	/*
67284d9c625SLionel Sambuc 	** Absent the above,
67384d9c625SLionel Sambuc 	** if there are transition times
67484d9c625SLionel Sambuc 	** and the first transition is to a daylight time
67584d9c625SLionel Sambuc 	** find the standard type less than and closest to
67684d9c625SLionel Sambuc 	** the type of the first transition.
67784d9c625SLionel Sambuc 	*/
67884d9c625SLionel Sambuc 	if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
67984d9c625SLionel Sambuc 		i = sp->types[0];
68084d9c625SLionel Sambuc 		while (--i >= 0)
68184d9c625SLionel Sambuc 			if (!sp->ttis[i].tt_isdst)
68284d9c625SLionel Sambuc 				break;
68384d9c625SLionel Sambuc 	}
68484d9c625SLionel Sambuc 	/*
68584d9c625SLionel Sambuc 	** If no result yet, find the first standard type.
68684d9c625SLionel Sambuc 	** If there is none, punt to type zero.
68784d9c625SLionel Sambuc 	*/
68884d9c625SLionel Sambuc 	if (i < 0) {
68984d9c625SLionel Sambuc 		i = 0;
69084d9c625SLionel Sambuc 		while (sp->ttis[i].tt_isdst)
69184d9c625SLionel Sambuc 			if (++i >= sp->typecnt) {
69284d9c625SLionel Sambuc 				i = 0;
69384d9c625SLionel Sambuc 				break;
69484d9c625SLionel Sambuc 			}
69584d9c625SLionel Sambuc 	}
69684d9c625SLionel Sambuc 	sp->defaulttype = i;
6972fe8fb19SBen Gras 	return 0;
6982fe8fb19SBen Gras }
6992fe8fb19SBen Gras 
700*0a6a1f1dSLionel Sambuc /* Load tz data from the file named NAME into *SP.  Read extended
701*0a6a1f1dSLionel Sambuc    format if DOEXTEND.  Return 0 on success, an errno value on failure.  */
7022fe8fb19SBen Gras static int
tzload(char const * name,struct state * sp,bool doextend)703*0a6a1f1dSLionel Sambuc tzload(char const *name, struct state *sp, bool doextend)
7042fe8fb19SBen Gras {
705*0a6a1f1dSLionel Sambuc 	union local_storage *lsp = malloc(sizeof *lsp);
706*0a6a1f1dSLionel Sambuc 	if (!lsp)
707*0a6a1f1dSLionel Sambuc 		return errno;
708*0a6a1f1dSLionel Sambuc 	else {
709*0a6a1f1dSLionel Sambuc 		int err = tzloadbody(name, sp, doextend, lsp);
710*0a6a1f1dSLionel Sambuc 		free(lsp);
711*0a6a1f1dSLionel Sambuc 		return err;
712*0a6a1f1dSLionel Sambuc 	}
713*0a6a1f1dSLionel Sambuc }
714*0a6a1f1dSLionel Sambuc 
715*0a6a1f1dSLionel Sambuc static bool
typesequiv(const struct state * sp,int a,int b)716*0a6a1f1dSLionel Sambuc typesequiv(const struct state *sp, int a, int b)
717*0a6a1f1dSLionel Sambuc {
718*0a6a1f1dSLionel Sambuc 	bool result;
7192fe8fb19SBen Gras 
7202fe8fb19SBen Gras 	if (sp == NULL ||
7212fe8fb19SBen Gras 		a < 0 || a >= sp->typecnt ||
7222fe8fb19SBen Gras 		b < 0 || b >= sp->typecnt)
723*0a6a1f1dSLionel Sambuc 			result = false;
7242fe8fb19SBen Gras 	else {
7252fe8fb19SBen Gras 		const struct ttinfo *	ap = &sp->ttis[a];
7262fe8fb19SBen Gras 		const struct ttinfo *	bp = &sp->ttis[b];
7272fe8fb19SBen Gras 		result = ap->tt_gmtoff == bp->tt_gmtoff &&
7282fe8fb19SBen Gras 			ap->tt_isdst == bp->tt_isdst &&
7292fe8fb19SBen Gras 			ap->tt_ttisstd == bp->tt_ttisstd &&
7302fe8fb19SBen Gras 			ap->tt_ttisgmt == bp->tt_ttisgmt &&
7312fe8fb19SBen Gras 			strcmp(&sp->chars[ap->tt_abbrind],
7322fe8fb19SBen Gras 			&sp->chars[bp->tt_abbrind]) == 0;
7332fe8fb19SBen Gras 	}
7342fe8fb19SBen Gras 	return result;
7352fe8fb19SBen Gras }
7362fe8fb19SBen Gras 
7372fe8fb19SBen Gras static const int	mon_lengths[2][MONSPERYEAR] = {
7382fe8fb19SBen Gras 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
7392fe8fb19SBen Gras 	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
7402fe8fb19SBen Gras };
7412fe8fb19SBen Gras 
7422fe8fb19SBen Gras static const int	year_lengths[2] = {
7432fe8fb19SBen Gras 	DAYSPERNYEAR, DAYSPERLYEAR
7442fe8fb19SBen Gras };
7452fe8fb19SBen Gras 
7462fe8fb19SBen Gras /*
7472fe8fb19SBen Gras ** Given a pointer into a time zone string, scan until a character that is not
7482fe8fb19SBen Gras ** a valid character in a zone name is found. Return a pointer to that
7492fe8fb19SBen Gras ** character.
7502fe8fb19SBen Gras */
7512fe8fb19SBen Gras 
752*0a6a1f1dSLionel Sambuc static const char * ATTRIBUTE_PURE
getzname(const char * strp)753f14fb602SLionel Sambuc getzname(const char *strp)
7542fe8fb19SBen Gras {
7552fe8fb19SBen Gras 	char	c;
7562fe8fb19SBen Gras 
7572fe8fb19SBen Gras 	while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
7582fe8fb19SBen Gras 		c != '+')
7592fe8fb19SBen Gras 			++strp;
7602fe8fb19SBen Gras 	return strp;
7612fe8fb19SBen Gras }
7622fe8fb19SBen Gras 
7632fe8fb19SBen Gras /*
7642fe8fb19SBen Gras ** Given a pointer into an extended time zone string, scan until the ending
7652fe8fb19SBen Gras ** delimiter of the zone name is located. Return a pointer to the delimiter.
7662fe8fb19SBen Gras **
7672fe8fb19SBen Gras ** As with getzname above, the legal character set is actually quite
7682fe8fb19SBen Gras ** restricted, with other characters producing undefined results.
7692fe8fb19SBen Gras ** We don't do any checking here; checking is done later in common-case code.
7702fe8fb19SBen Gras */
7712fe8fb19SBen Gras 
772*0a6a1f1dSLionel Sambuc static const char * ATTRIBUTE_PURE
getqzname(const char * strp,const int delim)7732fe8fb19SBen Gras getqzname(const char *strp, const int delim)
7742fe8fb19SBen Gras {
7752fe8fb19SBen Gras 	int	c;
7762fe8fb19SBen Gras 
7772fe8fb19SBen Gras 	while ((c = *strp) != '\0' && c != delim)
7782fe8fb19SBen Gras 		++strp;
7792fe8fb19SBen Gras 	return strp;
7802fe8fb19SBen Gras }
7812fe8fb19SBen Gras 
7822fe8fb19SBen Gras /*
7832fe8fb19SBen Gras ** Given a pointer into a time zone string, extract a number from that string.
7842fe8fb19SBen Gras ** Check that the number is within a specified range; if it is not, return
7852fe8fb19SBen Gras ** NULL.
7862fe8fb19SBen Gras ** Otherwise, return a pointer to the first character not part of the number.
7872fe8fb19SBen Gras */
7882fe8fb19SBen Gras 
7892fe8fb19SBen Gras static const char *
getnum(const char * strp,int * const nump,const int min,const int max)790f14fb602SLionel Sambuc getnum(const char *strp, int *const nump, const int min, const int max)
7912fe8fb19SBen Gras {
7922fe8fb19SBen Gras 	char	c;
7932fe8fb19SBen Gras 	int	num;
7942fe8fb19SBen Gras 
7952fe8fb19SBen Gras 	if (strp == NULL || !is_digit(c = *strp)) {
7962fe8fb19SBen Gras 		errno = EINVAL;
7972fe8fb19SBen Gras 		return NULL;
7982fe8fb19SBen Gras 	}
7992fe8fb19SBen Gras 	num = 0;
8002fe8fb19SBen Gras 	do {
8012fe8fb19SBen Gras 		num = num * 10 + (c - '0');
8022fe8fb19SBen Gras 		if (num > max) {
8032fe8fb19SBen Gras 			errno = EOVERFLOW;
8042fe8fb19SBen Gras 			return NULL;	/* illegal value */
8052fe8fb19SBen Gras 		}
8062fe8fb19SBen Gras 		c = *++strp;
8072fe8fb19SBen Gras 	} while (is_digit(c));
8082fe8fb19SBen Gras 	if (num < min) {
8092fe8fb19SBen Gras 		errno = EINVAL;
8102fe8fb19SBen Gras 		return NULL;		/* illegal value */
8112fe8fb19SBen Gras 	}
8122fe8fb19SBen Gras 	*nump = num;
8132fe8fb19SBen Gras 	return strp;
8142fe8fb19SBen Gras }
8152fe8fb19SBen Gras 
8162fe8fb19SBen Gras /*
8172fe8fb19SBen Gras ** Given a pointer into a time zone string, extract a number of seconds,
8182fe8fb19SBen Gras ** in hh[:mm[:ss]] form, from the string.
8192fe8fb19SBen Gras ** If any error occurs, return NULL.
8202fe8fb19SBen Gras ** Otherwise, return a pointer to the first character not part of the number
8212fe8fb19SBen Gras ** of seconds.
8222fe8fb19SBen Gras */
8232fe8fb19SBen Gras 
8242fe8fb19SBen Gras static const char *
getsecs(const char * strp,int_fast32_t * const secsp)82584d9c625SLionel Sambuc getsecs(const char *strp, int_fast32_t *const secsp)
8262fe8fb19SBen Gras {
8272fe8fb19SBen Gras 	int	num;
8282fe8fb19SBen Gras 
8292fe8fb19SBen Gras 	/*
830*0a6a1f1dSLionel Sambuc 	** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
8312fe8fb19SBen Gras 	** "M10.4.6/26", which does not conform to Posix,
8322fe8fb19SBen Gras 	** but which specifies the equivalent of
833*0a6a1f1dSLionel Sambuc 	** "02:00 on the first Sunday on or after 23 Oct".
8342fe8fb19SBen Gras 	*/
8352fe8fb19SBen Gras 	strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
8362fe8fb19SBen Gras 	if (strp == NULL)
8372fe8fb19SBen Gras 		return NULL;
83884d9c625SLionel Sambuc 	*secsp = num * (int_fast32_t) SECSPERHOUR;
8392fe8fb19SBen Gras 	if (*strp == ':') {
8402fe8fb19SBen Gras 		++strp;
8412fe8fb19SBen Gras 		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
8422fe8fb19SBen Gras 		if (strp == NULL)
8432fe8fb19SBen Gras 			return NULL;
8442fe8fb19SBen Gras 		*secsp += num * SECSPERMIN;
8452fe8fb19SBen Gras 		if (*strp == ':') {
8462fe8fb19SBen Gras 			++strp;
847*0a6a1f1dSLionel Sambuc 			/* 'SECSPERMIN' allows for leap seconds.  */
8482fe8fb19SBen Gras 			strp = getnum(strp, &num, 0, SECSPERMIN);
8492fe8fb19SBen Gras 			if (strp == NULL)
8502fe8fb19SBen Gras 				return NULL;
8512fe8fb19SBen Gras 			*secsp += num;
8522fe8fb19SBen Gras 		}
8532fe8fb19SBen Gras 	}
8542fe8fb19SBen Gras 	return strp;
8552fe8fb19SBen Gras }
8562fe8fb19SBen Gras 
8572fe8fb19SBen Gras /*
8582fe8fb19SBen Gras ** Given a pointer into a time zone string, extract an offset, in
8592fe8fb19SBen Gras ** [+-]hh[:mm[:ss]] form, from the string.
8602fe8fb19SBen Gras ** If any error occurs, return NULL.
8612fe8fb19SBen Gras ** Otherwise, return a pointer to the first character not part of the time.
8622fe8fb19SBen Gras */
8632fe8fb19SBen Gras 
8642fe8fb19SBen Gras static const char *
getoffset(const char * strp,int_fast32_t * const offsetp)86584d9c625SLionel Sambuc getoffset(const char *strp, int_fast32_t *const offsetp)
8662fe8fb19SBen Gras {
867*0a6a1f1dSLionel Sambuc 	bool neg = false;
8682fe8fb19SBen Gras 
8692fe8fb19SBen Gras 	if (*strp == '-') {
870*0a6a1f1dSLionel Sambuc 		neg = true;
8712fe8fb19SBen Gras 		++strp;
8722fe8fb19SBen Gras 	} else if (*strp == '+')
8732fe8fb19SBen Gras 		++strp;
8742fe8fb19SBen Gras 	strp = getsecs(strp, offsetp);
8752fe8fb19SBen Gras 	if (strp == NULL)
8762fe8fb19SBen Gras 		return NULL;		/* illegal time */
8772fe8fb19SBen Gras 	if (neg)
8782fe8fb19SBen Gras 		*offsetp = -*offsetp;
8792fe8fb19SBen Gras 	return strp;
8802fe8fb19SBen Gras }
8812fe8fb19SBen Gras 
8822fe8fb19SBen Gras /*
8832fe8fb19SBen Gras ** Given a pointer into a time zone string, extract a rule in the form
8842fe8fb19SBen Gras ** date[/time]. See POSIX section 8 for the format of "date" and "time".
8852fe8fb19SBen Gras ** If a valid rule is not found, return NULL.
8862fe8fb19SBen Gras ** Otherwise, return a pointer to the first character not part of the rule.
8872fe8fb19SBen Gras */
8882fe8fb19SBen Gras 
8892fe8fb19SBen Gras static const char *
getrule(const char * strp,struct rule * const rulep)8902fe8fb19SBen Gras getrule(const char *strp, struct rule *const rulep)
8912fe8fb19SBen Gras {
8922fe8fb19SBen Gras 	if (*strp == 'J') {
8932fe8fb19SBen Gras 		/*
8942fe8fb19SBen Gras 		** Julian day.
8952fe8fb19SBen Gras 		*/
8962fe8fb19SBen Gras 		rulep->r_type = JULIAN_DAY;
8972fe8fb19SBen Gras 		++strp;
8982fe8fb19SBen Gras 		strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
8992fe8fb19SBen Gras 	} else if (*strp == 'M') {
9002fe8fb19SBen Gras 		/*
9012fe8fb19SBen Gras 		** Month, week, day.
9022fe8fb19SBen Gras 		*/
9032fe8fb19SBen Gras 		rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
9042fe8fb19SBen Gras 		++strp;
9052fe8fb19SBen Gras 		strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
9062fe8fb19SBen Gras 		if (strp == NULL)
9072fe8fb19SBen Gras 			return NULL;
9082fe8fb19SBen Gras 		if (*strp++ != '.')
9092fe8fb19SBen Gras 			return NULL;
9102fe8fb19SBen Gras 		strp = getnum(strp, &rulep->r_week, 1, 5);
9112fe8fb19SBen Gras 		if (strp == NULL)
9122fe8fb19SBen Gras 			return NULL;
9132fe8fb19SBen Gras 		if (*strp++ != '.')
9142fe8fb19SBen Gras 			return NULL;
9152fe8fb19SBen Gras 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
9162fe8fb19SBen Gras 	} else if (is_digit(*strp)) {
9172fe8fb19SBen Gras 		/*
9182fe8fb19SBen Gras 		** Day of year.
9192fe8fb19SBen Gras 		*/
9202fe8fb19SBen Gras 		rulep->r_type = DAY_OF_YEAR;
9212fe8fb19SBen Gras 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
9222fe8fb19SBen Gras 	} else	return NULL;		/* invalid format */
9232fe8fb19SBen Gras 	if (strp == NULL)
9242fe8fb19SBen Gras 		return NULL;
9252fe8fb19SBen Gras 	if (*strp == '/') {
9262fe8fb19SBen Gras 		/*
9272fe8fb19SBen Gras 		** Time specified.
9282fe8fb19SBen Gras 		*/
9292fe8fb19SBen Gras 		++strp;
93084d9c625SLionel Sambuc 		strp = getoffset(strp, &rulep->r_time);
9312fe8fb19SBen Gras 	} else	rulep->r_time = 2 * SECSPERHOUR;	/* default = 2:00:00 */
9322fe8fb19SBen Gras 	return strp;
9332fe8fb19SBen Gras }
9342fe8fb19SBen Gras 
9352fe8fb19SBen Gras /*
936*0a6a1f1dSLionel Sambuc ** Given a year, a rule, and the offset from UT at the time that rule takes
937*0a6a1f1dSLionel Sambuc ** effect, calculate the year-relative time that rule takes effect.
9382fe8fb19SBen Gras */
9392fe8fb19SBen Gras 
940*0a6a1f1dSLionel Sambuc static int_fast32_t ATTRIBUTE_PURE
transtime(const int year,const struct rule * const rulep,const int_fast32_t offset)941*0a6a1f1dSLionel Sambuc transtime(const int year, const struct rule *const rulep,
94284d9c625SLionel Sambuc 	  const int_fast32_t offset)
9432fe8fb19SBen Gras {
944*0a6a1f1dSLionel Sambuc 	bool	leapyear;
945*0a6a1f1dSLionel Sambuc 	int_fast32_t value;
9462fe8fb19SBen Gras 	int	i;
9472fe8fb19SBen Gras 	int		d, m1, yy0, yy1, yy2, dow;
9482fe8fb19SBen Gras 
9492fe8fb19SBen Gras 	INITIALIZE(value);
9502fe8fb19SBen Gras 	leapyear = isleap(year);
9512fe8fb19SBen Gras 	switch (rulep->r_type) {
9522fe8fb19SBen Gras 
9532fe8fb19SBen Gras 	case JULIAN_DAY:
9542fe8fb19SBen Gras 		/*
9552fe8fb19SBen Gras 		** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
9562fe8fb19SBen Gras 		** years.
9572fe8fb19SBen Gras 		** In non-leap years, or if the day number is 59 or less, just
9582fe8fb19SBen Gras 		** add SECSPERDAY times the day number-1 to the time of
9592fe8fb19SBen Gras 		** January 1, midnight, to get the day.
9602fe8fb19SBen Gras 		*/
961*0a6a1f1dSLionel Sambuc 		value = (rulep->r_day - 1) * SECSPERDAY;
9622fe8fb19SBen Gras 		if (leapyear && rulep->r_day >= 60)
9632fe8fb19SBen Gras 			value += SECSPERDAY;
9642fe8fb19SBen Gras 		break;
9652fe8fb19SBen Gras 
9662fe8fb19SBen Gras 	case DAY_OF_YEAR:
9672fe8fb19SBen Gras 		/*
9682fe8fb19SBen Gras 		** n - day of year.
9692fe8fb19SBen Gras 		** Just add SECSPERDAY times the day number to the time of
9702fe8fb19SBen Gras 		** January 1, midnight, to get the day.
9712fe8fb19SBen Gras 		*/
972*0a6a1f1dSLionel Sambuc 		value = rulep->r_day * SECSPERDAY;
9732fe8fb19SBen Gras 		break;
9742fe8fb19SBen Gras 
9752fe8fb19SBen Gras 	case MONTH_NTH_DAY_OF_WEEK:
9762fe8fb19SBen Gras 		/*
9772fe8fb19SBen Gras 		** Mm.n.d - nth "dth day" of month m.
9782fe8fb19SBen Gras 		*/
9792fe8fb19SBen Gras 
9802fe8fb19SBen Gras 		/*
9812fe8fb19SBen Gras 		** Use Zeller's Congruence to get day-of-week of first day of
9822fe8fb19SBen Gras 		** month.
9832fe8fb19SBen Gras 		*/
9842fe8fb19SBen Gras 		m1 = (rulep->r_mon + 9) % 12 + 1;
9852fe8fb19SBen Gras 		yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
9862fe8fb19SBen Gras 		yy1 = yy0 / 100;
9872fe8fb19SBen Gras 		yy2 = yy0 % 100;
9882fe8fb19SBen Gras 		dow = ((26 * m1 - 2) / 10 +
9892fe8fb19SBen Gras 			1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
9902fe8fb19SBen Gras 		if (dow < 0)
9912fe8fb19SBen Gras 			dow += DAYSPERWEEK;
9922fe8fb19SBen Gras 
9932fe8fb19SBen Gras 		/*
9942fe8fb19SBen Gras 		** "dow" is the day-of-week of the first day of the month. Get
9952fe8fb19SBen Gras 		** the day-of-month (zero-origin) of the first "dow" day of the
9962fe8fb19SBen Gras 		** month.
9972fe8fb19SBen Gras 		*/
9982fe8fb19SBen Gras 		d = rulep->r_day - dow;
9992fe8fb19SBen Gras 		if (d < 0)
10002fe8fb19SBen Gras 			d += DAYSPERWEEK;
10012fe8fb19SBen Gras 		for (i = 1; i < rulep->r_week; ++i) {
10022fe8fb19SBen Gras 			if (d + DAYSPERWEEK >=
10032fe8fb19SBen Gras 				mon_lengths[leapyear][rulep->r_mon - 1])
10042fe8fb19SBen Gras 					break;
10052fe8fb19SBen Gras 			d += DAYSPERWEEK;
10062fe8fb19SBen Gras 		}
10072fe8fb19SBen Gras 
10082fe8fb19SBen Gras 		/*
10092fe8fb19SBen Gras 		** "d" is the day-of-month (zero-origin) of the day we want.
10102fe8fb19SBen Gras 		*/
1011*0a6a1f1dSLionel Sambuc 		value = d * SECSPERDAY;
1012*0a6a1f1dSLionel Sambuc 		for (i = 0; i < rulep->r_mon - 1; ++i)
1013*0a6a1f1dSLionel Sambuc 			value += mon_lengths[leapyear][i] * SECSPERDAY;
10142fe8fb19SBen Gras 		break;
10152fe8fb19SBen Gras 	}
10162fe8fb19SBen Gras 
10172fe8fb19SBen Gras 	/*
1018*0a6a1f1dSLionel Sambuc 	** "value" is the year-relative time of 00:00:00 UT on the day in
1019*0a6a1f1dSLionel Sambuc 	** question. To get the year-relative time of the specified local
10202fe8fb19SBen Gras 	** time on that day, add the transition time and the current offset
102184d9c625SLionel Sambuc 	** from UT.
10222fe8fb19SBen Gras 	*/
1023*0a6a1f1dSLionel Sambuc 	return value + rulep->r_time + offset;
10242fe8fb19SBen Gras }
10252fe8fb19SBen Gras 
10262fe8fb19SBen Gras /*
10272fe8fb19SBen Gras ** Given a POSIX section 8-style TZ string, fill in the rule tables as
10282fe8fb19SBen Gras ** appropriate.
10292fe8fb19SBen Gras */
10302fe8fb19SBen Gras 
1031*0a6a1f1dSLionel Sambuc static bool
tzparse(const char * name,struct state * sp,bool lastditch)1032*0a6a1f1dSLionel Sambuc tzparse(const char *name, struct state *sp, bool lastditch)
10332fe8fb19SBen Gras {
10342fe8fb19SBen Gras 	const char *	stdname;
10352fe8fb19SBen Gras 	const char *	dstname;
10362fe8fb19SBen Gras 	size_t		stdlen;
10372fe8fb19SBen Gras 	size_t		dstlen;
1038*0a6a1f1dSLionel Sambuc 	size_t		charcnt;
103984d9c625SLionel Sambuc 	int_fast32_t	stdoffset;
104084d9c625SLionel Sambuc 	int_fast32_t	dstoffset;
10412fe8fb19SBen Gras 	char *		cp;
1042*0a6a1f1dSLionel Sambuc 	bool		load_ok;
10432fe8fb19SBen Gras 
1044*0a6a1f1dSLionel Sambuc 	dstname = NULL; /* XXX gcc */
10452fe8fb19SBen Gras 	stdname = name;
10462fe8fb19SBen Gras 	if (lastditch) {
1047*0a6a1f1dSLionel Sambuc 		stdlen = sizeof gmt - 1;
10482fe8fb19SBen Gras 		name += stdlen;
10492fe8fb19SBen Gras 		stdoffset = 0;
10502fe8fb19SBen Gras 	} else {
10512fe8fb19SBen Gras 		if (*name == '<') {
10522fe8fb19SBen Gras 			name++;
10532fe8fb19SBen Gras 			stdname = name;
10542fe8fb19SBen Gras 			name = getqzname(name, '>');
10552fe8fb19SBen Gras 			if (*name != '>')
1056*0a6a1f1dSLionel Sambuc 			  return false;
10572fe8fb19SBen Gras 			stdlen = name - stdname;
10582fe8fb19SBen Gras 			name++;
10592fe8fb19SBen Gras 		} else {
10602fe8fb19SBen Gras 			name = getzname(name);
10612fe8fb19SBen Gras 			stdlen = name - stdname;
10622fe8fb19SBen Gras 		}
1063*0a6a1f1dSLionel Sambuc 		if (!stdlen)
1064*0a6a1f1dSLionel Sambuc 			return false;
10652fe8fb19SBen Gras 		name = getoffset(name, &stdoffset);
10662fe8fb19SBen Gras 		if (name == NULL)
1067*0a6a1f1dSLionel Sambuc 			return false;
10682fe8fb19SBen Gras 	}
1069*0a6a1f1dSLionel Sambuc 	charcnt = stdlen + 1;
1070*0a6a1f1dSLionel Sambuc 	if (sizeof sp->chars < charcnt)
1071*0a6a1f1dSLionel Sambuc 		return false;
1072*0a6a1f1dSLionel Sambuc 	load_ok = tzload(TZDEFRULES, sp, false) == 0;
1073*0a6a1f1dSLionel Sambuc 	if (!load_ok)
10742fe8fb19SBen Gras 		sp->leapcnt = 0;		/* so, we're off a little */
10752fe8fb19SBen Gras 	if (*name != '\0') {
10762fe8fb19SBen Gras 		if (*name == '<') {
10772fe8fb19SBen Gras 			dstname = ++name;
10782fe8fb19SBen Gras 			name = getqzname(name, '>');
10792fe8fb19SBen Gras 			if (*name != '>')
1080*0a6a1f1dSLionel Sambuc 				return false;
10812fe8fb19SBen Gras 			dstlen = name - dstname;
10822fe8fb19SBen Gras 			name++;
10832fe8fb19SBen Gras 		} else {
10842fe8fb19SBen Gras 			dstname = name;
10852fe8fb19SBen Gras 			name = getzname(name);
10862fe8fb19SBen Gras 			dstlen = name - dstname; /* length of DST zone name */
10872fe8fb19SBen Gras 		}
1088*0a6a1f1dSLionel Sambuc 		if (!dstlen)
1089*0a6a1f1dSLionel Sambuc 		  return false;
1090*0a6a1f1dSLionel Sambuc 		charcnt += dstlen + 1;
1091*0a6a1f1dSLionel Sambuc 		if (sizeof sp->chars < charcnt)
1092*0a6a1f1dSLionel Sambuc 		  return false;
10932fe8fb19SBen Gras 		if (*name != '\0' && *name != ',' && *name != ';') {
10942fe8fb19SBen Gras 			name = getoffset(name, &dstoffset);
10952fe8fb19SBen Gras 			if (name == NULL)
1096*0a6a1f1dSLionel Sambuc 			  return false;
10972fe8fb19SBen Gras 		} else	dstoffset = stdoffset - SECSPERHOUR;
1098*0a6a1f1dSLionel Sambuc 		if (*name == '\0' && !load_ok)
10992fe8fb19SBen Gras 			name = TZDEFRULESTRING;
11002fe8fb19SBen Gras 		if (*name == ',' || *name == ';') {
11012fe8fb19SBen Gras 			struct rule	start;
11022fe8fb19SBen Gras 			struct rule	end;
11032fe8fb19SBen Gras 			int		year;
110484d9c625SLionel Sambuc 			int		yearlim;
1105*0a6a1f1dSLionel Sambuc 			int		timecnt;
11062fe8fb19SBen Gras 			time_t		janfirst;
11072fe8fb19SBen Gras 
11082fe8fb19SBen Gras 			++name;
11092fe8fb19SBen Gras 			if ((name = getrule(name, &start)) == NULL)
1110*0a6a1f1dSLionel Sambuc 				return false;
11112fe8fb19SBen Gras 			if (*name++ != ',')
1112*0a6a1f1dSLionel Sambuc 				return false;
11132fe8fb19SBen Gras 			if ((name = getrule(name, &end)) == NULL)
1114*0a6a1f1dSLionel Sambuc 				return false;
11152fe8fb19SBen Gras 			if (*name != '\0')
1116*0a6a1f1dSLionel Sambuc 				return false;
11172fe8fb19SBen Gras 			sp->typecnt = 2;	/* standard time and DST */
11182fe8fb19SBen Gras 			/*
11192fe8fb19SBen Gras 			** Two transitions per year, from EPOCH_YEAR forward.
11202fe8fb19SBen Gras 			*/
1121*0a6a1f1dSLionel Sambuc 			init_ttinfo(&sp->ttis[0], -dstoffset, true,
1122*0a6a1f1dSLionel Sambuc 			    (int)(stdlen + 1));
1123*0a6a1f1dSLionel Sambuc 			init_ttinfo(&sp->ttis[1], -stdoffset, false, 0);
1124*0a6a1f1dSLionel Sambuc 			sp->defaulttype = 0;
1125*0a6a1f1dSLionel Sambuc 			timecnt = 0;
11262fe8fb19SBen Gras 			janfirst = 0;
112784d9c625SLionel Sambuc 			yearlim = EPOCH_YEAR + YEARSPERREPEAT;
112884d9c625SLionel Sambuc 			for (year = EPOCH_YEAR; year < yearlim; year++) {
1129*0a6a1f1dSLionel Sambuc 				int_fast32_t
1130*0a6a1f1dSLionel Sambuc 				  starttime = transtime(year, &start, stdoffset),
1131*0a6a1f1dSLionel Sambuc 				  endtime = transtime(year, &end, dstoffset);
1132*0a6a1f1dSLionel Sambuc 				int_fast32_t
113384d9c625SLionel Sambuc 				  yearsecs = (year_lengths[isleap(year)]
113484d9c625SLionel Sambuc 					      * SECSPERDAY);
1135*0a6a1f1dSLionel Sambuc 				bool reversed = endtime < starttime;
1136*0a6a1f1dSLionel Sambuc 				if (reversed) {
1137*0a6a1f1dSLionel Sambuc 					int_fast32_t swap = starttime;
1138*0a6a1f1dSLionel Sambuc 					starttime = endtime;
1139*0a6a1f1dSLionel Sambuc 					endtime = swap;
1140*0a6a1f1dSLionel Sambuc 				}
1141*0a6a1f1dSLionel Sambuc 				if (reversed
114284d9c625SLionel Sambuc 				    || (starttime < endtime
114384d9c625SLionel Sambuc 					&& (endtime - starttime
114484d9c625SLionel Sambuc 					    < (yearsecs
114584d9c625SLionel Sambuc 					       + (stdoffset - dstoffset))))) {
1146*0a6a1f1dSLionel Sambuc 					if (TZ_MAX_TIMES - 2 < timecnt)
114784d9c625SLionel Sambuc 						break;
114884d9c625SLionel Sambuc 					yearlim = year + YEARSPERREPEAT + 1;
1149*0a6a1f1dSLionel Sambuc 					sp->ats[timecnt] = janfirst;
1150*0a6a1f1dSLionel Sambuc 					if (increment_overflow_time
1151*0a6a1f1dSLionel Sambuc 					    (&sp->ats[timecnt], starttime))
115284d9c625SLionel Sambuc 						break;
1153*0a6a1f1dSLionel Sambuc 					sp->types[timecnt++] = reversed;
1154*0a6a1f1dSLionel Sambuc 					sp->ats[timecnt] = janfirst;
1155*0a6a1f1dSLionel Sambuc 					if (increment_overflow_time
1156*0a6a1f1dSLionel Sambuc 					    (&sp->ats[timecnt], endtime))
1157*0a6a1f1dSLionel Sambuc 						break;
1158*0a6a1f1dSLionel Sambuc 					sp->types[timecnt++] = !reversed;
115984d9c625SLionel Sambuc 				}
1160*0a6a1f1dSLionel Sambuc 				if (increment_overflow_time(&janfirst, yearsecs))
1161*0a6a1f1dSLionel Sambuc 					break;
1162*0a6a1f1dSLionel Sambuc 			}
1163*0a6a1f1dSLionel Sambuc 			sp->timecnt = timecnt;
1164*0a6a1f1dSLionel Sambuc 			if (!timecnt)
116584d9c625SLionel Sambuc 				sp->typecnt = 1;	/* Perpetual DST.  */
11662fe8fb19SBen Gras 		} else {
116784d9c625SLionel Sambuc 			int_fast32_t	theirstdoffset;
116884d9c625SLionel Sambuc 			int_fast32_t	theirdstoffset;
116984d9c625SLionel Sambuc 			int_fast32_t	theiroffset;
1170*0a6a1f1dSLionel Sambuc 			bool		isdst;
11712fe8fb19SBen Gras 			int		i;
11722fe8fb19SBen Gras 			int		j;
11732fe8fb19SBen Gras 
11742fe8fb19SBen Gras 			if (*name != '\0')
1175*0a6a1f1dSLionel Sambuc 				return false;
11762fe8fb19SBen Gras 			/*
117784d9c625SLionel Sambuc 			** Initial values of theirstdoffset and theirdstoffset.
11782fe8fb19SBen Gras 			*/
11792fe8fb19SBen Gras 			theirstdoffset = 0;
11802fe8fb19SBen Gras 			for (i = 0; i < sp->timecnt; ++i) {
11812fe8fb19SBen Gras 				j = sp->types[i];
11822fe8fb19SBen Gras 				if (!sp->ttis[j].tt_isdst) {
11832fe8fb19SBen Gras 					theirstdoffset =
11842fe8fb19SBen Gras 						-sp->ttis[j].tt_gmtoff;
11852fe8fb19SBen Gras 					break;
11862fe8fb19SBen Gras 				}
11872fe8fb19SBen Gras 			}
11882fe8fb19SBen Gras 			theirdstoffset = 0;
11892fe8fb19SBen Gras 			for (i = 0; i < sp->timecnt; ++i) {
11902fe8fb19SBen Gras 				j = sp->types[i];
11912fe8fb19SBen Gras 				if (sp->ttis[j].tt_isdst) {
11922fe8fb19SBen Gras 					theirdstoffset =
11932fe8fb19SBen Gras 						-sp->ttis[j].tt_gmtoff;
11942fe8fb19SBen Gras 					break;
11952fe8fb19SBen Gras 				}
11962fe8fb19SBen Gras 			}
11972fe8fb19SBen Gras 			/*
11982fe8fb19SBen Gras 			** Initially we're assumed to be in standard time.
11992fe8fb19SBen Gras 			*/
1200*0a6a1f1dSLionel Sambuc 			isdst = false;
12012fe8fb19SBen Gras 			theiroffset = theirstdoffset;
12022fe8fb19SBen Gras 			/*
12032fe8fb19SBen Gras 			** Now juggle transition times and types
12042fe8fb19SBen Gras 			** tracking offsets as you do.
12052fe8fb19SBen Gras 			*/
12062fe8fb19SBen Gras 			for (i = 0; i < sp->timecnt; ++i) {
12072fe8fb19SBen Gras 				j = sp->types[i];
12082fe8fb19SBen Gras 				sp->types[i] = sp->ttis[j].tt_isdst;
12092fe8fb19SBen Gras 				if (sp->ttis[j].tt_ttisgmt) {
12102fe8fb19SBen Gras 					/* No adjustment to transition time */
12112fe8fb19SBen Gras 				} else {
12122fe8fb19SBen Gras 					/*
12132fe8fb19SBen Gras 					** If summer time is in effect, and the
12142fe8fb19SBen Gras 					** transition time was not specified as
12152fe8fb19SBen Gras 					** standard time, add the summer time
12162fe8fb19SBen Gras 					** offset to the transition time;
12172fe8fb19SBen Gras 					** otherwise, add the standard time
12182fe8fb19SBen Gras 					** offset to the transition time.
12192fe8fb19SBen Gras 					*/
12202fe8fb19SBen Gras 					/*
12212fe8fb19SBen Gras 					** Transitions from DST to DDST
12222fe8fb19SBen Gras 					** will effectively disappear since
12232fe8fb19SBen Gras 					** POSIX provides for only one DST
12242fe8fb19SBen Gras 					** offset.
12252fe8fb19SBen Gras 					*/
12262fe8fb19SBen Gras 					if (isdst && !sp->ttis[j].tt_ttisstd) {
1227f14fb602SLionel Sambuc 						sp->ats[i] += (time_t)
1228f14fb602SLionel Sambuc 						    (dstoffset - theirdstoffset);
12292fe8fb19SBen Gras 					} else {
1230f14fb602SLionel Sambuc 						sp->ats[i] += (time_t)
1231f14fb602SLionel Sambuc 						    (stdoffset - theirstdoffset);
12322fe8fb19SBen Gras 					}
12332fe8fb19SBen Gras 				}
12342fe8fb19SBen Gras 				theiroffset = -sp->ttis[j].tt_gmtoff;
1235*0a6a1f1dSLionel Sambuc 				if (sp->ttis[j].tt_isdst)
12362fe8fb19SBen Gras 					theirstdoffset = theiroffset;
12372fe8fb19SBen Gras 				else	theirdstoffset = theiroffset;
12382fe8fb19SBen Gras 			}
12392fe8fb19SBen Gras 			/*
12402fe8fb19SBen Gras 			** Finally, fill in ttis.
12412fe8fb19SBen Gras 			*/
1242*0a6a1f1dSLionel Sambuc 			init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1243*0a6a1f1dSLionel Sambuc 			init_ttinfo(&sp->ttis[1], -dstoffset, true,
1244*0a6a1f1dSLionel Sambuc 			    (int)(stdlen + 1));
12452fe8fb19SBen Gras 			sp->typecnt = 2;
1246*0a6a1f1dSLionel Sambuc 			sp->defaulttype = 0;
12472fe8fb19SBen Gras 		}
12482fe8fb19SBen Gras 	} else {
12492fe8fb19SBen Gras 		dstlen = 0;
12502fe8fb19SBen Gras 		sp->typecnt = 1;		/* only standard time */
12512fe8fb19SBen Gras 		sp->timecnt = 0;
1252*0a6a1f1dSLionel Sambuc 		init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1253*0a6a1f1dSLionel Sambuc 		init_ttinfo(&sp->ttis[1], 0, false, 0);
1254*0a6a1f1dSLionel Sambuc 		sp->defaulttype = 0;
12552fe8fb19SBen Gras 	}
1256*0a6a1f1dSLionel Sambuc 	sp->charcnt = charcnt;
12572fe8fb19SBen Gras 	cp = sp->chars;
1258*0a6a1f1dSLionel Sambuc 	(void) memcpy(cp, stdname, stdlen);
12592fe8fb19SBen Gras 	cp += stdlen;
12602fe8fb19SBen Gras 	*cp++ = '\0';
12612fe8fb19SBen Gras 	if (dstlen != 0) {
1262*0a6a1f1dSLionel Sambuc 		(void) memcpy(cp, dstname, dstlen);
12632fe8fb19SBen Gras 		*(cp + dstlen) = '\0';
12642fe8fb19SBen Gras 	}
1265*0a6a1f1dSLionel Sambuc 	return true;
12662fe8fb19SBen Gras }
12672fe8fb19SBen Gras 
12682fe8fb19SBen Gras static void
gmtload(struct state * const sp)1269*0a6a1f1dSLionel Sambuc gmtload(struct state *const sp)
12702fe8fb19SBen Gras {
1271*0a6a1f1dSLionel Sambuc 	if (tzload(gmt, sp, true) != 0)
1272*0a6a1f1dSLionel Sambuc 		(void) tzparse(gmt, sp, true);
12732fe8fb19SBen Gras }
12742fe8fb19SBen Gras 
1275*0a6a1f1dSLionel Sambuc static int
zoneinit(struct state * sp,char const * name)1276*0a6a1f1dSLionel Sambuc zoneinit(struct state *sp, char const *name)
12772fe8fb19SBen Gras {
1278*0a6a1f1dSLionel Sambuc 	if (name && ! name[0]) {
12792fe8fb19SBen Gras 		/*
1280*0a6a1f1dSLionel Sambuc 		** User wants it fast rather than right.
12812fe8fb19SBen Gras 		*/
1282*0a6a1f1dSLionel Sambuc 		sp->leapcnt = 0;		/* so, we're off a little */
1283*0a6a1f1dSLionel Sambuc 		sp->timecnt = 0;
1284*0a6a1f1dSLionel Sambuc 		sp->typecnt = 0;
1285*0a6a1f1dSLionel Sambuc 		sp->charcnt = 0;
1286*0a6a1f1dSLionel Sambuc 		sp->goback = sp->goahead = false;
1287*0a6a1f1dSLionel Sambuc 		init_ttinfo(&sp->ttis[0], 0, false, 0);
1288*0a6a1f1dSLionel Sambuc 		strcpy(sp->chars, gmt);
1289*0a6a1f1dSLionel Sambuc 		sp->defaulttype = 0;
1290*0a6a1f1dSLionel Sambuc 		return 0;
1291*0a6a1f1dSLionel Sambuc 	} else {
1292*0a6a1f1dSLionel Sambuc 		int err = tzload(name, sp, true);
1293*0a6a1f1dSLionel Sambuc 		if (err != 0 && name && name[0] != ':' &&
1294*0a6a1f1dSLionel Sambuc 		    tzparse(name, sp, false))
1295*0a6a1f1dSLionel Sambuc 			err = 0;
1296*0a6a1f1dSLionel Sambuc 		if (err == 0)
1297*0a6a1f1dSLionel Sambuc 			scrub_abbrs(sp);
1298*0a6a1f1dSLionel Sambuc 		return err;
1299*0a6a1f1dSLionel Sambuc 	}
1300*0a6a1f1dSLionel Sambuc }
1301*0a6a1f1dSLionel Sambuc 
1302*0a6a1f1dSLionel Sambuc static void
tzsetlcl(char const * name)1303*0a6a1f1dSLionel Sambuc tzsetlcl(char const *name)
1304*0a6a1f1dSLionel Sambuc {
1305*0a6a1f1dSLionel Sambuc 	struct state *sp = lclptr;
1306*0a6a1f1dSLionel Sambuc 	int lcl = name ? strlen(name) < sizeof lcl_TZname : -1;
1307*0a6a1f1dSLionel Sambuc 	if (lcl < 0 ? lcl_is_set < 0
1308*0a6a1f1dSLionel Sambuc 	    : 0 < lcl_is_set && strcmp(lcl_TZname, name) == 0)
1309*0a6a1f1dSLionel Sambuc 		return;
1310*0a6a1f1dSLionel Sambuc 
1311*0a6a1f1dSLionel Sambuc 	if (! sp)
1312*0a6a1f1dSLionel Sambuc 		lclptr = sp = malloc(sizeof *lclptr);
1313*0a6a1f1dSLionel Sambuc 	if (sp) {
1314*0a6a1f1dSLionel Sambuc 		if (zoneinit(sp, name) != 0)
1315*0a6a1f1dSLionel Sambuc 			zoneinit(sp, "");
1316*0a6a1f1dSLionel Sambuc 		if (0 < lcl)
1317*0a6a1f1dSLionel Sambuc 			strcpy(lcl_TZname, name);
1318*0a6a1f1dSLionel Sambuc 	}
1319*0a6a1f1dSLionel Sambuc 	settzname();
1320*0a6a1f1dSLionel Sambuc 	lcl_is_set = lcl;
1321*0a6a1f1dSLionel Sambuc }
1322*0a6a1f1dSLionel Sambuc 
1323*0a6a1f1dSLionel Sambuc #ifdef STD_INSPIRED
13242fe8fb19SBen Gras void
tzsetwall(void)13252fe8fb19SBen Gras tzsetwall(void)
13262fe8fb19SBen Gras {
13272fe8fb19SBen Gras 	rwlock_wrlock(&lcl_lock);
1328*0a6a1f1dSLionel Sambuc 	tzsetlcl(NULL);
13292fe8fb19SBen Gras 	rwlock_unlock(&lcl_lock);
13302fe8fb19SBen Gras }
1331*0a6a1f1dSLionel Sambuc #endif
13322fe8fb19SBen Gras 
1333*0a6a1f1dSLionel Sambuc static void
tzset_unlocked(void)13342fe8fb19SBen Gras tzset_unlocked(void)
13352fe8fb19SBen Gras {
1336*0a6a1f1dSLionel Sambuc 	tzsetlcl(getenv("TZ"));
13372fe8fb19SBen Gras }
13382fe8fb19SBen Gras 
13392fe8fb19SBen Gras void
tzset(void)13402fe8fb19SBen Gras tzset(void)
13412fe8fb19SBen Gras {
13422fe8fb19SBen Gras 	rwlock_wrlock(&lcl_lock);
13432fe8fb19SBen Gras 	tzset_unlocked();
13442fe8fb19SBen Gras 	rwlock_unlock(&lcl_lock);
13452fe8fb19SBen Gras }
13462fe8fb19SBen Gras 
1347*0a6a1f1dSLionel Sambuc static void
gmtcheck(void)1348*0a6a1f1dSLionel Sambuc gmtcheck(void)
1349*0a6a1f1dSLionel Sambuc {
1350*0a6a1f1dSLionel Sambuc 	static bool gmt_is_set;
1351*0a6a1f1dSLionel Sambuc 	rwlock_wrlock(&lcl_lock);
1352*0a6a1f1dSLionel Sambuc 	if (! gmt_is_set) {
1353*0a6a1f1dSLionel Sambuc 		gmtptr = malloc(sizeof *gmtptr);
1354*0a6a1f1dSLionel Sambuc 		if (gmtptr)
1355*0a6a1f1dSLionel Sambuc 			gmtload(gmtptr);
1356*0a6a1f1dSLionel Sambuc 		gmt_is_set = true;
1357*0a6a1f1dSLionel Sambuc 	}
1358*0a6a1f1dSLionel Sambuc 	rwlock_unlock(&lcl_lock);
1359*0a6a1f1dSLionel Sambuc }
1360*0a6a1f1dSLionel Sambuc 
1361*0a6a1f1dSLionel Sambuc #if NETBSD_INSPIRED
1362*0a6a1f1dSLionel Sambuc 
1363*0a6a1f1dSLionel Sambuc timezone_t
tzalloc(const char * name)1364*0a6a1f1dSLionel Sambuc tzalloc(const char *name)
1365*0a6a1f1dSLionel Sambuc {
1366*0a6a1f1dSLionel Sambuc 	timezone_t sp = malloc(sizeof *sp);
1367*0a6a1f1dSLionel Sambuc 	if (sp) {
1368*0a6a1f1dSLionel Sambuc 		int err = zoneinit(sp, name);
1369*0a6a1f1dSLionel Sambuc 		if (err != 0) {
1370*0a6a1f1dSLionel Sambuc 			free(sp);
1371*0a6a1f1dSLionel Sambuc 			errno = err;
1372*0a6a1f1dSLionel Sambuc 			return NULL;
1373*0a6a1f1dSLionel Sambuc 		}
1374*0a6a1f1dSLionel Sambuc 	}
1375*0a6a1f1dSLionel Sambuc 	return sp;
1376*0a6a1f1dSLionel Sambuc }
1377*0a6a1f1dSLionel Sambuc 
1378*0a6a1f1dSLionel Sambuc void
tzfree(timezone_t sp)1379*0a6a1f1dSLionel Sambuc tzfree(timezone_t sp)
1380*0a6a1f1dSLionel Sambuc {
1381*0a6a1f1dSLionel Sambuc 	free(sp);
1382*0a6a1f1dSLionel Sambuc }
1383*0a6a1f1dSLionel Sambuc 
1384*0a6a1f1dSLionel Sambuc /*
1385*0a6a1f1dSLionel Sambuc ** NetBSD 6.1.4 has ctime_rz, but omit it because POSIX says ctime and
1386*0a6a1f1dSLionel Sambuc ** ctime_r are obsolescent and have potential security problems that
1387*0a6a1f1dSLionel Sambuc ** ctime_rz would share.  Callers can instead use localtime_rz + strftime.
1388*0a6a1f1dSLionel Sambuc **
1389*0a6a1f1dSLionel Sambuc ** NetBSD 6.1.4 has tzgetname, but omit it because it doesn't work
1390*0a6a1f1dSLionel Sambuc ** in zones with three or more time zone abbreviations.
1391*0a6a1f1dSLionel Sambuc ** Callers can instead use localtime_rz + strftime.
1392*0a6a1f1dSLionel Sambuc */
1393*0a6a1f1dSLionel Sambuc 
1394*0a6a1f1dSLionel Sambuc #endif
1395*0a6a1f1dSLionel Sambuc 
13962fe8fb19SBen Gras /*
13972fe8fb19SBen Gras ** The easy way to behave "as if no library function calls" localtime
1398*0a6a1f1dSLionel Sambuc ** is to not call it, so we drop its guts into "localsub", which can be
1399*0a6a1f1dSLionel Sambuc ** freely called. (And no, the PANS doesn't require the above behavior,
14002fe8fb19SBen Gras ** but it *is* desirable.)
14012fe8fb19SBen Gras **
1402*0a6a1f1dSLionel Sambuc ** If successful and SETNAME is nonzero,
1403*0a6a1f1dSLionel Sambuc ** set the applicable parts of tzname, timezone and altzone;
1404*0a6a1f1dSLionel Sambuc ** however, it's OK to omit this step if the time zone is POSIX-compatible,
1405*0a6a1f1dSLionel Sambuc ** since in that case tzset should have already done this step correctly.
1406*0a6a1f1dSLionel Sambuc ** SETNAME's type is intfast32_t for compatibility with gmtsub,
1407*0a6a1f1dSLionel Sambuc ** but it is actually a boolean and its value should be 0 or 1.
14082fe8fb19SBen Gras */
14092fe8fb19SBen Gras 
14102fe8fb19SBen Gras /*ARGSUSED*/
14112fe8fb19SBen Gras static struct tm *
localsub(struct state const * sp,time_t const * timep,int_fast32_t setname,struct tm * const tmp)1412*0a6a1f1dSLionel Sambuc localsub(struct state const *sp, time_t const *timep, int_fast32_t setname,
14132fe8fb19SBen Gras 	 struct tm *const tmp)
14142fe8fb19SBen Gras {
14152fe8fb19SBen Gras 	const struct ttinfo *	ttisp;
14162fe8fb19SBen Gras 	int			i;
14172fe8fb19SBen Gras 	struct tm *		result;
14182fe8fb19SBen Gras 	const time_t			t = *timep;
14192fe8fb19SBen Gras 
1420*0a6a1f1dSLionel Sambuc 	if (sp == NULL) {
1421*0a6a1f1dSLionel Sambuc 		/* Don't bother to set tzname etc.; tzset has already done it.  */
1422*0a6a1f1dSLionel Sambuc 		return gmtsub(gmtptr, timep, 0, tmp);
1423*0a6a1f1dSLionel Sambuc 	}
14242fe8fb19SBen Gras 	if ((sp->goback && t < sp->ats[0]) ||
14252fe8fb19SBen Gras 		(sp->goahead && t > sp->ats[sp->timecnt - 1])) {
14262fe8fb19SBen Gras 			time_t			newt = t;
14272fe8fb19SBen Gras 			time_t		seconds;
142884d9c625SLionel Sambuc 			time_t		years;
14292fe8fb19SBen Gras 
14302fe8fb19SBen Gras 			if (t < sp->ats[0])
14312fe8fb19SBen Gras 				seconds = sp->ats[0] - t;
14322fe8fb19SBen Gras 			else	seconds = t - sp->ats[sp->timecnt - 1];
14332fe8fb19SBen Gras 			--seconds;
143484d9c625SLionel Sambuc 			years = (time_t)((seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT);
143584d9c625SLionel Sambuc 			seconds = (time_t)(years * AVGSECSPERYEAR);
14362fe8fb19SBen Gras 			if (t < sp->ats[0])
14372fe8fb19SBen Gras 				newt += seconds;
14382fe8fb19SBen Gras 			else	newt -= seconds;
14392fe8fb19SBen Gras 			if (newt < sp->ats[0] ||
1440*0a6a1f1dSLionel Sambuc 				newt > sp->ats[sp->timecnt - 1]) {
1441*0a6a1f1dSLionel Sambuc 				errno = EINVAL;
14422fe8fb19SBen Gras 				return NULL;	/* "cannot happen" */
1443*0a6a1f1dSLionel Sambuc 			}
1444*0a6a1f1dSLionel Sambuc 			result = localsub(sp, &newt, setname, tmp);
1445*0a6a1f1dSLionel Sambuc 			if (result) {
1446*0a6a1f1dSLionel Sambuc 				int_fast64_t newy;
14472fe8fb19SBen Gras 
1448*0a6a1f1dSLionel Sambuc 				newy = result->tm_year;
14492fe8fb19SBen Gras 				if (t < sp->ats[0])
145084d9c625SLionel Sambuc 					newy -= years;
145184d9c625SLionel Sambuc 				else	newy += years;
1452*0a6a1f1dSLionel Sambuc 				if (! (INT_MIN <= newy && newy <= INT_MAX)) {
1453*0a6a1f1dSLionel Sambuc 					errno = EOVERFLOW;
14542fe8fb19SBen Gras 					return NULL;
14552fe8fb19SBen Gras 				}
1456*0a6a1f1dSLionel Sambuc 				result->tm_year = (int)newy;
1457*0a6a1f1dSLionel Sambuc 			}
14582fe8fb19SBen Gras 			return result;
14592fe8fb19SBen Gras 	}
14602fe8fb19SBen Gras 	if (sp->timecnt == 0 || t < sp->ats[0]) {
146184d9c625SLionel Sambuc 		i = sp->defaulttype;
14622fe8fb19SBen Gras 	} else {
14632fe8fb19SBen Gras 		int	lo = 1;
14642fe8fb19SBen Gras 		int	hi = sp->timecnt;
14652fe8fb19SBen Gras 
14662fe8fb19SBen Gras 		while (lo < hi) {
14672fe8fb19SBen Gras 			int	mid = (lo + hi) / 2;
14682fe8fb19SBen Gras 
14692fe8fb19SBen Gras 			if (t < sp->ats[mid])
14702fe8fb19SBen Gras 				hi = mid;
14712fe8fb19SBen Gras 			else	lo = mid + 1;
14722fe8fb19SBen Gras 		}
14732fe8fb19SBen Gras 		i = (int) sp->types[lo - 1];
14742fe8fb19SBen Gras 	}
14752fe8fb19SBen Gras 	ttisp = &sp->ttis[i];
14762fe8fb19SBen Gras 	/*
14772fe8fb19SBen Gras 	** To get (wrong) behavior that's compatible with System V Release 2.0
14782fe8fb19SBen Gras 	** you'd replace the statement below with
14792fe8fb19SBen Gras 	**	t += ttisp->tt_gmtoff;
14802fe8fb19SBen Gras 	**	timesub(&t, 0L, sp, tmp);
14812fe8fb19SBen Gras 	*/
1482*0a6a1f1dSLionel Sambuc 	result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1483*0a6a1f1dSLionel Sambuc 	if (result) {
1484*0a6a1f1dSLionel Sambuc 		result->tm_isdst = ttisp->tt_isdst;
14852fe8fb19SBen Gras #ifdef TM_ZONE
1486*0a6a1f1dSLionel Sambuc 		result->TM_ZONE = __UNCONST(&sp->chars[ttisp->tt_abbrind]);
14872fe8fb19SBen Gras #endif /* defined TM_ZONE */
1488*0a6a1f1dSLionel Sambuc 		if (setname)
1489*0a6a1f1dSLionel Sambuc 			update_tzname_etc(sp, ttisp);
1490*0a6a1f1dSLionel Sambuc 	}
14912fe8fb19SBen Gras 	return result;
14922fe8fb19SBen Gras }
14932fe8fb19SBen Gras 
1494*0a6a1f1dSLionel Sambuc #if NETBSD_INSPIRED
14952fe8fb19SBen Gras 
14962fe8fb19SBen Gras struct tm *
localtime_rz(timezone_t sp,time_t const * timep,struct tm * tmp)1497*0a6a1f1dSLionel Sambuc localtime_rz(timezone_t sp, time_t const *timep, struct tm *tmp)
14982fe8fb19SBen Gras {
1499*0a6a1f1dSLionel Sambuc 	return localsub(sp, timep, 0, tmp);
1500*0a6a1f1dSLionel Sambuc }
1501*0a6a1f1dSLionel Sambuc 
1502*0a6a1f1dSLionel Sambuc #endif
1503*0a6a1f1dSLionel Sambuc 
1504*0a6a1f1dSLionel Sambuc static struct tm *
localtime_tzset(time_t const * timep,struct tm * tmp,bool setname)1505*0a6a1f1dSLionel Sambuc localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
1506*0a6a1f1dSLionel Sambuc {
1507*0a6a1f1dSLionel Sambuc 	rwlock_wrlock(&lcl_lock);
1508*0a6a1f1dSLionel Sambuc 	if (setname || !lcl_is_set)
15092fe8fb19SBen Gras 		tzset_unlocked();
1510*0a6a1f1dSLionel Sambuc 	tmp = localsub(lclptr, timep, setname, tmp);
15112fe8fb19SBen Gras 	rwlock_unlock(&lcl_lock);
15122fe8fb19SBen Gras 	return tmp;
15132fe8fb19SBen Gras }
15142fe8fb19SBen Gras 
15152fe8fb19SBen Gras struct tm *
localtime(const time_t * timep)1516*0a6a1f1dSLionel Sambuc localtime(const time_t *timep)
15172fe8fb19SBen Gras {
1518*0a6a1f1dSLionel Sambuc 	return localtime_tzset(timep, &tm, true);
15192fe8fb19SBen Gras }
15202fe8fb19SBen Gras 
15212fe8fb19SBen Gras struct tm *
localtime_r(const time_t * __restrict timep,struct tm * tmp)1522*0a6a1f1dSLionel Sambuc localtime_r(const time_t * __restrict timep, struct tm *tmp)
15232fe8fb19SBen Gras {
1524*0a6a1f1dSLionel Sambuc 	return localtime_tzset(timep, tmp, false);
15252fe8fb19SBen Gras }
15262fe8fb19SBen Gras 
15272fe8fb19SBen Gras /*
15282fe8fb19SBen Gras ** gmtsub is to gmtime as localsub is to localtime.
15292fe8fb19SBen Gras */
15302fe8fb19SBen Gras 
15312fe8fb19SBen Gras static struct tm *
gmtsub(struct state const * sp,const time_t * timep,int_fast32_t offset,struct tm * tmp)1532*0a6a1f1dSLionel Sambuc gmtsub(struct state const *sp, const time_t *timep, int_fast32_t offset,
1533*0a6a1f1dSLionel Sambuc        struct tm *tmp)
15342fe8fb19SBen Gras {
15352fe8fb19SBen Gras 	struct tm *	result;
15362fe8fb19SBen Gras 
1537*0a6a1f1dSLionel Sambuc 	result = timesub(timep, offset, gmtptr, tmp);
15382fe8fb19SBen Gras #ifdef TM_ZONE
15392fe8fb19SBen Gras 	/*
15402fe8fb19SBen Gras 	** Could get fancy here and deliver something such as
154184d9c625SLionel Sambuc 	** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
15422fe8fb19SBen Gras 	** but this is no time for a treasure hunt.
15432fe8fb19SBen Gras 	*/
1544*0a6a1f1dSLionel Sambuc 	if (result)
1545*0a6a1f1dSLionel Sambuc 		result->TM_ZONE = offset ? __UNCONST(wildabbr) : gmtptr ?
1546*0a6a1f1dSLionel Sambuc 		    gmtptr->chars : __UNCONST(gmt);
15472fe8fb19SBen Gras #endif /* defined TM_ZONE */
15482fe8fb19SBen Gras 	return result;
15492fe8fb19SBen Gras }
15502fe8fb19SBen Gras 
15512fe8fb19SBen Gras 
15522fe8fb19SBen Gras /*
15532fe8fb19SBen Gras ** Re-entrant version of gmtime.
15542fe8fb19SBen Gras */
15552fe8fb19SBen Gras 
15562fe8fb19SBen Gras struct tm *
gmtime_r(const time_t * timep,struct tm * tmp)1557*0a6a1f1dSLionel Sambuc gmtime_r(const time_t *timep, struct tm *tmp)
15582fe8fb19SBen Gras {
1559*0a6a1f1dSLionel Sambuc 	gmtcheck();
1560*0a6a1f1dSLionel Sambuc 	return gmtsub(NULL, timep, 0, tmp);
15612fe8fb19SBen Gras }
15622fe8fb19SBen Gras 
1563*0a6a1f1dSLionel Sambuc struct tm *
gmtime(const time_t * timep)1564*0a6a1f1dSLionel Sambuc gmtime(const time_t *timep)
1565*0a6a1f1dSLionel Sambuc {
1566*0a6a1f1dSLionel Sambuc 	return gmtime_r(timep, &tm);
1567*0a6a1f1dSLionel Sambuc }
15682fe8fb19SBen Gras #ifdef STD_INSPIRED
15692fe8fb19SBen Gras 
15702fe8fb19SBen Gras struct tm *
offtime(const time_t * timep,long offset)1571*0a6a1f1dSLionel Sambuc offtime(const time_t *timep, long offset)
15722fe8fb19SBen Gras {
1573*0a6a1f1dSLionel Sambuc 	gmtcheck();
1574*0a6a1f1dSLionel Sambuc 	return gmtsub(gmtptr, timep, (int_fast32_t)offset, &tm);
15752fe8fb19SBen Gras }
15762fe8fb19SBen Gras 
15772fe8fb19SBen Gras struct tm *
offtime_r(const time_t * timep,long offset,struct tm * tmp)15782fe8fb19SBen Gras offtime_r(const time_t *timep, long offset, struct tm *tmp)
15792fe8fb19SBen Gras {
1580*0a6a1f1dSLionel Sambuc 	gmtcheck();
1581*0a6a1f1dSLionel Sambuc 	return gmtsub(NULL, timep, (int_fast32_t)offset, tmp);
15822fe8fb19SBen Gras }
15832fe8fb19SBen Gras 
15842fe8fb19SBen Gras #endif /* defined STD_INSPIRED */
15852fe8fb19SBen Gras 
15862fe8fb19SBen Gras /*
15872fe8fb19SBen Gras ** Return the number of leap years through the end of the given year
15882fe8fb19SBen Gras ** where, to make the math easy, the answer for year zero is defined as zero.
15892fe8fb19SBen Gras */
15902fe8fb19SBen Gras 
1591*0a6a1f1dSLionel Sambuc static int ATTRIBUTE_PURE
leaps_thru_end_of(const int y)15922fe8fb19SBen Gras leaps_thru_end_of(const int y)
15932fe8fb19SBen Gras {
15942fe8fb19SBen Gras 	return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
15952fe8fb19SBen Gras 		-(leaps_thru_end_of(-(y + 1)) + 1);
15962fe8fb19SBen Gras }
15972fe8fb19SBen Gras 
15982fe8fb19SBen Gras static struct tm *
timesub(const time_t * timep,int_fast32_t offset,const struct state * sp,struct tm * tmp)1599*0a6a1f1dSLionel Sambuc timesub(const time_t *timep, int_fast32_t offset,
1600*0a6a1f1dSLionel Sambuc     const struct state *sp, struct tm *tmp)
16012fe8fb19SBen Gras {
16022fe8fb19SBen Gras 	const struct lsinfo *	lp;
16032fe8fb19SBen Gras 	time_t			tdays;
16042fe8fb19SBen Gras 	int			idays;	/* unsigned would be so 2003 */
160584d9c625SLionel Sambuc 	int_fast64_t		rem;
16062fe8fb19SBen Gras 	int			y;
16072fe8fb19SBen Gras 	const int *		ip;
160884d9c625SLionel Sambuc 	int_fast64_t		corr;
1609*0a6a1f1dSLionel Sambuc 	bool			hit;
16102fe8fb19SBen Gras 	int			i;
16112fe8fb19SBen Gras 
16122fe8fb19SBen Gras 	corr = 0;
1613*0a6a1f1dSLionel Sambuc 	hit = false;
16142fe8fb19SBen Gras 	i = (sp == NULL) ? 0 : sp->leapcnt;
16152fe8fb19SBen Gras 	while (--i >= 0) {
16162fe8fb19SBen Gras 		lp = &sp->lsis[i];
16172fe8fb19SBen Gras 		if (*timep >= lp->ls_trans) {
16182fe8fb19SBen Gras 			if (*timep == lp->ls_trans) {
16192fe8fb19SBen Gras 				hit = ((i == 0 && lp->ls_corr > 0) ||
16202fe8fb19SBen Gras 					lp->ls_corr > sp->lsis[i - 1].ls_corr);
16212fe8fb19SBen Gras 				if (hit)
16222fe8fb19SBen Gras 					while (i > 0 &&
16232fe8fb19SBen Gras 						sp->lsis[i].ls_trans ==
16242fe8fb19SBen Gras 						sp->lsis[i - 1].ls_trans + 1 &&
16252fe8fb19SBen Gras 						sp->lsis[i].ls_corr ==
16262fe8fb19SBen Gras 						sp->lsis[i - 1].ls_corr + 1) {
16272fe8fb19SBen Gras 							++hit;
16282fe8fb19SBen Gras 							--i;
16292fe8fb19SBen Gras 					}
16302fe8fb19SBen Gras 			}
16312fe8fb19SBen Gras 			corr = lp->ls_corr;
16322fe8fb19SBen Gras 			break;
16332fe8fb19SBen Gras 		}
16342fe8fb19SBen Gras 	}
16352fe8fb19SBen Gras 	y = EPOCH_YEAR;
1636f14fb602SLionel Sambuc 	tdays = (time_t)(*timep / SECSPERDAY);
1637*0a6a1f1dSLionel Sambuc 	rem = *timep % SECSPERDAY;
16382fe8fb19SBen Gras 	while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
16392fe8fb19SBen Gras 		int		newy;
16402fe8fb19SBen Gras 		time_t	tdelta;
16412fe8fb19SBen Gras 		int	idelta;
16422fe8fb19SBen Gras 		int	leapdays;
16432fe8fb19SBen Gras 
16442fe8fb19SBen Gras 		tdelta = tdays / DAYSPERLYEAR;
164584d9c625SLionel Sambuc 		if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
164684d9c625SLionel Sambuc 		       && tdelta <= INT_MAX))
1647*0a6a1f1dSLionel Sambuc 			goto out_of_range;
1648*0a6a1f1dSLionel Sambuc 		_DIAGASSERT(__type_fit(int, tdelta));
1649*0a6a1f1dSLionel Sambuc 		idelta = (int)tdelta;
16502fe8fb19SBen Gras 		if (idelta == 0)
16512fe8fb19SBen Gras 			idelta = (tdays < 0) ? -1 : 1;
16522fe8fb19SBen Gras 		newy = y;
16532fe8fb19SBen Gras 		if (increment_overflow(&newy, idelta))
1654*0a6a1f1dSLionel Sambuc 			goto out_of_range;
16552fe8fb19SBen Gras 		leapdays = leaps_thru_end_of(newy - 1) -
16562fe8fb19SBen Gras 			leaps_thru_end_of(y - 1);
16572fe8fb19SBen Gras 		tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
16582fe8fb19SBen Gras 		tdays -= leapdays;
16592fe8fb19SBen Gras 		y = newy;
16602fe8fb19SBen Gras 	}
16612fe8fb19SBen Gras 	/*
16622fe8fb19SBen Gras 	** Given the range, we can now fearlessly cast...
16632fe8fb19SBen Gras 	*/
16642fe8fb19SBen Gras 	idays = (int) tdays;
16652fe8fb19SBen Gras 	rem += offset - corr;
16662fe8fb19SBen Gras 	while (rem < 0) {
16672fe8fb19SBen Gras 		rem += SECSPERDAY;
16682fe8fb19SBen Gras 		--idays;
16692fe8fb19SBen Gras 	}
16702fe8fb19SBen Gras 	while (rem >= SECSPERDAY) {
16712fe8fb19SBen Gras 		rem -= SECSPERDAY;
16722fe8fb19SBen Gras 		++idays;
16732fe8fb19SBen Gras 	}
16742fe8fb19SBen Gras 	while (idays < 0) {
16752fe8fb19SBen Gras 		if (increment_overflow(&y, -1))
1676*0a6a1f1dSLionel Sambuc 			goto out_of_range;
16772fe8fb19SBen Gras 		idays += year_lengths[isleap(y)];
16782fe8fb19SBen Gras 	}
16792fe8fb19SBen Gras 	while (idays >= year_lengths[isleap(y)]) {
16802fe8fb19SBen Gras 		idays -= year_lengths[isleap(y)];
16812fe8fb19SBen Gras 		if (increment_overflow(&y, 1))
1682*0a6a1f1dSLionel Sambuc 			goto out_of_range;
16832fe8fb19SBen Gras 	}
16842fe8fb19SBen Gras 	tmp->tm_year = y;
16852fe8fb19SBen Gras 	if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1686*0a6a1f1dSLionel Sambuc 		goto out_of_range;
16872fe8fb19SBen Gras 	tmp->tm_yday = idays;
16882fe8fb19SBen Gras 	/*
16892fe8fb19SBen Gras 	** The "extra" mods below avoid overflow problems.
16902fe8fb19SBen Gras 	*/
16912fe8fb19SBen Gras 	tmp->tm_wday = EPOCH_WDAY +
16922fe8fb19SBen Gras 		((y - EPOCH_YEAR) % DAYSPERWEEK) *
16932fe8fb19SBen Gras 		(DAYSPERNYEAR % DAYSPERWEEK) +
16942fe8fb19SBen Gras 		leaps_thru_end_of(y - 1) -
16952fe8fb19SBen Gras 		leaps_thru_end_of(EPOCH_YEAR - 1) +
16962fe8fb19SBen Gras 		idays;
16972fe8fb19SBen Gras 	tmp->tm_wday %= DAYSPERWEEK;
16982fe8fb19SBen Gras 	if (tmp->tm_wday < 0)
16992fe8fb19SBen Gras 		tmp->tm_wday += DAYSPERWEEK;
17002fe8fb19SBen Gras 	tmp->tm_hour = (int) (rem / SECSPERHOUR);
17012fe8fb19SBen Gras 	rem %= SECSPERHOUR;
17022fe8fb19SBen Gras 	tmp->tm_min = (int) (rem / SECSPERMIN);
17032fe8fb19SBen Gras 	/*
17042fe8fb19SBen Gras 	** A positive leap second requires a special
17052fe8fb19SBen Gras 	** representation. This uses "... ??:59:60" et seq.
17062fe8fb19SBen Gras 	*/
17072fe8fb19SBen Gras 	tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
17082fe8fb19SBen Gras 	ip = mon_lengths[isleap(y)];
17092fe8fb19SBen Gras 	for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
17102fe8fb19SBen Gras 		idays -= ip[tmp->tm_mon];
17112fe8fb19SBen Gras 	tmp->tm_mday = (int) (idays + 1);
17122fe8fb19SBen Gras 	tmp->tm_isdst = 0;
17132fe8fb19SBen Gras #ifdef TM_GMTOFF
17142fe8fb19SBen Gras 	tmp->TM_GMTOFF = offset;
17152fe8fb19SBen Gras #endif /* defined TM_GMTOFF */
17162fe8fb19SBen Gras 	return tmp;
1717*0a6a1f1dSLionel Sambuc out_of_range:
1718*0a6a1f1dSLionel Sambuc 	errno = EOVERFLOW;
1719*0a6a1f1dSLionel Sambuc 	return NULL;
17202fe8fb19SBen Gras }
17212fe8fb19SBen Gras 
1722af4345b0SDavid van Moolenbroek #if !defined(__minix) || !defined(_LIBMINC)
17232fe8fb19SBen Gras char *
ctime(const time_t * timep)1724*0a6a1f1dSLionel Sambuc ctime(const time_t *timep)
17252fe8fb19SBen Gras {
17262fe8fb19SBen Gras /*
17272fe8fb19SBen Gras ** Section 4.12.3.2 of X3.159-1989 requires that
17282fe8fb19SBen Gras **	The ctime function converts the calendar time pointed to by timer
17292fe8fb19SBen Gras **	to local time in the form of a string. It is equivalent to
17302fe8fb19SBen Gras **		asctime(localtime(timer))
17312fe8fb19SBen Gras */
1732*0a6a1f1dSLionel Sambuc 	struct tm *tmp = localtime(timep);
1733*0a6a1f1dSLionel Sambuc 	return tmp ? asctime(tmp) : NULL;
17342fe8fb19SBen Gras }
17352fe8fb19SBen Gras 
17362fe8fb19SBen Gras char *
ctime_r(const time_t * timep,char * buf)1737*0a6a1f1dSLionel Sambuc ctime_r(const time_t *timep, char *buf)
17382fe8fb19SBen Gras {
1739*0a6a1f1dSLionel Sambuc 	struct tm mytm;
1740*0a6a1f1dSLionel Sambuc 	struct tm *tmp = localtime_r(timep, &mytm);
1741*0a6a1f1dSLionel Sambuc 	return tmp ? asctime_r(tmp, buf) : NULL;
17422fe8fb19SBen Gras }
17432fe8fb19SBen Gras 
17442fe8fb19SBen Gras char *
ctime_rz(const timezone_t sp,const time_t * timep,char * buf)17452fe8fb19SBen Gras ctime_rz(const timezone_t sp, const time_t * timep, char *buf)
17462fe8fb19SBen Gras {
17472fe8fb19SBen Gras 	struct tm	mytm, *rtm;
17482fe8fb19SBen Gras 
17492fe8fb19SBen Gras 	rtm = localtime_rz(sp, timep, &mytm);
17502fe8fb19SBen Gras 	if (rtm == NULL)
17512fe8fb19SBen Gras 		return NULL;
17522fe8fb19SBen Gras 	return asctime_r(rtm, buf);
17532fe8fb19SBen Gras }
1754af4345b0SDavid van Moolenbroek #endif /* !defined(__minix) || !defined(_LIBMINC) */
17552fe8fb19SBen Gras 
17562fe8fb19SBen Gras /*
17572fe8fb19SBen Gras ** Adapted from code provided by Robert Elz, who writes:
17582fe8fb19SBen Gras **	The "best" way to do mktime I think is based on an idea of Bob
17592fe8fb19SBen Gras **	Kridle's (so its said...) from a long time ago.
17602fe8fb19SBen Gras **	It does a binary search of the time_t space. Since time_t's are
17612fe8fb19SBen Gras **	just 32 bits, its a max of 32 iterations (even at 64 bits it
17622fe8fb19SBen Gras **	would still be very reasonable).
17632fe8fb19SBen Gras */
17642fe8fb19SBen Gras 
17652fe8fb19SBen Gras #ifndef WRONG
17662fe8fb19SBen Gras #define WRONG	((time_t)-1)
17672fe8fb19SBen Gras #endif /* !defined WRONG */
17682fe8fb19SBen Gras 
17692fe8fb19SBen Gras /*
1770*0a6a1f1dSLionel Sambuc ** Normalize logic courtesy Paul Eggert.
17712fe8fb19SBen Gras */
17722fe8fb19SBen Gras 
1773*0a6a1f1dSLionel Sambuc static bool
increment_overflow(int * ip,int j)1774*0a6a1f1dSLionel Sambuc increment_overflow(int *ip, int j)
17752fe8fb19SBen Gras {
1776*0a6a1f1dSLionel Sambuc 	int const	i = *ip;
17772fe8fb19SBen Gras 
1778f14fb602SLionel Sambuc 	/*
1779f14fb602SLionel Sambuc 	** If i >= 0 there can only be overflow if i + j > INT_MAX
1780f14fb602SLionel Sambuc 	** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1781f14fb602SLionel Sambuc 	** If i < 0 there can only be overflow if i + j < INT_MIN
1782f14fb602SLionel Sambuc 	** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1783f14fb602SLionel Sambuc 	*/
1784f14fb602SLionel Sambuc 	if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1785*0a6a1f1dSLionel Sambuc 		return true;
1786f14fb602SLionel Sambuc 	*ip += j;
1787*0a6a1f1dSLionel Sambuc 	return false;
17882fe8fb19SBen Gras }
17892fe8fb19SBen Gras 
1790*0a6a1f1dSLionel Sambuc static bool
increment_overflow32(int_fast32_t * const lp,int const m)179184d9c625SLionel Sambuc increment_overflow32(int_fast32_t *const lp, int const m)
17922fe8fb19SBen Gras {
1793*0a6a1f1dSLionel Sambuc 	int_fast32_t const l = *lp;
17942fe8fb19SBen Gras 
179584d9c625SLionel Sambuc 	if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1796*0a6a1f1dSLionel Sambuc 		return true;
1797f14fb602SLionel Sambuc 	*lp += m;
1798*0a6a1f1dSLionel Sambuc 	return false;
17992fe8fb19SBen Gras }
18002fe8fb19SBen Gras 
1801*0a6a1f1dSLionel Sambuc static bool
increment_overflow_time(time_t * tp,int_fast32_t j)1802*0a6a1f1dSLionel Sambuc increment_overflow_time(time_t *tp, int_fast32_t j)
1803*0a6a1f1dSLionel Sambuc {
1804*0a6a1f1dSLionel Sambuc 	/*
1805*0a6a1f1dSLionel Sambuc 	** This is like
1806*0a6a1f1dSLionel Sambuc 	** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
1807*0a6a1f1dSLionel Sambuc 	** except that it does the right thing even if *tp + j would overflow.
1808*0a6a1f1dSLionel Sambuc 	*/
1809*0a6a1f1dSLionel Sambuc 	if (! (j < 0
1810*0a6a1f1dSLionel Sambuc 	       ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
1811*0a6a1f1dSLionel Sambuc 	       : *tp <= time_t_max - j))
1812*0a6a1f1dSLionel Sambuc 		return true;
1813*0a6a1f1dSLionel Sambuc 	*tp += j;
1814*0a6a1f1dSLionel Sambuc 	return false;
1815*0a6a1f1dSLionel Sambuc }
1816*0a6a1f1dSLionel Sambuc 
1817*0a6a1f1dSLionel Sambuc static bool
normalize_overflow(int * const tensptr,int * const unitsptr,const int base)18182fe8fb19SBen Gras normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
18192fe8fb19SBen Gras {
18202fe8fb19SBen Gras 	int	tensdelta;
18212fe8fb19SBen Gras 
18222fe8fb19SBen Gras 	tensdelta = (*unitsptr >= 0) ?
18232fe8fb19SBen Gras 		(*unitsptr / base) :
18242fe8fb19SBen Gras 		(-1 - (-1 - *unitsptr) / base);
18252fe8fb19SBen Gras 	*unitsptr -= tensdelta * base;
18262fe8fb19SBen Gras 	return increment_overflow(tensptr, tensdelta);
18272fe8fb19SBen Gras }
18282fe8fb19SBen Gras 
1829*0a6a1f1dSLionel Sambuc static bool
normalize_overflow32(int_fast32_t * tensptr,int * unitsptr,int base)1830*0a6a1f1dSLionel Sambuc normalize_overflow32(int_fast32_t *tensptr, int *unitsptr, int base)
18312fe8fb19SBen Gras {
18322fe8fb19SBen Gras 	int	tensdelta;
18332fe8fb19SBen Gras 
18342fe8fb19SBen Gras 	tensdelta = (*unitsptr >= 0) ?
18352fe8fb19SBen Gras 		(*unitsptr / base) :
18362fe8fb19SBen Gras 		(-1 - (-1 - *unitsptr) / base);
18372fe8fb19SBen Gras 	*unitsptr -= tensdelta * base;
183884d9c625SLionel Sambuc 	return increment_overflow32(tensptr, tensdelta);
18392fe8fb19SBen Gras }
18402fe8fb19SBen Gras 
18412fe8fb19SBen Gras static int
tmcomp(const struct tm * const atmp,const struct tm * const btmp)1842*0a6a1f1dSLionel Sambuc tmcomp(const struct tm *const atmp,
1843*0a6a1f1dSLionel Sambuc        const struct tm *const btmp)
18442fe8fb19SBen Gras {
18452fe8fb19SBen Gras 	int	result;
18462fe8fb19SBen Gras 
184784d9c625SLionel Sambuc 	if (atmp->tm_year != btmp->tm_year)
184884d9c625SLionel Sambuc 		return atmp->tm_year < btmp->tm_year ? -1 : 1;
184984d9c625SLionel Sambuc 	if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
18502fe8fb19SBen Gras 		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
18512fe8fb19SBen Gras 		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
18522fe8fb19SBen Gras 		(result = (atmp->tm_min - btmp->tm_min)) == 0)
18532fe8fb19SBen Gras 			result = atmp->tm_sec - btmp->tm_sec;
18542fe8fb19SBen Gras 	return result;
18552fe8fb19SBen Gras }
18562fe8fb19SBen Gras 
18572fe8fb19SBen Gras static time_t
time2sub(struct tm * const tmp,struct tm * (* funcp)(struct state const *,time_t const *,int_fast32_t,struct tm *),struct state const * sp,const int_fast32_t offset,bool * okayp,bool do_norm_secs)1858*0a6a1f1dSLionel Sambuc time2sub(struct tm *const tmp,
1859*0a6a1f1dSLionel Sambuc 	 struct tm *(*funcp)(struct state const *, time_t const *,
1860*0a6a1f1dSLionel Sambuc 			     int_fast32_t, struct tm *),
1861*0a6a1f1dSLionel Sambuc 	 struct state const *sp,
1862*0a6a1f1dSLionel Sambuc  	 const int_fast32_t offset,
1863*0a6a1f1dSLionel Sambuc 	 bool *okayp,
1864*0a6a1f1dSLionel Sambuc 	 bool do_norm_secs)
18652fe8fb19SBen Gras {
18662fe8fb19SBen Gras 	int			dir;
18672fe8fb19SBen Gras 	int			i, j;
18682fe8fb19SBen Gras 	int			saved_seconds;
186984d9c625SLionel Sambuc 	int_fast32_t		li;
18702fe8fb19SBen Gras 	time_t			lo;
18712fe8fb19SBen Gras 	time_t			hi;
1872f14fb602SLionel Sambuc #ifdef NO_ERROR_IN_DST_GAP
1873f14fb602SLionel Sambuc 	time_t			ilo;
1874f14fb602SLionel Sambuc #endif
187584d9c625SLionel Sambuc 	int_fast32_t		y;
18762fe8fb19SBen Gras 	time_t			newt;
18772fe8fb19SBen Gras 	time_t			t;
18782fe8fb19SBen Gras 	struct tm		yourtm, mytm;
18792fe8fb19SBen Gras 
1880*0a6a1f1dSLionel Sambuc 	*okayp = false;
18812fe8fb19SBen Gras 	yourtm = *tmp;
1882f14fb602SLionel Sambuc #ifdef NO_ERROR_IN_DST_GAP
1883f14fb602SLionel Sambuc again:
1884f14fb602SLionel Sambuc #endif
18852fe8fb19SBen Gras 	if (do_norm_secs) {
18862fe8fb19SBen Gras 		if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
18872fe8fb19SBen Gras 		    SECSPERMIN))
1888*0a6a1f1dSLionel Sambuc 			goto out_of_range;
18892fe8fb19SBen Gras 	}
18902fe8fb19SBen Gras 	if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1891*0a6a1f1dSLionel Sambuc 		goto out_of_range;
18922fe8fb19SBen Gras 	if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1893*0a6a1f1dSLionel Sambuc 		goto out_of_range;
18942fe8fb19SBen Gras 	y = yourtm.tm_year;
189584d9c625SLionel Sambuc 	if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
1896*0a6a1f1dSLionel Sambuc 		goto out_of_range;
18972fe8fb19SBen Gras 	/*
18982fe8fb19SBen Gras 	** Turn y into an actual year number for now.
18992fe8fb19SBen Gras 	** It is converted back to an offset from TM_YEAR_BASE later.
19002fe8fb19SBen Gras 	*/
190184d9c625SLionel Sambuc 	if (increment_overflow32(&y, TM_YEAR_BASE))
1902*0a6a1f1dSLionel Sambuc 		goto out_of_range;
19032fe8fb19SBen Gras 	while (yourtm.tm_mday <= 0) {
190484d9c625SLionel Sambuc 		if (increment_overflow32(&y, -1))
1905*0a6a1f1dSLionel Sambuc 			goto out_of_range;
19062fe8fb19SBen Gras 		li = y + (1 < yourtm.tm_mon);
19072fe8fb19SBen Gras 		yourtm.tm_mday += year_lengths[isleap(li)];
19082fe8fb19SBen Gras 	}
19092fe8fb19SBen Gras 	while (yourtm.tm_mday > DAYSPERLYEAR) {
19102fe8fb19SBen Gras 		li = y + (1 < yourtm.tm_mon);
19112fe8fb19SBen Gras 		yourtm.tm_mday -= year_lengths[isleap(li)];
191284d9c625SLionel Sambuc 		if (increment_overflow32(&y, 1))
1913*0a6a1f1dSLionel Sambuc 			goto out_of_range;
19142fe8fb19SBen Gras 	}
19152fe8fb19SBen Gras 	for ( ; ; ) {
19162fe8fb19SBen Gras 		i = mon_lengths[isleap(y)][yourtm.tm_mon];
19172fe8fb19SBen Gras 		if (yourtm.tm_mday <= i)
19182fe8fb19SBen Gras 			break;
19192fe8fb19SBen Gras 		yourtm.tm_mday -= i;
19202fe8fb19SBen Gras 		if (++yourtm.tm_mon >= MONSPERYEAR) {
19212fe8fb19SBen Gras 			yourtm.tm_mon = 0;
192284d9c625SLionel Sambuc 			if (increment_overflow32(&y, 1))
1923*0a6a1f1dSLionel Sambuc 				goto out_of_range;
19242fe8fb19SBen Gras 		}
19252fe8fb19SBen Gras 	}
192684d9c625SLionel Sambuc 	if (increment_overflow32(&y, -TM_YEAR_BASE))
1927*0a6a1f1dSLionel Sambuc 		goto out_of_range;
1928*0a6a1f1dSLionel Sambuc 	if (! (INT_MIN <= y && y <= INT_MAX))
1929*0a6a1f1dSLionel Sambuc 		goto out_of_range;
1930f14fb602SLionel Sambuc 	yourtm.tm_year = (int)y;
19312fe8fb19SBen Gras 	if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
19322fe8fb19SBen Gras 		saved_seconds = 0;
19332fe8fb19SBen Gras 	else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
19342fe8fb19SBen Gras 		/*
19352fe8fb19SBen Gras 		** We can't set tm_sec to 0, because that might push the
19362fe8fb19SBen Gras 		** time below the minimum representable time.
19372fe8fb19SBen Gras 		** Set tm_sec to 59 instead.
19382fe8fb19SBen Gras 		** This assumes that the minimum representable time is
19392fe8fb19SBen Gras 		** not in the same minute that a leap second was deleted from,
19402fe8fb19SBen Gras 		** which is a safer assumption than using 58 would be.
19412fe8fb19SBen Gras 		*/
19422fe8fb19SBen Gras 		if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1943*0a6a1f1dSLionel Sambuc 			goto out_of_range;
19442fe8fb19SBen Gras 		saved_seconds = yourtm.tm_sec;
19452fe8fb19SBen Gras 		yourtm.tm_sec = SECSPERMIN - 1;
19462fe8fb19SBen Gras 	} else {
19472fe8fb19SBen Gras 		saved_seconds = yourtm.tm_sec;
19482fe8fb19SBen Gras 		yourtm.tm_sec = 0;
19492fe8fb19SBen Gras 	}
19502fe8fb19SBen Gras 	/*
19512fe8fb19SBen Gras 	** Do a binary search (this works whatever time_t's type is).
19522fe8fb19SBen Gras 	*/
1953*0a6a1f1dSLionel Sambuc 	lo = time_t_min;
1954*0a6a1f1dSLionel Sambuc 	hi = time_t_max;
1955f14fb602SLionel Sambuc #ifdef NO_ERROR_IN_DST_GAP
1956f14fb602SLionel Sambuc 	ilo = lo;
1957f14fb602SLionel Sambuc #endif
19582fe8fb19SBen Gras 	for ( ; ; ) {
19592fe8fb19SBen Gras 		t = lo / 2 + hi / 2;
19602fe8fb19SBen Gras 		if (t < lo)
19612fe8fb19SBen Gras 			t = lo;
19622fe8fb19SBen Gras 		else if (t > hi)
19632fe8fb19SBen Gras 			t = hi;
1964*0a6a1f1dSLionel Sambuc 		if (! funcp(sp, &t, offset, &mytm)) {
19652fe8fb19SBen Gras 			/*
19662fe8fb19SBen Gras 			** Assume that t is too extreme to be represented in
19672fe8fb19SBen Gras 			** a struct tm; arrange things so that it is less
19682fe8fb19SBen Gras 			** extreme on the next pass.
19692fe8fb19SBen Gras 			*/
19702fe8fb19SBen Gras 			dir = (t > 0) ? 1 : -1;
19712fe8fb19SBen Gras 		} else	dir = tmcomp(&mytm, &yourtm);
19722fe8fb19SBen Gras 		if (dir != 0) {
19732fe8fb19SBen Gras 			if (t == lo) {
197484d9c625SLionel Sambuc 				if (t == time_t_max)
1975*0a6a1f1dSLionel Sambuc 					goto out_of_range;
197684d9c625SLionel Sambuc 				++t;
19772fe8fb19SBen Gras 				++lo;
19782fe8fb19SBen Gras 			} else if (t == hi) {
197984d9c625SLionel Sambuc 				if (t == time_t_min)
1980*0a6a1f1dSLionel Sambuc 					goto out_of_range;
198184d9c625SLionel Sambuc 				--t;
19822fe8fb19SBen Gras 				--hi;
19832fe8fb19SBen Gras 			}
1984f14fb602SLionel Sambuc #ifdef NO_ERROR_IN_DST_GAP
1985f14fb602SLionel Sambuc 			if (ilo != lo && lo - 1 == hi && yourtm.tm_isdst < 0 &&
1986f14fb602SLionel Sambuc 			    do_norm_secs) {
1987f14fb602SLionel Sambuc 				for (i = sp->typecnt - 1; i >= 0; --i) {
1988f14fb602SLionel Sambuc 					for (j = sp->typecnt - 1; j >= 0; --j) {
1989f14fb602SLionel Sambuc 						time_t off;
1990f14fb602SLionel Sambuc 						if (sp->ttis[j].tt_isdst ==
1991f14fb602SLionel Sambuc 						    sp->ttis[i].tt_isdst)
1992f14fb602SLionel Sambuc 							continue;
1993f14fb602SLionel Sambuc 						off = sp->ttis[j].tt_gmtoff -
1994f14fb602SLionel Sambuc 						    sp->ttis[i].tt_gmtoff;
1995f14fb602SLionel Sambuc 						yourtm.tm_sec += off < 0 ?
1996f14fb602SLionel Sambuc 						    -off : off;
1997f14fb602SLionel Sambuc 						goto again;
1998f14fb602SLionel Sambuc 					}
1999f14fb602SLionel Sambuc 				}
2000f14fb602SLionel Sambuc 			}
2001f14fb602SLionel Sambuc #endif
20022fe8fb19SBen Gras 			if (lo > hi)
2003f14fb602SLionel Sambuc 				goto invalid;
20042fe8fb19SBen Gras 			if (dir > 0)
20052fe8fb19SBen Gras 				hi = t;
20062fe8fb19SBen Gras 			else	lo = t;
20072fe8fb19SBen Gras 			continue;
20082fe8fb19SBen Gras 		}
2009*0a6a1f1dSLionel Sambuc #if defined TM_GMTOFF && ! UNINIT_TRAP
2010*0a6a1f1dSLionel Sambuc 		if (mytm.TM_GMTOFF != yourtm.TM_GMTOFF
2011*0a6a1f1dSLionel Sambuc 		    && (yourtm.TM_GMTOFF < 0
2012*0a6a1f1dSLionel Sambuc 			? (-SECSPERDAY <= yourtm.TM_GMTOFF
2013*0a6a1f1dSLionel Sambuc 			   && (mytm.TM_GMTOFF <=
2014*0a6a1f1dSLionel Sambuc 			       (/*CONSTCOND*/SMALLEST (INT_FAST32_MAX, LONG_MAX)
2015*0a6a1f1dSLionel Sambuc 				+ yourtm.TM_GMTOFF)))
2016*0a6a1f1dSLionel Sambuc 			: (yourtm.TM_GMTOFF <= SECSPERDAY
2017*0a6a1f1dSLionel Sambuc 			   && ((/*CONSTCOND*/BIGGEST (INT_FAST32_MIN, LONG_MIN)
2018*0a6a1f1dSLionel Sambuc 				+ yourtm.TM_GMTOFF)
2019*0a6a1f1dSLionel Sambuc 			       <= mytm.TM_GMTOFF)))) {
2020*0a6a1f1dSLionel Sambuc 		  /* MYTM matches YOURTM except with the wrong UTC offset.
2021*0a6a1f1dSLionel Sambuc 		     YOURTM.TM_GMTOFF is plausible, so try it instead.
2022*0a6a1f1dSLionel Sambuc 		     It's OK if YOURTM.TM_GMTOFF contains uninitialized data,
2023*0a6a1f1dSLionel Sambuc 		     since the guess gets checked.  */
2024*0a6a1f1dSLionel Sambuc 		  time_t altt = t;
2025*0a6a1f1dSLionel Sambuc 		  int_fast32_t diff = (int_fast32_t)
2026*0a6a1f1dSLionel Sambuc 		      (mytm.TM_GMTOFF - yourtm.TM_GMTOFF);
2027*0a6a1f1dSLionel Sambuc 		  if (!increment_overflow_time(&altt, diff)) {
2028*0a6a1f1dSLionel Sambuc 		    struct tm alttm;
2029*0a6a1f1dSLionel Sambuc 		    if (! funcp(sp, &altt, offset, &alttm)
2030*0a6a1f1dSLionel Sambuc 			&& alttm.tm_isdst == mytm.tm_isdst
2031*0a6a1f1dSLionel Sambuc 			&& alttm.TM_GMTOFF == yourtm.TM_GMTOFF
2032*0a6a1f1dSLionel Sambuc 			&& tmcomp(&alttm, &yourtm)) {
2033*0a6a1f1dSLionel Sambuc 		      t = altt;
2034*0a6a1f1dSLionel Sambuc 		      mytm = alttm;
2035*0a6a1f1dSLionel Sambuc 		    }
2036*0a6a1f1dSLionel Sambuc 		  }
2037*0a6a1f1dSLionel Sambuc 		}
2038*0a6a1f1dSLionel Sambuc #endif
20392fe8fb19SBen Gras 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
20402fe8fb19SBen Gras 			break;
20412fe8fb19SBen Gras 		/*
20422fe8fb19SBen Gras 		** Right time, wrong type.
20432fe8fb19SBen Gras 		** Hunt for right time, right type.
20442fe8fb19SBen Gras 		** It's okay to guess wrong since the guess
20452fe8fb19SBen Gras 		** gets checked.
20462fe8fb19SBen Gras 		*/
20472fe8fb19SBen Gras 		if (sp == NULL)
2048f14fb602SLionel Sambuc 			goto invalid;
20492fe8fb19SBen Gras 		for (i = sp->typecnt - 1; i >= 0; --i) {
20502fe8fb19SBen Gras 			if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
20512fe8fb19SBen Gras 				continue;
20522fe8fb19SBen Gras 			for (j = sp->typecnt - 1; j >= 0; --j) {
20532fe8fb19SBen Gras 				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
20542fe8fb19SBen Gras 					continue;
2055f14fb602SLionel Sambuc 				newt = (time_t)(t + sp->ttis[j].tt_gmtoff -
2056f14fb602SLionel Sambuc 				    sp->ttis[i].tt_gmtoff);
2057*0a6a1f1dSLionel Sambuc 				if (! funcp(sp, &newt, offset, &mytm))
20582fe8fb19SBen Gras 					continue;
20592fe8fb19SBen Gras 				if (tmcomp(&mytm, &yourtm) != 0)
20602fe8fb19SBen Gras 					continue;
20612fe8fb19SBen Gras 				if (mytm.tm_isdst != yourtm.tm_isdst)
20622fe8fb19SBen Gras 					continue;
20632fe8fb19SBen Gras 				/*
20642fe8fb19SBen Gras 				** We have a match.
20652fe8fb19SBen Gras 				*/
20662fe8fb19SBen Gras 				t = newt;
20672fe8fb19SBen Gras 				goto label;
20682fe8fb19SBen Gras 			}
20692fe8fb19SBen Gras 		}
2070f14fb602SLionel Sambuc 		goto invalid;
20712fe8fb19SBen Gras 	}
20722fe8fb19SBen Gras label:
20732fe8fb19SBen Gras 	newt = t + saved_seconds;
20742fe8fb19SBen Gras 	if ((newt < t) != (saved_seconds < 0))
2075*0a6a1f1dSLionel Sambuc 		goto out_of_range;
20762fe8fb19SBen Gras 	t = newt;
2077*0a6a1f1dSLionel Sambuc 	if (funcp(sp, &t, offset, tmp)) {
2078*0a6a1f1dSLionel Sambuc 		*okayp = true;
20792fe8fb19SBen Gras 		return t;
2080f14fb602SLionel Sambuc 	}
2081*0a6a1f1dSLionel Sambuc out_of_range:
2082f14fb602SLionel Sambuc 	errno = EOVERFLOW;
2083f14fb602SLionel Sambuc 	return WRONG;
2084f14fb602SLionel Sambuc invalid:
2085f14fb602SLionel Sambuc 	errno = EINVAL;
20862fe8fb19SBen Gras 	return WRONG;
20872fe8fb19SBen Gras }
20882fe8fb19SBen Gras 
20892fe8fb19SBen Gras static time_t
time2(struct tm * const tmp,struct tm * (* funcp)(struct state const *,time_t const *,int_fast32_t,struct tm *),struct state const * sp,const int_fast32_t offset,bool * okayp)2090*0a6a1f1dSLionel Sambuc time2(struct tm * const	tmp,
2091*0a6a1f1dSLionel Sambuc       struct tm *(*funcp)(struct state const *, time_t const *,
2092*0a6a1f1dSLionel Sambuc 			  int_fast32_t, struct tm *),
2093*0a6a1f1dSLionel Sambuc       struct state const *sp,
2094*0a6a1f1dSLionel Sambuc       const int_fast32_t offset,
2095*0a6a1f1dSLionel Sambuc       bool *okayp)
20962fe8fb19SBen Gras {
20972fe8fb19SBen Gras 	time_t	t;
20982fe8fb19SBen Gras 
20992fe8fb19SBen Gras 	/*
21002fe8fb19SBen Gras 	** First try without normalization of seconds
21012fe8fb19SBen Gras 	** (in case tm_sec contains a value associated with a leap second).
21022fe8fb19SBen Gras 	** If that fails, try with normalization of seconds.
21032fe8fb19SBen Gras 	*/
2104*0a6a1f1dSLionel Sambuc 	t = time2sub(tmp, funcp, sp, offset, okayp, false);
2105*0a6a1f1dSLionel Sambuc 	return *okayp ? t : time2sub(tmp, funcp, sp, offset, okayp, true);
21062fe8fb19SBen Gras }
21072fe8fb19SBen Gras 
21082fe8fb19SBen Gras static time_t
time1(struct tm * const tmp,struct tm * (* funcp)(struct state const *,time_t const *,int_fast32_t,struct tm *),struct state const * sp,const int_fast32_t offset)2109*0a6a1f1dSLionel Sambuc time1(struct tm *const tmp,
2110*0a6a1f1dSLionel Sambuc       struct tm *(*funcp) (struct state const *, time_t const *,
2111*0a6a1f1dSLionel Sambuc 			   int_fast32_t, struct tm *),
2112*0a6a1f1dSLionel Sambuc       struct state const *sp,
211384d9c625SLionel Sambuc       const int_fast32_t offset)
21142fe8fb19SBen Gras {
21152fe8fb19SBen Gras 	time_t			t;
21162fe8fb19SBen Gras 	int			samei, otheri;
21172fe8fb19SBen Gras 	int			sameind, otherind;
21182fe8fb19SBen Gras 	int			i;
21192fe8fb19SBen Gras 	int			nseen;
2120*0a6a1f1dSLionel Sambuc 	int			save_errno;
2121*0a6a1f1dSLionel Sambuc 	char				seen[TZ_MAX_TYPES];
2122*0a6a1f1dSLionel Sambuc 	unsigned char			types[TZ_MAX_TYPES];
2123*0a6a1f1dSLionel Sambuc 	bool				okay;
21242fe8fb19SBen Gras 
2125f14fb602SLionel Sambuc 	if (tmp == NULL) {
2126f14fb602SLionel Sambuc 		errno = EINVAL;
2127f14fb602SLionel Sambuc 		return WRONG;
2128f14fb602SLionel Sambuc 	}
21292fe8fb19SBen Gras 	if (tmp->tm_isdst > 1)
21302fe8fb19SBen Gras 		tmp->tm_isdst = 1;
2131*0a6a1f1dSLionel Sambuc 	save_errno = errno;
2132*0a6a1f1dSLionel Sambuc 	t = time2(tmp, funcp, sp, offset, &okay);
2133*0a6a1f1dSLionel Sambuc 	if (okay) {
2134*0a6a1f1dSLionel Sambuc 		errno = save_errno;
2135*0a6a1f1dSLionel Sambuc 		return t;
2136*0a6a1f1dSLionel Sambuc 	}
2137*0a6a1f1dSLionel Sambuc 	if (tmp->tm_isdst < 0)
21382fe8fb19SBen Gras #ifdef PCTS
21392fe8fb19SBen Gras 		/*
2140*0a6a1f1dSLionel Sambuc 		** POSIX Conformance Test Suite code courtesy Grant Sullivan.
21412fe8fb19SBen Gras 		*/
21422fe8fb19SBen Gras 		tmp->tm_isdst = 0;	/* reset to std and try again */
2143*0a6a1f1dSLionel Sambuc #else
21442fe8fb19SBen Gras 		return t;
21452fe8fb19SBen Gras #endif /* !defined PCTS */
21462fe8fb19SBen Gras 	/*
21472fe8fb19SBen Gras 	** We're supposed to assume that somebody took a time of one type
21482fe8fb19SBen Gras 	** and did some math on it that yielded a "struct tm" that's bad.
21492fe8fb19SBen Gras 	** We try to divine the type they started from and adjust to the
21502fe8fb19SBen Gras 	** type they need.
21512fe8fb19SBen Gras 	*/
2152f14fb602SLionel Sambuc 	if (sp == NULL) {
2153f14fb602SLionel Sambuc 		errno = EINVAL;
21542fe8fb19SBen Gras 		return WRONG;
2155f14fb602SLionel Sambuc 	}
21562fe8fb19SBen Gras 	for (i = 0; i < sp->typecnt; ++i)
2157*0a6a1f1dSLionel Sambuc 		seen[i] = false;
21582fe8fb19SBen Gras 	nseen = 0;
21592fe8fb19SBen Gras 	for (i = sp->timecnt - 1; i >= 0; --i)
21602fe8fb19SBen Gras 		if (!seen[sp->types[i]]) {
2161*0a6a1f1dSLionel Sambuc 			seen[sp->types[i]] = true;
21622fe8fb19SBen Gras 			types[nseen++] = sp->types[i];
21632fe8fb19SBen Gras 		}
21642fe8fb19SBen Gras 	for (sameind = 0; sameind < nseen; ++sameind) {
21652fe8fb19SBen Gras 		samei = types[sameind];
21662fe8fb19SBen Gras 		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
21672fe8fb19SBen Gras 			continue;
21682fe8fb19SBen Gras 		for (otherind = 0; otherind < nseen; ++otherind) {
21692fe8fb19SBen Gras 			otheri = types[otherind];
21702fe8fb19SBen Gras 			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
21712fe8fb19SBen Gras 				continue;
21722fe8fb19SBen Gras 			tmp->tm_sec += (int)(sp->ttis[otheri].tt_gmtoff -
21732fe8fb19SBen Gras 					sp->ttis[samei].tt_gmtoff);
21742fe8fb19SBen Gras 			tmp->tm_isdst = !tmp->tm_isdst;
2175*0a6a1f1dSLionel Sambuc 			t = time2(tmp, funcp, sp, offset, &okay);
2176*0a6a1f1dSLionel Sambuc 			if (okay) {
2177*0a6a1f1dSLionel Sambuc 				errno = save_errno;
21782fe8fb19SBen Gras 				return t;
2179*0a6a1f1dSLionel Sambuc 			}
21802fe8fb19SBen Gras 			tmp->tm_sec -= (int)(sp->ttis[otheri].tt_gmtoff -
21812fe8fb19SBen Gras 					sp->ttis[samei].tt_gmtoff);
21822fe8fb19SBen Gras 			tmp->tm_isdst = !tmp->tm_isdst;
21832fe8fb19SBen Gras 		}
21842fe8fb19SBen Gras 	}
2185f14fb602SLionel Sambuc 	errno = EOVERFLOW;
21862fe8fb19SBen Gras 	return WRONG;
21872fe8fb19SBen Gras }
21882fe8fb19SBen Gras 
2189*0a6a1f1dSLionel Sambuc static time_t
mktime_tzname(timezone_t sp,struct tm * tmp,bool setname)2190*0a6a1f1dSLionel Sambuc mktime_tzname(timezone_t sp, struct tm *tmp, bool setname)
21912fe8fb19SBen Gras {
2192*0a6a1f1dSLionel Sambuc 	if (sp)
2193*0a6a1f1dSLionel Sambuc 		return time1(tmp, localsub, sp, setname);
2194*0a6a1f1dSLionel Sambuc 	else {
2195*0a6a1f1dSLionel Sambuc 		gmtcheck();
2196*0a6a1f1dSLionel Sambuc 		return time1(tmp, gmtsub, gmtptr, 0);
2197*0a6a1f1dSLionel Sambuc 	}
21982fe8fb19SBen Gras }
21992fe8fb19SBen Gras 
2200*0a6a1f1dSLionel Sambuc #if NETBSD_INSPIRED
2201*0a6a1f1dSLionel Sambuc 
22022fe8fb19SBen Gras time_t
mktime_z(timezone_t sp,struct tm * const tmp)2203*0a6a1f1dSLionel Sambuc mktime_z(timezone_t sp, struct tm *const tmp)
22042fe8fb19SBen Gras {
2205*0a6a1f1dSLionel Sambuc 	return mktime_tzname(sp, tmp, false);
2206*0a6a1f1dSLionel Sambuc }
2207*0a6a1f1dSLionel Sambuc 
2208*0a6a1f1dSLionel Sambuc #endif
2209*0a6a1f1dSLionel Sambuc 
2210*0a6a1f1dSLionel Sambuc time_t
mktime(struct tm * tmp)2211*0a6a1f1dSLionel Sambuc mktime(struct tm *tmp)
2212*0a6a1f1dSLionel Sambuc {
2213*0a6a1f1dSLionel Sambuc 	time_t t;
22142fe8fb19SBen Gras 
22152fe8fb19SBen Gras 	rwlock_wrlock(&lcl_lock);
22162fe8fb19SBen Gras 	tzset_unlocked();
2217*0a6a1f1dSLionel Sambuc 	t = mktime_tzname(lclptr, tmp, true);
22182fe8fb19SBen Gras 	rwlock_unlock(&lcl_lock);
2219*0a6a1f1dSLionel Sambuc 	return t;
22202fe8fb19SBen Gras }
22212fe8fb19SBen Gras 
22222fe8fb19SBen Gras #ifdef STD_INSPIRED
22232fe8fb19SBen Gras 
22242fe8fb19SBen Gras time_t
timelocal_z(const timezone_t sp,struct tm * const tmp)222584d9c625SLionel Sambuc timelocal_z(const timezone_t sp, struct tm *const tmp)
22262fe8fb19SBen Gras {
22272fe8fb19SBen Gras 	if (tmp != NULL)
22282fe8fb19SBen Gras 		tmp->tm_isdst = -1;	/* in case it wasn't initialized */
22292fe8fb19SBen Gras 	return mktime_z(sp, tmp);
22302fe8fb19SBen Gras }
22312fe8fb19SBen Gras 
22322fe8fb19SBen Gras time_t
timelocal(struct tm * tmp)2233*0a6a1f1dSLionel Sambuc timelocal(struct tm *tmp)
22342fe8fb19SBen Gras {
2235f14fb602SLionel Sambuc 	if (tmp != NULL)
22362fe8fb19SBen Gras 		tmp->tm_isdst = -1;	/* in case it wasn't initialized */
22372fe8fb19SBen Gras 	return mktime(tmp);
22382fe8fb19SBen Gras }
22392fe8fb19SBen Gras 
22402fe8fb19SBen Gras time_t
timegm(struct tm * tmp)2241*0a6a1f1dSLionel Sambuc timegm(struct tm *tmp)
22422fe8fb19SBen Gras {
22432fe8fb19SBen Gras 
2244*0a6a1f1dSLionel Sambuc 	return timeoff(tmp, 0);
22452fe8fb19SBen Gras }
22462fe8fb19SBen Gras 
22472fe8fb19SBen Gras time_t
timeoff(struct tm * tmp,long offset)2248*0a6a1f1dSLionel Sambuc timeoff(struct tm *tmp, long offset)
22492fe8fb19SBen Gras {
2250*0a6a1f1dSLionel Sambuc 	if (tmp)
22512fe8fb19SBen Gras 		tmp->tm_isdst = 0;
2252*0a6a1f1dSLionel Sambuc 	gmtcheck();
2253*0a6a1f1dSLionel Sambuc 	return time1(tmp, gmtsub, gmtptr, (int_fast32_t)offset);
22542fe8fb19SBen Gras }
22552fe8fb19SBen Gras 
22562fe8fb19SBen Gras #endif /* defined STD_INSPIRED */
22572fe8fb19SBen Gras 
22582fe8fb19SBen Gras /*
22592fe8fb19SBen Gras ** XXX--is the below the right way to conditionalize??
22602fe8fb19SBen Gras */
22612fe8fb19SBen Gras 
22622fe8fb19SBen Gras #ifdef STD_INSPIRED
22632fe8fb19SBen Gras 
22642fe8fb19SBen Gras /*
22652fe8fb19SBen Gras ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
22662fe8fb19SBen Gras ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
22672fe8fb19SBen Gras ** is not the case if we are accounting for leap seconds.
22682fe8fb19SBen Gras ** So, we provide the following conversion routines for use
22692fe8fb19SBen Gras ** when exchanging timestamps with POSIX conforming systems.
22702fe8fb19SBen Gras */
22712fe8fb19SBen Gras 
227284d9c625SLionel Sambuc static int_fast64_t
leapcorr(const timezone_t sp,time_t t)2273*0a6a1f1dSLionel Sambuc leapcorr(const timezone_t sp, time_t t)
22742fe8fb19SBen Gras {
2275*0a6a1f1dSLionel Sambuc 	struct lsinfo const * lp;
22762fe8fb19SBen Gras 	int		i;
22772fe8fb19SBen Gras 
22782fe8fb19SBen Gras 	i = sp->leapcnt;
22792fe8fb19SBen Gras 	while (--i >= 0) {
22802fe8fb19SBen Gras 		lp = &sp->lsis[i];
2281*0a6a1f1dSLionel Sambuc 		if (t >= lp->ls_trans)
22822fe8fb19SBen Gras 			return lp->ls_corr;
22832fe8fb19SBen Gras 	}
22842fe8fb19SBen Gras 	return 0;
22852fe8fb19SBen Gras }
22862fe8fb19SBen Gras 
2287*0a6a1f1dSLionel Sambuc NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
time2posix_z(timezone_t sp,time_t t)2288*0a6a1f1dSLionel Sambuc time2posix_z(timezone_t sp, time_t t)
22892fe8fb19SBen Gras {
2290*0a6a1f1dSLionel Sambuc 	return (time_t)(t - leapcorr(sp, t));
22912fe8fb19SBen Gras }
22922fe8fb19SBen Gras 
22932fe8fb19SBen Gras time_t
time2posix(time_t t)22942fe8fb19SBen Gras time2posix(time_t t)
22952fe8fb19SBen Gras {
22962fe8fb19SBen Gras 	rwlock_wrlock(&lcl_lock);
2297*0a6a1f1dSLionel Sambuc 	if (!lcl_is_set)
22982fe8fb19SBen Gras 		tzset_unlocked();
2299*0a6a1f1dSLionel Sambuc 	if (lclptr)
2300*0a6a1f1dSLionel Sambuc 		t = (time_t)(t - leapcorr(lclptr, t));
23012fe8fb19SBen Gras 	rwlock_unlock(&lcl_lock);
2302*0a6a1f1dSLionel Sambuc 	return t;
23032fe8fb19SBen Gras }
23042fe8fb19SBen Gras 
2305*0a6a1f1dSLionel Sambuc NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
posix2time_z(timezone_t sp,time_t t)2306*0a6a1f1dSLionel Sambuc posix2time_z(timezone_t sp, time_t t)
23072fe8fb19SBen Gras {
23082fe8fb19SBen Gras 	time_t	x;
23092fe8fb19SBen Gras 	time_t	y;
23102fe8fb19SBen Gras 
23112fe8fb19SBen Gras 	/*
23122fe8fb19SBen Gras 	** For a positive leap second hit, the result
23132fe8fb19SBen Gras 	** is not unique. For a negative leap second
23142fe8fb19SBen Gras 	** hit, the corresponding time doesn't exist,
23152fe8fb19SBen Gras 	** so we return an adjacent second.
23162fe8fb19SBen Gras 	*/
2317*0a6a1f1dSLionel Sambuc 	x = (time_t)(t + leapcorr(sp, t));
2318*0a6a1f1dSLionel Sambuc 	y = (time_t)(x - leapcorr(sp, x));
23192fe8fb19SBen Gras 	if (y < t) {
23202fe8fb19SBen Gras 		do {
23212fe8fb19SBen Gras 			x++;
2322*0a6a1f1dSLionel Sambuc 			y = (time_t)(x - leapcorr(sp, x));
23232fe8fb19SBen Gras 		} while (y < t);
2324*0a6a1f1dSLionel Sambuc 		x -= y != t;
23252fe8fb19SBen Gras 	} else if (y > t) {
23262fe8fb19SBen Gras 		do {
23272fe8fb19SBen Gras 			--x;
2328*0a6a1f1dSLionel Sambuc 			y = (time_t)(x - leapcorr(sp, x));
23292fe8fb19SBen Gras 		} while (y > t);
2330*0a6a1f1dSLionel Sambuc 		x += y != t;
23312fe8fb19SBen Gras 	}
23322fe8fb19SBen Gras 	return x;
23332fe8fb19SBen Gras }
23342fe8fb19SBen Gras 
23352fe8fb19SBen Gras time_t
posix2time(time_t t)23362fe8fb19SBen Gras posix2time(time_t t)
23372fe8fb19SBen Gras {
23382fe8fb19SBen Gras 	rwlock_wrlock(&lcl_lock);
2339*0a6a1f1dSLionel Sambuc 	if (!lcl_is_set)
23402fe8fb19SBen Gras 		tzset_unlocked();
2341*0a6a1f1dSLionel Sambuc 	if (lclptr)
2342*0a6a1f1dSLionel Sambuc 		t = posix2time_z(lclptr, t);
23432fe8fb19SBen Gras 	rwlock_unlock(&lcl_lock);
2344*0a6a1f1dSLionel Sambuc 	return t;
23452fe8fb19SBen Gras }
23462fe8fb19SBen Gras 
23472fe8fb19SBen Gras #endif /* defined STD_INSPIRED */
2348