Home | History | Annotate | Line # | Download | only in time
localtime.c revision 1.39.18.1
      1  1.39.18.1      yamt /*	$NetBSD: localtime.c,v 1.39.18.1 2008/06/04 02:04:34 yamt Exp $	*/
      2        1.7       jtc 
      3        1.7       jtc /*
      4        1.7       jtc ** This file is in the public domain, so clarified as of
      5       1.10       jtc ** 1996-06-05 by Arthur David Olson (arthur_david_olson (at) nih.gov).
      6        1.7       jtc */
      7        1.2       jtc 
      8       1.11  christos #include <sys/cdefs.h>
      9       1.24   msaitoh #if defined(LIBC_SCCS) && !defined(lint)
     10       1.11  christos #if 0
     11       1.35    kleink static char	elsieid[] = "@(#)localtime.c	7.78";
     12       1.11  christos #else
     13  1.39.18.1      yamt __RCSID("$NetBSD: localtime.c,v 1.39.18.1 2008/06/04 02:04:34 yamt Exp $");
     14       1.11  christos #endif
     15       1.24   msaitoh #endif /* LIBC_SCCS and not lint */
     16        1.1       jtc 
     17        1.1       jtc /*
     18        1.1       jtc ** Leap second handling from Bradley White (bww (at) k.gp.cs.cmu.edu).
     19        1.1       jtc ** POSIX-style TZ environment variable handling from Guy Harris
     20        1.1       jtc ** (guy (at) auspex.com).
     21        1.1       jtc */
     22        1.1       jtc 
     23        1.1       jtc /*LINTLIBRARY*/
     24        1.1       jtc 
     25       1.12       jtc #include "namespace.h"
     26        1.1       jtc #include "private.h"
     27        1.1       jtc #include "tzfile.h"
     28        1.1       jtc #include "fcntl.h"
     29       1.19    kleink #include "reentrant.h"
     30       1.12       jtc 
     31       1.12       jtc #ifdef __weak_alias
     32       1.23   mycroft __weak_alias(ctime_r,_ctime_r)
     33       1.25    kleink __weak_alias(daylight,_daylight)
     34       1.23   mycroft __weak_alias(gmtime_r,_gmtime_r)
     35       1.23   mycroft __weak_alias(localtime_r,_localtime_r)
     36       1.23   mycroft __weak_alias(offtime,_offtime)
     37       1.23   mycroft __weak_alias(posix2time,_posix2time)
     38       1.23   mycroft __weak_alias(time2posix,_time2posix)
     39       1.23   mycroft __weak_alias(timegm,_timegm)
     40       1.23   mycroft __weak_alias(timelocal,_timelocal)
     41       1.23   mycroft __weak_alias(timeoff,_timeoff)
     42       1.23   mycroft __weak_alias(tzname,_tzname)
     43       1.23   mycroft __weak_alias(tzset,_tzset)
     44       1.23   mycroft __weak_alias(tzsetwall,_tzsetwall)
     45       1.12       jtc #endif
     46        1.1       jtc 
     47        1.1       jtc /*
     48        1.1       jtc ** SunOS 4.1.1 headers lack O_BINARY.
     49        1.1       jtc */
     50        1.1       jtc 
     51        1.1       jtc #ifdef O_BINARY
     52        1.1       jtc #define OPEN_MODE	(O_RDONLY | O_BINARY)
     53        1.1       jtc #endif /* defined O_BINARY */
     54        1.1       jtc #ifndef O_BINARY
     55        1.1       jtc #define OPEN_MODE	O_RDONLY
     56        1.1       jtc #endif /* !defined O_BINARY */
     57        1.1       jtc 
     58        1.1       jtc #ifndef WILDABBR
     59        1.1       jtc /*
     60        1.1       jtc ** Someone might make incorrect use of a time zone abbreviation:
     61        1.1       jtc **	1.	They might reference tzname[0] before calling tzset (explicitly
     62        1.1       jtc **		or implicitly).
     63        1.1       jtc **	2.	They might reference tzname[1] before calling tzset (explicitly
     64        1.1       jtc **		or implicitly).
     65        1.1       jtc **	3.	They might reference tzname[1] after setting to a time zone
     66        1.1       jtc **		in which Daylight Saving Time is never observed.
     67        1.1       jtc **	4.	They might reference tzname[0] after setting to a time zone
     68        1.1       jtc **		in which Standard Time is never observed.
     69        1.1       jtc **	5.	They might reference tm.TM_ZONE after calling offtime.
     70        1.1       jtc ** What's best to do in the above cases is open to debate;
     71        1.1       jtc ** for now, we just set things up so that in any of the five cases
     72        1.1       jtc ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
     73        1.1       jtc ** string "tzname[0] used before set", and similarly for the other cases.
     74        1.1       jtc ** And another:  initialize tzname[0] to "ERA", with an explanation in the
     75        1.1       jtc ** manual page of what this "time zone abbreviation" means (doing this so
     76        1.1       jtc ** that tzname[0] has the "normal" length of three characters).
     77        1.1       jtc */
     78        1.1       jtc #define WILDABBR	"   "
     79        1.1       jtc #endif /* !defined WILDABBR */
     80        1.1       jtc 
     81       1.15   mycroft static const char	wildabbr[] = "WILDABBR";
     82        1.1       jtc 
     83        1.1       jtc static const char	gmt[] = "GMT";
     84        1.1       jtc 
     85       1.22    kleink /*
     86       1.22    kleink ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
     87       1.22    kleink ** We default to US rules as of 1999-08-17.
     88       1.22    kleink ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
     89       1.22    kleink ** implementation dependent; for historical reasons, US rules are a
     90       1.22    kleink ** common default.
     91       1.22    kleink */
     92       1.22    kleink #ifndef TZDEFRULESTRING
     93       1.22    kleink #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
     94       1.22    kleink #endif /* !defined TZDEFDST */
     95       1.22    kleink 
     96        1.1       jtc struct ttinfo {				/* time type information */
     97       1.14       jtc 	long		tt_gmtoff;	/* UTC offset in seconds */
     98        1.1       jtc 	int		tt_isdst;	/* used to set tm_isdst */
     99        1.1       jtc 	int		tt_abbrind;	/* abbreviation list index */
    100        1.1       jtc 	int		tt_ttisstd;	/* TRUE if transition is std time */
    101       1.14       jtc 	int		tt_ttisgmt;	/* TRUE if transition is UTC */
    102        1.1       jtc };
    103        1.1       jtc 
    104        1.1       jtc struct lsinfo {				/* leap second information */
    105        1.1       jtc 	time_t		ls_trans;	/* transition time */
    106        1.1       jtc 	long		ls_corr;	/* correction to apply */
    107        1.1       jtc };
    108        1.1       jtc 
    109        1.1       jtc #define BIGGEST(a, b)	(((a) > (b)) ? (a) : (b))
    110        1.1       jtc 
    111        1.1       jtc #ifdef TZNAME_MAX
    112        1.1       jtc #define MY_TZNAME_MAX	TZNAME_MAX
    113        1.1       jtc #endif /* defined TZNAME_MAX */
    114        1.1       jtc #ifndef TZNAME_MAX
    115        1.1       jtc #define MY_TZNAME_MAX	255
    116        1.1       jtc #endif /* !defined TZNAME_MAX */
    117        1.1       jtc 
    118        1.1       jtc struct state {
    119        1.1       jtc 	int		leapcnt;
    120        1.1       jtc 	int		timecnt;
    121        1.1       jtc 	int		typecnt;
    122        1.1       jtc 	int		charcnt;
    123        1.1       jtc 	time_t		ats[TZ_MAX_TIMES];
    124        1.1       jtc 	unsigned char	types[TZ_MAX_TIMES];
    125        1.1       jtc 	struct ttinfo	ttis[TZ_MAX_TYPES];
    126       1.37  christos 	char		chars[/*CONSTCOND*/BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
    127        1.1       jtc 				(2 * (MY_TZNAME_MAX + 1)))];
    128        1.1       jtc 	struct lsinfo	lsis[TZ_MAX_LEAPS];
    129        1.1       jtc };
    130        1.1       jtc 
    131        1.1       jtc struct rule {
    132        1.1       jtc 	int		r_type;		/* type of rule--see below */
    133        1.1       jtc 	int		r_day;		/* day number of rule */
    134        1.1       jtc 	int		r_week;		/* week number of rule */
    135        1.1       jtc 	int		r_mon;		/* month number of rule */
    136        1.1       jtc 	long		r_time;		/* transition time of rule */
    137        1.1       jtc };
    138        1.1       jtc 
    139        1.1       jtc #define JULIAN_DAY		0	/* Jn - Julian day */
    140        1.1       jtc #define DAY_OF_YEAR		1	/* n - day of year */
    141        1.1       jtc #define MONTH_NTH_DAY_OF_WEEK	2	/* Mm.n.d - month, week, day of week */
    142        1.1       jtc 
    143        1.1       jtc /*
    144        1.1       jtc ** Prototypes for static functions.
    145        1.1       jtc */
    146        1.1       jtc 
    147        1.1       jtc static long		detzcode P((const char * codep));
    148        1.1       jtc static const char *	getzname P((const char * strp));
    149        1.1       jtc static const char *	getnum P((const char * strp, int * nump, int min,
    150        1.1       jtc 				int max));
    151        1.1       jtc static const char *	getsecs P((const char * strp, long * secsp));
    152        1.1       jtc static const char *	getoffset P((const char * strp, long * offsetp));
    153        1.1       jtc static const char *	getrule P((const char * strp, struct rule * rulep));
    154        1.1       jtc static void		gmtload P((struct state * sp));
    155        1.1       jtc static void		gmtsub P((const time_t * timep, long offset,
    156        1.1       jtc 				struct tm * tmp));
    157        1.1       jtc static void		localsub P((const time_t * timep, long offset,
    158        1.1       jtc 				struct tm * tmp));
    159        1.1       jtc static int		increment_overflow P((int * number, int delta));
    160        1.1       jtc static int		normalize_overflow P((int * tensptr, int * unitsptr,
    161        1.1       jtc 				int base));
    162        1.1       jtc static void		settzname P((void));
    163        1.1       jtc static time_t		time1 P((struct tm * tmp,
    164        1.1       jtc 				void(*funcp) P((const time_t *,
    165        1.1       jtc 				long, struct tm *)),
    166        1.1       jtc 				long offset));
    167        1.1       jtc static time_t		time2 P((struct tm *tmp,
    168        1.1       jtc 				void(*funcp) P((const time_t *,
    169        1.1       jtc 				long, struct tm*)),
    170        1.1       jtc 				long offset, int * okayp));
    171       1.13       jtc static time_t		time2sub P((struct tm *tmp,
    172       1.13       jtc 				void(*funcp) P((const time_t *,
    173       1.13       jtc 				long, struct tm*)),
    174       1.13       jtc 				long offset, int * okayp, int do_norm_secs));
    175        1.1       jtc static void		timesub P((const time_t * timep, long offset,
    176        1.1       jtc 				const struct state * sp, struct tm * tmp));
    177        1.1       jtc static int		tmcomp P((const struct tm * atmp,
    178        1.1       jtc 				const struct tm * btmp));
    179        1.1       jtc static time_t		transtime P((time_t janfirst, int year,
    180        1.1       jtc 				const struct rule * rulep, long offset));
    181        1.1       jtc static int		tzload P((const char * name, struct state * sp));
    182        1.1       jtc static int		tzparse P((const char * name, struct state * sp,
    183        1.1       jtc 				int lastditch));
    184       1.19    kleink static void		tzset_unlocked P((void));
    185       1.19    kleink static void		tzsetwall_unlocked P((void));
    186       1.11  christos #ifdef STD_INSPIRED
    187       1.11  christos static long		leapcorr P((time_t * timep));
    188       1.11  christos #endif
    189        1.1       jtc 
    190        1.1       jtc #ifdef ALL_STATE
    191        1.1       jtc static struct state *	lclptr;
    192        1.1       jtc static struct state *	gmtptr;
    193        1.1       jtc #endif /* defined ALL_STATE */
    194        1.1       jtc 
    195        1.1       jtc #ifndef ALL_STATE
    196        1.1       jtc static struct state	lclmem;
    197        1.1       jtc static struct state	gmtmem;
    198        1.1       jtc #define lclptr		(&lclmem)
    199        1.1       jtc #define gmtptr		(&gmtmem)
    200        1.1       jtc #endif /* State Farm */
    201        1.1       jtc 
    202        1.1       jtc #ifndef TZ_STRLEN_MAX
    203        1.1       jtc #define TZ_STRLEN_MAX 255
    204        1.1       jtc #endif /* !defined TZ_STRLEN_MAX */
    205        1.1       jtc 
    206        1.1       jtc static char		lcl_TZname[TZ_STRLEN_MAX + 1];
    207        1.1       jtc static int		lcl_is_set;
    208        1.1       jtc static int		gmt_is_set;
    209        1.1       jtc 
    210       1.16   mycroft __aconst char *		tzname[2] = {
    211       1.37  christos 	(__aconst char *)__UNCONST(wildabbr),
    212       1.37  christos 	(__aconst char *)__UNCONST(wildabbr)
    213        1.1       jtc };
    214        1.1       jtc 
    215       1.33  christos #ifdef _REENTRANT
    216       1.19    kleink static rwlock_t lcl_lock = RWLOCK_INITIALIZER;
    217       1.19    kleink #endif
    218       1.19    kleink 
    219        1.1       jtc /*
    220        1.1       jtc ** Section 4.12.3 of X3.159-1989 requires that
    221        1.1       jtc **	Except for the strftime function, these functions [asctime,
    222        1.1       jtc **	ctime, gmtime, localtime] return values in one of two static
    223        1.1       jtc **	objects: a broken-down time structure and an array of char.
    224        1.1       jtc ** Thanks to Paul Eggert (eggert (at) twinsun.com) for noting this.
    225        1.1       jtc */
    226        1.1       jtc 
    227        1.1       jtc static struct tm	tm;
    228        1.1       jtc 
    229        1.1       jtc #ifdef USG_COMPAT
    230       1.26    kleink long int		timezone = 0;
    231        1.1       jtc int			daylight = 0;
    232        1.1       jtc #endif /* defined USG_COMPAT */
    233        1.1       jtc 
    234        1.1       jtc #ifdef ALTZONE
    235        1.1       jtc time_t			altzone = 0;
    236        1.1       jtc #endif /* defined ALTZONE */
    237        1.1       jtc 
    238        1.1       jtc static long
    239        1.1       jtc detzcode(codep)
    240        1.1       jtc const char * const	codep;
    241        1.1       jtc {
    242        1.1       jtc 	register long	result;
    243        1.1       jtc 
    244        1.4       jtc 	/*
    245        1.4       jtc         ** The first character must be sign extended on systems with >32bit
    246        1.4       jtc         ** longs.  This was solved differently in the master tzcode sources
    247        1.4       jtc         ** (the fix first appeared in tzcode95c.tar.gz).  But I believe
    248        1.4       jtc 	** that this implementation is superior.
    249        1.4       jtc         */
    250        1.4       jtc 
    251        1.4       jtc #define SIGN_EXTEND_CHAR(x)	((signed char) x)
    252        1.4       jtc 
    253        1.4       jtc 	result = (SIGN_EXTEND_CHAR(codep[0]) << 24) \
    254        1.3       jtc 	       | (codep[1] & 0xff) << 16 \
    255        1.3       jtc 	       | (codep[2] & 0xff) << 8
    256        1.3       jtc 	       | (codep[3] & 0xff);
    257        1.1       jtc 	return result;
    258        1.1       jtc }
    259        1.1       jtc 
    260        1.1       jtc static void
    261        1.1       jtc settzname P((void))
    262        1.1       jtc {
    263        1.5       jtc 	register struct state * const	sp = lclptr;
    264        1.5       jtc 	register int			i;
    265        1.1       jtc 
    266       1.37  christos 	tzname[0] = (__aconst char *)__UNCONST(wildabbr);
    267       1.37  christos 	tzname[1] = (__aconst char *)__UNCONST(wildabbr);
    268        1.1       jtc #ifdef USG_COMPAT
    269        1.1       jtc 	daylight = 0;
    270        1.1       jtc 	timezone = 0;
    271        1.1       jtc #endif /* defined USG_COMPAT */
    272        1.1       jtc #ifdef ALTZONE
    273        1.1       jtc 	altzone = 0;
    274        1.1       jtc #endif /* defined ALTZONE */
    275        1.1       jtc #ifdef ALL_STATE
    276        1.1       jtc 	if (sp == NULL) {
    277       1.37  christos 		tzname[0] = tzname[1] = (__aconst char *)__UNCONST(gmt);
    278        1.1       jtc 		return;
    279        1.1       jtc 	}
    280        1.1       jtc #endif /* defined ALL_STATE */
    281        1.1       jtc 	for (i = 0; i < sp->typecnt; ++i) {
    282        1.1       jtc 		register const struct ttinfo * const	ttisp = &sp->ttis[i];
    283        1.1       jtc 
    284        1.1       jtc 		tzname[ttisp->tt_isdst] =
    285        1.1       jtc 			&sp->chars[ttisp->tt_abbrind];
    286        1.1       jtc 	}
    287        1.1       jtc 	/*
    288        1.1       jtc 	** And to get the latest zone names into tzname. . .
    289        1.1       jtc 	*/
    290        1.1       jtc 	for (i = 0; i < sp->timecnt; ++i) {
    291        1.1       jtc 		register const struct ttinfo * const	ttisp =
    292        1.1       jtc 							&sp->ttis[
    293        1.1       jtc 								sp->types[i]];
    294        1.1       jtc 
    295        1.1       jtc 		tzname[ttisp->tt_isdst] =
    296        1.1       jtc 			&sp->chars[ttisp->tt_abbrind];
    297  1.39.18.1      yamt #ifdef USG_COMPAT
    298  1.39.18.1      yamt 		if (ttisp->tt_isdst)
    299  1.39.18.1      yamt 			daylight = 1;
    300  1.39.18.1      yamt 		if (i == 0 || !ttisp->tt_isdst)
    301  1.39.18.1      yamt 			timezone = -(ttisp->tt_gmtoff);
    302  1.39.18.1      yamt #endif /* defined USG_COMPAT */
    303  1.39.18.1      yamt #ifdef ALTZONE
    304  1.39.18.1      yamt 		if (i == 0 || ttisp->tt_isdst)
    305  1.39.18.1      yamt 			altzone = -(ttisp->tt_gmtoff);
    306  1.39.18.1      yamt #endif /* defined ALTZONE */
    307        1.1       jtc 	}
    308        1.1       jtc }
    309        1.1       jtc 
    310        1.1       jtc static int
    311        1.1       jtc tzload(name, sp)
    312        1.1       jtc register const char *		name;
    313        1.1       jtc register struct state * const	sp;
    314        1.1       jtc {
    315        1.1       jtc 	register const char *	p;
    316        1.1       jtc 	register int		i;
    317        1.1       jtc 	register int		fid;
    318        1.1       jtc 
    319        1.1       jtc 	if (name == NULL && (name = TZDEFAULT) == NULL)
    320        1.1       jtc 		return -1;
    321        1.9       mrg 
    322        1.1       jtc 	{
    323        1.1       jtc 		register int	doaccess;
    324        1.1       jtc 		/*
    325        1.1       jtc 		** Section 4.9.1 of the C standard says that
    326        1.1       jtc 		** "FILENAME_MAX expands to an integral constant expression
    327       1.10       jtc 		** that is the size needed for an array of char large enough
    328        1.1       jtc 		** to hold the longest file name string that the implementation
    329        1.1       jtc 		** guarantees can be opened."
    330        1.1       jtc 		*/
    331        1.1       jtc 		char		fullname[FILENAME_MAX + 1];
    332        1.1       jtc 
    333        1.1       jtc 		if (name[0] == ':')
    334        1.1       jtc 			++name;
    335        1.1       jtc 		doaccess = name[0] == '/';
    336        1.1       jtc 		if (!doaccess) {
    337        1.1       jtc 			if ((p = TZDIR) == NULL)
    338        1.1       jtc 				return -1;
    339        1.1       jtc 			if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
    340        1.1       jtc 				return -1;
    341        1.8       mrg 			(void) strcpy(fullname, p);	/* XXX strcpy is safe */
    342        1.8       mrg 			(void) strcat(fullname, "/");	/* XXX strcat is safe */
    343        1.8       mrg 			(void) strcat(fullname, name);	/* XXX strcat is safe */
    344        1.1       jtc 			/*
    345        1.1       jtc 			** Set doaccess if '.' (as in "../") shows up in name.
    346        1.1       jtc 			*/
    347        1.1       jtc 			if (strchr(name, '.') != NULL)
    348        1.1       jtc 				doaccess = TRUE;
    349        1.1       jtc 			name = fullname;
    350        1.1       jtc 		}
    351        1.1       jtc 		if (doaccess && access(name, R_OK) != 0)
    352        1.1       jtc 			return -1;
    353        1.9       mrg 		/*
    354        1.9       mrg 		 * XXX potential security problem here if user of a set-id
    355        1.9       mrg 		 * program has set TZ (which is passed in as name) here,
    356        1.9       mrg 		 * and uses a race condition trick to defeat the access(2)
    357        1.9       mrg 		 * above.
    358        1.9       mrg 		 */
    359        1.1       jtc 		if ((fid = open(name, OPEN_MODE)) == -1)
    360        1.1       jtc 			return -1;
    361        1.1       jtc 	}
    362        1.1       jtc 	{
    363        1.1       jtc 		struct tzhead *	tzhp;
    364       1.14       jtc 		union {
    365       1.29    kleink 			struct tzhead	tzhead;
    366       1.29    kleink 			char		buf[sizeof *sp + sizeof *tzhp];
    367       1.14       jtc 		} u;
    368        1.1       jtc 		int		ttisstdcnt;
    369        1.1       jtc 		int		ttisgmtcnt;
    370        1.1       jtc 
    371       1.14       jtc 		i = read(fid, u.buf, sizeof u.buf);
    372        1.1       jtc 		if (close(fid) != 0)
    373        1.1       jtc 			return -1;
    374       1.34    kleink 		ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
    375       1.34    kleink 		ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
    376       1.14       jtc 		sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
    377       1.14       jtc 		sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
    378       1.14       jtc 		sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
    379       1.14       jtc 		sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
    380       1.14       jtc 		p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
    381        1.1       jtc 		if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
    382        1.1       jtc 			sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
    383        1.1       jtc 			sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
    384        1.1       jtc 			sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
    385        1.1       jtc 			(ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
    386        1.1       jtc 			(ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
    387        1.1       jtc 				return -1;
    388       1.14       jtc 		if (i - (p - u.buf) < sp->timecnt * 4 +	/* ats */
    389        1.1       jtc 			sp->timecnt +			/* types */
    390        1.1       jtc 			sp->typecnt * (4 + 2) +		/* ttinfos */
    391        1.1       jtc 			sp->charcnt +			/* chars */
    392        1.1       jtc 			sp->leapcnt * (4 + 4) +		/* lsinfos */
    393        1.1       jtc 			ttisstdcnt +			/* ttisstds */
    394        1.1       jtc 			ttisgmtcnt)			/* ttisgmts */
    395        1.1       jtc 				return -1;
    396        1.1       jtc 		for (i = 0; i < sp->timecnt; ++i) {
    397        1.1       jtc 			sp->ats[i] = detzcode(p);
    398        1.1       jtc 			p += 4;
    399        1.1       jtc 		}
    400        1.1       jtc 		for (i = 0; i < sp->timecnt; ++i) {
    401        1.1       jtc 			sp->types[i] = (unsigned char) *p++;
    402        1.1       jtc 			if (sp->types[i] >= sp->typecnt)
    403        1.1       jtc 				return -1;
    404        1.1       jtc 		}
    405        1.1       jtc 		for (i = 0; i < sp->typecnt; ++i) {
    406        1.1       jtc 			register struct ttinfo *	ttisp;
    407        1.1       jtc 
    408        1.1       jtc 			ttisp = &sp->ttis[i];
    409        1.1       jtc 			ttisp->tt_gmtoff = detzcode(p);
    410        1.1       jtc 			p += 4;
    411        1.1       jtc 			ttisp->tt_isdst = (unsigned char) *p++;
    412        1.1       jtc 			if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
    413        1.1       jtc 				return -1;
    414        1.1       jtc 			ttisp->tt_abbrind = (unsigned char) *p++;
    415        1.1       jtc 			if (ttisp->tt_abbrind < 0 ||
    416        1.1       jtc 				ttisp->tt_abbrind > sp->charcnt)
    417        1.1       jtc 					return -1;
    418        1.1       jtc 		}
    419        1.1       jtc 		for (i = 0; i < sp->charcnt; ++i)
    420        1.1       jtc 			sp->chars[i] = *p++;
    421        1.1       jtc 		sp->chars[i] = '\0';	/* ensure '\0' at end */
    422        1.1       jtc 		for (i = 0; i < sp->leapcnt; ++i) {
    423        1.1       jtc 			register struct lsinfo *	lsisp;
    424        1.1       jtc 
    425        1.1       jtc 			lsisp = &sp->lsis[i];
    426        1.1       jtc 			lsisp->ls_trans = detzcode(p);
    427        1.1       jtc 			p += 4;
    428        1.1       jtc 			lsisp->ls_corr = detzcode(p);
    429        1.1       jtc 			p += 4;
    430        1.1       jtc 		}
    431        1.1       jtc 		for (i = 0; i < sp->typecnt; ++i) {
    432        1.1       jtc 			register struct ttinfo *	ttisp;
    433        1.1       jtc 
    434        1.1       jtc 			ttisp = &sp->ttis[i];
    435        1.1       jtc 			if (ttisstdcnt == 0)
    436        1.1       jtc 				ttisp->tt_ttisstd = FALSE;
    437        1.1       jtc 			else {
    438        1.1       jtc 				ttisp->tt_ttisstd = *p++;
    439        1.1       jtc 				if (ttisp->tt_ttisstd != TRUE &&
    440        1.1       jtc 					ttisp->tt_ttisstd != FALSE)
    441        1.1       jtc 						return -1;
    442        1.1       jtc 			}
    443        1.1       jtc 		}
    444        1.1       jtc 		for (i = 0; i < sp->typecnt; ++i) {
    445        1.1       jtc 			register struct ttinfo *	ttisp;
    446        1.1       jtc 
    447        1.1       jtc 			ttisp = &sp->ttis[i];
    448        1.1       jtc 			if (ttisgmtcnt == 0)
    449        1.1       jtc 				ttisp->tt_ttisgmt = FALSE;
    450        1.1       jtc 			else {
    451        1.1       jtc 				ttisp->tt_ttisgmt = *p++;
    452        1.1       jtc 				if (ttisp->tt_ttisgmt != TRUE &&
    453        1.1       jtc 					ttisp->tt_ttisgmt != FALSE)
    454        1.1       jtc 						return -1;
    455        1.1       jtc 			}
    456        1.1       jtc 		}
    457        1.1       jtc 	}
    458        1.1       jtc 	return 0;
    459        1.1       jtc }
    460        1.1       jtc 
    461        1.1       jtc static const int	mon_lengths[2][MONSPERYEAR] = {
    462        1.1       jtc 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    463        1.1       jtc 	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
    464        1.1       jtc };
    465        1.1       jtc 
    466        1.1       jtc static const int	year_lengths[2] = {
    467        1.1       jtc 	DAYSPERNYEAR, DAYSPERLYEAR
    468        1.1       jtc };
    469        1.1       jtc 
    470        1.1       jtc /*
    471        1.1       jtc ** Given a pointer into a time zone string, scan until a character that is not
    472        1.1       jtc ** a valid character in a zone name is found.  Return a pointer to that
    473        1.1       jtc ** character.
    474        1.1       jtc */
    475        1.1       jtc 
    476        1.1       jtc static const char *
    477        1.1       jtc getzname(strp)
    478        1.1       jtc register const char *	strp;
    479        1.1       jtc {
    480        1.1       jtc 	register char	c;
    481        1.1       jtc 
    482        1.5       jtc 	while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
    483        1.1       jtc 		c != '+')
    484        1.1       jtc 			++strp;
    485        1.1       jtc 	return strp;
    486        1.1       jtc }
    487        1.1       jtc 
    488        1.1       jtc /*
    489        1.1       jtc ** Given a pointer into a time zone string, extract a number from that string.
    490        1.1       jtc ** Check that the number is within a specified range; if it is not, return
    491        1.1       jtc ** NULL.
    492        1.1       jtc ** Otherwise, return a pointer to the first character not part of the number.
    493        1.1       jtc */
    494        1.1       jtc 
    495        1.1       jtc static const char *
    496        1.1       jtc getnum(strp, nump, min, max)
    497        1.1       jtc register const char *	strp;
    498        1.1       jtc int * const		nump;
    499        1.1       jtc const int		min;
    500        1.1       jtc const int		max;
    501        1.1       jtc {
    502        1.1       jtc 	register char	c;
    503        1.1       jtc 	register int	num;
    504        1.1       jtc 
    505        1.5       jtc 	if (strp == NULL || !is_digit(c = *strp))
    506        1.1       jtc 		return NULL;
    507        1.1       jtc 	num = 0;
    508        1.5       jtc 	do {
    509        1.1       jtc 		num = num * 10 + (c - '0');
    510        1.1       jtc 		if (num > max)
    511        1.1       jtc 			return NULL;	/* illegal value */
    512        1.5       jtc 		c = *++strp;
    513        1.5       jtc 	} while (is_digit(c));
    514        1.1       jtc 	if (num < min)
    515        1.1       jtc 		return NULL;		/* illegal value */
    516        1.1       jtc 	*nump = num;
    517        1.1       jtc 	return strp;
    518        1.1       jtc }
    519        1.1       jtc 
    520        1.1       jtc /*
    521        1.1       jtc ** Given a pointer into a time zone string, extract a number of seconds,
    522        1.1       jtc ** in hh[:mm[:ss]] form, from the string.
    523        1.1       jtc ** If any error occurs, return NULL.
    524        1.1       jtc ** Otherwise, return a pointer to the first character not part of the number
    525        1.1       jtc ** of seconds.
    526        1.1       jtc */
    527        1.1       jtc 
    528        1.1       jtc static const char *
    529        1.1       jtc getsecs(strp, secsp)
    530        1.1       jtc register const char *	strp;
    531        1.1       jtc long * const		secsp;
    532        1.1       jtc {
    533        1.1       jtc 	int	num;
    534        1.1       jtc 
    535        1.1       jtc 	/*
    536        1.1       jtc 	** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
    537        1.1       jtc 	** "M10.4.6/26", which does not conform to Posix,
    538        1.1       jtc 	** but which specifies the equivalent of
    539        1.1       jtc 	** ``02:00 on the first Sunday on or after 23 Oct''.
    540        1.1       jtc 	*/
    541        1.1       jtc 	strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
    542        1.1       jtc 	if (strp == NULL)
    543        1.1       jtc 		return NULL;
    544        1.1       jtc 	*secsp = num * (long) SECSPERHOUR;
    545        1.1       jtc 	if (*strp == ':') {
    546        1.1       jtc 		++strp;
    547        1.1       jtc 		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
    548        1.1       jtc 		if (strp == NULL)
    549        1.1       jtc 			return NULL;
    550        1.1       jtc 		*secsp += num * SECSPERMIN;
    551        1.1       jtc 		if (*strp == ':') {
    552        1.1       jtc 			++strp;
    553        1.1       jtc 			/* `SECSPERMIN' allows for leap seconds.  */
    554        1.1       jtc 			strp = getnum(strp, &num, 0, SECSPERMIN);
    555        1.1       jtc 			if (strp == NULL)
    556        1.1       jtc 				return NULL;
    557        1.1       jtc 			*secsp += num;
    558        1.1       jtc 		}
    559        1.1       jtc 	}
    560        1.1       jtc 	return strp;
    561        1.1       jtc }
    562        1.1       jtc 
    563        1.1       jtc /*
    564        1.1       jtc ** Given a pointer into a time zone string, extract an offset, in
    565        1.1       jtc ** [+-]hh[:mm[:ss]] form, from the string.
    566        1.1       jtc ** If any error occurs, return NULL.
    567        1.1       jtc ** Otherwise, return a pointer to the first character not part of the time.
    568        1.1       jtc */
    569        1.1       jtc 
    570        1.1       jtc static const char *
    571        1.1       jtc getoffset(strp, offsetp)
    572        1.1       jtc register const char *	strp;
    573        1.1       jtc long * const		offsetp;
    574        1.1       jtc {
    575        1.5       jtc 	register int	neg = 0;
    576        1.1       jtc 
    577        1.1       jtc 	if (*strp == '-') {
    578        1.1       jtc 		neg = 1;
    579        1.1       jtc 		++strp;
    580        1.5       jtc 	} else if (*strp == '+')
    581        1.5       jtc 		++strp;
    582        1.1       jtc 	strp = getsecs(strp, offsetp);
    583        1.1       jtc 	if (strp == NULL)
    584        1.1       jtc 		return NULL;		/* illegal time */
    585        1.1       jtc 	if (neg)
    586        1.1       jtc 		*offsetp = -*offsetp;
    587        1.1       jtc 	return strp;
    588        1.1       jtc }
    589        1.1       jtc 
    590        1.1       jtc /*
    591        1.1       jtc ** Given a pointer into a time zone string, extract a rule in the form
    592        1.1       jtc ** date[/time].  See POSIX section 8 for the format of "date" and "time".
    593        1.1       jtc ** If a valid rule is not found, return NULL.
    594        1.1       jtc ** Otherwise, return a pointer to the first character not part of the rule.
    595        1.1       jtc */
    596        1.1       jtc 
    597        1.1       jtc static const char *
    598        1.1       jtc getrule(strp, rulep)
    599        1.1       jtc const char *			strp;
    600        1.1       jtc register struct rule * const	rulep;
    601        1.1       jtc {
    602        1.1       jtc 	if (*strp == 'J') {
    603        1.1       jtc 		/*
    604        1.1       jtc 		** Julian day.
    605        1.1       jtc 		*/
    606        1.1       jtc 		rulep->r_type = JULIAN_DAY;
    607        1.1       jtc 		++strp;
    608        1.1       jtc 		strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
    609        1.1       jtc 	} else if (*strp == 'M') {
    610        1.1       jtc 		/*
    611        1.1       jtc 		** Month, week, day.
    612        1.1       jtc 		*/
    613        1.1       jtc 		rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
    614        1.1       jtc 		++strp;
    615        1.1       jtc 		strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
    616        1.1       jtc 		if (strp == NULL)
    617        1.1       jtc 			return NULL;
    618        1.1       jtc 		if (*strp++ != '.')
    619        1.1       jtc 			return NULL;
    620        1.1       jtc 		strp = getnum(strp, &rulep->r_week, 1, 5);
    621        1.1       jtc 		if (strp == NULL)
    622        1.1       jtc 			return NULL;
    623        1.1       jtc 		if (*strp++ != '.')
    624        1.1       jtc 			return NULL;
    625        1.1       jtc 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
    626        1.5       jtc 	} else if (is_digit(*strp)) {
    627        1.1       jtc 		/*
    628        1.1       jtc 		** Day of year.
    629        1.1       jtc 		*/
    630        1.1       jtc 		rulep->r_type = DAY_OF_YEAR;
    631        1.1       jtc 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
    632        1.1       jtc 	} else	return NULL;		/* invalid format */
    633        1.1       jtc 	if (strp == NULL)
    634        1.1       jtc 		return NULL;
    635        1.1       jtc 	if (*strp == '/') {
    636        1.1       jtc 		/*
    637        1.1       jtc 		** Time specified.
    638        1.1       jtc 		*/
    639        1.1       jtc 		++strp;
    640        1.1       jtc 		strp = getsecs(strp, &rulep->r_time);
    641        1.1       jtc 	} else	rulep->r_time = 2 * SECSPERHOUR;	/* default = 2:00:00 */
    642        1.1       jtc 	return strp;
    643        1.1       jtc }
    644        1.1       jtc 
    645        1.1       jtc /*
    646       1.14       jtc ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
    647       1.14       jtc ** year, a rule, and the offset from UTC at the time that rule takes effect,
    648        1.1       jtc ** calculate the Epoch-relative time that rule takes effect.
    649        1.1       jtc */
    650        1.1       jtc 
    651        1.1       jtc static time_t
    652        1.1       jtc transtime(janfirst, year, rulep, offset)
    653        1.1       jtc const time_t				janfirst;
    654        1.1       jtc const int				year;
    655        1.1       jtc register const struct rule * const	rulep;
    656        1.1       jtc const long				offset;
    657        1.1       jtc {
    658        1.1       jtc 	register int	leapyear;
    659        1.1       jtc 	register time_t	value;
    660        1.1       jtc 	register int	i;
    661        1.1       jtc 	int		d, m1, yy0, yy1, yy2, dow;
    662        1.1       jtc 
    663        1.1       jtc 	INITIALIZE(value);
    664        1.1       jtc 	leapyear = isleap(year);
    665        1.1       jtc 	switch (rulep->r_type) {
    666        1.1       jtc 
    667        1.1       jtc 	case JULIAN_DAY:
    668        1.1       jtc 		/*
    669        1.1       jtc 		** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
    670        1.1       jtc 		** years.
    671        1.1       jtc 		** In non-leap years, or if the day number is 59 or less, just
    672        1.1       jtc 		** add SECSPERDAY times the day number-1 to the time of
    673        1.1       jtc 		** January 1, midnight, to get the day.
    674        1.1       jtc 		*/
    675        1.1       jtc 		value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
    676        1.1       jtc 		if (leapyear && rulep->r_day >= 60)
    677        1.1       jtc 			value += SECSPERDAY;
    678        1.1       jtc 		break;
    679        1.1       jtc 
    680        1.1       jtc 	case DAY_OF_YEAR:
    681        1.1       jtc 		/*
    682        1.1       jtc 		** n - day of year.
    683        1.1       jtc 		** Just add SECSPERDAY times the day number to the time of
    684        1.1       jtc 		** January 1, midnight, to get the day.
    685        1.1       jtc 		*/
    686        1.1       jtc 		value = janfirst + rulep->r_day * SECSPERDAY;
    687        1.1       jtc 		break;
    688        1.1       jtc 
    689        1.1       jtc 	case MONTH_NTH_DAY_OF_WEEK:
    690        1.1       jtc 		/*
    691        1.1       jtc 		** Mm.n.d - nth "dth day" of month m.
    692        1.1       jtc 		*/
    693        1.1       jtc 		value = janfirst;
    694        1.1       jtc 		for (i = 0; i < rulep->r_mon - 1; ++i)
    695        1.1       jtc 			value += mon_lengths[leapyear][i] * SECSPERDAY;
    696        1.1       jtc 
    697        1.1       jtc 		/*
    698        1.1       jtc 		** Use Zeller's Congruence to get day-of-week of first day of
    699        1.1       jtc 		** month.
    700        1.1       jtc 		*/
    701        1.1       jtc 		m1 = (rulep->r_mon + 9) % 12 + 1;
    702        1.1       jtc 		yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
    703        1.1       jtc 		yy1 = yy0 / 100;
    704        1.1       jtc 		yy2 = yy0 % 100;
    705        1.1       jtc 		dow = ((26 * m1 - 2) / 10 +
    706        1.1       jtc 			1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
    707        1.1       jtc 		if (dow < 0)
    708        1.1       jtc 			dow += DAYSPERWEEK;
    709        1.1       jtc 
    710        1.1       jtc 		/*
    711        1.1       jtc 		** "dow" is the day-of-week of the first day of the month.  Get
    712        1.1       jtc 		** the day-of-month (zero-origin) of the first "dow" day of the
    713        1.1       jtc 		** month.
    714        1.1       jtc 		*/
    715        1.1       jtc 		d = rulep->r_day - dow;
    716        1.1       jtc 		if (d < 0)
    717        1.1       jtc 			d += DAYSPERWEEK;
    718        1.1       jtc 		for (i = 1; i < rulep->r_week; ++i) {
    719        1.1       jtc 			if (d + DAYSPERWEEK >=
    720        1.1       jtc 				mon_lengths[leapyear][rulep->r_mon - 1])
    721        1.1       jtc 					break;
    722        1.1       jtc 			d += DAYSPERWEEK;
    723        1.1       jtc 		}
    724        1.1       jtc 
    725        1.1       jtc 		/*
    726        1.1       jtc 		** "d" is the day-of-month (zero-origin) of the day we want.
    727        1.1       jtc 		*/
    728        1.1       jtc 		value += d * SECSPERDAY;
    729        1.1       jtc 		break;
    730        1.1       jtc 	}
    731        1.1       jtc 
    732        1.1       jtc 	/*
    733       1.14       jtc 	** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
    734        1.1       jtc 	** question.  To get the Epoch-relative time of the specified local
    735        1.1       jtc 	** time on that day, add the transition time and the current offset
    736       1.14       jtc 	** from UTC.
    737        1.1       jtc 	*/
    738        1.1       jtc 	return value + rulep->r_time + offset;
    739        1.1       jtc }
    740        1.1       jtc 
    741        1.1       jtc /*
    742        1.1       jtc ** Given a POSIX section 8-style TZ string, fill in the rule tables as
    743        1.1       jtc ** appropriate.
    744        1.1       jtc */
    745        1.1       jtc 
    746        1.1       jtc static int
    747        1.1       jtc tzparse(name, sp, lastditch)
    748        1.1       jtc const char *			name;
    749        1.1       jtc register struct state * const	sp;
    750        1.1       jtc const int			lastditch;
    751        1.1       jtc {
    752        1.1       jtc 	const char *			stdname;
    753        1.1       jtc 	const char *			dstname;
    754        1.1       jtc 	size_t				stdlen;
    755        1.1       jtc 	size_t				dstlen;
    756        1.1       jtc 	long				stdoffset;
    757        1.1       jtc 	long				dstoffset;
    758        1.1       jtc 	register time_t *		atp;
    759        1.1       jtc 	register unsigned char *	typep;
    760        1.1       jtc 	register char *			cp;
    761        1.1       jtc 	register int			load_result;
    762        1.1       jtc 
    763        1.1       jtc 	INITIALIZE(dstname);
    764        1.1       jtc 	stdname = name;
    765        1.1       jtc 	if (lastditch) {
    766        1.1       jtc 		stdlen = strlen(name);	/* length of standard zone name */
    767        1.1       jtc 		name += stdlen;
    768        1.1       jtc 		if (stdlen >= sizeof sp->chars)
    769        1.1       jtc 			stdlen = (sizeof sp->chars) - 1;
    770       1.10       jtc 		stdoffset = 0;
    771        1.1       jtc 	} else {
    772        1.1       jtc 		name = getzname(name);
    773        1.1       jtc 		stdlen = name - stdname;
    774        1.1       jtc 		if (stdlen < 3)
    775        1.1       jtc 			return -1;
    776       1.10       jtc 		if (*name == '\0')
    777       1.10       jtc 			return -1;
    778        1.1       jtc 		name = getoffset(name, &stdoffset);
    779        1.1       jtc 		if (name == NULL)
    780        1.1       jtc 			return -1;
    781        1.1       jtc 	}
    782        1.1       jtc 	load_result = tzload(TZDEFRULES, sp);
    783        1.1       jtc 	if (load_result != 0)
    784        1.1       jtc 		sp->leapcnt = 0;		/* so, we're off a little */
    785        1.1       jtc 	if (*name != '\0') {
    786        1.1       jtc 		dstname = name;
    787        1.1       jtc 		name = getzname(name);
    788        1.1       jtc 		dstlen = name - dstname;	/* length of DST zone name */
    789        1.1       jtc 		if (dstlen < 3)
    790        1.1       jtc 			return -1;
    791        1.1       jtc 		if (*name != '\0' && *name != ',' && *name != ';') {
    792        1.1       jtc 			name = getoffset(name, &dstoffset);
    793        1.1       jtc 			if (name == NULL)
    794        1.1       jtc 				return -1;
    795        1.1       jtc 		} else	dstoffset = stdoffset - SECSPERHOUR;
    796       1.22    kleink 		if (*name == '\0' && load_result != 0)
    797       1.22    kleink 			name = TZDEFRULESTRING;
    798        1.1       jtc 		if (*name == ',' || *name == ';') {
    799        1.1       jtc 			struct rule	start;
    800        1.1       jtc 			struct rule	end;
    801        1.1       jtc 			register int	year;
    802        1.1       jtc 			register time_t	janfirst;
    803        1.1       jtc 			time_t		starttime;
    804        1.1       jtc 			time_t		endtime;
    805        1.1       jtc 
    806        1.1       jtc 			++name;
    807        1.1       jtc 			if ((name = getrule(name, &start)) == NULL)
    808        1.1       jtc 				return -1;
    809        1.1       jtc 			if (*name++ != ',')
    810        1.1       jtc 				return -1;
    811        1.1       jtc 			if ((name = getrule(name, &end)) == NULL)
    812        1.1       jtc 				return -1;
    813        1.1       jtc 			if (*name != '\0')
    814        1.1       jtc 				return -1;
    815        1.1       jtc 			sp->typecnt = 2;	/* standard time and DST */
    816        1.1       jtc 			/*
    817        1.1       jtc 			** Two transitions per year, from EPOCH_YEAR to 2037.
    818        1.1       jtc 			*/
    819        1.1       jtc 			sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
    820        1.1       jtc 			if (sp->timecnt > TZ_MAX_TIMES)
    821        1.1       jtc 				return -1;
    822        1.1       jtc 			sp->ttis[0].tt_gmtoff = -dstoffset;
    823        1.1       jtc 			sp->ttis[0].tt_isdst = 1;
    824        1.1       jtc 			sp->ttis[0].tt_abbrind = stdlen + 1;
    825        1.1       jtc 			sp->ttis[1].tt_gmtoff = -stdoffset;
    826        1.1       jtc 			sp->ttis[1].tt_isdst = 0;
    827        1.1       jtc 			sp->ttis[1].tt_abbrind = 0;
    828        1.1       jtc 			atp = sp->ats;
    829        1.1       jtc 			typep = sp->types;
    830        1.1       jtc 			janfirst = 0;
    831        1.1       jtc 			for (year = EPOCH_YEAR; year <= 2037; ++year) {
    832        1.1       jtc 				starttime = transtime(janfirst, year, &start,
    833        1.1       jtc 					stdoffset);
    834        1.1       jtc 				endtime = transtime(janfirst, year, &end,
    835        1.1       jtc 					dstoffset);
    836        1.1       jtc 				if (starttime > endtime) {
    837        1.1       jtc 					*atp++ = endtime;
    838        1.1       jtc 					*typep++ = 1;	/* DST ends */
    839        1.1       jtc 					*atp++ = starttime;
    840        1.1       jtc 					*typep++ = 0;	/* DST begins */
    841        1.1       jtc 				} else {
    842        1.1       jtc 					*atp++ = starttime;
    843        1.1       jtc 					*typep++ = 0;	/* DST begins */
    844        1.1       jtc 					*atp++ = endtime;
    845        1.1       jtc 					*typep++ = 1;	/* DST ends */
    846        1.1       jtc 				}
    847        1.1       jtc 				janfirst += year_lengths[isleap(year)] *
    848        1.1       jtc 					SECSPERDAY;
    849        1.1       jtc 			}
    850        1.1       jtc 		} else {
    851        1.1       jtc 			register long	theirstdoffset;
    852        1.1       jtc 			register long	theiroffset;
    853        1.1       jtc 			register int	i;
    854        1.1       jtc 			register int	j;
    855        1.1       jtc 
    856        1.1       jtc 			if (*name != '\0')
    857        1.1       jtc 				return -1;
    858        1.1       jtc 			/*
    859       1.39  christos 			** Initial values of theirstdoffset
    860        1.1       jtc 			*/
    861        1.1       jtc 			theirstdoffset = 0;
    862        1.1       jtc 			for (i = 0; i < sp->timecnt; ++i) {
    863        1.1       jtc 				j = sp->types[i];
    864        1.1       jtc 				if (!sp->ttis[j].tt_isdst) {
    865        1.5       jtc 					theirstdoffset =
    866        1.5       jtc 						-sp->ttis[j].tt_gmtoff;
    867        1.1       jtc 					break;
    868        1.1       jtc 				}
    869        1.1       jtc 			}
    870        1.1       jtc 			/*
    871        1.1       jtc 			** Initially we're assumed to be in standard time.
    872        1.1       jtc 			*/
    873        1.1       jtc 			theiroffset = theirstdoffset;
    874        1.1       jtc 			/*
    875        1.1       jtc 			** Now juggle transition times and types
    876        1.1       jtc 			** tracking offsets as you do.
    877        1.1       jtc 			*/
    878        1.1       jtc 			for (i = 0; i < sp->timecnt; ++i) {
    879        1.1       jtc 				j = sp->types[i];
    880        1.1       jtc 				sp->types[i] = sp->ttis[j].tt_isdst;
    881        1.1       jtc 				if (sp->ttis[j].tt_ttisgmt) {
    882        1.1       jtc 					/* No adjustment to transition time */
    883        1.1       jtc 				} else {
    884        1.1       jtc 					/*
    885        1.1       jtc 					** If summer time is in effect, and the
    886        1.1       jtc 					** transition time was not specified as
    887        1.1       jtc 					** standard time, add the summer time
    888        1.1       jtc 					** offset to the transition time;
    889        1.1       jtc 					** otherwise, add the standard time
    890        1.1       jtc 					** offset to the transition time.
    891        1.1       jtc 					*/
    892        1.1       jtc 					/*
    893        1.1       jtc 					** Transitions from DST to DDST
    894        1.1       jtc 					** will effectively disappear since
    895        1.1       jtc 					** POSIX provides for only one DST
    896        1.1       jtc 					** offset.
    897        1.1       jtc 					*/
    898       1.38  christos 					sp->ats[i] += stdoffset -
    899       1.38  christos 					    theirstdoffset;
    900        1.1       jtc 				}
    901        1.1       jtc 				theiroffset = -sp->ttis[j].tt_gmtoff;
    902       1.39  christos 				if (!sp->ttis[j].tt_isdst)
    903       1.39  christos 					theirstdoffset = theiroffset;
    904        1.1       jtc 			}
    905        1.1       jtc 			/*
    906        1.1       jtc 			** Finally, fill in ttis.
    907        1.1       jtc 			** ttisstd and ttisgmt need not be handled.
    908        1.1       jtc 			*/
    909        1.1       jtc 			sp->ttis[0].tt_gmtoff = -stdoffset;
    910        1.1       jtc 			sp->ttis[0].tt_isdst = FALSE;
    911        1.1       jtc 			sp->ttis[0].tt_abbrind = 0;
    912        1.1       jtc 			sp->ttis[1].tt_gmtoff = -dstoffset;
    913        1.1       jtc 			sp->ttis[1].tt_isdst = TRUE;
    914        1.1       jtc 			sp->ttis[1].tt_abbrind = stdlen + 1;
    915        1.7       jtc 			sp->typecnt = 2;
    916        1.1       jtc 		}
    917        1.1       jtc 	} else {
    918        1.1       jtc 		dstlen = 0;
    919        1.1       jtc 		sp->typecnt = 1;		/* only standard time */
    920        1.1       jtc 		sp->timecnt = 0;
    921        1.1       jtc 		sp->ttis[0].tt_gmtoff = -stdoffset;
    922        1.1       jtc 		sp->ttis[0].tt_isdst = 0;
    923        1.1       jtc 		sp->ttis[0].tt_abbrind = 0;
    924        1.1       jtc 	}
    925        1.1       jtc 	sp->charcnt = stdlen + 1;
    926        1.1       jtc 	if (dstlen != 0)
    927        1.1       jtc 		sp->charcnt += dstlen + 1;
    928       1.10       jtc 	if ((size_t) sp->charcnt > sizeof sp->chars)
    929        1.1       jtc 		return -1;
    930        1.1       jtc 	cp = sp->chars;
    931        1.1       jtc 	(void) strncpy(cp, stdname, stdlen);
    932        1.1       jtc 	cp += stdlen;
    933        1.1       jtc 	*cp++ = '\0';
    934        1.1       jtc 	if (dstlen != 0) {
    935        1.1       jtc 		(void) strncpy(cp, dstname, dstlen);
    936        1.1       jtc 		*(cp + dstlen) = '\0';
    937        1.1       jtc 	}
    938        1.1       jtc 	return 0;
    939        1.1       jtc }
    940        1.1       jtc 
    941        1.1       jtc static void
    942        1.1       jtc gmtload(sp)
    943        1.1       jtc struct state * const	sp;
    944        1.1       jtc {
    945        1.1       jtc 	if (tzload(gmt, sp) != 0)
    946        1.1       jtc 		(void) tzparse(gmt, sp, TRUE);
    947        1.1       jtc }
    948        1.1       jtc 
    949       1.19    kleink static void
    950       1.19    kleink tzsetwall_unlocked P((void))
    951        1.1       jtc {
    952        1.1       jtc 	if (lcl_is_set < 0)
    953        1.1       jtc 		return;
    954        1.1       jtc 	lcl_is_set = -1;
    955        1.1       jtc 
    956        1.1       jtc #ifdef ALL_STATE
    957        1.1       jtc 	if (lclptr == NULL) {
    958        1.1       jtc 		lclptr = (struct state *) malloc(sizeof *lclptr);
    959        1.1       jtc 		if (lclptr == NULL) {
    960        1.1       jtc 			settzname();	/* all we can do */
    961        1.1       jtc 			return;
    962        1.1       jtc 		}
    963        1.1       jtc 	}
    964        1.1       jtc #endif /* defined ALL_STATE */
    965        1.1       jtc 	if (tzload((char *) NULL, lclptr) != 0)
    966        1.1       jtc 		gmtload(lclptr);
    967        1.1       jtc 	settzname();
    968        1.1       jtc }
    969        1.1       jtc 
    970       1.19    kleink #ifndef STD_INSPIRED
    971       1.19    kleink /*
    972       1.19    kleink ** A non-static declaration of tzsetwall in a system header file
    973       1.19    kleink ** may cause a warning about this upcoming static declaration...
    974       1.19    kleink */
    975       1.19    kleink static
    976       1.19    kleink #endif /* !defined STD_INSPIRED */
    977        1.1       jtc void
    978       1.19    kleink tzsetwall P((void))
    979       1.19    kleink {
    980       1.19    kleink 	rwlock_wrlock(&lcl_lock);
    981       1.19    kleink 	tzsetwall_unlocked();
    982       1.19    kleink 	rwlock_unlock(&lcl_lock);
    983       1.19    kleink }
    984       1.19    kleink 
    985       1.19    kleink static void
    986       1.19    kleink tzset_unlocked P((void))
    987        1.1       jtc {
    988        1.1       jtc 	register const char *	name;
    989        1.1       jtc 
    990        1.1       jtc 	name = getenv("TZ");
    991        1.1       jtc 	if (name == NULL) {
    992       1.19    kleink 		tzsetwall_unlocked();
    993        1.1       jtc 		return;
    994        1.1       jtc 	}
    995        1.1       jtc 
    996       1.29    kleink 	if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
    997        1.1       jtc 		return;
    998       1.29    kleink 	lcl_is_set = strlen(name) < sizeof lcl_TZname;
    999        1.1       jtc 	if (lcl_is_set)
   1000       1.30      groo 		(void)strlcpy(lcl_TZname, name, sizeof(lcl_TZname));
   1001        1.1       jtc 
   1002        1.1       jtc #ifdef ALL_STATE
   1003        1.1       jtc 	if (lclptr == NULL) {
   1004        1.1       jtc 		lclptr = (struct state *) malloc(sizeof *lclptr);
   1005        1.1       jtc 		if (lclptr == NULL) {
   1006        1.1       jtc 			settzname();	/* all we can do */
   1007        1.1       jtc 			return;
   1008        1.1       jtc 		}
   1009        1.1       jtc 	}
   1010        1.1       jtc #endif /* defined ALL_STATE */
   1011        1.1       jtc 	if (*name == '\0') {
   1012        1.1       jtc 		/*
   1013        1.1       jtc 		** User wants it fast rather than right.
   1014        1.1       jtc 		*/
   1015        1.1       jtc 		lclptr->leapcnt = 0;		/* so, we're off a little */
   1016        1.1       jtc 		lclptr->timecnt = 0;
   1017       1.27    atatat 		lclptr->typecnt = 0;
   1018       1.27    atatat 		lclptr->ttis[0].tt_isdst = 0;
   1019        1.1       jtc 		lclptr->ttis[0].tt_gmtoff = 0;
   1020        1.1       jtc 		lclptr->ttis[0].tt_abbrind = 0;
   1021       1.32    itojun 		(void)strlcpy(lclptr->chars, gmt, sizeof(lclptr->chars));
   1022        1.1       jtc 	} else if (tzload(name, lclptr) != 0)
   1023        1.1       jtc 		if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
   1024        1.1       jtc 			(void) gmtload(lclptr);
   1025        1.1       jtc 	settzname();
   1026        1.1       jtc }
   1027        1.1       jtc 
   1028       1.19    kleink void
   1029       1.19    kleink tzset P((void))
   1030       1.19    kleink {
   1031       1.19    kleink 	rwlock_wrlock(&lcl_lock);
   1032       1.19    kleink 	tzset_unlocked();
   1033       1.19    kleink 	rwlock_unlock(&lcl_lock);
   1034       1.19    kleink }
   1035       1.19    kleink 
   1036        1.1       jtc /*
   1037        1.1       jtc ** The easy way to behave "as if no library function calls" localtime
   1038        1.1       jtc ** is to not call it--so we drop its guts into "localsub", which can be
   1039        1.1       jtc ** freely called.  (And no, the PANS doesn't require the above behavior--
   1040        1.1       jtc ** but it *is* desirable.)
   1041        1.1       jtc **
   1042        1.1       jtc ** The unused offset argument is for the benefit of mktime variants.
   1043        1.1       jtc */
   1044        1.1       jtc 
   1045        1.1       jtc /*ARGSUSED*/
   1046        1.1       jtc static void
   1047        1.1       jtc localsub(timep, offset, tmp)
   1048        1.1       jtc const time_t * const	timep;
   1049        1.1       jtc const long		offset;
   1050        1.1       jtc struct tm * const	tmp;
   1051        1.1       jtc {
   1052        1.1       jtc 	register struct state *		sp;
   1053        1.1       jtc 	register const struct ttinfo *	ttisp;
   1054        1.1       jtc 	register int			i;
   1055        1.1       jtc 	const time_t			t = *timep;
   1056        1.1       jtc 
   1057        1.1       jtc 	sp = lclptr;
   1058        1.1       jtc #ifdef ALL_STATE
   1059        1.1       jtc 	if (sp == NULL) {
   1060        1.1       jtc 		gmtsub(timep, offset, tmp);
   1061        1.1       jtc 		return;
   1062        1.1       jtc 	}
   1063        1.1       jtc #endif /* defined ALL_STATE */
   1064        1.1       jtc 	if (sp->timecnt == 0 || t < sp->ats[0]) {
   1065        1.1       jtc 		i = 0;
   1066        1.1       jtc 		while (sp->ttis[i].tt_isdst)
   1067        1.1       jtc 			if (++i >= sp->typecnt) {
   1068        1.1       jtc 				i = 0;
   1069        1.1       jtc 				break;
   1070        1.1       jtc 			}
   1071        1.1       jtc 	} else {
   1072        1.1       jtc 		for (i = 1; i < sp->timecnt; ++i)
   1073        1.1       jtc 			if (t < sp->ats[i])
   1074        1.1       jtc 				break;
   1075        1.1       jtc 		i = sp->types[i - 1];
   1076        1.1       jtc 	}
   1077        1.1       jtc 	ttisp = &sp->ttis[i];
   1078        1.1       jtc 	/*
   1079        1.1       jtc 	** To get (wrong) behavior that's compatible with System V Release 2.0
   1080        1.1       jtc 	** you'd replace the statement below with
   1081        1.1       jtc 	**	t += ttisp->tt_gmtoff;
   1082        1.1       jtc 	**	timesub(&t, 0L, sp, tmp);
   1083        1.1       jtc 	*/
   1084        1.1       jtc 	timesub(&t, ttisp->tt_gmtoff, sp, tmp);
   1085        1.1       jtc 	tmp->tm_isdst = ttisp->tt_isdst;
   1086        1.1       jtc 	tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
   1087        1.1       jtc #ifdef TM_ZONE
   1088        1.1       jtc 	tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
   1089        1.1       jtc #endif /* defined TM_ZONE */
   1090        1.1       jtc }
   1091        1.1       jtc 
   1092        1.1       jtc struct tm *
   1093        1.1       jtc localtime(timep)
   1094        1.1       jtc const time_t * const	timep;
   1095        1.1       jtc {
   1096       1.19    kleink 	rwlock_wrlock(&lcl_lock);
   1097       1.19    kleink 	tzset_unlocked();
   1098        1.1       jtc 	localsub(timep, 0L, &tm);
   1099       1.19    kleink 	rwlock_unlock(&lcl_lock);
   1100        1.1       jtc 	return &tm;
   1101        1.1       jtc }
   1102        1.1       jtc 
   1103        1.1       jtc /*
   1104       1.35    kleink ** Re-entrant version of localtime.
   1105       1.35    kleink */
   1106       1.35    kleink 
   1107       1.18    kleink struct tm *
   1108       1.28     lukem localtime_r(timep, tmp)
   1109       1.18    kleink const time_t * const	timep;
   1110       1.28     lukem struct tm *		tmp;
   1111       1.18    kleink {
   1112       1.19    kleink 	rwlock_rdlock(&lcl_lock);
   1113       1.36  christos 	tzset_unlocked();
   1114       1.28     lukem 	localsub(timep, 0L, tmp);
   1115       1.19    kleink 	rwlock_unlock(&lcl_lock);
   1116       1.28     lukem 	return tmp;
   1117       1.18    kleink }
   1118       1.18    kleink 
   1119       1.18    kleink /*
   1120        1.1       jtc ** gmtsub is to gmtime as localsub is to localtime.
   1121        1.1       jtc */
   1122        1.1       jtc 
   1123        1.1       jtc static void
   1124        1.1       jtc gmtsub(timep, offset, tmp)
   1125        1.1       jtc const time_t * const	timep;
   1126        1.1       jtc const long		offset;
   1127        1.1       jtc struct tm * const	tmp;
   1128        1.1       jtc {
   1129       1.33  christos #ifdef _REENTRANT
   1130       1.19    kleink 	static mutex_t gmt_mutex = MUTEX_INITIALIZER;
   1131       1.19    kleink #endif
   1132       1.19    kleink 
   1133       1.19    kleink 	mutex_lock(&gmt_mutex);
   1134        1.1       jtc 	if (!gmt_is_set) {
   1135        1.1       jtc 		gmt_is_set = TRUE;
   1136        1.1       jtc #ifdef ALL_STATE
   1137        1.1       jtc 		gmtptr = (struct state *) malloc(sizeof *gmtptr);
   1138        1.1       jtc 		if (gmtptr != NULL)
   1139        1.1       jtc #endif /* defined ALL_STATE */
   1140        1.1       jtc 			gmtload(gmtptr);
   1141        1.1       jtc 	}
   1142       1.19    kleink 	mutex_unlock(&gmt_mutex);
   1143        1.1       jtc 	timesub(timep, offset, gmtptr, tmp);
   1144        1.1       jtc #ifdef TM_ZONE
   1145        1.1       jtc 	/*
   1146        1.1       jtc 	** Could get fancy here and deliver something such as
   1147       1.14       jtc 	** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
   1148        1.1       jtc 	** but this is no time for a treasure hunt.
   1149        1.1       jtc 	*/
   1150        1.1       jtc 	if (offset != 0)
   1151       1.37  christos 		tmp->TM_ZONE = (__aconst char *)__UNCONST(wildabbr);
   1152        1.1       jtc 	else {
   1153        1.1       jtc #ifdef ALL_STATE
   1154        1.1       jtc 		if (gmtptr == NULL)
   1155       1.37  christos 			tmp->TM_ZONE = (__aconst char *)__UNCONST(gmt);
   1156        1.1       jtc 		else	tmp->TM_ZONE = gmtptr->chars;
   1157        1.1       jtc #endif /* defined ALL_STATE */
   1158        1.1       jtc #ifndef ALL_STATE
   1159        1.1       jtc 		tmp->TM_ZONE = gmtptr->chars;
   1160        1.1       jtc #endif /* State Farm */
   1161        1.1       jtc 	}
   1162        1.1       jtc #endif /* defined TM_ZONE */
   1163        1.1       jtc }
   1164        1.1       jtc 
   1165        1.1       jtc struct tm *
   1166        1.1       jtc gmtime(timep)
   1167        1.1       jtc const time_t * const	timep;
   1168        1.1       jtc {
   1169        1.1       jtc 	gmtsub(timep, 0L, &tm);
   1170        1.1       jtc 	return &tm;
   1171        1.1       jtc }
   1172        1.1       jtc 
   1173       1.18    kleink /*
   1174       1.35    kleink ** Re-entrant version of gmtime.
   1175       1.35    kleink */
   1176       1.35    kleink 
   1177       1.18    kleink struct tm *
   1178       1.28     lukem gmtime_r(timep, tmp)
   1179       1.18    kleink const time_t * const	timep;
   1180       1.28     lukem struct tm *		tmp;
   1181       1.18    kleink {
   1182       1.28     lukem 	gmtsub(timep, 0L, tmp);
   1183       1.28     lukem 	return tmp;
   1184       1.18    kleink }
   1185       1.18    kleink 
   1186        1.1       jtc #ifdef STD_INSPIRED
   1187        1.1       jtc 
   1188        1.1       jtc struct tm *
   1189        1.1       jtc offtime(timep, offset)
   1190        1.1       jtc const time_t * const	timep;
   1191        1.1       jtc const long		offset;
   1192        1.1       jtc {
   1193        1.1       jtc 	gmtsub(timep, offset, &tm);
   1194        1.1       jtc 	return &tm;
   1195        1.1       jtc }
   1196        1.1       jtc 
   1197        1.1       jtc #endif /* defined STD_INSPIRED */
   1198        1.1       jtc 
   1199        1.1       jtc static void
   1200        1.1       jtc timesub(timep, offset, sp, tmp)
   1201        1.1       jtc const time_t * const			timep;
   1202        1.1       jtc const long				offset;
   1203        1.1       jtc register const struct state * const	sp;
   1204        1.1       jtc register struct tm * const		tmp;
   1205        1.1       jtc {
   1206        1.1       jtc 	register const struct lsinfo *	lp;
   1207        1.1       jtc 	register long			days;
   1208        1.1       jtc 	register long			rem;
   1209        1.1       jtc 	register int			y;
   1210        1.1       jtc 	register int			yleap;
   1211        1.1       jtc 	register const int *		ip;
   1212        1.1       jtc 	register long			corr;
   1213        1.1       jtc 	register int			hit;
   1214        1.1       jtc 	register int			i;
   1215        1.1       jtc 
   1216        1.1       jtc 	corr = 0;
   1217        1.1       jtc 	hit = 0;
   1218        1.1       jtc #ifdef ALL_STATE
   1219        1.1       jtc 	i = (sp == NULL) ? 0 : sp->leapcnt;
   1220        1.1       jtc #endif /* defined ALL_STATE */
   1221        1.1       jtc #ifndef ALL_STATE
   1222        1.1       jtc 	i = sp->leapcnt;
   1223        1.1       jtc #endif /* State Farm */
   1224        1.1       jtc 	while (--i >= 0) {
   1225        1.1       jtc 		lp = &sp->lsis[i];
   1226        1.1       jtc 		if (*timep >= lp->ls_trans) {
   1227        1.1       jtc 			if (*timep == lp->ls_trans) {
   1228        1.1       jtc 				hit = ((i == 0 && lp->ls_corr > 0) ||
   1229        1.1       jtc 					lp->ls_corr > sp->lsis[i - 1].ls_corr);
   1230        1.1       jtc 				if (hit)
   1231        1.1       jtc 					while (i > 0 &&
   1232        1.1       jtc 						sp->lsis[i].ls_trans ==
   1233        1.1       jtc 						sp->lsis[i - 1].ls_trans + 1 &&
   1234        1.1       jtc 						sp->lsis[i].ls_corr ==
   1235        1.1       jtc 						sp->lsis[i - 1].ls_corr + 1) {
   1236        1.1       jtc 							++hit;
   1237        1.1       jtc 							--i;
   1238        1.1       jtc 					}
   1239        1.1       jtc 			}
   1240        1.1       jtc 			corr = lp->ls_corr;
   1241        1.1       jtc 			break;
   1242        1.1       jtc 		}
   1243        1.1       jtc 	}
   1244        1.1       jtc 	days = *timep / SECSPERDAY;
   1245        1.1       jtc 	rem = *timep % SECSPERDAY;
   1246        1.1       jtc #ifdef mc68k
   1247        1.1       jtc 	if (*timep == 0x80000000) {
   1248        1.1       jtc 		/*
   1249        1.1       jtc 		** A 3B1 muffs the division on the most negative number.
   1250        1.1       jtc 		*/
   1251        1.1       jtc 		days = -24855;
   1252        1.1       jtc 		rem = -11648;
   1253        1.1       jtc 	}
   1254        1.1       jtc #endif /* defined mc68k */
   1255        1.1       jtc 	rem += (offset - corr);
   1256        1.1       jtc 	while (rem < 0) {
   1257        1.1       jtc 		rem += SECSPERDAY;
   1258        1.1       jtc 		--days;
   1259        1.1       jtc 	}
   1260        1.1       jtc 	while (rem >= SECSPERDAY) {
   1261        1.1       jtc 		rem -= SECSPERDAY;
   1262        1.1       jtc 		++days;
   1263        1.1       jtc 	}
   1264        1.1       jtc 	tmp->tm_hour = (int) (rem / SECSPERHOUR);
   1265        1.1       jtc 	rem = rem % SECSPERHOUR;
   1266        1.1       jtc 	tmp->tm_min = (int) (rem / SECSPERMIN);
   1267        1.6       jtc 	/*
   1268        1.6       jtc 	** A positive leap second requires a special
   1269        1.6       jtc 	** representation.  This uses "... ??:59:60" et seq.
   1270        1.6       jtc 	*/
   1271        1.6       jtc 	tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
   1272        1.1       jtc 	tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
   1273        1.1       jtc 	if (tmp->tm_wday < 0)
   1274        1.1       jtc 		tmp->tm_wday += DAYSPERWEEK;
   1275        1.1       jtc 	y = EPOCH_YEAR;
   1276        1.5       jtc #define LEAPS_THRU_END_OF(y)	((y) / 4 - (y) / 100 + (y) / 400)
   1277        1.5       jtc 	while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
   1278        1.5       jtc 		register int	newy;
   1279        1.5       jtc 
   1280       1.21  christos 		newy = (int)(y + days / DAYSPERNYEAR);
   1281        1.5       jtc 		if (days < 0)
   1282        1.5       jtc 			--newy;
   1283        1.5       jtc 		days -= (newy - y) * DAYSPERNYEAR +
   1284        1.5       jtc 			LEAPS_THRU_END_OF(newy - 1) -
   1285        1.5       jtc 			LEAPS_THRU_END_OF(y - 1);
   1286        1.5       jtc 		y = newy;
   1287        1.5       jtc 	}
   1288        1.1       jtc 	tmp->tm_year = y - TM_YEAR_BASE;
   1289        1.1       jtc 	tmp->tm_yday = (int) days;
   1290        1.1       jtc 	ip = mon_lengths[yleap];
   1291        1.1       jtc 	for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
   1292        1.1       jtc 		days = days - (long) ip[tmp->tm_mon];
   1293        1.1       jtc 	tmp->tm_mday = (int) (days + 1);
   1294        1.1       jtc 	tmp->tm_isdst = 0;
   1295        1.1       jtc #ifdef TM_GMTOFF
   1296        1.1       jtc 	tmp->TM_GMTOFF = offset;
   1297        1.1       jtc #endif /* defined TM_GMTOFF */
   1298        1.1       jtc }
   1299        1.1       jtc 
   1300        1.1       jtc char *
   1301        1.1       jtc ctime(timep)
   1302        1.1       jtc const time_t * const	timep;
   1303        1.1       jtc {
   1304        1.1       jtc /*
   1305        1.1       jtc ** Section 4.12.3.2 of X3.159-1989 requires that
   1306       1.18    kleink **	The ctime function converts the calendar time pointed to by timer
   1307        1.1       jtc **	to local time in the form of a string.  It is equivalent to
   1308        1.1       jtc **		asctime(localtime(timer))
   1309        1.1       jtc */
   1310        1.1       jtc 	return asctime(localtime(timep));
   1311       1.18    kleink }
   1312       1.18    kleink 
   1313       1.18    kleink char *
   1314       1.18    kleink ctime_r(timep, buf)
   1315       1.18    kleink const time_t * const	timep;
   1316       1.18    kleink char *			buf;
   1317       1.18    kleink {
   1318       1.21  christos 	struct tm	tmp;
   1319       1.18    kleink 
   1320       1.21  christos 	return asctime_r(localtime_r(timep, &tmp), buf);
   1321        1.1       jtc }
   1322        1.1       jtc 
   1323        1.1       jtc /*
   1324        1.1       jtc ** Adapted from code provided by Robert Elz, who writes:
   1325        1.1       jtc **	The "best" way to do mktime I think is based on an idea of Bob
   1326        1.7       jtc **	Kridle's (so its said...) from a long time ago.
   1327        1.7       jtc **	[kridle (at) xinet.com as of 1996-01-16.]
   1328        1.1       jtc **	It does a binary search of the time_t space.  Since time_t's are
   1329        1.1       jtc **	just 32 bits, its a max of 32 iterations (even at 64 bits it
   1330        1.1       jtc **	would still be very reasonable).
   1331        1.1       jtc */
   1332        1.1       jtc 
   1333        1.1       jtc #ifndef WRONG
   1334        1.1       jtc #define WRONG	(-1)
   1335        1.1       jtc #endif /* !defined WRONG */
   1336        1.1       jtc 
   1337        1.1       jtc /*
   1338        1.1       jtc ** Simplified normalize logic courtesy Paul Eggert (eggert (at) twinsun.com).
   1339        1.1       jtc */
   1340        1.1       jtc 
   1341        1.1       jtc static int
   1342        1.1       jtc increment_overflow(number, delta)
   1343        1.1       jtc int *	number;
   1344        1.1       jtc int	delta;
   1345        1.1       jtc {
   1346        1.1       jtc 	int	number0;
   1347        1.1       jtc 
   1348        1.1       jtc 	number0 = *number;
   1349        1.1       jtc 	*number += delta;
   1350        1.1       jtc 	return (*number < number0) != (delta < 0);
   1351        1.1       jtc }
   1352        1.1       jtc 
   1353        1.1       jtc static int
   1354        1.1       jtc normalize_overflow(tensptr, unitsptr, base)
   1355        1.1       jtc int * const	tensptr;
   1356        1.1       jtc int * const	unitsptr;
   1357        1.1       jtc const int	base;
   1358        1.1       jtc {
   1359        1.1       jtc 	register int	tensdelta;
   1360        1.1       jtc 
   1361        1.1       jtc 	tensdelta = (*unitsptr >= 0) ?
   1362        1.1       jtc 		(*unitsptr / base) :
   1363        1.1       jtc 		(-1 - (-1 - *unitsptr) / base);
   1364        1.1       jtc 	*unitsptr -= tensdelta * base;
   1365        1.1       jtc 	return increment_overflow(tensptr, tensdelta);
   1366        1.1       jtc }
   1367        1.1       jtc 
   1368        1.1       jtc static int
   1369        1.1       jtc tmcomp(atmp, btmp)
   1370        1.1       jtc register const struct tm * const atmp;
   1371        1.1       jtc register const struct tm * const btmp;
   1372        1.1       jtc {
   1373        1.1       jtc 	register int	result;
   1374        1.1       jtc 
   1375        1.1       jtc 	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
   1376        1.1       jtc 		(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
   1377        1.1       jtc 		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
   1378        1.1       jtc 		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
   1379        1.1       jtc 		(result = (atmp->tm_min - btmp->tm_min)) == 0)
   1380        1.1       jtc 			result = atmp->tm_sec - btmp->tm_sec;
   1381        1.1       jtc 	return result;
   1382        1.1       jtc }
   1383        1.1       jtc 
   1384        1.1       jtc static time_t
   1385       1.13       jtc time2sub(tmp, funcp, offset, okayp, do_norm_secs)
   1386        1.1       jtc struct tm * const	tmp;
   1387        1.1       jtc void (* const		funcp) P((const time_t*, long, struct tm*));
   1388        1.1       jtc const long		offset;
   1389        1.1       jtc int * const		okayp;
   1390       1.13       jtc const int		do_norm_secs;
   1391        1.1       jtc {
   1392        1.1       jtc 	register const struct state *	sp;
   1393        1.1       jtc 	register int			dir;
   1394        1.1       jtc 	register int			bits;
   1395        1.1       jtc 	register int			i, j ;
   1396        1.1       jtc 	register int			saved_seconds;
   1397        1.1       jtc 	time_t				newt;
   1398        1.1       jtc 	time_t				t;
   1399        1.1       jtc 	struct tm			yourtm, mytm;
   1400        1.1       jtc 
   1401        1.1       jtc 	*okayp = FALSE;
   1402        1.1       jtc 	yourtm = *tmp;
   1403       1.13       jtc 	if (do_norm_secs) {
   1404       1.13       jtc 		if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
   1405       1.13       jtc 			SECSPERMIN))
   1406       1.13       jtc 				return WRONG;
   1407       1.13       jtc 	}
   1408        1.1       jtc 	if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
   1409        1.1       jtc 		return WRONG;
   1410        1.1       jtc 	if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
   1411        1.1       jtc 		return WRONG;
   1412        1.1       jtc 	if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
   1413        1.1       jtc 		return WRONG;
   1414        1.1       jtc 	/*
   1415        1.1       jtc 	** Turn yourtm.tm_year into an actual year number for now.
   1416        1.1       jtc 	** It is converted back to an offset from TM_YEAR_BASE later.
   1417        1.1       jtc 	*/
   1418        1.1       jtc 	if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
   1419        1.1       jtc 		return WRONG;
   1420        1.1       jtc 	while (yourtm.tm_mday <= 0) {
   1421        1.1       jtc 		if (increment_overflow(&yourtm.tm_year, -1))
   1422        1.1       jtc 			return WRONG;
   1423        1.7       jtc 		i = yourtm.tm_year + (1 < yourtm.tm_mon);
   1424        1.7       jtc 		yourtm.tm_mday += year_lengths[isleap(i)];
   1425        1.1       jtc 	}
   1426        1.1       jtc 	while (yourtm.tm_mday > DAYSPERLYEAR) {
   1427        1.7       jtc 		i = yourtm.tm_year + (1 < yourtm.tm_mon);
   1428        1.7       jtc 		yourtm.tm_mday -= year_lengths[isleap(i)];
   1429        1.1       jtc 		if (increment_overflow(&yourtm.tm_year, 1))
   1430        1.1       jtc 			return WRONG;
   1431        1.1       jtc 	}
   1432        1.1       jtc 	for ( ; ; ) {
   1433        1.1       jtc 		i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
   1434        1.1       jtc 		if (yourtm.tm_mday <= i)
   1435        1.1       jtc 			break;
   1436        1.1       jtc 		yourtm.tm_mday -= i;
   1437        1.1       jtc 		if (++yourtm.tm_mon >= MONSPERYEAR) {
   1438        1.1       jtc 			yourtm.tm_mon = 0;
   1439        1.1       jtc 			if (increment_overflow(&yourtm.tm_year, 1))
   1440        1.1       jtc 				return WRONG;
   1441        1.1       jtc 		}
   1442        1.1       jtc 	}
   1443        1.1       jtc 	if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
   1444        1.1       jtc 		return WRONG;
   1445       1.29    kleink 	if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
   1446       1.29    kleink 		saved_seconds = 0;
   1447       1.29    kleink 	else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
   1448        1.1       jtc 		/*
   1449        1.1       jtc 		** We can't set tm_sec to 0, because that might push the
   1450        1.1       jtc 		** time below the minimum representable time.
   1451        1.1       jtc 		** Set tm_sec to 59 instead.
   1452        1.1       jtc 		** This assumes that the minimum representable time is
   1453        1.1       jtc 		** not in the same minute that a leap second was deleted from,
   1454        1.1       jtc 		** which is a safer assumption than using 58 would be.
   1455        1.1       jtc 		*/
   1456        1.1       jtc 		if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
   1457        1.1       jtc 			return WRONG;
   1458        1.1       jtc 		saved_seconds = yourtm.tm_sec;
   1459        1.1       jtc 		yourtm.tm_sec = SECSPERMIN - 1;
   1460        1.1       jtc 	} else {
   1461        1.1       jtc 		saved_seconds = yourtm.tm_sec;
   1462        1.1       jtc 		yourtm.tm_sec = 0;
   1463        1.1       jtc 	}
   1464        1.1       jtc 	/*
   1465        1.6       jtc 	** Divide the search space in half
   1466        1.6       jtc 	** (this works whether time_t is signed or unsigned).
   1467        1.1       jtc 	*/
   1468        1.6       jtc 	bits = TYPE_BIT(time_t) - 1;
   1469        1.1       jtc 	/*
   1470        1.6       jtc 	** If time_t is signed, then 0 is just above the median,
   1471        1.6       jtc 	** assuming two's complement arithmetic.
   1472        1.6       jtc 	** If time_t is unsigned, then (1 << bits) is just above the median.
   1473        1.1       jtc 	*/
   1474       1.37  christos 	/*CONSTCOND*/
   1475        1.6       jtc 	t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
   1476        1.1       jtc 	for ( ; ; ) {
   1477        1.1       jtc 		(*funcp)(&t, offset, &mytm);
   1478        1.1       jtc 		dir = tmcomp(&mytm, &yourtm);
   1479        1.1       jtc 		if (dir != 0) {
   1480        1.1       jtc 			if (bits-- < 0)
   1481        1.1       jtc 				return WRONG;
   1482        1.1       jtc 			if (bits < 0)
   1483        1.6       jtc 				--t; /* may be needed if new t is minimal */
   1484        1.1       jtc 			else if (dir > 0)
   1485        1.6       jtc 				t -= ((time_t) 1) << bits;
   1486        1.6       jtc 			else	t += ((time_t) 1) << bits;
   1487        1.1       jtc 			continue;
   1488        1.1       jtc 		}
   1489        1.1       jtc 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
   1490        1.1       jtc 			break;
   1491        1.1       jtc 		/*
   1492        1.1       jtc 		** Right time, wrong type.
   1493        1.1       jtc 		** Hunt for right time, right type.
   1494        1.1       jtc 		** It's okay to guess wrong since the guess
   1495        1.1       jtc 		** gets checked.
   1496        1.1       jtc 		*/
   1497        1.1       jtc 		/*
   1498        1.1       jtc 		** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
   1499        1.1       jtc 		*/
   1500        1.1       jtc 		sp = (const struct state *)
   1501        1.1       jtc 			(((void *) funcp == (void *) localsub) ?
   1502        1.1       jtc 			lclptr : gmtptr);
   1503        1.1       jtc #ifdef ALL_STATE
   1504        1.1       jtc 		if (sp == NULL)
   1505        1.1       jtc 			return WRONG;
   1506        1.1       jtc #endif /* defined ALL_STATE */
   1507        1.5       jtc 		for (i = sp->typecnt - 1; i >= 0; --i) {
   1508        1.1       jtc 			if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
   1509        1.1       jtc 				continue;
   1510        1.5       jtc 			for (j = sp->typecnt - 1; j >= 0; --j) {
   1511        1.1       jtc 				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
   1512        1.1       jtc 					continue;
   1513        1.1       jtc 				newt = t + sp->ttis[j].tt_gmtoff -
   1514        1.1       jtc 					sp->ttis[i].tt_gmtoff;
   1515        1.1       jtc 				(*funcp)(&newt, offset, &mytm);
   1516        1.1       jtc 				if (tmcomp(&mytm, &yourtm) != 0)
   1517        1.1       jtc 					continue;
   1518        1.1       jtc 				if (mytm.tm_isdst != yourtm.tm_isdst)
   1519        1.1       jtc 					continue;
   1520        1.1       jtc 				/*
   1521        1.1       jtc 				** We have a match.
   1522        1.1       jtc 				*/
   1523        1.1       jtc 				t = newt;
   1524        1.1       jtc 				goto label;
   1525        1.1       jtc 			}
   1526        1.1       jtc 		}
   1527        1.1       jtc 		return WRONG;
   1528        1.1       jtc 	}
   1529        1.1       jtc label:
   1530        1.1       jtc 	newt = t + saved_seconds;
   1531        1.1       jtc 	if ((newt < t) != (saved_seconds < 0))
   1532        1.1       jtc 		return WRONG;
   1533        1.1       jtc 	t = newt;
   1534        1.1       jtc 	(*funcp)(&t, offset, tmp);
   1535        1.1       jtc 	*okayp = TRUE;
   1536        1.1       jtc 	return t;
   1537       1.13       jtc }
   1538       1.13       jtc 
   1539       1.13       jtc static time_t
   1540       1.13       jtc time2(tmp, funcp, offset, okayp)
   1541       1.13       jtc struct tm * const	tmp;
   1542       1.13       jtc void (* const		funcp) P((const time_t*, long, struct tm*));
   1543       1.13       jtc const long		offset;
   1544       1.13       jtc int * const		okayp;
   1545       1.13       jtc {
   1546       1.13       jtc 	time_t	t;
   1547       1.13       jtc 
   1548       1.13       jtc 	/*
   1549       1.13       jtc 	** First try without normalization of seconds
   1550       1.13       jtc 	** (in case tm_sec contains a value associated with a leap second).
   1551       1.13       jtc 	** If that fails, try with normalization of seconds.
   1552       1.13       jtc 	*/
   1553       1.13       jtc 	t = time2sub(tmp, funcp, offset, okayp, FALSE);
   1554       1.13       jtc 	return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
   1555        1.1       jtc }
   1556        1.1       jtc 
   1557        1.1       jtc static time_t
   1558        1.1       jtc time1(tmp, funcp, offset)
   1559        1.1       jtc struct tm * const	tmp;
   1560        1.5       jtc void (* const		funcp) P((const time_t *, long, struct tm *));
   1561        1.1       jtc const long		offset;
   1562        1.1       jtc {
   1563        1.1       jtc 	register time_t			t;
   1564        1.1       jtc 	register const struct state *	sp;
   1565        1.1       jtc 	register int			samei, otheri;
   1566       1.35    kleink 	register int			sameind, otherind;
   1567       1.35    kleink 	register int			i;
   1568       1.35    kleink 	register int			nseen;
   1569       1.35    kleink 	int				seen[TZ_MAX_TYPES];
   1570       1.35    kleink 	int				types[TZ_MAX_TYPES];
   1571        1.1       jtc 	int				okay;
   1572        1.1       jtc 
   1573        1.1       jtc 	if (tmp->tm_isdst > 1)
   1574        1.1       jtc 		tmp->tm_isdst = 1;
   1575        1.1       jtc 	t = time2(tmp, funcp, offset, &okay);
   1576        1.1       jtc #ifdef PCTS
   1577        1.1       jtc 	/*
   1578        1.1       jtc 	** PCTS code courtesy Grant Sullivan (grant (at) osf.org).
   1579        1.1       jtc 	*/
   1580        1.1       jtc 	if (okay)
   1581        1.1       jtc 		return t;
   1582        1.1       jtc 	if (tmp->tm_isdst < 0)
   1583        1.1       jtc 		tmp->tm_isdst = 0;	/* reset to std and try again */
   1584        1.1       jtc #endif /* defined PCTS */
   1585        1.1       jtc #ifndef PCTS
   1586        1.1       jtc 	if (okay || tmp->tm_isdst < 0)
   1587        1.1       jtc 		return t;
   1588        1.1       jtc #endif /* !defined PCTS */
   1589        1.1       jtc 	/*
   1590        1.1       jtc 	** We're supposed to assume that somebody took a time of one type
   1591        1.1       jtc 	** and did some math on it that yielded a "struct tm" that's bad.
   1592        1.1       jtc 	** We try to divine the type they started from and adjust to the
   1593        1.1       jtc 	** type they need.
   1594        1.1       jtc 	*/
   1595        1.1       jtc 	/*
   1596        1.1       jtc 	** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
   1597        1.1       jtc 	*/
   1598        1.1       jtc 	sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
   1599        1.1       jtc 		lclptr : gmtptr);
   1600        1.1       jtc #ifdef ALL_STATE
   1601        1.1       jtc 	if (sp == NULL)
   1602        1.1       jtc 		return WRONG;
   1603        1.1       jtc #endif /* defined ALL_STATE */
   1604       1.35    kleink 	for (i = 0; i < sp->typecnt; ++i)
   1605       1.35    kleink 		seen[i] = FALSE;
   1606       1.35    kleink 	nseen = 0;
   1607       1.35    kleink 	for (i = sp->timecnt - 1; i >= 0; --i)
   1608       1.35    kleink 		if (!seen[sp->types[i]]) {
   1609       1.35    kleink 			seen[sp->types[i]] = TRUE;
   1610       1.35    kleink 			types[nseen++] = sp->types[i];
   1611       1.35    kleink 		}
   1612       1.35    kleink 	for (sameind = 0; sameind < nseen; ++sameind) {
   1613       1.35    kleink 		samei = types[sameind];
   1614        1.1       jtc 		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
   1615        1.1       jtc 			continue;
   1616       1.35    kleink 		for (otherind = 0; otherind < nseen; ++otherind) {
   1617       1.35    kleink 			otheri = types[otherind];
   1618        1.1       jtc 			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
   1619        1.1       jtc 				continue;
   1620       1.21  christos 			tmp->tm_sec += (int)(sp->ttis[otheri].tt_gmtoff -
   1621       1.21  christos 					sp->ttis[samei].tt_gmtoff);
   1622        1.1       jtc 			tmp->tm_isdst = !tmp->tm_isdst;
   1623        1.1       jtc 			t = time2(tmp, funcp, offset, &okay);
   1624        1.1       jtc 			if (okay)
   1625        1.1       jtc 				return t;
   1626       1.21  christos 			tmp->tm_sec -= (int)(sp->ttis[otheri].tt_gmtoff -
   1627       1.21  christos 					sp->ttis[samei].tt_gmtoff);
   1628        1.1       jtc 			tmp->tm_isdst = !tmp->tm_isdst;
   1629        1.1       jtc 		}
   1630        1.1       jtc 	}
   1631        1.1       jtc 	return WRONG;
   1632        1.1       jtc }
   1633        1.1       jtc 
   1634        1.1       jtc time_t
   1635        1.1       jtc mktime(tmp)
   1636        1.1       jtc struct tm * const	tmp;
   1637        1.1       jtc {
   1638       1.19    kleink 	time_t result;
   1639       1.19    kleink 
   1640       1.19    kleink 	rwlock_wrlock(&lcl_lock);
   1641       1.19    kleink 	tzset_unlocked();
   1642       1.19    kleink 	result = time1(tmp, localsub, 0L);
   1643       1.19    kleink 	rwlock_unlock(&lcl_lock);
   1644       1.19    kleink 	return (result);
   1645        1.1       jtc }
   1646        1.1       jtc 
   1647        1.1       jtc #ifdef STD_INSPIRED
   1648        1.1       jtc 
   1649        1.1       jtc time_t
   1650        1.1       jtc timelocal(tmp)
   1651        1.1       jtc struct tm * const	tmp;
   1652        1.1       jtc {
   1653        1.1       jtc 	tmp->tm_isdst = -1;	/* in case it wasn't initialized */
   1654        1.1       jtc 	return mktime(tmp);
   1655        1.1       jtc }
   1656        1.1       jtc 
   1657        1.1       jtc time_t
   1658        1.1       jtc timegm(tmp)
   1659        1.1       jtc struct tm * const	tmp;
   1660        1.1       jtc {
   1661        1.1       jtc 	tmp->tm_isdst = 0;
   1662        1.1       jtc 	return time1(tmp, gmtsub, 0L);
   1663        1.1       jtc }
   1664        1.1       jtc 
   1665        1.1       jtc time_t
   1666        1.1       jtc timeoff(tmp, offset)
   1667        1.1       jtc struct tm * const	tmp;
   1668        1.1       jtc const long		offset;
   1669        1.1       jtc {
   1670        1.1       jtc 	tmp->tm_isdst = 0;
   1671        1.1       jtc 	return time1(tmp, gmtsub, offset);
   1672        1.1       jtc }
   1673        1.1       jtc 
   1674        1.1       jtc #endif /* defined STD_INSPIRED */
   1675        1.1       jtc 
   1676        1.1       jtc #ifdef CMUCS
   1677        1.1       jtc 
   1678        1.1       jtc /*
   1679        1.1       jtc ** The following is supplied for compatibility with
   1680        1.1       jtc ** previous versions of the CMUCS runtime library.
   1681        1.1       jtc */
   1682        1.1       jtc 
   1683        1.1       jtc long
   1684        1.1       jtc gtime(tmp)
   1685        1.1       jtc struct tm * const	tmp;
   1686        1.1       jtc {
   1687        1.1       jtc 	const time_t	t = mktime(tmp);
   1688        1.1       jtc 
   1689        1.1       jtc 	if (t == WRONG)
   1690        1.1       jtc 		return -1;
   1691        1.1       jtc 	return t;
   1692        1.1       jtc }
   1693        1.1       jtc 
   1694        1.1       jtc #endif /* defined CMUCS */
   1695        1.1       jtc 
   1696        1.1       jtc /*
   1697        1.1       jtc ** XXX--is the below the right way to conditionalize??
   1698        1.1       jtc */
   1699        1.1       jtc 
   1700        1.1       jtc #ifdef STD_INSPIRED
   1701        1.1       jtc 
   1702        1.1       jtc /*
   1703        1.1       jtc ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
   1704       1.14       jtc ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
   1705        1.1       jtc ** is not the case if we are accounting for leap seconds.
   1706        1.1       jtc ** So, we provide the following conversion routines for use
   1707        1.1       jtc ** when exchanging timestamps with POSIX conforming systems.
   1708        1.1       jtc */
   1709        1.1       jtc 
   1710        1.1       jtc static long
   1711        1.1       jtc leapcorr(timep)
   1712        1.1       jtc time_t *	timep;
   1713        1.1       jtc {
   1714        1.1       jtc 	register struct state *		sp;
   1715        1.1       jtc 	register struct lsinfo *	lp;
   1716        1.1       jtc 	register int			i;
   1717        1.1       jtc 
   1718        1.1       jtc 	sp = lclptr;
   1719        1.1       jtc 	i = sp->leapcnt;
   1720        1.1       jtc 	while (--i >= 0) {
   1721        1.1       jtc 		lp = &sp->lsis[i];
   1722        1.1       jtc 		if (*timep >= lp->ls_trans)
   1723        1.1       jtc 			return lp->ls_corr;
   1724        1.1       jtc 	}
   1725        1.1       jtc 	return 0;
   1726        1.1       jtc }
   1727        1.1       jtc 
   1728        1.1       jtc time_t
   1729        1.1       jtc time2posix(t)
   1730        1.1       jtc time_t	t;
   1731        1.1       jtc {
   1732       1.19    kleink 	time_t result;
   1733       1.19    kleink 
   1734       1.19    kleink 	rwlock_wrlock(&lcl_lock);
   1735       1.19    kleink 	tzset_unlocked();
   1736       1.19    kleink 	result = t - leapcorr(&t);
   1737       1.19    kleink 	rwlock_unlock(&lcl_lock);
   1738       1.19    kleink 	return (result);
   1739        1.1       jtc }
   1740        1.1       jtc 
   1741        1.1       jtc time_t
   1742        1.1       jtc posix2time(t)
   1743        1.1       jtc time_t	t;
   1744        1.1       jtc {
   1745        1.1       jtc 	time_t	x;
   1746        1.1       jtc 	time_t	y;
   1747        1.1       jtc 
   1748       1.19    kleink 	rwlock_wrlock(&lcl_lock);
   1749       1.19    kleink 	tzset_unlocked();
   1750        1.1       jtc 	/*
   1751        1.1       jtc 	** For a positive leap second hit, the result
   1752        1.1       jtc 	** is not unique.  For a negative leap second
   1753        1.1       jtc 	** hit, the corresponding time doesn't exist,
   1754        1.1       jtc 	** so we return an adjacent second.
   1755        1.1       jtc 	*/
   1756        1.1       jtc 	x = t + leapcorr(&t);
   1757        1.1       jtc 	y = x - leapcorr(&x);
   1758        1.1       jtc 	if (y < t) {
   1759        1.1       jtc 		do {
   1760        1.1       jtc 			x++;
   1761        1.1       jtc 			y = x - leapcorr(&x);
   1762        1.1       jtc 		} while (y < t);
   1763       1.19    kleink 		if (t != y) {
   1764       1.19    kleink 			rwlock_unlock(&lcl_lock);
   1765        1.1       jtc 			return x - 1;
   1766       1.19    kleink 		}
   1767        1.1       jtc 	} else if (y > t) {
   1768        1.1       jtc 		do {
   1769        1.1       jtc 			--x;
   1770        1.1       jtc 			y = x - leapcorr(&x);
   1771        1.1       jtc 		} while (y > t);
   1772       1.19    kleink 		if (t != y) {
   1773       1.19    kleink 			rwlock_unlock(&lcl_lock);
   1774        1.1       jtc 			return x + 1;
   1775       1.19    kleink 		}
   1776        1.1       jtc 	}
   1777       1.19    kleink 	rwlock_unlock(&lcl_lock);
   1778        1.1       jtc 	return x;
   1779        1.1       jtc }
   1780        1.1       jtc 
   1781        1.1       jtc #endif /* defined STD_INSPIRED */
   1782