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