xref: /csrg-svn/lib/libc/gen/ctime.c (revision 42621)
122084Smckusick /*
237140Sbostic  * Copyright (c) 1987, 1989 Regents of the University of California.
332754Sbostic  * All rights reserved.
432754Sbostic  *
534952Sbostic  * This code is derived from software contributed to Berkeley by
634952Sbostic  * Arthur Olson.
734952Sbostic  *
8*42621Sbostic  * %sccs.include.redist.c%
922084Smckusick  */
1022084Smckusick 
1130684Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*42621Sbostic static char sccsid[] = "@(#)ctime.c	5.21 (Berkeley) 06/01/90";
1332754Sbostic #endif /* LIBC_SCCS and not lint */
1422084Smckusick 
1537140Sbostic /*
1637140Sbostic ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
1737140Sbostic ** POSIX-style TZ environment variable handling from Guy Harris
1837140Sbostic ** (guy@auspex.com).
1937140Sbostic */
201959Swnj 
2137140Sbostic /*LINTLIBRARY*/
221959Swnj 
2337140Sbostic #include <sys/param.h>
2437140Sbostic #include <fcntl.h>
2537140Sbostic #include <time.h>
2637140Sbostic #include <tzfile.h>
2737140Sbostic #include <string.h>
2837140Sbostic #include <ctype.h>
2937140Sbostic #include <stdio.h>
3030608Sbostic 
3137140Sbostic #ifdef __STDC__
3237140Sbostic #include <stdlib.h>
3337140Sbostic 
3437140Sbostic #define P(s)		s
3537140Sbostic #define alloc_size_t	size_t
3637140Sbostic #define qsort_size_t	size_t
3737140Sbostic #define fread_size_t	size_t
3837140Sbostic #define fwrite_size_t	size_t
3937140Sbostic 
4037140Sbostic #else /* !defined __STDC__ */
4137140Sbostic 
4239720Sbostic #define P(s)		()
4337140Sbostic #define const
4437140Sbostic #define volatile
4537140Sbostic 
4637140Sbostic typedef char *		genericptr_t;
4737140Sbostic typedef unsigned	alloc_size_t;
4837140Sbostic typedef int		qsort_size_t;
4937140Sbostic typedef int		fread_size_t;
5037140Sbostic typedef int		fwrite_size_t;
5137140Sbostic 
5237140Sbostic extern char *	calloc();
5337140Sbostic extern char *	malloc();
5437140Sbostic extern char *	realloc();
5537140Sbostic extern char *	getenv();
5637140Sbostic 
5737140Sbostic #endif /* !defined __STDC__ */
5837140Sbostic 
5937140Sbostic extern time_t	time();
6037140Sbostic 
6137140Sbostic #define FILENAME_MAX	MAXPATHLEN
6237140Sbostic #define ACCESS_MODE	O_RDONLY
6337140Sbostic #define OPEN_MODE	O_RDONLY
6437140Sbostic 
6537140Sbostic #ifndef WILDABBR
661959Swnj /*
6737140Sbostic ** Someone might make incorrect use of a time zone abbreviation:
6837140Sbostic **	1.	They might reference tzname[0] before calling tzset (explicitly
6937140Sbostic **	 	or implicitly).
7037140Sbostic **	2.	They might reference tzname[1] before calling tzset (explicitly
7137140Sbostic **	 	or implicitly).
7237140Sbostic **	3.	They might reference tzname[1] after setting to a time zone
7337140Sbostic **		in which Daylight Saving Time is never observed.
7437140Sbostic **	4.	They might reference tzname[0] after setting to a time zone
7537140Sbostic **		in which Standard Time is never observed.
7637140Sbostic **	5.	They might reference tm.TM_ZONE after calling offtime.
7737140Sbostic ** What's best to do in the above cases is open to debate;
7837140Sbostic ** for now, we just set things up so that in any of the five cases
7937140Sbostic ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
8037140Sbostic ** string "tzname[0] used before set", and similarly for the other cases.
8137140Sbostic ** And another:  initialize tzname[0] to "ERA", with an explanation in the
8237140Sbostic ** manual page of what this "time zone abbreviation" means (doing this so
8337140Sbostic ** that tzname[0] has the "normal" length of three characters).
8430608Sbostic */
8537140Sbostic #define WILDABBR	"   "
8637140Sbostic #endif /* !defined WILDABBR */
871959Swnj 
8830608Sbostic #ifndef TRUE
8930608Sbostic #define TRUE		1
9030608Sbostic #define FALSE		0
9137140Sbostic #endif /* !defined TRUE */
9230608Sbostic 
9337140Sbostic static const char GMT[] = "GMT";
9430608Sbostic 
9530608Sbostic struct ttinfo {				/* time type information */
9630608Sbostic 	long		tt_gmtoff;	/* GMT offset in seconds */
9730608Sbostic 	int		tt_isdst;	/* used to set tm_isdst */
9830608Sbostic 	int		tt_abbrind;	/* abbreviation list index */
9937140Sbostic 	int		tt_ttisstd;	/* TRUE if transition is std time */
10013876Ssam };
10113876Ssam 
10237140Sbostic struct lsinfo {				/* leap second information */
10337140Sbostic 	time_t		ls_trans;	/* transition time */
10437140Sbostic 	long		ls_corr;	/* correction to apply */
10537140Sbostic };
10637140Sbostic 
10730608Sbostic struct state {
10837140Sbostic 	int		leapcnt;
10930608Sbostic 	int		timecnt;
11030608Sbostic 	int		typecnt;
11130608Sbostic 	int		charcnt;
11230608Sbostic 	time_t		ats[TZ_MAX_TIMES];
11330608Sbostic 	unsigned char	types[TZ_MAX_TIMES];
11430608Sbostic 	struct ttinfo	ttis[TZ_MAX_TYPES];
11537140Sbostic 	char		chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ?
11637140Sbostic 				TZ_MAX_CHARS + 1 : sizeof GMT];
11737140Sbostic 	struct lsinfo	lsis[TZ_MAX_LEAPS];
11823720Skre };
11923720Skre 
12037140Sbostic struct rule {
12137140Sbostic 	int		r_type;		/* type of rule--see below */
12237140Sbostic 	int		r_day;		/* day number of rule */
12337140Sbostic 	int		r_week;		/* week number of rule */
12437140Sbostic 	int		r_mon;		/* month number of rule */
12537140Sbostic 	long		r_time;		/* transition time of rule */
12637140Sbostic };
12730608Sbostic 
12837140Sbostic #define	JULIAN_DAY		0	/* Jn - Julian day */
12937140Sbostic #define	DAY_OF_YEAR		1	/* n - day of year */
13037140Sbostic #define	MONTH_NTH_DAY_OF_WEEK	2	/* Mm.n.d - month, week, day of week */
13130608Sbostic 
13237140Sbostic /*
13337140Sbostic ** Prototypes for static functions.
13437140Sbostic */
13537140Sbostic 
13637140Sbostic static long		detzcode P((const char * codep));
13737140Sbostic static const char *	getzname P((const char * strp));
13837140Sbostic static const char *	getnum P((const char * strp, int * nump, int min,
13937140Sbostic 				int max));
14037140Sbostic static const char *	getsecs P((const char * strp, long * secsp));
14137140Sbostic static const char *	getoffset P((const char * strp, long * offsetp));
14237140Sbostic static const char *	getrule P((const char * strp, struct rule * rulep));
14337140Sbostic static void		gmtload P((struct state * sp));
14437140Sbostic static void		gmtsub P((const time_t * timep, long offset,
14537140Sbostic 				struct tm * tmp));
14637140Sbostic static void		localsub P((const time_t * timep, long offset,
14737140Sbostic 				struct tm * tmp));
14837140Sbostic static void		normalize P((int * tensptr, int * unitsptr, int base));
14937140Sbostic static void		settzname P((void));
15037140Sbostic static time_t		time1 P((struct tm * tmp, void (* funcp)(),
15137140Sbostic 				long offset));
15237140Sbostic static time_t		time2 P((struct tm *tmp, void (* funcp)(),
15337140Sbostic 				long offset, int * okayp));
15437140Sbostic static void		timesub P((const time_t * timep, long offset,
15537140Sbostic 				const struct state * sp, struct tm * tmp));
15637140Sbostic static int		tmcomp P((const struct tm * atmp,
15737140Sbostic 				const struct tm * btmp));
15837140Sbostic static time_t		transtime P((time_t janfirst, int year,
15937140Sbostic 				const struct rule * rulep, long offset));
16037140Sbostic static int		tzload P((const char * name, struct state * sp));
16137140Sbostic static int		tzparse P((const char * name, struct state * sp,
16237140Sbostic 				int lastditch));
16337140Sbostic 
16437140Sbostic #ifdef ALL_STATE
16537140Sbostic static struct state *	lclptr;
16637140Sbostic static struct state *	gmtptr;
16737140Sbostic #endif /* defined ALL_STATE */
16837140Sbostic 
16937140Sbostic #ifndef ALL_STATE
17037140Sbostic static struct state	lclmem;
17137140Sbostic static struct state	gmtmem;
17237140Sbostic #define lclptr		(&lclmem)
17337140Sbostic #define gmtptr		(&gmtmem)
17437140Sbostic #endif /* State Farm */
17537140Sbostic 
17637140Sbostic static int		lcl_is_set;
17737140Sbostic static int		gmt_is_set;
17837140Sbostic 
17930608Sbostic char *			tzname[2] = {
18037140Sbostic 	WILDABBR,
18137140Sbostic 	WILDABBR
18212974Ssam };
18312974Ssam 
18430608Sbostic #ifdef USG_COMPAT
18530608Sbostic time_t			timezone = 0;
18630608Sbostic int			daylight = 0;
18737140Sbostic #endif /* defined USG_COMPAT */
1881959Swnj 
18937140Sbostic #ifdef ALTZONE
19037140Sbostic time_t			altzone = 0;
19137140Sbostic #endif /* defined ALTZONE */
19237140Sbostic 
19330608Sbostic static long
19430608Sbostic detzcode(codep)
19537140Sbostic const char * const	codep;
1961959Swnj {
19730608Sbostic 	register long	result;
19830608Sbostic 	register int	i;
19930608Sbostic 
20030608Sbostic 	result = 0;
20130608Sbostic 	for (i = 0; i < 4; ++i)
20230608Sbostic 		result = (result << 8) | (codep[i] & 0xff);
20330608Sbostic 	return result;
2041959Swnj }
2051959Swnj 
20637140Sbostic static void
20737140Sbostic settzname()
2081959Swnj {
20937140Sbostic 	register const struct state * const	sp = lclptr;
21037140Sbostic 	register int				i;
2111959Swnj 
21237140Sbostic 	tzname[0] = WILDABBR;
21337140Sbostic 	tzname[1] = WILDABBR;
21437140Sbostic #ifdef USG_COMPAT
21537140Sbostic 	daylight = 0;
21637140Sbostic 	timezone = 0;
21737140Sbostic #endif /* defined USG_COMPAT */
21837140Sbostic #ifdef ALTZONE
21937140Sbostic 	altzone = 0;
22037140Sbostic #endif /* defined ALTZONE */
22137140Sbostic #ifdef ALL_STATE
22237140Sbostic 	if (sp == NULL) {
22337140Sbostic 		tzname[0] = tzname[1] = GMT;
22437140Sbostic 		return;
22537140Sbostic 	}
22637140Sbostic #endif /* defined ALL_STATE */
22737140Sbostic 	for (i = 0; i < sp->typecnt; ++i) {
22837140Sbostic 		register const struct ttinfo * const	ttisp = &sp->ttis[i];
22937140Sbostic 
23037140Sbostic 		tzname[ttisp->tt_isdst] =
23137140Sbostic 			(char *) &sp->chars[ttisp->tt_abbrind];
23237140Sbostic #ifdef USG_COMPAT
23337140Sbostic 		if (ttisp->tt_isdst)
23437140Sbostic 			daylight = 1;
23537140Sbostic 		if (i == 0 || !ttisp->tt_isdst)
23637140Sbostic 			timezone = -(ttisp->tt_gmtoff);
23737140Sbostic #endif /* defined USG_COMPAT */
23837140Sbostic #ifdef ALTZONE
23937140Sbostic 		if (i == 0 || ttisp->tt_isdst)
24037140Sbostic 			altzone = -(ttisp->tt_gmtoff);
24137140Sbostic #endif /* defined ALTZONE */
24237140Sbostic 	}
24337140Sbostic 	/*
24437140Sbostic 	** And to get the latest zone names into tzname. . .
24537140Sbostic 	*/
24637140Sbostic 	for (i = 0; i < sp->timecnt; ++i) {
24737140Sbostic 		register const struct ttinfo * const	ttisp =
24837140Sbostic 							&sp->ttis[sp->types[i]];
24937140Sbostic 
25037140Sbostic 		tzname[ttisp->tt_isdst] =
25137140Sbostic 			(char *) &sp->chars[ttisp->tt_abbrind];
25237140Sbostic 	}
25337140Sbostic }
25437140Sbostic 
25537140Sbostic static int
25637140Sbostic tzload(name, sp)
25737140Sbostic register const char *		name;
25837140Sbostic register struct state * const	sp;
25937140Sbostic {
26037140Sbostic 	register const char *	p;
26137140Sbostic 	register int		i;
26237140Sbostic 	register int		fid;
26337140Sbostic 
26437140Sbostic 	if (name == NULL && (name = TZDEFAULT) == NULL)
26530608Sbostic 		return -1;
26630608Sbostic 	{
26737140Sbostic 		char		fullname[FILENAME_MAX + 1];
26830608Sbostic 
26937140Sbostic 		if (name[0] == ':')
27037140Sbostic 			++name;
27140007Sbostic 		if (name[0] != '/') {
27237140Sbostic 			if ((p = TZDIR) == NULL)
27330608Sbostic 				return -1;
27430608Sbostic 			if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
27530608Sbostic 				return -1;
27630608Sbostic 			(void) strcpy(fullname, p);
27730608Sbostic 			(void) strcat(fullname, "/");
27830608Sbostic 			(void) strcat(fullname, name);
27930608Sbostic 			name = fullname;
28030608Sbostic 		}
28137140Sbostic 		if ((fid = open(name, OPEN_MODE)) == -1)
28230608Sbostic 			return -1;
28325662Sbloom 	}
28430608Sbostic 	{
28537140Sbostic 		register const struct tzhead *	tzhp;
28637140Sbostic 		char				buf[sizeof *sp + sizeof *tzhp];
28737140Sbostic 		int				ttisstdcnt;
28830608Sbostic 
28930608Sbostic 		i = read(fid, buf, sizeof buf);
29030608Sbostic 		if (close(fid) != 0 || i < sizeof *tzhp)
29130608Sbostic 			return -1;
29230608Sbostic 		tzhp = (struct tzhead *) buf;
29337140Sbostic 		ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt);
29437140Sbostic 		sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt);
29537140Sbostic 		sp->timecnt = (int) detzcode(tzhp->tzh_timecnt);
29637140Sbostic 		sp->typecnt = (int) detzcode(tzhp->tzh_typecnt);
29737140Sbostic 		sp->charcnt = (int) detzcode(tzhp->tzh_charcnt);
29837140Sbostic 		if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
29937140Sbostic 			sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
30037140Sbostic 			sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
30137140Sbostic 			sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
30237140Sbostic 			(ttisstdcnt != sp->typecnt && ttisstdcnt != 0))
30330608Sbostic 				return -1;
30430608Sbostic 		if (i < sizeof *tzhp +
30537140Sbostic 			sp->timecnt * (4 + sizeof (char)) +
30637140Sbostic 			sp->typecnt * (4 + 2 * sizeof (char)) +
30737140Sbostic 			sp->charcnt * sizeof (char) +
30837140Sbostic 			sp->leapcnt * 2 * 4 +
30937140Sbostic 			ttisstdcnt * sizeof (char))
31030608Sbostic 				return -1;
31130608Sbostic 		p = buf + sizeof *tzhp;
31237140Sbostic 		for (i = 0; i < sp->timecnt; ++i) {
31337140Sbostic 			sp->ats[i] = detzcode(p);
31430608Sbostic 			p += 4;
31512974Ssam 		}
31637140Sbostic 		for (i = 0; i < sp->timecnt; ++i) {
31737140Sbostic 			sp->types[i] = (unsigned char) *p++;
31837140Sbostic 			if (sp->types[i] >= sp->typecnt)
31937140Sbostic 				return -1;
32037140Sbostic 		}
32137140Sbostic 		for (i = 0; i < sp->typecnt; ++i) {
32230608Sbostic 			register struct ttinfo *	ttisp;
32330608Sbostic 
32437140Sbostic 			ttisp = &sp->ttis[i];
32530608Sbostic 			ttisp->tt_gmtoff = detzcode(p);
32630608Sbostic 			p += 4;
32730608Sbostic 			ttisp->tt_isdst = (unsigned char) *p++;
32837140Sbostic 			if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
32937140Sbostic 				return -1;
33030608Sbostic 			ttisp->tt_abbrind = (unsigned char) *p++;
33137140Sbostic 			if (ttisp->tt_abbrind < 0 ||
33237140Sbostic 				ttisp->tt_abbrind > sp->charcnt)
33337140Sbostic 					return -1;
33430608Sbostic 		}
33537140Sbostic 		for (i = 0; i < sp->charcnt; ++i)
33637140Sbostic 			sp->chars[i] = *p++;
33737140Sbostic 		sp->chars[i] = '\0';	/* ensure '\0' at end */
33837140Sbostic 		for (i = 0; i < sp->leapcnt; ++i) {
33937140Sbostic 			register struct lsinfo *	lsisp;
34037140Sbostic 
34137140Sbostic 			lsisp = &sp->lsis[i];
34237140Sbostic 			lsisp->ls_trans = detzcode(p);
34337140Sbostic 			p += 4;
34437140Sbostic 			lsisp->ls_corr = detzcode(p);
34537140Sbostic 			p += 4;
34637140Sbostic 		}
34737140Sbostic 		for (i = 0; i < sp->typecnt; ++i) {
34837140Sbostic 			register struct ttinfo *	ttisp;
34937140Sbostic 
35037140Sbostic 			ttisp = &sp->ttis[i];
35137140Sbostic 			if (ttisstdcnt == 0)
35237140Sbostic 				ttisp->tt_ttisstd = FALSE;
35337140Sbostic 			else {
35437140Sbostic 				ttisp->tt_ttisstd = *p++;
35537140Sbostic 				if (ttisp->tt_ttisstd != TRUE &&
35637140Sbostic 					ttisp->tt_ttisstd != FALSE)
35737140Sbostic 						return -1;
35837140Sbostic 			}
35937140Sbostic 		}
3601959Swnj 	}
36137140Sbostic 	return 0;
36237140Sbostic }
36337140Sbostic 
36437140Sbostic static const int	mon_lengths[2][MONSPERYEAR] = {
36537140Sbostic 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
36637140Sbostic 	31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
36737140Sbostic };
36837140Sbostic 
36937140Sbostic static const int	year_lengths[2] = {
37037140Sbostic 	DAYSPERNYEAR, DAYSPERLYEAR
37137140Sbostic };
37237140Sbostic 
37337140Sbostic /*
37437140Sbostic ** Given a pointer into a time zone string, scan until a character that is not
37537140Sbostic ** a valid character in a zone name is found.  Return a pointer to that
37637140Sbostic ** character.
37737140Sbostic */
37837140Sbostic 
37937140Sbostic static const char *
38037140Sbostic getzname(strp)
38137140Sbostic register const char *	strp;
38237140Sbostic {
38337140Sbostic 	register char	c;
38437140Sbostic 
38537140Sbostic 	while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
38637140Sbostic 		c != '+')
38737140Sbostic 			++strp;
38837140Sbostic 	return strp;
38937140Sbostic }
39037140Sbostic 
39137140Sbostic /*
39237140Sbostic ** Given a pointer into a time zone string, extract a number from that string.
39337140Sbostic ** Check that the number is within a specified range; if it is not, return
39437140Sbostic ** NULL.
39537140Sbostic ** Otherwise, return a pointer to the first character not part of the number.
39637140Sbostic */
39737140Sbostic 
39837140Sbostic static const char *
39937140Sbostic getnum(strp, nump, min, max)
40037140Sbostic register const char *	strp;
40137140Sbostic int * const		nump;
40237140Sbostic const int		min;
40337140Sbostic const int		max;
40437140Sbostic {
40537140Sbostic 	register char	c;
40637140Sbostic 	register int	num;
40737140Sbostic 
40837140Sbostic 	if (strp == NULL || !isdigit(*strp))
40937140Sbostic 		return NULL;
41037140Sbostic 	num = 0;
41137140Sbostic 	while ((c = *strp) != '\0' && isdigit(c)) {
41237140Sbostic 		num = num * 10 + (c - '0');
41337140Sbostic 		if (num > max)
41437140Sbostic 			return NULL;	/* illegal value */
41537140Sbostic 		++strp;
41637140Sbostic 	}
41737140Sbostic 	if (num < min)
41837140Sbostic 		return NULL;		/* illegal value */
41937140Sbostic 	*nump = num;
42037140Sbostic 	return strp;
42137140Sbostic }
42237140Sbostic 
42337140Sbostic /*
42437140Sbostic ** Given a pointer into a time zone string, extract a number of seconds,
42537140Sbostic ** in hh[:mm[:ss]] form, from the string.
42637140Sbostic ** If any error occurs, return NULL.
42737140Sbostic ** Otherwise, return a pointer to the first character not part of the number
42837140Sbostic ** of seconds.
42937140Sbostic */
43037140Sbostic 
43137140Sbostic static const char *
43237140Sbostic getsecs(strp, secsp)
43337140Sbostic register const char *	strp;
43437140Sbostic long * const		secsp;
43537140Sbostic {
43637140Sbostic 	int	num;
43737140Sbostic 
43837140Sbostic 	strp = getnum(strp, &num, 0, HOURSPERDAY);
43937140Sbostic 	if (strp == NULL)
44037140Sbostic 		return NULL;
44137140Sbostic 	*secsp = num * SECSPERHOUR;
44237140Sbostic 	if (*strp == ':') {
44337140Sbostic 		++strp;
44437140Sbostic 		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
44537140Sbostic 		if (strp == NULL)
44637140Sbostic 			return NULL;
44737140Sbostic 		*secsp += num * SECSPERMIN;
44837140Sbostic 		if (*strp == ':') {
44937140Sbostic 			++strp;
45037140Sbostic 			strp = getnum(strp, &num, 0, SECSPERMIN - 1);
45137140Sbostic 			if (strp == NULL)
45237140Sbostic 				return NULL;
45337140Sbostic 			*secsp += num;
45437140Sbostic 		}
45537140Sbostic 	}
45637140Sbostic 	return strp;
45737140Sbostic }
45837140Sbostic 
45937140Sbostic /*
46037140Sbostic ** Given a pointer into a time zone string, extract an offset, in
46137140Sbostic ** [+-]hh[:mm[:ss]] form, from the string.
46237140Sbostic ** If any error occurs, return NULL.
46337140Sbostic ** Otherwise, return a pointer to the first character not part of the time.
46437140Sbostic */
46537140Sbostic 
46637140Sbostic static const char *
46737140Sbostic getoffset(strp, offsetp)
46837140Sbostic register const char *	strp;
46937140Sbostic long * const		offsetp;
47037140Sbostic {
47137140Sbostic 	register int	neg;
47237140Sbostic 
47337140Sbostic 	if (*strp == '-') {
47437140Sbostic 		neg = 1;
47537140Sbostic 		++strp;
47637140Sbostic 	} else if (isdigit(*strp) || *strp++ == '+')
47737140Sbostic 		neg = 0;
47837140Sbostic 	else	return NULL;		/* illegal offset */
47937140Sbostic 	strp = getsecs(strp, offsetp);
48037140Sbostic 	if (strp == NULL)
48137140Sbostic 		return NULL;		/* illegal time */
48237140Sbostic 	if (neg)
48337140Sbostic 		*offsetp = -*offsetp;
48437140Sbostic 	return strp;
48537140Sbostic }
48637140Sbostic 
48737140Sbostic /*
48837140Sbostic ** Given a pointer into a time zone string, extract a rule in the form
48937140Sbostic ** date[/time].  See POSIX section 8 for the format of "date" and "time".
49037140Sbostic ** If a valid rule is not found, return NULL.
49137140Sbostic ** Otherwise, return a pointer to the first character not part of the rule.
49237140Sbostic */
49337140Sbostic 
49437140Sbostic static const char *
49537140Sbostic getrule(strp, rulep)
49637140Sbostic const char *			strp;
49737140Sbostic register struct rule * const	rulep;
49837140Sbostic {
49937140Sbostic 	if (*strp == 'J') {
50037140Sbostic 		/*
50137140Sbostic 		** Julian day.
50237140Sbostic 		*/
50337140Sbostic 		rulep->r_type = JULIAN_DAY;
50437140Sbostic 		++strp;
50537140Sbostic 		strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
50637140Sbostic 	} else if (*strp == 'M') {
50737140Sbostic 		/*
50837140Sbostic 		** Month, week, day.
50937140Sbostic 		*/
51037140Sbostic 		rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
51137140Sbostic 		++strp;
51237140Sbostic 		strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
51337140Sbostic 		if (strp == NULL)
51437140Sbostic 			return NULL;
51537140Sbostic 		if (*strp++ != '.')
51637140Sbostic 			return NULL;
51737140Sbostic 		strp = getnum(strp, &rulep->r_week, 1, 5);
51837140Sbostic 		if (strp == NULL)
51937140Sbostic 			return NULL;
52037140Sbostic 		if (*strp++ != '.')
52137140Sbostic 			return NULL;
52237140Sbostic 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
52337140Sbostic 	} else if (isdigit(*strp)) {
52437140Sbostic 		/*
52537140Sbostic 		** Day of year.
52637140Sbostic 		*/
52737140Sbostic 		rulep->r_type = DAY_OF_YEAR;
52837140Sbostic 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
52937140Sbostic 	} else	return NULL;		/* invalid format */
53037140Sbostic 	if (strp == NULL)
53137140Sbostic 		return NULL;
53237140Sbostic 	if (*strp == '/') {
53337140Sbostic 		/*
53437140Sbostic 		** Time specified.
53537140Sbostic 		*/
53637140Sbostic 		++strp;
53737140Sbostic 		strp = getsecs(strp, &rulep->r_time);
53837140Sbostic 	} else	rulep->r_time = 2 * SECSPERHOUR;	/* default = 2:00:00 */
53937140Sbostic 	return strp;
54037140Sbostic }
54137140Sbostic 
54237140Sbostic /*
54337140Sbostic ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
54437140Sbostic ** year, a rule, and the offset from GMT at the time that rule takes effect,
54537140Sbostic ** calculate the Epoch-relative time that rule takes effect.
54637140Sbostic */
54737140Sbostic 
54837140Sbostic static time_t
54937140Sbostic transtime(janfirst, year, rulep, offset)
55037140Sbostic const time_t				janfirst;
55137140Sbostic const int				year;
55237140Sbostic register const struct rule * const	rulep;
55337140Sbostic const long				offset;
55437140Sbostic {
55537140Sbostic 	register int	leapyear;
55637140Sbostic 	register time_t	value;
55737140Sbostic 	register int	i;
55837140Sbostic 	int		d, m1, yy0, yy1, yy2, dow;
55937140Sbostic 
56037140Sbostic 	leapyear = isleap(year);
56137140Sbostic 	switch (rulep->r_type) {
56237140Sbostic 
56337140Sbostic 	case JULIAN_DAY:
56437140Sbostic 		/*
56537140Sbostic 		** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
56637140Sbostic 		** years.
56737140Sbostic 		** In non-leap years, or if the day number is 59 or less, just
56837140Sbostic 		** add SECSPERDAY times the day number-1 to the time of
56937140Sbostic 		** January 1, midnight, to get the day.
57037140Sbostic 		*/
57137140Sbostic 		value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
57237140Sbostic 		if (leapyear && rulep->r_day >= 60)
57337140Sbostic 			value += SECSPERDAY;
57437140Sbostic 		break;
57537140Sbostic 
57637140Sbostic 	case DAY_OF_YEAR:
57737140Sbostic 		/*
57837140Sbostic 		** n - day of year.
57937140Sbostic 		** Just add SECSPERDAY times the day number to the time of
58037140Sbostic 		** January 1, midnight, to get the day.
58137140Sbostic 		*/
58237140Sbostic 		value = janfirst + rulep->r_day * SECSPERDAY;
58337140Sbostic 		break;
58437140Sbostic 
58537140Sbostic 	case MONTH_NTH_DAY_OF_WEEK:
58637140Sbostic 		/*
58737140Sbostic 		** Mm.n.d - nth "dth day" of month m.
58837140Sbostic 		*/
58937140Sbostic 		value = janfirst;
59037140Sbostic 		for (i = 0; i < rulep->r_mon - 1; ++i)
59137140Sbostic 			value += mon_lengths[leapyear][i] * SECSPERDAY;
59237140Sbostic 
59337140Sbostic 		/*
59437140Sbostic 		** Use Zeller's Congruence to get day-of-week of first day of
59537140Sbostic 		** month.
59637140Sbostic 		*/
59737140Sbostic 		m1 = (rulep->r_mon + 9) % 12 + 1;
59837140Sbostic 		yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
59937140Sbostic 		yy1 = yy0 / 100;
60037140Sbostic 		yy2 = yy0 % 100;
60137140Sbostic 		dow = ((26 * m1 - 2) / 10 +
60237140Sbostic 			1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
60337140Sbostic 		if (dow < 0)
60437140Sbostic 			dow += DAYSPERWEEK;
60537140Sbostic 
60637140Sbostic 		/*
60737140Sbostic 		** "dow" is the day-of-week of the first day of the month.  Get
60837140Sbostic 		** the day-of-month (zero-origin) of the first "dow" day of the
60937140Sbostic 		** month.
61037140Sbostic 		*/
61137140Sbostic 		d = rulep->r_day - dow;
61237140Sbostic 		if (d < 0)
61337140Sbostic 			d += DAYSPERWEEK;
61437140Sbostic 		for (i = 1; i < rulep->r_week; ++i) {
61537140Sbostic 			if (d + DAYSPERWEEK >=
61637140Sbostic 				mon_lengths[leapyear][rulep->r_mon - 1])
61737140Sbostic 					break;
61837140Sbostic 			d += DAYSPERWEEK;
61937140Sbostic 		}
62037140Sbostic 
62137140Sbostic 		/*
62237140Sbostic 		** "d" is the day-of-month (zero-origin) of the day we want.
62337140Sbostic 		*/
62437140Sbostic 		value += d * SECSPERDAY;
62537140Sbostic 		break;
62637140Sbostic 	}
62737140Sbostic 
62830608Sbostic 	/*
62937140Sbostic 	** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
63037140Sbostic 	** question.  To get the Epoch-relative time of the specified local
63137140Sbostic 	** time on that day, add the transition time and the current offset
63237140Sbostic 	** from GMT.
63330608Sbostic 	*/
63437140Sbostic 	return value + rulep->r_time + offset;
63537140Sbostic }
63637140Sbostic 
63737140Sbostic /*
63837140Sbostic ** Given a POSIX section 8-style TZ string, fill in the rule tables as
63937140Sbostic ** appropriate.
64037140Sbostic */
64137140Sbostic 
64237140Sbostic static int
64337140Sbostic tzparse(name, sp, lastditch)
64437140Sbostic const char *			name;
64537140Sbostic register struct state * const	sp;
64637140Sbostic const int			lastditch;
64737140Sbostic {
64837140Sbostic 	const char *			stdname;
64937140Sbostic 	const char *			dstname;
65037140Sbostic 	int				stdlen;
65137140Sbostic 	int				dstlen;
65237140Sbostic 	long				stdoffset;
65337140Sbostic 	long				dstoffset;
65437140Sbostic 	register time_t *		atp;
65537140Sbostic 	register unsigned char *	typep;
65637140Sbostic 	register char *			cp;
65737140Sbostic 	register int			load_result;
65837140Sbostic 
65937140Sbostic 	stdname = name;
66037140Sbostic 	if (lastditch) {
66137140Sbostic 		stdlen = strlen(name);	/* length of standard zone name */
66237140Sbostic 		name += stdlen;
66337140Sbostic 		if (stdlen >= sizeof sp->chars)
66437140Sbostic 			stdlen = (sizeof sp->chars) - 1;
66537140Sbostic 	} else {
66637140Sbostic 		name = getzname(name);
66737140Sbostic 		stdlen = name - stdname;
66837140Sbostic 		if (stdlen < 3)
66930608Sbostic 			return -1;
67037140Sbostic 	}
67137140Sbostic 	if (*name == '\0')
67239105Sbostic 		return -1;
67337140Sbostic 	else {
67437140Sbostic 		name = getoffset(name, &stdoffset);
67537140Sbostic 		if (name == NULL)
67630608Sbostic 			return -1;
67737140Sbostic 	}
67837140Sbostic 	load_result = tzload(TZDEFRULES, sp);
67937140Sbostic 	if (load_result != 0)
68037140Sbostic 		sp->leapcnt = 0;		/* so, we're off a little */
68137140Sbostic 	if (*name != '\0') {
68237140Sbostic 		dstname = name;
68337140Sbostic 		name = getzname(name);
68437140Sbostic 		dstlen = name - dstname;	/* length of DST zone name */
68537140Sbostic 		if (dstlen < 3)
68637140Sbostic 			return -1;
68737140Sbostic 		if (*name != '\0' && *name != ',' && *name != ';') {
68837140Sbostic 			name = getoffset(name, &dstoffset);
68937140Sbostic 			if (name == NULL)
69037140Sbostic 				return -1;
69137140Sbostic 		} else	dstoffset = stdoffset - SECSPERHOUR;
69237140Sbostic 		if (*name == ',' || *name == ';') {
69337140Sbostic 			struct rule	start;
69437140Sbostic 			struct rule	end;
69537140Sbostic 			register int	year;
69637140Sbostic 			register time_t	janfirst;
69737140Sbostic 			time_t		starttime;
69837140Sbostic 			time_t		endtime;
69930608Sbostic 
70037140Sbostic 			++name;
70137140Sbostic 			if ((name = getrule(name, &start)) == NULL)
70237140Sbostic 				return -1;
70337140Sbostic 			if (*name++ != ',')
70437140Sbostic 				return -1;
70537140Sbostic 			if ((name = getrule(name, &end)) == NULL)
70637140Sbostic 				return -1;
70737140Sbostic 			if (*name != '\0')
70837140Sbostic 				return -1;
70937140Sbostic 			sp->typecnt = 2;	/* standard time and DST */
71037140Sbostic 			/*
71137140Sbostic 			** Two transitions per year, from EPOCH_YEAR to 2037.
71237140Sbostic 			*/
71337140Sbostic 			sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
71437140Sbostic 			if (sp->timecnt > TZ_MAX_TIMES)
71537140Sbostic 				return -1;
71637140Sbostic 			sp->ttis[0].tt_gmtoff = -dstoffset;
71737140Sbostic 			sp->ttis[0].tt_isdst = 1;
71837140Sbostic 			sp->ttis[0].tt_abbrind = stdlen + 1;
71937140Sbostic 			sp->ttis[1].tt_gmtoff = -stdoffset;
72037140Sbostic 			sp->ttis[1].tt_isdst = 0;
72137140Sbostic 			sp->ttis[1].tt_abbrind = 0;
72237140Sbostic 			atp = sp->ats;
72337140Sbostic 			typep = sp->types;
72437140Sbostic 			janfirst = 0;
72537140Sbostic 			for (year = EPOCH_YEAR; year <= 2037; ++year) {
72637140Sbostic 				starttime = transtime(janfirst, year, &start,
72737140Sbostic 					stdoffset);
72837140Sbostic 				endtime = transtime(janfirst, year, &end,
72937140Sbostic 					dstoffset);
73037140Sbostic 				if (starttime > endtime) {
73137140Sbostic 					*atp++ = endtime;
73237140Sbostic 					*typep++ = 1;	/* DST ends */
73337140Sbostic 					*atp++ = starttime;
73437140Sbostic 					*typep++ = 0;	/* DST begins */
73537140Sbostic 				} else {
73637140Sbostic 					*atp++ = starttime;
73737140Sbostic 					*typep++ = 0;	/* DST begins */
73837140Sbostic 					*atp++ = endtime;
73937140Sbostic 					*typep++ = 1;	/* DST ends */
74037140Sbostic 				}
74137140Sbostic 				janfirst +=
74237140Sbostic 					year_lengths[isleap(year)] * SECSPERDAY;
74337140Sbostic 			}
74430608Sbostic 		} else {
74537140Sbostic 			int		sawstd;
74637140Sbostic 			int		sawdst;
74737140Sbostic 			long		stdfix;
74837140Sbostic 			long		dstfix;
74937140Sbostic 			long		oldfix;
75037140Sbostic 			int		isdst;
75137140Sbostic 			register int	i;
75237140Sbostic 
75337140Sbostic 			if (*name != '\0')
75437140Sbostic 				return -1;
75537140Sbostic 			if (load_result != 0)
75637140Sbostic 				return -1;
75737140Sbostic 			/*
75837140Sbostic 			** Compute the difference between the real and
75937140Sbostic 			** prototype standard and summer time offsets
76037140Sbostic 			** from GMT, and put the real standard and summer
76137140Sbostic 			** time offsets into the rules in place of the
76237140Sbostic 			** prototype offsets.
76337140Sbostic 			*/
76437140Sbostic 			sawstd = FALSE;
76537140Sbostic 			sawdst = FALSE;
76637140Sbostic 			stdfix = 0;
76737140Sbostic 			dstfix = 0;
76837140Sbostic 			for (i = 0; i < sp->typecnt; ++i) {
76937140Sbostic 				if (sp->ttis[i].tt_isdst) {
77037140Sbostic 					oldfix = dstfix;
77137140Sbostic 					dstfix =
77237140Sbostic 					    sp->ttis[i].tt_gmtoff + dstoffset;
77337140Sbostic 					if (sawdst && (oldfix != dstfix))
77437140Sbostic 						return -1;
77537140Sbostic 					sp->ttis[i].tt_gmtoff = -dstoffset;
77637140Sbostic 					sp->ttis[i].tt_abbrind = stdlen + 1;
77737140Sbostic 					sawdst = TRUE;
77837140Sbostic 				} else {
77937140Sbostic 					oldfix = stdfix;
78037140Sbostic 					stdfix =
78137140Sbostic 					    sp->ttis[i].tt_gmtoff + stdoffset;
78237140Sbostic 					if (sawstd && (oldfix != stdfix))
78337140Sbostic 						return -1;
78437140Sbostic 					sp->ttis[i].tt_gmtoff = -stdoffset;
78537140Sbostic 					sp->ttis[i].tt_abbrind = 0;
78637140Sbostic 					sawstd = TRUE;
78737140Sbostic 				}
78837140Sbostic 			}
78937140Sbostic 			/*
79037140Sbostic 			** Make sure we have both standard and summer time.
79137140Sbostic 			*/
79237140Sbostic 			if (!sawdst || !sawstd)
79337140Sbostic 				return -1;
79437140Sbostic 			/*
79537140Sbostic 			** Now correct the transition times by shifting
79637140Sbostic 			** them by the difference between the real and
79737140Sbostic 			** prototype offsets.  Note that this difference
79837140Sbostic 			** can be different in standard and summer time;
79937140Sbostic 			** the prototype probably has a 1-hour difference
80037140Sbostic 			** between standard and summer time, but a different
80137140Sbostic 			** difference can be specified in TZ.
80237140Sbostic 			*/
80337140Sbostic 			isdst = FALSE;	/* we start in standard time */
80437140Sbostic 			for (i = 0; i < sp->timecnt; ++i) {
80537140Sbostic 				register const struct ttinfo *	ttisp;
80637140Sbostic 
80737140Sbostic 				/*
80837140Sbostic 				** If summer time is in effect, and the
80937140Sbostic 				** transition time was not specified as
81037140Sbostic 				** standard time, add the summer time
81137140Sbostic 				** offset to the transition time;
81237140Sbostic 				** otherwise, add the standard time offset
81337140Sbostic 				** to the transition time.
81437140Sbostic 				*/
81537140Sbostic 				ttisp = &sp->ttis[sp->types[i]];
81637140Sbostic 				sp->ats[i] +=
81737140Sbostic 					(isdst && !ttisp->tt_ttisstd) ?
81837140Sbostic 						dstfix : stdfix;
81937140Sbostic 				isdst = ttisp->tt_isdst;
82037140Sbostic 			}
82130608Sbostic 		}
82237140Sbostic 	} else {
82337140Sbostic 		dstlen = 0;
82437140Sbostic 		sp->typecnt = 1;		/* only standard time */
82537140Sbostic 		sp->timecnt = 0;
82637140Sbostic 		sp->ttis[0].tt_gmtoff = -stdoffset;
82737140Sbostic 		sp->ttis[0].tt_isdst = 0;
82837140Sbostic 		sp->ttis[0].tt_abbrind = 0;
82930608Sbostic 	}
83037140Sbostic 	sp->charcnt = stdlen + 1;
83137140Sbostic 	if (dstlen != 0)
83237140Sbostic 		sp->charcnt += dstlen + 1;
83337140Sbostic 	if (sp->charcnt > sizeof sp->chars)
83430682Sbostic 		return -1;
83537140Sbostic 	cp = sp->chars;
83637140Sbostic 	(void) strncpy(cp, stdname, stdlen);
83737140Sbostic 	cp += stdlen;
83837140Sbostic 	*cp++ = '\0';
83937140Sbostic 	if (dstlen != 0) {
84037140Sbostic 		(void) strncpy(cp, dstname, dstlen);
84137140Sbostic 		*(cp + dstlen) = '\0';
84237140Sbostic 	}
84330682Sbostic 	return 0;
84430682Sbostic }
84530682Sbostic 
84637140Sbostic static void
84737140Sbostic gmtload(sp)
84837140Sbostic struct state * const	sp;
8491959Swnj {
85037140Sbostic 	if (tzload(GMT, sp) != 0)
85137140Sbostic 		(void) tzparse(GMT, sp, TRUE);
8521959Swnj }
8531959Swnj 
85430608Sbostic void
85530608Sbostic tzset()
85630608Sbostic {
85737140Sbostic 	register const char *	name;
85837140Sbostic 	void tzsetwall();
85930608Sbostic 
86030608Sbostic 	name = getenv("TZ");
86137140Sbostic 	if (name == NULL) {
86237140Sbostic 		tzsetwall();
86337140Sbostic 		return;
86437140Sbostic 	}
86537140Sbostic 	lcl_is_set = TRUE;
86637140Sbostic #ifdef ALL_STATE
86737140Sbostic 	if (lclptr == NULL) {
86837140Sbostic 		lclptr = (struct state *) malloc(sizeof *lclptr);
86937140Sbostic 		if (lclptr == NULL) {
87037140Sbostic 			settzname();	/* all we can do */
87130682Sbostic 			return;
87237140Sbostic 		}
87337140Sbostic 	}
87437140Sbostic #endif /* defined ALL_STATE */
87537140Sbostic 	if (*name == '\0') {
87637140Sbostic 		/*
87737140Sbostic 		** User wants it fast rather than right.
87837140Sbostic 		*/
87937140Sbostic 		lclptr->leapcnt = 0;		/* so, we're off a little */
88037140Sbostic 		lclptr->timecnt = 0;
88137140Sbostic 		lclptr->ttis[0].tt_gmtoff = 0;
88237140Sbostic 		lclptr->ttis[0].tt_abbrind = 0;
88337140Sbostic 		(void) strcpy(lclptr->chars, GMT);
88437140Sbostic 	} else if (tzload(name, lclptr) != 0)
88537140Sbostic 		if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
88639105Sbostic 			(void) gmtload(lclptr);
88737140Sbostic 	settzname();
88837140Sbostic }
88937140Sbostic 
89037140Sbostic void
89137140Sbostic tzsetwall()
89237140Sbostic {
89337140Sbostic 	lcl_is_set = TRUE;
89437140Sbostic #ifdef ALL_STATE
89537140Sbostic 	if (lclptr == NULL) {
89637140Sbostic 		lclptr = (struct state *) malloc(sizeof *lclptr);
89737140Sbostic 		if (lclptr == NULL) {
89837140Sbostic 			settzname();	/* all we can do */
89930682Sbostic 			return;
90037140Sbostic 		}
90130682Sbostic 	}
90237140Sbostic #endif /* defined ALL_STATE */
90337140Sbostic 	if (tzload((char *) NULL, lclptr) != 0)
90437140Sbostic 		gmtload(lclptr);
90537140Sbostic 	settzname();
90630608Sbostic }
90730608Sbostic 
90837140Sbostic /*
90937140Sbostic ** The easy way to behave "as if no library function calls" localtime
91037140Sbostic ** is to not call it--so we drop its guts into "localsub", which can be
91137140Sbostic ** freely called.  (And no, the PANS doesn't require the above behavior--
91237140Sbostic ** but it *is* desirable.)
91337140Sbostic **
91437140Sbostic ** The unused offset argument is for the benefit of mktime variants.
91537140Sbostic */
91637140Sbostic 
91737140Sbostic /*ARGSUSED*/
91837140Sbostic static void
91937140Sbostic localsub(timep, offset, tmp)
92037140Sbostic const time_t * const	timep;
92137140Sbostic const long		offset;
92237140Sbostic struct tm * const	tmp;
9231959Swnj {
92437140Sbostic 	register const struct state *	sp;
92537140Sbostic 	register const struct ttinfo *	ttisp;
92630608Sbostic 	register int			i;
92737140Sbostic 	const time_t			t = *timep;
9281959Swnj 
92937140Sbostic 	if (!lcl_is_set)
93037140Sbostic 		tzset();
93137140Sbostic 	sp = lclptr;
93237140Sbostic #ifdef ALL_STATE
93337140Sbostic 	if (sp == NULL) {
93437140Sbostic 		gmtsub(timep, offset, tmp);
93537140Sbostic 		return;
93637140Sbostic 	}
93737140Sbostic #endif /* defined ALL_STATE */
93837140Sbostic 	if (sp->timecnt == 0 || t < sp->ats[0]) {
93930608Sbostic 		i = 0;
94037140Sbostic 		while (sp->ttis[i].tt_isdst)
94137140Sbostic 			if (++i >= sp->typecnt) {
94230608Sbostic 				i = 0;
94330608Sbostic 				break;
94430608Sbostic 			}
94530608Sbostic 	} else {
94637140Sbostic 		for (i = 1; i < sp->timecnt; ++i)
94737140Sbostic 			if (t < sp->ats[i])
94830608Sbostic 				break;
94937140Sbostic 		i = sp->types[i - 1];
9501959Swnj 	}
95137140Sbostic 	ttisp = &sp->ttis[i];
9521959Swnj 	/*
95330608Sbostic 	** To get (wrong) behavior that's compatible with System V Release 2.0
95430608Sbostic 	** you'd replace the statement below with
95537140Sbostic 	**	t += ttisp->tt_gmtoff;
95637140Sbostic 	**	timesub(&t, 0L, sp, tmp);
95730608Sbostic 	*/
95837140Sbostic 	timesub(&t, ttisp->tt_gmtoff, sp, tmp);
95930608Sbostic 	tmp->tm_isdst = ttisp->tt_isdst;
96037140Sbostic 	tzname[tmp->tm_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
96137140Sbostic 	tmp->tm_zone = &sp->chars[ttisp->tt_abbrind];
96230608Sbostic }
9631959Swnj 
96430608Sbostic struct tm *
96537140Sbostic localtime(timep)
96637140Sbostic const time_t * const	timep;
96730608Sbostic {
96837140Sbostic 	static struct tm	tm;
9691959Swnj 
97037140Sbostic 	localsub(timep, 0L, &tm);
97137140Sbostic 	return &tm;
97230608Sbostic }
9731959Swnj 
97437140Sbostic /*
97537140Sbostic ** gmtsub is to gmtime as localsub is to localtime.
97637140Sbostic */
9771959Swnj 
97837140Sbostic static void
97937140Sbostic gmtsub(timep, offset, tmp)
98037140Sbostic const time_t * const	timep;
98137140Sbostic const long		offset;
98237140Sbostic struct tm * const	tmp;
98337140Sbostic {
98437140Sbostic 	if (!gmt_is_set) {
98537140Sbostic 		gmt_is_set = TRUE;
98637140Sbostic #ifdef ALL_STATE
98737140Sbostic 		gmtptr = (struct state *) malloc(sizeof *gmtptr);
98837140Sbostic 		if (gmtptr != NULL)
98937140Sbostic #endif /* defined ALL_STATE */
99037140Sbostic 			gmtload(gmtptr);
99137140Sbostic 	}
99237140Sbostic 	timesub(timep, offset, gmtptr, tmp);
99337140Sbostic 	/*
99437140Sbostic 	** Could get fancy here and deliver something such as
99537140Sbostic 	** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
99637140Sbostic 	** but this is no time for a treasure hunt.
99737140Sbostic 	*/
99837140Sbostic 	if (offset != 0)
99937140Sbostic 		tmp->tm_zone = WILDABBR;
100037140Sbostic 	else {
100137140Sbostic #ifdef ALL_STATE
100237140Sbostic 		if (gmtptr == NULL)
100337140Sbostic 			tmp->TM_ZONE = GMT;
100437140Sbostic 		else	tmp->TM_ZONE = gmtptr->chars;
100537140Sbostic #endif /* defined ALL_STATE */
100637140Sbostic #ifndef ALL_STATE
100737140Sbostic 		tmp->tm_zone = gmtptr->chars;
100837140Sbostic #endif /* State Farm */
100937140Sbostic 	}
101037140Sbostic }
10111959Swnj 
101230608Sbostic struct tm *
101337140Sbostic gmtime(timep)
101437140Sbostic const time_t * const	timep;
10151959Swnj {
101630608Sbostic 	static struct tm	tm;
10171959Swnj 
101837140Sbostic 	gmtsub(timep, 0L, &tm);
101937140Sbostic 	return &tm;
102037140Sbostic }
102137140Sbostic 
102237140Sbostic static void
102337140Sbostic timesub(timep, offset, sp, tmp)
102437140Sbostic const time_t * const			timep;
102537140Sbostic const long				offset;
102637140Sbostic register const struct state * const	sp;
102737140Sbostic register struct tm * const		tmp;
102837140Sbostic {
102937140Sbostic 	register const struct lsinfo *	lp;
103037140Sbostic 	register long			days;
103137140Sbostic 	register long			rem;
103237140Sbostic 	register int			y;
103337140Sbostic 	register int			yleap;
103437140Sbostic 	register const int *		ip;
103537140Sbostic 	register long			corr;
103637140Sbostic 	register int			hit;
103737140Sbostic 	register int			i;
103837140Sbostic 
103937140Sbostic 	corr = 0;
104037140Sbostic 	hit = FALSE;
104137140Sbostic #ifdef ALL_STATE
104237140Sbostic 	i = (sp == NULL) ? 0 : sp->leapcnt;
104337140Sbostic #endif /* defined ALL_STATE */
104437140Sbostic #ifndef ALL_STATE
104537140Sbostic 	i = sp->leapcnt;
104637140Sbostic #endif /* State Farm */
104737140Sbostic 	while (--i >= 0) {
104837140Sbostic 		lp = &sp->lsis[i];
104937140Sbostic 		if (*timep >= lp->ls_trans) {
105037140Sbostic 			if (*timep == lp->ls_trans)
105137140Sbostic 				hit = ((i == 0 && lp->ls_corr > 0) ||
105237140Sbostic 					lp->ls_corr > sp->lsis[i - 1].ls_corr);
105337140Sbostic 			corr = lp->ls_corr;
105437140Sbostic 			break;
105537140Sbostic 		}
105637140Sbostic 	}
105737140Sbostic 	days = *timep / SECSPERDAY;
105837140Sbostic 	rem = *timep % SECSPERDAY;
105937140Sbostic #ifdef mc68k
106037140Sbostic 	if (*timep == 0x80000000) {
106137140Sbostic 		/*
106237140Sbostic 		** A 3B1 muffs the division on the most negative number.
106337140Sbostic 		*/
106437140Sbostic 		days = -24855;
106537140Sbostic 		rem = -11648;
106637140Sbostic 	}
106737140Sbostic #endif /* mc68k */
106837140Sbostic 	rem += (offset - corr);
106930608Sbostic 	while (rem < 0) {
107037140Sbostic 		rem += SECSPERDAY;
107130608Sbostic 		--days;
10721959Swnj 	}
107337140Sbostic 	while (rem >= SECSPERDAY) {
107437140Sbostic 		rem -= SECSPERDAY;
107530608Sbostic 		++days;
107630608Sbostic 	}
107737140Sbostic 	tmp->tm_hour = (int) (rem / SECSPERHOUR);
107837140Sbostic 	rem = rem % SECSPERHOUR;
107937140Sbostic 	tmp->tm_min = (int) (rem / SECSPERMIN);
108037140Sbostic 	tmp->tm_sec = (int) (rem % SECSPERMIN);
108137140Sbostic 	if (hit)
108237140Sbostic 		/*
108337140Sbostic 		** A positive leap second requires a special
108437140Sbostic 		** representation.  This uses "... ??:59:60".
108537140Sbostic 		*/
108637140Sbostic 		++(tmp->tm_sec);
108737140Sbostic 	tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
108830608Sbostic 	if (tmp->tm_wday < 0)
108937140Sbostic 		tmp->tm_wday += DAYSPERWEEK;
109030608Sbostic 	y = EPOCH_YEAR;
109130608Sbostic 	if (days >= 0)
109230608Sbostic 		for ( ; ; ) {
109330608Sbostic 			yleap = isleap(y);
109430608Sbostic 			if (days < (long) year_lengths[yleap])
109530608Sbostic 				break;
109630608Sbostic 			++y;
109730608Sbostic 			days = days - (long) year_lengths[yleap];
109830608Sbostic 		}
109930608Sbostic 	else do {
110030608Sbostic 		--y;
110130608Sbostic 		yleap = isleap(y);
110230608Sbostic 		days = days + (long) year_lengths[yleap];
110330608Sbostic 	} while (days < 0);
110430608Sbostic 	tmp->tm_year = y - TM_YEAR_BASE;
110530608Sbostic 	tmp->tm_yday = (int) days;
110630608Sbostic 	ip = mon_lengths[yleap];
110730608Sbostic 	for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
110830608Sbostic 		days = days - (long) ip[tmp->tm_mon];
110930608Sbostic 	tmp->tm_mday = (int) (days + 1);
111030608Sbostic 	tmp->tm_isdst = 0;
111137140Sbostic #ifdef TM_GMTOFF
111237140Sbostic 	tmp->TM_GMTOFF = offset;
111337140Sbostic #endif /* defined TM_GMTOFF */
11141959Swnj }
111537140Sbostic 
111637140Sbostic /*
111737140Sbostic ** A la X3J11
111837140Sbostic */
111937140Sbostic 
112037140Sbostic char *
112137140Sbostic asctime(timeptr)
112237140Sbostic register const struct tm *	timeptr;
112337140Sbostic {
112437140Sbostic 	static const char	wday_name[DAYSPERWEEK][3] = {
112537140Sbostic 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
112637140Sbostic 	};
112737140Sbostic 	static const char	mon_name[MONSPERYEAR][3] = {
112837140Sbostic 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
112937140Sbostic 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
113037140Sbostic 	};
113137140Sbostic 	static char	result[26];
113237140Sbostic 
113337140Sbostic 	(void) sprintf(result, "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n",
113437140Sbostic 		wday_name[timeptr->tm_wday],
113537140Sbostic 		mon_name[timeptr->tm_mon],
113637140Sbostic 		timeptr->tm_mday, timeptr->tm_hour,
113737140Sbostic 		timeptr->tm_min, timeptr->tm_sec,
113837140Sbostic 		TM_YEAR_BASE + timeptr->tm_year);
113937140Sbostic 	return result;
114037140Sbostic }
114137140Sbostic 
114237140Sbostic char *
114337140Sbostic ctime(timep)
114437140Sbostic const time_t * const	timep;
114537140Sbostic {
114637140Sbostic 	return asctime(localtime(timep));
114737140Sbostic }
114837140Sbostic 
114937140Sbostic /*
115037140Sbostic ** Adapted from code provided by Robert Elz, who writes:
115137140Sbostic **	The "best" way to do mktime I think is based on an idea of Bob
115237140Sbostic **	Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
115337140Sbostic **	It does a binary search of the time_t space.  Since time_t's are
115437140Sbostic **	just 32 bits, its a max of 32 iterations (even at 64 bits it
115537140Sbostic **	would still be very reasonable).
115637140Sbostic */
115737140Sbostic 
115837140Sbostic #ifndef WRONG
115937140Sbostic #define WRONG	(-1)
116037140Sbostic #endif /* !defined WRONG */
116137140Sbostic 
116237140Sbostic static void
116337140Sbostic normalize(tensptr, unitsptr, base)
116437140Sbostic int * const	tensptr;
116537140Sbostic int * const	unitsptr;
116637140Sbostic const int	base;
116737140Sbostic {
116837140Sbostic 	if (*unitsptr >= base) {
116937140Sbostic 		*tensptr += *unitsptr / base;
117037140Sbostic 		*unitsptr %= base;
117137140Sbostic 	} else if (*unitsptr < 0) {
117237140Sbostic 		--*tensptr;
117337140Sbostic 		*unitsptr += base;
117437140Sbostic 		if (*unitsptr < 0) {
117537140Sbostic 			*tensptr -= 1 + (-*unitsptr) / base;
117637140Sbostic 			*unitsptr = base - (-*unitsptr) % base;
117737140Sbostic 		}
117837140Sbostic 	}
117937140Sbostic }
118037140Sbostic 
118137140Sbostic static int
118237140Sbostic tmcomp(atmp, btmp)
118337140Sbostic register const struct tm * const atmp;
118437140Sbostic register const struct tm * const btmp;
118537140Sbostic {
118637140Sbostic 	register int	result;
118737140Sbostic 
118837140Sbostic 	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
118937140Sbostic 		(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
119037140Sbostic 		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
119137140Sbostic 		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
119237140Sbostic 		(result = (atmp->tm_min - btmp->tm_min)) == 0)
119337140Sbostic 			result = atmp->tm_sec - btmp->tm_sec;
119437140Sbostic 	return result;
119537140Sbostic }
119637140Sbostic 
119737140Sbostic static time_t
119837140Sbostic time2(tmp, funcp, offset, okayp)
119937140Sbostic struct tm * const	tmp;
120037140Sbostic void (* const		funcp)();
120137140Sbostic const long		offset;
120237140Sbostic int * const		okayp;
120337140Sbostic {
120437140Sbostic 	register const struct state *	sp;
120537140Sbostic 	register int			dir;
120637140Sbostic 	register int			bits;
120737140Sbostic 	register int			i, j ;
120837140Sbostic 	register int			saved_seconds;
120937140Sbostic 	time_t				newt;
121037140Sbostic 	time_t				t;
121137140Sbostic 	struct tm			yourtm, mytm;
121237140Sbostic 
121337140Sbostic 	*okayp = FALSE;
121437140Sbostic 	yourtm = *tmp;
121537140Sbostic 	if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
121637140Sbostic 		normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
121737140Sbostic 	normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
121837140Sbostic 	normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
121937140Sbostic 	normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
122037140Sbostic 	while (yourtm.tm_mday <= 0) {
122137140Sbostic 		--yourtm.tm_year;
122237140Sbostic 		yourtm.tm_mday +=
122337140Sbostic 			year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
122437140Sbostic 	}
122537140Sbostic 	for ( ; ; ) {
122637140Sbostic 		i = mon_lengths[isleap(yourtm.tm_year +
122737140Sbostic 			TM_YEAR_BASE)][yourtm.tm_mon];
122837140Sbostic 		if (yourtm.tm_mday <= i)
122937140Sbostic 			break;
123037140Sbostic 		yourtm.tm_mday -= i;
123137140Sbostic 		if (++yourtm.tm_mon >= MONSPERYEAR) {
123237140Sbostic 			yourtm.tm_mon = 0;
123337140Sbostic 			++yourtm.tm_year;
123437140Sbostic 		}
123537140Sbostic 	}
123637140Sbostic 	saved_seconds = yourtm.tm_sec;
123737140Sbostic 	yourtm.tm_sec = 0;
123837140Sbostic 	/*
123937140Sbostic 	** Calculate the number of magnitude bits in a time_t
124037140Sbostic 	** (this works regardless of whether time_t is
124137140Sbostic 	** signed or unsigned, though lint complains if unsigned).
124237140Sbostic 	*/
124337140Sbostic 	for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
124437140Sbostic 		;
124537140Sbostic 	/*
124637140Sbostic 	** If time_t is signed, then 0 is the median value,
124737140Sbostic 	** if time_t is unsigned, then 1 << bits is median.
124837140Sbostic 	*/
124937140Sbostic 	t = (t < 0) ? 0 : ((time_t) 1 << bits);
125037140Sbostic 	for ( ; ; ) {
125137140Sbostic 		(*funcp)(&t, offset, &mytm);
125237140Sbostic 		dir = tmcomp(&mytm, &yourtm);
125337140Sbostic 		if (dir != 0) {
125437140Sbostic 			if (bits-- < 0)
125537140Sbostic 				return WRONG;
125637140Sbostic 			if (bits < 0)
125737140Sbostic 				--t;
125837140Sbostic 			else if (dir > 0)
125937140Sbostic 				t -= (time_t) 1 << bits;
126037140Sbostic 			else	t += (time_t) 1 << bits;
126137140Sbostic 			continue;
126237140Sbostic 		}
126337140Sbostic 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
126437140Sbostic 			break;
126537140Sbostic 		/*
126637140Sbostic 		** Right time, wrong type.
126737140Sbostic 		** Hunt for right time, right type.
126837140Sbostic 		** It's okay to guess wrong since the guess
126937140Sbostic 		** gets checked.
127037140Sbostic 		*/
127137140Sbostic 		sp = (const struct state *)
127237140Sbostic 			((funcp == localsub) ? lclptr : gmtptr);
127337140Sbostic #ifdef ALL_STATE
127437140Sbostic 		if (sp == NULL)
127537140Sbostic 			return WRONG;
127637140Sbostic #endif /* defined ALL_STATE */
127737140Sbostic 		for (i = 0; i < sp->typecnt; ++i) {
127837140Sbostic 			if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
127937140Sbostic 				continue;
128037140Sbostic 			for (j = 0; j < sp->typecnt; ++j) {
128137140Sbostic 				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
128237140Sbostic 					continue;
128337140Sbostic 				newt = t + sp->ttis[j].tt_gmtoff -
128437140Sbostic 					sp->ttis[i].tt_gmtoff;
128537140Sbostic 				(*funcp)(&newt, offset, &mytm);
128637140Sbostic 				if (tmcomp(&mytm, &yourtm) != 0)
128737140Sbostic 					continue;
128837140Sbostic 				if (mytm.tm_isdst != yourtm.tm_isdst)
128937140Sbostic 					continue;
129037140Sbostic 				/*
129137140Sbostic 				** We have a match.
129237140Sbostic 				*/
129337140Sbostic 				t = newt;
129437140Sbostic 				goto label;
129537140Sbostic 			}
129637140Sbostic 		}
129737140Sbostic 		return WRONG;
129837140Sbostic 	}
129937140Sbostic label:
130037140Sbostic 	t += saved_seconds;
130137140Sbostic 	(*funcp)(&t, offset, tmp);
130237140Sbostic 	*okayp = TRUE;
130337140Sbostic 	return t;
130437140Sbostic }
130537140Sbostic 
130637140Sbostic static time_t
130737140Sbostic time1(tmp, funcp, offset)
130837140Sbostic struct tm * const	tmp;
130937140Sbostic void (* const		funcp)();
131037140Sbostic const long		offset;
131137140Sbostic {
131237140Sbostic 	register time_t			t;
131337140Sbostic 	register const struct state *	sp;
131437140Sbostic 	register int			samei, otheri;
131537140Sbostic 	int				okay;
131637140Sbostic 
131737140Sbostic 	if (tmp->tm_isdst > 1)
131838558Sbostic 		tmp->tm_isdst = 1;
131937140Sbostic 	t = time2(tmp, funcp, offset, &okay);
132037140Sbostic 	if (okay || tmp->tm_isdst < 0)
132137140Sbostic 		return t;
132237140Sbostic 	/*
132337140Sbostic 	** We're supposed to assume that somebody took a time of one type
132437140Sbostic 	** and did some math on it that yielded a "struct tm" that's bad.
132537140Sbostic 	** We try to divine the type they started from and adjust to the
132637140Sbostic 	** type they need.
132737140Sbostic 	*/
132837140Sbostic 	sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
132937140Sbostic #ifdef ALL_STATE
133037140Sbostic 	if (sp == NULL)
133137140Sbostic 		return WRONG;
133237140Sbostic #endif /* defined ALL_STATE */
133337140Sbostic 	for (samei = 0; samei < sp->typecnt; ++samei) {
133437140Sbostic 		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
133537140Sbostic 			continue;
133637140Sbostic 		for (otheri = 0; otheri < sp->typecnt; ++otheri) {
133737140Sbostic 			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
133837140Sbostic 				continue;
133937140Sbostic 			tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
134037140Sbostic 					sp->ttis[samei].tt_gmtoff;
134137140Sbostic 			tmp->tm_isdst = !tmp->tm_isdst;
134237140Sbostic 			t = time2(tmp, funcp, offset, &okay);
134337140Sbostic 			if (okay)
134437140Sbostic 				return t;
134537140Sbostic 			tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
134637140Sbostic 					sp->ttis[samei].tt_gmtoff;
134737140Sbostic 			tmp->tm_isdst = !tmp->tm_isdst;
134837140Sbostic 		}
134937140Sbostic 	}
135037140Sbostic 	return WRONG;
135137140Sbostic }
135237140Sbostic 
135337140Sbostic time_t
135437140Sbostic mktime(tmp)
135537140Sbostic struct tm * const	tmp;
135637140Sbostic {
135737140Sbostic 	return time1(tmp, localsub, 0L);
135837140Sbostic }
1359