localtime.c revision 18834
1117632Sharti/*
2117632Sharti** This file is in the public domain, so clarified as of
3117632Sharti** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
4117632Sharti*/
5117632Sharti
6117632Sharti#ifndef lint
7117632Sharti#ifndef NOID
8117632Shartistatic char	elsieid[] = "@(#)localtime.c	7.57";
9117632Sharti#endif /* !defined NOID */
10117632Sharti#endif /* !defined lint */
11117632Sharti
12117632Sharti/*
13117632Sharti** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
14117632Sharti** POSIX-style TZ environment variable handling from Guy Harris
15117632Sharti** (guy@auspex.com).
16117632Sharti*/
17117632Sharti
18117632Sharti/*LINTLIBRARY*/
19117632Sharti
20117632Sharti#include <sys/types.h>
21117632Sharti#include <sys/stat.h>
22117632Sharti
23117632Sharti#include "private.h"
24117632Sharti#include "tzfile.h"
25117632Sharti#include "fcntl.h"
26117632Sharti#ifdef	_THREAD_SAFE
27117632Sharti#include <pthread.h>
28117632Sharti#include "pthread_private.h"
29117632Sharti#endif
30117632Sharti
31117632Sharti/*
32117632Sharti** SunOS 4.1.1 headers lack O_BINARY.
33117632Sharti*/
34117632Sharti
35117632Sharti#ifdef O_BINARY
36117632Sharti#define OPEN_MODE	(O_RDONLY | O_BINARY)
37117632Sharti#endif /* defined O_BINARY */
38117632Sharti#ifndef O_BINARY
39117632Sharti#define OPEN_MODE	O_RDONLY
40117632Sharti#endif /* !defined O_BINARY */
41117632Sharti
42117632Sharti#ifndef WILDABBR
43117632Sharti/*
44117632Sharti** Someone might make incorrect use of a time zone abbreviation:
45117632Sharti**	1.	They might reference tzname[0] before calling tzset (explicitly
46117632Sharti**		or implicitly).
47117632Sharti**	2.	They might reference tzname[1] before calling tzset (explicitly
48117632Sharti**		or implicitly).
49117632Sharti**	3.	They might reference tzname[1] after setting to a time zone
50117632Sharti**		in which Daylight Saving Time is never observed.
51117632Sharti**	4.	They might reference tzname[0] after setting to a time zone
52117632Sharti**		in which Standard Time is never observed.
53117632Sharti**	5.	They might reference tm.TM_ZONE after calling offtime.
54117632Sharti** What's best to do in the above cases is open to debate;
55117632Sharti** for now, we just set things up so that in any of the five cases
56117632Sharti** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
57117632Sharti** string "tzname[0] used before set", and similarly for the other cases.
58117632Sharti** And another:  initialize tzname[0] to "ERA", with an explanation in the
59117632Sharti** manual page of what this "time zone abbreviation" means (doing this so
60117632Sharti** that tzname[0] has the "normal" length of three characters).
61117632Sharti*/
62117632Sharti#define WILDABBR	"   "
63117632Sharti#endif /* !defined WILDABBR */
64117632Sharti
65117632Shartistatic char		wildabbr[] = "WILDABBR";
66117632Sharti
67117632Shartistatic const char	gmt[] = "GMT";
68117632Sharti
69117632Shartistruct ttinfo {				/* time type information */
70117632Sharti	long		tt_gmtoff;	/* GMT offset in seconds */
71117632Sharti	int		tt_isdst;	/* used to set tm_isdst */
72117632Sharti	int		tt_abbrind;	/* abbreviation list index */
73117632Sharti	int		tt_ttisstd;	/* TRUE if transition is std time */
74117632Sharti	int		tt_ttisgmt;	/* TRUE if transition is GMT */
75117632Sharti};
76117632Sharti
77117632Shartistruct lsinfo {				/* leap second information */
78117632Sharti	time_t		ls_trans;	/* transition time */
79117632Sharti	long		ls_corr;	/* correction to apply */
80117632Sharti};
81117632Sharti
82117632Sharti#define BIGGEST(a, b)	(((a) > (b)) ? (a) : (b))
83117632Sharti
84117632Sharti#ifdef TZNAME_MAX
85117632Sharti#define MY_TZNAME_MAX	TZNAME_MAX
86117632Sharti#endif /* defined TZNAME_MAX */
87117632Sharti#ifndef TZNAME_MAX
88117632Sharti#define MY_TZNAME_MAX	255
89117632Sharti#endif /* !defined TZNAME_MAX */
90117632Sharti
91117632Shartistruct state {
92117632Sharti	int		leapcnt;
93117632Sharti	int		timecnt;
94117632Sharti	int		typecnt;
95117632Sharti	int		charcnt;
96117632Sharti	time_t		ats[TZ_MAX_TIMES];
97117632Sharti	unsigned char	types[TZ_MAX_TIMES];
98117632Sharti	struct ttinfo	ttis[TZ_MAX_TYPES];
99117632Sharti	char		chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
100117632Sharti				(2 * (MY_TZNAME_MAX + 1)))];
101117632Sharti	struct lsinfo	lsis[TZ_MAX_LEAPS];
102117632Sharti};
103117632Sharti
104117632Shartistruct rule {
105117632Sharti	int		r_type;		/* type of rule--see below */
106117632Sharti	int		r_day;		/* day number of rule */
107117632Sharti	int		r_week;		/* week number of rule */
108117632Sharti	int		r_mon;		/* month number of rule */
109117632Sharti	long		r_time;		/* transition time of rule */
110117632Sharti};
111117632Sharti
112117632Sharti#define JULIAN_DAY		0	/* Jn - Julian day */
113117632Sharti#define DAY_OF_YEAR		1	/* n - day of year */
114117632Sharti#define MONTH_NTH_DAY_OF_WEEK	2	/* Mm.n.d - month, week, day of week */
115117632Sharti
116117632Sharti/*
117117632Sharti** Prototypes for static functions.
118117632Sharti*/
119117632Sharti
120117632Shartistatic long		detzcode P((const char * codep));
121117632Shartistatic const char *	getzname P((const char * strp));
122117632Shartistatic const char *	getnum P((const char * strp, int * nump, int min,
123117632Sharti				int max));
124117632Shartistatic const char *	getsecs P((const char * strp, long * secsp));
125117632Shartistatic const char *	getoffset P((const char * strp, long * offsetp));
126117632Shartistatic const char *	getrule P((const char * strp, struct rule * rulep));
127117632Shartistatic void		gmtload P((struct state * sp));
128117632Shartistatic void		gmtsub P((const time_t * timep, long offset,
129117632Sharti				struct tm * tmp));
130117632Shartistatic void		localsub P((const time_t * timep, long offset,
131117632Sharti				struct tm * tmp));
132117632Shartistatic int		increment_overflow P((int * number, int delta));
133117632Shartistatic int		normalize_overflow P((int * tensptr, int * unitsptr,
134117632Sharti				int base));
135117632Shartistatic void		settzname P((void));
136117632Shartistatic time_t		time1 P((struct tm * tmp,
137117632Sharti				void(*funcp) P((const time_t *,
138117632Sharti				long, struct tm *)),
139117632Sharti				long offset));
140117632Shartistatic time_t		time2 P((struct tm *tmp,
141117632Sharti				void(*funcp) P((const time_t *,
142117632Sharti				long, struct tm*)),
143117632Sharti				long offset, int * okayp));
144117632Shartistatic void		timesub P((const time_t * timep, long offset,
145117632Sharti				const struct state * sp, struct tm * tmp));
146117632Shartistatic int		tmcomp P((const struct tm * atmp,
147117632Sharti				const struct tm * btmp));
148117632Shartistatic time_t		transtime P((time_t janfirst, int year,
149117632Sharti				const struct rule * rulep, long offset));
150117632Shartistatic int		tzload P((const char * name, struct state * sp));
151117632Shartistatic int		tzparse P((const char * name, struct state * sp,
152117632Sharti				int lastditch));
153117632Sharti
154117632Sharti#ifdef ALL_STATE
155117632Shartistatic struct state *	lclptr;
156117632Shartistatic struct state *	gmtptr;
157117632Sharti#endif /* defined ALL_STATE */
158117632Sharti
159117632Sharti#ifndef ALL_STATE
160117632Shartistatic struct state	lclmem;
161117632Shartistatic struct state	gmtmem;
162117632Sharti#define lclptr		(&lclmem)
163117632Sharti#define gmtptr		(&gmtmem)
164117632Sharti#endif /* State Farm */
165117632Sharti
166117632Sharti#ifndef TZ_STRLEN_MAX
167117632Sharti#define TZ_STRLEN_MAX 255
168117632Sharti#endif /* !defined TZ_STRLEN_MAX */
169117632Sharti
170117632Shartistatic char		lcl_TZname[TZ_STRLEN_MAX + 1];
171117632Shartistatic int		lcl_is_set;
172117632Shartistatic int		gmt_is_set;
173117632Sharti#ifdef	_THREAD_SAFE
174117632Shartistatic struct pthread_mutex	_lcl_mutexd = PTHREAD_MUTEX_INITIALIZER;
175117632Shartistatic struct pthread_mutex	_gmt_mutexd = PTHREAD_MUTEX_INITIALIZER;
176117632Shartistatic pthread_mutex_t		lcl_mutex   = &_lcl_mutexd;
177117632Shartistatic pthread_mutex_t		gmt_mutex   = &_gmt_mutexd;
178117632Sharti#endif
179117632Sharti
180117632Shartichar *			tzname[2] = {
181117632Sharti	wildabbr,
182117632Sharti	wildabbr
183117632Sharti};
184117632Sharti
185117632Sharti/*
186117632Sharti** Section 4.12.3 of X3.159-1989 requires that
187117632Sharti**	Except for the strftime function, these functions [asctime,
188117632Sharti**	ctime, gmtime, localtime] return values in one of two static
189117632Sharti**	objects: a broken-down time structure and an array of char.
190117632Sharti** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
191117632Sharti*/
192117632Sharti
193117632Shartistatic struct tm	tm;
194117632Sharti
195117632Sharti#ifdef USG_COMPAT
196117632Shartitime_t			timezone = 0;
197117632Shartiint			daylight = 0;
198117632Sharti#endif /* defined USG_COMPAT */
199117632Sharti
200117632Sharti#ifdef ALTZONE
201117632Shartitime_t			altzone = 0;
202117632Sharti#endif /* defined ALTZONE */
203117632Sharti
204117632Shartistatic long
205117632Shartidetzcode(codep)
206117632Sharticonst char * const	codep;
207117632Sharti{
208117632Sharti	register long	result;
209117632Sharti	register int	i;
210117632Sharti
211117632Sharti	result = (codep[0] & 0x80) ? ~0L : 0L;
212117632Sharti	for (i = 0; i < 4; ++i)
213117632Sharti		result = (result << 8) | (codep[i] & 0xff);
214117632Sharti	return result;
215117632Sharti}
216117632Sharti
217117632Shartistatic void
218117632Shartisettzname P((void))
219117632Sharti{
220117632Sharti	register struct state * const	sp = lclptr;
221117632Sharti	register int			i;
222117632Sharti
223117632Sharti	tzname[0] = wildabbr;
224117632Sharti	tzname[1] = wildabbr;
225117632Sharti#ifdef USG_COMPAT
226117632Sharti	daylight = 0;
227117632Sharti	timezone = 0;
228117632Sharti#endif /* defined USG_COMPAT */
229117632Sharti#ifdef ALTZONE
230117632Sharti	altzone = 0;
231117632Sharti#endif /* defined ALTZONE */
232117632Sharti#ifdef ALL_STATE
233117632Sharti	if (sp == NULL) {
234117632Sharti		tzname[0] = tzname[1] = gmt;
235117632Sharti		return;
236117632Sharti	}
237117632Sharti#endif /* defined ALL_STATE */
238117632Sharti	for (i = 0; i < sp->typecnt; ++i) {
239117632Sharti		register const struct ttinfo * const	ttisp = &sp->ttis[i];
240117632Sharti
241117632Sharti		tzname[ttisp->tt_isdst] =
242117632Sharti			&sp->chars[ttisp->tt_abbrind];
243117632Sharti#ifdef USG_COMPAT
244117632Sharti		if (ttisp->tt_isdst)
245117632Sharti			daylight = 1;
246117632Sharti		if (i == 0 || !ttisp->tt_isdst)
247117632Sharti			timezone = -(ttisp->tt_gmtoff);
248117632Sharti#endif /* defined USG_COMPAT */
249117632Sharti#ifdef ALTZONE
250117632Sharti		if (i == 0 || ttisp->tt_isdst)
251117632Sharti			altzone = -(ttisp->tt_gmtoff);
252117632Sharti#endif /* defined ALTZONE */
253117632Sharti	}
254117632Sharti	/*
255117632Sharti	** And to get the latest zone names into tzname. . .
256117632Sharti	*/
257117632Sharti	for (i = 0; i < sp->timecnt; ++i) {
258117632Sharti		register const struct ttinfo * const	ttisp =
259117632Sharti							&sp->ttis[
260117632Sharti								sp->types[i]];
261117632Sharti
262117632Sharti		tzname[ttisp->tt_isdst] =
263117632Sharti			&sp->chars[ttisp->tt_abbrind];
264117632Sharti	}
265117632Sharti}
266117632Sharti
267117632Shartistatic int
268117632Shartitzload(name, sp)
269117632Shartiregister const char *		name;
270117632Shartiregister struct state * const	sp;
271117632Sharti{
272117632Sharti	register const char *	p;
273117632Sharti	register int		i;
274117632Sharti	register int		fid;
275117632Sharti
276117632Sharti	if (name == NULL && (name = TZDEFAULT) == NULL)
277117632Sharti		return -1;
278117632Sharti	{
279117632Sharti		register int	doaccess;
280117632Sharti		struct stat	stab;
281117632Sharti		/*
282117632Sharti		** Section 4.9.1 of the C standard says that
283117632Sharti		** "FILENAME_MAX expands to an integral constant expression
284117632Sharti		** that is the size needed for an array of char large enough
285117632Sharti		** to hold the longest file name string that the implementation
286117632Sharti		** guarantees can be opened."
287117632Sharti		*/
288117632Sharti		char		fullname[FILENAME_MAX + 1];
289117632Sharti
290117632Sharti		if (name[0] == ':')
291117632Sharti			++name;
292117632Sharti		doaccess = name[0] == '/';
293117632Sharti		if (!doaccess) {
294117632Sharti			if ((p = TZDIR) == NULL)
295117632Sharti				return -1;
296117632Sharti			if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
297117632Sharti				return -1;
298117632Sharti			(void) strcpy(fullname, p);
299117632Sharti			(void) strcat(fullname, "/");
300117632Sharti			(void) strcat(fullname, name);
301117632Sharti			/*
302117632Sharti			** Set doaccess if '.' (as in "../") shows up in name.
303117632Sharti			*/
304117632Sharti			if (strchr(name, '.') != NULL)
305117632Sharti				doaccess = TRUE;
306117632Sharti			name = fullname;
307117632Sharti		}
308117632Sharti		if (doaccess && access(name, R_OK) != 0)
309117632Sharti			return -1;
310117632Sharti		if ((fid = open(name, OPEN_MODE)) == -1)
311117632Sharti			return -1;
312117632Sharti		if ((fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode))
313117632Sharti			return -1;
314117632Sharti	}
315117632Sharti	{
316117632Sharti		struct tzhead *	tzhp;
317117632Sharti		char		buf[sizeof *sp + sizeof *tzhp];
318117632Sharti		int		ttisstdcnt;
319117632Sharti		int		ttisgmtcnt;
320117632Sharti
321117632Sharti		i = read(fid, buf, sizeof buf);
322117632Sharti		if (close(fid) != 0)
323117632Sharti			return -1;
324117632Sharti		p = buf;
325117632Sharti		p += sizeof tzhp->tzh_reserved;
326117632Sharti		ttisstdcnt = (int) detzcode(p);
327117632Sharti		p += 4;
328117632Sharti		ttisgmtcnt = (int) detzcode(p);
329117632Sharti		p += 4;
330117632Sharti		sp->leapcnt = (int) detzcode(p);
331117632Sharti		p += 4;
332117632Sharti		sp->timecnt = (int) detzcode(p);
333117632Sharti		p += 4;
334117632Sharti		sp->typecnt = (int) detzcode(p);
335117632Sharti		p += 4;
336117632Sharti		sp->charcnt = (int) detzcode(p);
337117632Sharti		p += 4;
338117632Sharti		if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
339117632Sharti			sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
340117632Sharti			sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
341117632Sharti			sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
342117632Sharti			(ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
343117632Sharti			(ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
344117632Sharti				return -1;
345117632Sharti		if (i - (p - buf) < sp->timecnt * 4 +	/* ats */
346117632Sharti			sp->timecnt +			/* types */
347117632Sharti			sp->typecnt * (4 + 2) +		/* ttinfos */
348117632Sharti			sp->charcnt +			/* chars */
349117632Sharti			sp->leapcnt * (4 + 4) +		/* lsinfos */
350117632Sharti			ttisstdcnt +			/* ttisstds */
351117632Sharti			ttisgmtcnt)			/* ttisgmts */
352117632Sharti				return -1;
353117632Sharti		for (i = 0; i < sp->timecnt; ++i) {
354117632Sharti			sp->ats[i] = detzcode(p);
355117632Sharti			p += 4;
356117632Sharti		}
357117632Sharti		for (i = 0; i < sp->timecnt; ++i) {
358117632Sharti			sp->types[i] = (unsigned char) *p++;
359117632Sharti			if (sp->types[i] >= sp->typecnt)
360117632Sharti				return -1;
361117632Sharti		}
362117632Sharti		for (i = 0; i < sp->typecnt; ++i) {
363117632Sharti			register struct ttinfo *	ttisp;
364117632Sharti
365117632Sharti			ttisp = &sp->ttis[i];
366117632Sharti			ttisp->tt_gmtoff = detzcode(p);
367117632Sharti			p += 4;
368117632Sharti			ttisp->tt_isdst = (unsigned char) *p++;
369117632Sharti			if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
370117632Sharti				return -1;
371117632Sharti			ttisp->tt_abbrind = (unsigned char) *p++;
372117632Sharti			if (ttisp->tt_abbrind < 0 ||
373117632Sharti				ttisp->tt_abbrind > sp->charcnt)
374117632Sharti					return -1;
375117632Sharti		}
376117632Sharti		for (i = 0; i < sp->charcnt; ++i)
377117632Sharti			sp->chars[i] = *p++;
378117632Sharti		sp->chars[i] = '\0';	/* ensure '\0' at end */
379117632Sharti		for (i = 0; i < sp->leapcnt; ++i) {
380117632Sharti			register struct lsinfo *	lsisp;
381117632Sharti
382117632Sharti			lsisp = &sp->lsis[i];
383117632Sharti			lsisp->ls_trans = detzcode(p);
384117632Sharti			p += 4;
385117632Sharti			lsisp->ls_corr = detzcode(p);
386117632Sharti			p += 4;
387117632Sharti		}
388117632Sharti		for (i = 0; i < sp->typecnt; ++i) {
389117632Sharti			register struct ttinfo *	ttisp;
390117632Sharti
391117632Sharti			ttisp = &sp->ttis[i];
392117632Sharti			if (ttisstdcnt == 0)
393117632Sharti				ttisp->tt_ttisstd = FALSE;
394117632Sharti			else {
395117632Sharti				ttisp->tt_ttisstd = *p++;
396117632Sharti				if (ttisp->tt_ttisstd != TRUE &&
397117632Sharti					ttisp->tt_ttisstd != FALSE)
398117632Sharti						return -1;
399117632Sharti			}
400117632Sharti		}
401117632Sharti		for (i = 0; i < sp->typecnt; ++i) {
402117632Sharti			register struct ttinfo *	ttisp;
403117632Sharti
404117632Sharti			ttisp = &sp->ttis[i];
405117632Sharti			if (ttisgmtcnt == 0)
406117632Sharti				ttisp->tt_ttisgmt = FALSE;
407117632Sharti			else {
408117632Sharti				ttisp->tt_ttisgmt = *p++;
409117632Sharti				if (ttisp->tt_ttisgmt != TRUE &&
410117632Sharti					ttisp->tt_ttisgmt != FALSE)
411117632Sharti						return -1;
412117632Sharti			}
413117632Sharti		}
414117632Sharti	}
415117632Sharti	return 0;
416117632Sharti}
417117632Sharti
418117632Shartistatic const int	mon_lengths[2][MONSPERYEAR] = {
419117632Sharti	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
420117632Sharti	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
421117632Sharti};
422117632Sharti
423117632Shartistatic const int	year_lengths[2] = {
424117632Sharti	DAYSPERNYEAR, DAYSPERLYEAR
425117632Sharti};
426117632Sharti
427117632Sharti/*
428117632Sharti** Given a pointer into a time zone string, scan until a character that is not
429117632Sharti** a valid character in a zone name is found.  Return a pointer to that
430117632Sharti** character.
431117632Sharti*/
432117632Sharti
433117632Shartistatic const char *
434117632Shartigetzname(strp)
435117632Shartiregister const char *	strp;
436117632Sharti{
437117632Sharti	register char	c;
438117632Sharti
439117632Sharti	while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
440117632Sharti		c != '+')
441117632Sharti			++strp;
442117632Sharti	return strp;
443117632Sharti}
444117632Sharti
445117632Sharti/*
446117632Sharti** Given a pointer into a time zone string, extract a number from that string.
447117632Sharti** Check that the number is within a specified range; if it is not, return
448117632Sharti** NULL.
449117632Sharti** Otherwise, return a pointer to the first character not part of the number.
450117632Sharti*/
451117632Sharti
452117632Shartistatic const char *
453117632Shartigetnum(strp, nump, min, max)
454117632Shartiregister const char *	strp;
455117632Shartiint * const		nump;
456117632Sharticonst int		min;
457117632Sharticonst int		max;
458117632Sharti{
459117632Sharti	register char	c;
460117632Sharti	register int	num;
461117632Sharti
462117632Sharti	if (strp == NULL || !is_digit(c = *strp))
463117632Sharti		return NULL;
464117632Sharti	num = 0;
465117632Sharti	do {
466117632Sharti		num = num * 10 + (c - '0');
467117632Sharti		if (num > max)
468117632Sharti			return NULL;	/* illegal value */
469117632Sharti		c = *++strp;
470117632Sharti	} while (is_digit(c));
471117632Sharti	if (num < min)
472117632Sharti		return NULL;		/* illegal value */
473117632Sharti	*nump = num;
474117632Sharti	return strp;
475117632Sharti}
476117632Sharti
477117632Sharti/*
478117632Sharti** Given a pointer into a time zone string, extract a number of seconds,
479117632Sharti** in hh[:mm[:ss]] form, from the string.
480117632Sharti** If any error occurs, return NULL.
481117632Sharti** Otherwise, return a pointer to the first character not part of the number
482117632Sharti** of seconds.
483117632Sharti*/
484117632Sharti
485117632Shartistatic const char *
486117632Shartigetsecs(strp, secsp)
487117632Shartiregister const char *	strp;
488117632Shartilong * const		secsp;
489117632Sharti{
490117632Sharti	int	num;
491117632Sharti
492117632Sharti	/*
493117632Sharti	** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
494117632Sharti	** "M10.4.6/26", which does not conform to Posix,
495117632Sharti	** but which specifies the equivalent of
496117632Sharti	** ``02:00 on the first Sunday on or after 23 Oct''.
497117632Sharti	*/
498117632Sharti	strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
499117632Sharti	if (strp == NULL)
500117632Sharti		return NULL;
501117632Sharti	*secsp = num * (long) SECSPERHOUR;
502117632Sharti	if (*strp == ':') {
503117632Sharti		++strp;
504117632Sharti		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
505117632Sharti		if (strp == NULL)
506117632Sharti			return NULL;
507117632Sharti		*secsp += num * SECSPERMIN;
508117632Sharti		if (*strp == ':') {
509117632Sharti			++strp;
510117632Sharti			/* `SECSPERMIN' allows for leap seconds.  */
511117632Sharti			strp = getnum(strp, &num, 0, SECSPERMIN);
512117632Sharti			if (strp == NULL)
513117632Sharti				return NULL;
514117632Sharti			*secsp += num;
515117632Sharti		}
516117632Sharti	}
517117632Sharti	return strp;
518117632Sharti}
519117632Sharti
520117632Sharti/*
521117632Sharti** Given a pointer into a time zone string, extract an offset, in
522117632Sharti** [+-]hh[:mm[:ss]] form, from the string.
523117632Sharti** If any error occurs, return NULL.
524117632Sharti** Otherwise, return a pointer to the first character not part of the time.
525117632Sharti*/
526117632Sharti
527117632Shartistatic const char *
528117632Shartigetoffset(strp, offsetp)
529117632Shartiregister const char *	strp;
530117632Shartilong * const		offsetp;
531117632Sharti{
532117632Sharti	register int	neg = 0;
533117632Sharti
534117632Sharti	if (*strp == '-') {
535117632Sharti		neg = 1;
536117632Sharti		++strp;
537117632Sharti	} else if (*strp == '+')
538117632Sharti		++strp;
539117632Sharti	strp = getsecs(strp, offsetp);
540117632Sharti	if (strp == NULL)
541117632Sharti		return NULL;		/* illegal time */
542117632Sharti	if (neg)
543117632Sharti		*offsetp = -*offsetp;
544117632Sharti	return strp;
545117632Sharti}
546117632Sharti
547117632Sharti/*
548117632Sharti** Given a pointer into a time zone string, extract a rule in the form
549117632Sharti** date[/time].  See POSIX section 8 for the format of "date" and "time".
550117632Sharti** If a valid rule is not found, return NULL.
551117632Sharti** Otherwise, return a pointer to the first character not part of the rule.
552117632Sharti*/
553117632Sharti
554117632Shartistatic const char *
555117632Shartigetrule(strp, rulep)
556117632Sharticonst char *			strp;
557117632Shartiregister struct rule * const	rulep;
558117632Sharti{
559117632Sharti	if (*strp == 'J') {
560117632Sharti		/*
561117632Sharti		** Julian day.
562117632Sharti		*/
563117632Sharti		rulep->r_type = JULIAN_DAY;
564117632Sharti		++strp;
565117632Sharti		strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
566117632Sharti	} else if (*strp == 'M') {
567117632Sharti		/*
568117632Sharti		** Month, week, day.
569117632Sharti		*/
570117632Sharti		rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
571117632Sharti		++strp;
572117632Sharti		strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
573117632Sharti		if (strp == NULL)
574117632Sharti			return NULL;
575117632Sharti		if (*strp++ != '.')
576117632Sharti			return NULL;
577117632Sharti		strp = getnum(strp, &rulep->r_week, 1, 5);
578117632Sharti		if (strp == NULL)
579117632Sharti			return NULL;
580117632Sharti		if (*strp++ != '.')
581117632Sharti			return NULL;
582117632Sharti		strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
583117632Sharti	} else if (is_digit(*strp)) {
584117632Sharti		/*
585117632Sharti		** Day of year.
586117632Sharti		*/
587117632Sharti		rulep->r_type = DAY_OF_YEAR;
588117632Sharti		strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
589117632Sharti	} else	return NULL;		/* invalid format */
590117632Sharti	if (strp == NULL)
591117632Sharti		return NULL;
592117632Sharti	if (*strp == '/') {
593117632Sharti		/*
594117632Sharti		** Time specified.
595117632Sharti		*/
596117632Sharti		++strp;
597117632Sharti		strp = getsecs(strp, &rulep->r_time);
598117632Sharti	} else	rulep->r_time = 2 * SECSPERHOUR;	/* default = 2:00:00 */
599117632Sharti	return strp;
600117632Sharti}
601117632Sharti
602117632Sharti/*
603117632Sharti** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
604117632Sharti** year, a rule, and the offset from GMT at the time that rule takes effect,
605117632Sharti** calculate the Epoch-relative time that rule takes effect.
606117632Sharti*/
607117632Sharti
608117632Shartistatic time_t
609117632Shartitranstime(janfirst, year, rulep, offset)
610117632Sharticonst time_t				janfirst;
611117632Sharticonst int				year;
612117632Shartiregister const struct rule * const	rulep;
613117632Sharticonst long				offset;
614117632Sharti{
615117632Sharti	register int	leapyear;
616117632Sharti	register time_t	value;
617117632Sharti	register int	i;
618117632Sharti	int		d, m1, yy0, yy1, yy2, dow;
619117632Sharti
620117632Sharti	INITIALIZE(value);
621117632Sharti	leapyear = isleap(year);
622117632Sharti	switch (rulep->r_type) {
623117632Sharti
624117632Sharti	case JULIAN_DAY:
625117632Sharti		/*
626117632Sharti		** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
627117632Sharti		** years.
628117632Sharti		** In non-leap years, or if the day number is 59 or less, just
629117632Sharti		** add SECSPERDAY times the day number-1 to the time of
630117632Sharti		** January 1, midnight, to get the day.
631117632Sharti		*/
632117632Sharti		value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
633117632Sharti		if (leapyear && rulep->r_day >= 60)
634117632Sharti			value += SECSPERDAY;
635117632Sharti		break;
636117632Sharti
637117632Sharti	case DAY_OF_YEAR:
638117632Sharti		/*
639117632Sharti		** n - day of year.
640117632Sharti		** Just add SECSPERDAY times the day number to the time of
641117632Sharti		** January 1, midnight, to get the day.
642117632Sharti		*/
643117632Sharti		value = janfirst + rulep->r_day * SECSPERDAY;
644117632Sharti		break;
645117632Sharti
646117632Sharti	case MONTH_NTH_DAY_OF_WEEK:
647117632Sharti		/*
648117632Sharti		** Mm.n.d - nth "dth day" of month m.
649117632Sharti		*/
650117632Sharti		value = janfirst;
651117632Sharti		for (i = 0; i < rulep->r_mon - 1; ++i)
652117632Sharti			value += mon_lengths[leapyear][i] * SECSPERDAY;
653117632Sharti
654117632Sharti		/*
655117632Sharti		** Use Zeller's Congruence to get day-of-week of first day of
656117632Sharti		** month.
657117632Sharti		*/
658117632Sharti		m1 = (rulep->r_mon + 9) % 12 + 1;
659117632Sharti		yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
660117632Sharti		yy1 = yy0 / 100;
661117632Sharti		yy2 = yy0 % 100;
662117632Sharti		dow = ((26 * m1 - 2) / 10 +
663117632Sharti			1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
664117632Sharti		if (dow < 0)
665117632Sharti			dow += DAYSPERWEEK;
666117632Sharti
667117632Sharti		/*
668117632Sharti		** "dow" is the day-of-week of the first day of the month.  Get
669117632Sharti		** the day-of-month (zero-origin) of the first "dow" day of the
670117632Sharti		** month.
671117632Sharti		*/
672117632Sharti		d = rulep->r_day - dow;
673117632Sharti		if (d < 0)
674117632Sharti			d += DAYSPERWEEK;
675117632Sharti		for (i = 1; i < rulep->r_week; ++i) {
676117632Sharti			if (d + DAYSPERWEEK >=
677117632Sharti				mon_lengths[leapyear][rulep->r_mon - 1])
678117632Sharti					break;
679117632Sharti			d += DAYSPERWEEK;
680117632Sharti		}
681117632Sharti
682117632Sharti		/*
683117632Sharti		** "d" is the day-of-month (zero-origin) of the day we want.
684117632Sharti		*/
685117632Sharti		value += d * SECSPERDAY;
686117632Sharti		break;
687117632Sharti	}
688117632Sharti
689117632Sharti	/*
690117632Sharti	** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
691117632Sharti	** question.  To get the Epoch-relative time of the specified local
692117632Sharti	** time on that day, add the transition time and the current offset
693117632Sharti	** from GMT.
694117632Sharti	*/
695117632Sharti	return value + rulep->r_time + offset;
696117632Sharti}
697117632Sharti
698117632Sharti/*
699117632Sharti** Given a POSIX section 8-style TZ string, fill in the rule tables as
700117632Sharti** appropriate.
701117632Sharti*/
702117632Sharti
703117632Shartistatic int
704117632Shartitzparse(name, sp, lastditch)
705117632Sharticonst char *			name;
706117632Shartiregister struct state * const	sp;
707117632Sharticonst int			lastditch;
708117632Sharti{
709117632Sharti	const char *			stdname;
710117632Sharti	const char *			dstname;
711117632Sharti	size_t				stdlen;
712117632Sharti	size_t				dstlen;
713117632Sharti	long				stdoffset;
714117632Sharti	long				dstoffset;
715117632Sharti	register time_t *		atp;
716117632Sharti	register unsigned char *	typep;
717117632Sharti	register char *			cp;
718117632Sharti	register int			load_result;
719117632Sharti
720117632Sharti	INITIALIZE(dstname);
721117632Sharti	stdname = name;
722117632Sharti	if (lastditch) {
723117632Sharti		stdlen = strlen(name);	/* length of standard zone name */
724117632Sharti		name += stdlen;
725117632Sharti		if (stdlen >= sizeof sp->chars)
726117632Sharti			stdlen = (sizeof sp->chars) - 1;
727117632Sharti	} else {
728117632Sharti		name = getzname(name);
729117632Sharti		stdlen = name - stdname;
730117632Sharti		if (stdlen < 3)
731117632Sharti			return -1;
732117632Sharti	}
733117632Sharti	if (*name == '\0')
734117632Sharti		return -1;	/* was "stdoffset = 0;" */
735117632Sharti	else {
736117632Sharti		name = getoffset(name, &stdoffset);
737117632Sharti		if (name == NULL)
738117632Sharti			return -1;
739117632Sharti	}
740117632Sharti	load_result = tzload(TZDEFRULES, sp);
741117632Sharti	if (load_result != 0)
742117632Sharti		sp->leapcnt = 0;		/* so, we're off a little */
743117632Sharti	if (*name != '\0') {
744117632Sharti		dstname = name;
745117632Sharti		name = getzname(name);
746117632Sharti		dstlen = name - dstname;	/* length of DST zone name */
747117632Sharti		if (dstlen < 3)
748117632Sharti			return -1;
749117632Sharti		if (*name != '\0' && *name != ',' && *name != ';') {
750117632Sharti			name = getoffset(name, &dstoffset);
751117632Sharti			if (name == NULL)
752117632Sharti				return -1;
753117632Sharti		} else	dstoffset = stdoffset - SECSPERHOUR;
754117632Sharti		if (*name == ',' || *name == ';') {
755117632Sharti			struct rule	start;
756117632Sharti			struct rule	end;
757117632Sharti			register int	year;
758117632Sharti			register time_t	janfirst;
759117632Sharti			time_t		starttime;
760117632Sharti			time_t		endtime;
761117632Sharti
762117632Sharti			++name;
763117632Sharti			if ((name = getrule(name, &start)) == NULL)
764117632Sharti				return -1;
765117632Sharti			if (*name++ != ',')
766117632Sharti				return -1;
767117632Sharti			if ((name = getrule(name, &end)) == NULL)
768117632Sharti				return -1;
769117632Sharti			if (*name != '\0')
770117632Sharti				return -1;
771117632Sharti			sp->typecnt = 2;	/* standard time and DST */
772117632Sharti			/*
773117632Sharti			** Two transitions per year, from EPOCH_YEAR to 2037.
774117632Sharti			*/
775117632Sharti			sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
776117632Sharti			if (sp->timecnt > TZ_MAX_TIMES)
777117632Sharti				return -1;
778117632Sharti			sp->ttis[0].tt_gmtoff = -dstoffset;
779117632Sharti			sp->ttis[0].tt_isdst = 1;
780117632Sharti			sp->ttis[0].tt_abbrind = stdlen + 1;
781117632Sharti			sp->ttis[1].tt_gmtoff = -stdoffset;
782117632Sharti			sp->ttis[1].tt_isdst = 0;
783117632Sharti			sp->ttis[1].tt_abbrind = 0;
784117632Sharti			atp = sp->ats;
785117632Sharti			typep = sp->types;
786117632Sharti			janfirst = 0;
787117632Sharti			for (year = EPOCH_YEAR; year <= 2037; ++year) {
788117632Sharti				starttime = transtime(janfirst, year, &start,
789117632Sharti					stdoffset);
790117632Sharti				endtime = transtime(janfirst, year, &end,
791117632Sharti					dstoffset);
792117632Sharti				if (starttime > endtime) {
793117632Sharti					*atp++ = endtime;
794117632Sharti					*typep++ = 1;	/* DST ends */
795117632Sharti					*atp++ = starttime;
796117632Sharti					*typep++ = 0;	/* DST begins */
797117632Sharti				} else {
798117632Sharti					*atp++ = starttime;
799117632Sharti					*typep++ = 0;	/* DST begins */
800117632Sharti					*atp++ = endtime;
801117632Sharti					*typep++ = 1;	/* DST ends */
802117632Sharti				}
803117632Sharti				janfirst += year_lengths[isleap(year)] *
804117632Sharti					SECSPERDAY;
805117632Sharti			}
806117632Sharti		} else {
807117632Sharti			register long	theirstdoffset;
808117632Sharti			register long	theirdstoffset;
809117632Sharti			register long	theiroffset;
810117632Sharti			register int	isdst;
811117632Sharti			register int	i;
812117632Sharti			register int	j;
813117632Sharti
814117632Sharti			if (*name != '\0')
815117632Sharti				return -1;
816117632Sharti			if (load_result != 0)
817117632Sharti				return -1;
818117632Sharti			/*
819117632Sharti			** Initial values of theirstdoffset and theirdstoffset.
820117632Sharti			*/
821117632Sharti			theirstdoffset = 0;
822117632Sharti			for (i = 0; i < sp->timecnt; ++i) {
823117632Sharti				j = sp->types[i];
824117632Sharti				if (!sp->ttis[j].tt_isdst) {
825117632Sharti					theirstdoffset =
826117632Sharti						-sp->ttis[j].tt_gmtoff;
827117632Sharti					break;
828117632Sharti				}
829117632Sharti			}
830117632Sharti			theirdstoffset = 0;
831117632Sharti			for (i = 0; i < sp->timecnt; ++i) {
832117632Sharti				j = sp->types[i];
833117632Sharti				if (sp->ttis[j].tt_isdst) {
834117632Sharti					theirdstoffset =
835117632Sharti						-sp->ttis[j].tt_gmtoff;
836117632Sharti					break;
837117632Sharti				}
838117632Sharti			}
839117632Sharti			/*
840117632Sharti			** Initially we're assumed to be in standard time.
841117632Sharti			*/
842117632Sharti			isdst = FALSE;
843117632Sharti			theiroffset = theirstdoffset;
844117632Sharti			/*
845117632Sharti			** Now juggle transition times and types
846117632Sharti			** tracking offsets as you do.
847117632Sharti			*/
848117632Sharti			for (i = 0; i < sp->timecnt; ++i) {
849117632Sharti				j = sp->types[i];
850117632Sharti				sp->types[i] = sp->ttis[j].tt_isdst;
851117632Sharti				if (sp->ttis[j].tt_ttisgmt) {
852117632Sharti					/* No adjustment to transition time */
853117632Sharti				} else {
854117632Sharti					/*
855117632Sharti					** If summer time is in effect, and the
856117632Sharti					** transition time was not specified as
857117632Sharti					** standard time, add the summer time
858117632Sharti					** offset to the transition time;
859117632Sharti					** otherwise, add the standard time
860117632Sharti					** offset to the transition time.
861117632Sharti					*/
862117632Sharti					/*
863117632Sharti					** Transitions from DST to DDST
864117632Sharti					** will effectively disappear since
865117632Sharti					** POSIX provides for only one DST
866117632Sharti					** offset.
867117632Sharti					*/
868117632Sharti					if (isdst && !sp->ttis[j].tt_ttisstd) {
869117632Sharti						sp->ats[i] += dstoffset -
870117632Sharti							theirdstoffset;
871117632Sharti					} else {
872117632Sharti						sp->ats[i] += stdoffset -
873117632Sharti							theirstdoffset;
874117632Sharti					}
875117632Sharti				}
876117632Sharti				theiroffset = -sp->ttis[j].tt_gmtoff;
877117632Sharti				if (sp->ttis[j].tt_isdst)
878117632Sharti					theirdstoffset = theiroffset;
879117632Sharti				else	theirstdoffset = theiroffset;
880117632Sharti			}
881117632Sharti			/*
882117632Sharti			** Finally, fill in ttis.
883117632Sharti			** ttisstd and ttisgmt need not be handled.
884117632Sharti			*/
885117632Sharti			sp->ttis[0].tt_gmtoff = -stdoffset;
886117632Sharti			sp->ttis[0].tt_isdst = FALSE;
887117632Sharti			sp->ttis[0].tt_abbrind = 0;
888117632Sharti			sp->ttis[1].tt_gmtoff = -dstoffset;
889117632Sharti			sp->ttis[1].tt_isdst = TRUE;
890117632Sharti			sp->ttis[1].tt_abbrind = stdlen + 1;
891117632Sharti		}
892117632Sharti	} else {
893117632Sharti		dstlen = 0;
894117632Sharti		sp->typecnt = 1;		/* only standard time */
895117632Sharti		sp->timecnt = 0;
896117632Sharti		sp->ttis[0].tt_gmtoff = -stdoffset;
897117632Sharti		sp->ttis[0].tt_isdst = 0;
898117632Sharti		sp->ttis[0].tt_abbrind = 0;
899117632Sharti	}
900117632Sharti	sp->charcnt = stdlen + 1;
901117632Sharti	if (dstlen != 0)
902117632Sharti		sp->charcnt += dstlen + 1;
903117632Sharti	if (sp->charcnt > sizeof sp->chars)
904117632Sharti		return -1;
905117632Sharti	cp = sp->chars;
906117632Sharti	(void) strncpy(cp, stdname, stdlen);
907117632Sharti	cp += stdlen;
908117632Sharti	*cp++ = '\0';
909117632Sharti	if (dstlen != 0) {
910117632Sharti		(void) strncpy(cp, dstname, dstlen);
911117632Sharti		*(cp + dstlen) = '\0';
912117632Sharti	}
913117632Sharti	return 0;
914117632Sharti}
915117632Sharti
916117632Shartistatic void
917117632Shartigmtload(sp)
918117632Shartistruct state * const	sp;
919117632Sharti{
920117632Sharti	if (tzload(gmt, sp) != 0)
921117632Sharti		(void) tzparse(gmt, sp, TRUE);
922117632Sharti}
923117632Sharti
924117632Sharti#ifndef STD_INSPIRED
925117632Sharti/*
926117632Sharti** A non-static declaration of tzsetwall in a system header file
927117632Sharti** may cause a warning about this upcoming static declaration...
928117632Sharti*/
929117632Shartistatic
930117632Sharti#endif /* !defined STD_INSPIRED */
931117632Sharti#ifdef	_THREAD_SAFE
932117632Shartivoid
933117632Shartitzsetwall_basic P((void))
934117632Sharti#else
935117632Shartivoid
936117632Shartitzsetwall P((void))
937117632Sharti#endif
938117632Sharti{
939117632Sharti	if (lcl_is_set < 0)
940117632Sharti		return;
941117632Sharti	lcl_is_set = -1;
942117632Sharti
943117632Sharti#ifdef ALL_STATE
944117632Sharti	if (lclptr == NULL) {
945117632Sharti		lclptr = (struct state *) malloc(sizeof *lclptr);
946117632Sharti		if (lclptr == NULL) {
947117632Sharti			settzname();	/* all we can do */
948117632Sharti			return;
949117632Sharti		}
950117632Sharti	}
951117632Sharti#endif /* defined ALL_STATE */
952117632Sharti	if (tzload((char *) NULL, lclptr) != 0)
953117632Sharti		gmtload(lclptr);
954117632Sharti	settzname();
955117632Sharti}
956117632Sharti
957117632Sharti#ifdef	_THREAD_SAFE
958117632Shartivoid
959117632Shartitzsetwall P((void))
960117632Sharti{
961117632Sharti	pthread_mutex_lock(&lcl_mutex);
962117632Sharti	tzsetwall_basic();
963117632Sharti	pthread_mutex_unlock(&lcl_mutex);
964117632Sharti}
965117632Sharti#endif
966117632Sharti
967117632Sharti#ifdef	_THREAD_SAFE
968117632Shartistatic void
969117632Shartitzset_basic P((void))
970117632Sharti#else
971117632Shartivoid
972117632Shartitzset P((void))
973117632Sharti#endif
974117632Sharti{
975117632Sharti	register const char *	name;
976117632Sharti
977117632Sharti	name = getenv("TZ");
978117632Sharti	if (name == NULL) {
979117632Sharti		tzsetwall();
980117632Sharti		return;
981117632Sharti	}
982117632Sharti
983117632Sharti	if (lcl_is_set > 0  &&  strcmp(lcl_TZname, name) == 0)
984117632Sharti		return;
985117632Sharti	lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
986117632Sharti	if (lcl_is_set)
987117632Sharti		(void) strcpy(lcl_TZname, name);
988117632Sharti
989117632Sharti#ifdef ALL_STATE
990117632Sharti	if (lclptr == NULL) {
991117632Sharti		lclptr = (struct state *) malloc(sizeof *lclptr);
992117632Sharti		if (lclptr == NULL) {
993117632Sharti			settzname();	/* all we can do */
994117632Sharti			return;
995117632Sharti		}
996117632Sharti	}
997117632Sharti#endif /* defined ALL_STATE */
998117632Sharti	if (*name == '\0') {
999117632Sharti		/*
1000117632Sharti		** User wants it fast rather than right.
1001117632Sharti		*/
1002117632Sharti		lclptr->leapcnt = 0;		/* so, we're off a little */
1003117632Sharti		lclptr->timecnt = 0;
1004117632Sharti		lclptr->ttis[0].tt_gmtoff = 0;
1005117632Sharti		lclptr->ttis[0].tt_abbrind = 0;
1006117632Sharti		(void) strcpy(lclptr->chars, gmt);
1007117632Sharti	} else if (tzload(name, lclptr) != 0)
1008117632Sharti		if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1009117632Sharti			(void) gmtload(lclptr);
1010117632Sharti	settzname();
1011117632Sharti}
1012117632Sharti
1013117632Sharti#ifdef	_THREAD_SAFE
1014117632Shartivoid
1015117632Shartitzset P((void))
1016117632Sharti{
1017117632Sharti	pthread_mutex_lock(&lcl_mutex);
1018117632Sharti	tzset_basic();
1019117632Sharti	pthread_mutex_unlock(&lcl_mutex);
1020117632Sharti}
1021117632Sharti#endif
1022117632Sharti
1023117632Sharti/*
1024117632Sharti** The easy way to behave "as if no library function calls" localtime
1025117632Sharti** is to not call it--so we drop its guts into "localsub", which can be
1026117632Sharti** freely called.  (And no, the PANS doesn't require the above behavior--
1027117632Sharti** but it *is* desirable.)
1028117632Sharti**
1029117632Sharti** The unused offset argument is for the benefit of mktime variants.
1030117632Sharti*/
1031117632Sharti
1032117632Sharti/*ARGSUSED*/
1033117632Shartistatic void
1034117632Shartilocalsub(timep, offset, tmp)
1035117632Sharticonst time_t * const	timep;
1036117632Sharticonst long		offset;
1037117632Shartistruct tm * const	tmp;
1038117632Sharti{
1039117632Sharti	register struct state *		sp;
1040117632Sharti	register const struct ttinfo *	ttisp;
1041117632Sharti	register int			i;
1042117632Sharti	const time_t			t = *timep;
1043117632Sharti
1044117632Sharti	sp = lclptr;
1045117632Sharti#ifdef ALL_STATE
1046117632Sharti	if (sp == NULL) {
1047117632Sharti		gmtsub(timep, offset, tmp);
1048117632Sharti		return;
1049117632Sharti	}
1050117632Sharti#endif /* defined ALL_STATE */
1051117632Sharti	if (sp->timecnt == 0 || t < sp->ats[0]) {
1052117632Sharti		i = 0;
1053117632Sharti		while (sp->ttis[i].tt_isdst)
1054117632Sharti			if (++i >= sp->typecnt) {
1055117632Sharti				i = 0;
1056117632Sharti				break;
1057117632Sharti			}
1058117632Sharti	} else {
1059117632Sharti		for (i = 1; i < sp->timecnt; ++i)
1060117632Sharti			if (t < sp->ats[i])
1061117632Sharti				break;
1062117632Sharti		i = sp->types[i - 1];
1063117632Sharti	}
1064117632Sharti	ttisp = &sp->ttis[i];
1065117632Sharti	/*
1066117632Sharti	** To get (wrong) behavior that's compatible with System V Release 2.0
1067117632Sharti	** you'd replace the statement below with
1068117632Sharti	**	t += ttisp->tt_gmtoff;
1069117632Sharti	**	timesub(&t, 0L, sp, tmp);
1070117632Sharti	*/
1071117632Sharti	timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1072117632Sharti	tmp->tm_isdst = ttisp->tt_isdst;
1073117632Sharti	tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1074117632Sharti#ifdef TM_ZONE
1075117632Sharti	tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1076117632Sharti#endif /* defined TM_ZONE */
1077117632Sharti}
1078117632Sharti
1079117632Sharti#ifdef	_THREAD_SAFE
1080117632Shartiint
1081117632Shartilocaltime_r(timep, p_tm)
1082117632Sharticonst time_t * const	timep;
1083117632Shartistruct tm *p_tm;
1084117632Sharti{
1085117632Sharti	pthread_mutex_lock(&lcl_mutex);
1086117632Sharti	tzset();
1087117632Sharti	localsub(timep, 0L, p_tm);
1088117632Sharti	pthread_mutex_unlock(&lcl_mutex);
1089117632Sharti	return(0);
1090117632Sharti}
1091117632Sharti#endif
1092117632Sharti
1093117632Shartistruct tm *
1094117632Shartilocaltime(timep)
1095117632Sharticonst time_t * const	timep;
1096117632Sharti{
1097117632Sharti#ifdef	_THREAD_SAFE
1098117632Sharti	static struct pthread_mutex _localtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1099117632Sharti	static pthread_mutex_t localtime_mutex = &_localtime_mutex;
1100117632Sharti	static pthread_key_t localtime_key = -1;
1101117632Sharti	struct tm *p_tm;
1102117632Sharti
1103117632Sharti	pthread_mutex_lock(&localtime_mutex);
1104117632Sharti	if (localtime_key < 0) {
1105117632Sharti		if (pthread_keycreate(&localtime_key, free) < 0) {
1106117632Sharti			pthread_mutex_unlock(&localtime_mutex);
1107117632Sharti			return(NULL);
1108117632Sharti		}
1109117632Sharti	}
1110117632Sharti	pthread_mutex_unlock(&localtime_mutex);
1111117632Sharti	if (pthread_getspecific(localtime_key,(void **) &p_tm) != 0) {
1112117632Sharti		return(NULL);
1113117632Sharti	} else if (p_tm == NULL) {
1114117632Sharti		if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) == NULL) {
1115117632Sharti			return(NULL);
1116117632Sharti		}
1117117632Sharti		pthread_setspecific(localtime_key, p_tm);
1118117632Sharti	}
1119117632Sharti	pthread_mutex_lock(&lcl_mutex);
1120117632Sharti	tzset();
1121117632Sharti	localsub(timep, 0L, p_tm);
1122117632Sharti	pthread_mutex_unlock(&lcl_mutex);
1123117632Sharti	return p_tm;
1124117632Sharti#else
1125117632Sharti	tzset();
1126117632Sharti	localsub(timep, 0L, &tm);
1127117632Sharti	return &tm;
1128117632Sharti#endif
1129117632Sharti}
1130117632Sharti
1131117632Sharti/*
1132117632Sharti** gmtsub is to gmtime as localsub is to localtime.
1133117632Sharti*/
1134117632Sharti
1135117632Shartistatic void
1136117632Shartigmtsub(timep, offset, tmp)
1137117632Sharticonst time_t * const	timep;
1138117632Sharticonst long		offset;
1139117632Shartistruct tm * const	tmp;
1140117632Sharti{
1141117632Sharti#ifdef	_THREAD_SAFE
1142117632Sharti	pthread_mutex_lock(&gmt_mutex);
1143117632Sharti#endif
1144117632Sharti	if (!gmt_is_set) {
1145117632Sharti		gmt_is_set = TRUE;
1146117632Sharti#ifdef ALL_STATE
1147117632Sharti		gmtptr = (struct state *) malloc(sizeof *gmtptr);
1148117632Sharti		if (gmtptr != NULL)
1149117632Sharti#endif /* defined ALL_STATE */
1150117632Sharti			gmtload(gmtptr);
1151117632Sharti	}
1152117632Sharti#ifdef	_THREAD_SAFE
1153117632Sharti	pthread_mutex_unlock(&gmt_mutex);
1154117632Sharti#endif
1155117632Sharti	timesub(timep, offset, gmtptr, tmp);
1156117632Sharti#ifdef TM_ZONE
1157117632Sharti	/*
1158117632Sharti	** Could get fancy here and deliver something such as
1159117632Sharti	** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
1160117632Sharti	** but this is no time for a treasure hunt.
1161117632Sharti	*/
1162117632Sharti	if (offset != 0)
1163117632Sharti		tmp->TM_ZONE = wildabbr;
1164117632Sharti	else {
1165117632Sharti#ifdef ALL_STATE
1166117632Sharti		if (gmtptr == NULL)
1167117632Sharti			tmp->TM_ZONE = gmt;
1168117632Sharti		else	tmp->TM_ZONE = gmtptr->chars;
1169117632Sharti#endif /* defined ALL_STATE */
1170117632Sharti#ifndef ALL_STATE
1171117632Sharti		tmp->TM_ZONE = gmtptr->chars;
1172117632Sharti#endif /* State Farm */
1173117632Sharti	}
1174117632Sharti#endif /* defined TM_ZONE */
1175117632Sharti}
1176117632Sharti
1177117632Shartistruct tm *
1178117632Shartigmtime(timep)
1179117632Sharticonst time_t * const	timep;
1180117632Sharti{
1181117632Sharti#ifdef	_THREAD_SAFE
1182117632Sharti	static struct pthread_mutex _gmtime_mutex = PTHREAD_MUTEX_INITIALIZER;
1183117632Sharti	static pthread_mutex_t gmtime_mutex = &_gmtime_mutex;
1184117632Sharti	static pthread_key_t gmtime_key = -1;
1185117632Sharti	struct tm *p_tm;
1186117632Sharti
1187117632Sharti	pthread_mutex_lock(&gmtime_mutex);
1188117632Sharti	if (gmtime_key < 0) {
1189117632Sharti		if (pthread_keycreate(&gmtime_key, free) < 0) {
1190117632Sharti			pthread_mutex_unlock(&gmtime_mutex);
1191117632Sharti			return(NULL);
1192117632Sharti		}
1193117632Sharti	}
1194117632Sharti	pthread_mutex_unlock(&gmtime_mutex);
1195117632Sharti	if (pthread_getspecific(gmtime_key,(void **) &p_tm) != 0) {
1196117632Sharti		return(NULL);
1197117632Sharti	} else if (p_tm == NULL) {
1198117632Sharti		if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) == NULL) {
1199117632Sharti			return(NULL);
1200117632Sharti		}
1201117632Sharti		pthread_setspecific(gmtime_key, p_tm);
1202117632Sharti	}
1203117632Sharti	gmtsub(timep, 0L, p_tm);
1204117632Sharti	return(p_tm);
1205117632Sharti#else
1206117632Sharti	gmtsub(timep, 0L, &tm);
1207117632Sharti	return &tm;
1208117632Sharti#endif
1209117632Sharti}
1210117632Sharti
1211117632Sharti#ifdef	_THREAD_SAFE
1212117632Shartiint
1213117632Shartigmtime_r(const time_t * timep, struct tm * tm)
1214117632Sharti{
1215117632Sharti	gmtsub(timep, 0L, tm);
1216117632Sharti	return(0);
1217117632Sharti}
1218117632Sharti#endif
1219117632Sharti
1220117632Sharti#ifdef STD_INSPIRED
1221117632Sharti
1222117632Shartistruct tm *
1223117632Shartiofftime(timep, offset)
1224117632Sharticonst time_t * const	timep;
1225117632Sharticonst long		offset;
1226117632Sharti{
1227117632Sharti	gmtsub(timep, offset, &tm);
1228117632Sharti	return &tm;
1229117632Sharti}
1230117632Sharti
1231117632Sharti#endif /* defined STD_INSPIRED */
1232117632Sharti
1233117632Shartistatic void
1234117632Shartitimesub(timep, offset, sp, tmp)
1235117632Sharticonst time_t * const			timep;
1236117632Sharticonst long				offset;
1237117632Shartiregister const struct state * const	sp;
1238117632Shartiregister struct tm * const		tmp;
1239117632Sharti{
1240117632Sharti	register const struct lsinfo *	lp;
1241117632Sharti	register long			days;
1242117632Sharti	register long			rem;
1243117632Sharti	register int			y;
1244117632Sharti	register int			yleap;
1245117632Sharti	register const int *		ip;
1246117632Sharti	register long			corr;
1247117632Sharti	register int			hit;
1248117632Sharti	register int			i;
1249117632Sharti
1250117632Sharti	corr = 0;
1251117632Sharti	hit = 0;
1252117632Sharti#ifdef ALL_STATE
1253117632Sharti	i = (sp == NULL) ? 0 : sp->leapcnt;
1254117632Sharti#endif /* defined ALL_STATE */
1255117632Sharti#ifndef ALL_STATE
1256117632Sharti	i = sp->leapcnt;
1257117632Sharti#endif /* State Farm */
1258117632Sharti	while (--i >= 0) {
1259117632Sharti		lp = &sp->lsis[i];
1260117632Sharti		if (*timep >= lp->ls_trans) {
1261117632Sharti			if (*timep == lp->ls_trans) {
1262117632Sharti				hit = ((i == 0 && lp->ls_corr > 0) ||
1263117632Sharti					lp->ls_corr > sp->lsis[i - 1].ls_corr);
1264117632Sharti				if (hit)
1265117632Sharti					while (i > 0 &&
1266117632Sharti						sp->lsis[i].ls_trans ==
1267117632Sharti						sp->lsis[i - 1].ls_trans + 1 &&
1268117632Sharti						sp->lsis[i].ls_corr ==
1269117632Sharti						sp->lsis[i - 1].ls_corr + 1) {
1270117632Sharti							++hit;
1271117632Sharti							--i;
1272117632Sharti					}
1273117632Sharti			}
1274			corr = lp->ls_corr;
1275			break;
1276		}
1277	}
1278	days = *timep / SECSPERDAY;
1279	rem = *timep % SECSPERDAY;
1280#ifdef mc68k
1281	if (*timep == 0x80000000) {
1282		/*
1283		** A 3B1 muffs the division on the most negative number.
1284		*/
1285		days = -24855;
1286		rem = -11648;
1287	}
1288#endif /* defined mc68k */
1289	rem += (offset - corr);
1290	while (rem < 0) {
1291		rem += SECSPERDAY;
1292		--days;
1293	}
1294	while (rem >= SECSPERDAY) {
1295		rem -= SECSPERDAY;
1296		++days;
1297	}
1298	tmp->tm_hour = (int) (rem / SECSPERHOUR);
1299	rem = rem % SECSPERHOUR;
1300	tmp->tm_min = (int) (rem / SECSPERMIN);
1301	/*
1302	** A positive leap second requires a special
1303	** representation.  This uses "... ??:59:60" et seq.
1304	*/
1305	tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1306	tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1307	if (tmp->tm_wday < 0)
1308		tmp->tm_wday += DAYSPERWEEK;
1309	y = EPOCH_YEAR;
1310#define LEAPS_THRU_END_OF(y)	((y) / 4 - (y) / 100 + (y) / 400)
1311	while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1312		register int	newy;
1313
1314		newy = y + days / DAYSPERNYEAR;
1315		if (days < 0)
1316			--newy;
1317		days -= (newy - y) * DAYSPERNYEAR +
1318			LEAPS_THRU_END_OF(newy - 1) -
1319			LEAPS_THRU_END_OF(y - 1);
1320		y = newy;
1321	}
1322	tmp->tm_year = y - TM_YEAR_BASE;
1323	tmp->tm_yday = (int) days;
1324	ip = mon_lengths[yleap];
1325	for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1326		days = days - (long) ip[tmp->tm_mon];
1327	tmp->tm_mday = (int) (days + 1);
1328	tmp->tm_isdst = 0;
1329#ifdef TM_GMTOFF
1330	tmp->TM_GMTOFF = offset;
1331#endif /* defined TM_GMTOFF */
1332}
1333
1334char *
1335ctime(timep)
1336const time_t * const	timep;
1337{
1338/*
1339** Section 4.12.3.2 of X3.159-1989 requires that
1340**	The ctime funciton converts the calendar time pointed to by timer
1341**	to local time in the form of a string.  It is equivalent to
1342**		asctime(localtime(timer))
1343*/
1344	return asctime(localtime(timep));
1345}
1346
1347/*
1348** Adapted from code provided by Robert Elz, who writes:
1349**	The "best" way to do mktime I think is based on an idea of Bob
1350**	Kridle's (so its said...) from a long time ago.
1351**	[kridle@xinet.com as of 1996-01-16.]
1352**	It does a binary search of the time_t space.  Since time_t's are
1353**	just 32 bits, its a max of 32 iterations (even at 64 bits it
1354**	would still be very reasonable).
1355*/
1356
1357#ifndef WRONG
1358#define WRONG	(-1)
1359#endif /* !defined WRONG */
1360
1361/*
1362** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1363*/
1364
1365static int
1366increment_overflow(number, delta)
1367int *	number;
1368int	delta;
1369{
1370	int	number0;
1371
1372	number0 = *number;
1373	*number += delta;
1374	return (*number < number0) != (delta < 0);
1375}
1376
1377static int
1378normalize_overflow(tensptr, unitsptr, base)
1379int * const	tensptr;
1380int * const	unitsptr;
1381const int	base;
1382{
1383	register int	tensdelta;
1384
1385	tensdelta = (*unitsptr >= 0) ?
1386		(*unitsptr / base) :
1387		(-1 - (-1 - *unitsptr) / base);
1388	*unitsptr -= tensdelta * base;
1389	return increment_overflow(tensptr, tensdelta);
1390}
1391
1392static int
1393tmcomp(atmp, btmp)
1394register const struct tm * const atmp;
1395register const struct tm * const btmp;
1396{
1397	register int	result;
1398
1399	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1400		(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1401		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1402		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1403		(result = (atmp->tm_min - btmp->tm_min)) == 0)
1404			result = atmp->tm_sec - btmp->tm_sec;
1405	return result;
1406}
1407
1408static time_t
1409time2(tmp, funcp, offset, okayp)
1410struct tm * const	tmp;
1411void (* const		funcp) P((const time_t*, long, struct tm*));
1412const long		offset;
1413int * const		okayp;
1414{
1415	register const struct state *	sp;
1416	register int			dir;
1417	register int			bits;
1418	register int			i, j ;
1419	register int			saved_seconds;
1420	time_t				newt;
1421	time_t				t;
1422	struct tm			yourtm, mytm;
1423
1424	*okayp = FALSE;
1425	yourtm = *tmp;
1426	if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1427		return WRONG;
1428	if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1429		return WRONG;
1430	if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1431		return WRONG;
1432	/*
1433	** Turn yourtm.tm_year into an actual year number for now.
1434	** It is converted back to an offset from TM_YEAR_BASE later.
1435	*/
1436	if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1437		return WRONG;
1438	while (yourtm.tm_mday <= 0) {
1439		if (increment_overflow(&yourtm.tm_year, -1))
1440			return WRONG;
1441		i = yourtm.tm_year + (1 < yourtm.tm_mon);
1442		yourtm.tm_mday += year_lengths[isleap(i)];
1443	}
1444	while (yourtm.tm_mday > DAYSPERLYEAR) {
1445		i = yourtm.tm_year + (1 < yourtm.tm_mon);
1446		yourtm.tm_mday -= year_lengths[isleap(i)];
1447		if (increment_overflow(&yourtm.tm_year, 1))
1448			return WRONG;
1449	}
1450	for ( ; ; ) {
1451		i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1452		if (yourtm.tm_mday <= i)
1453			break;
1454		yourtm.tm_mday -= i;
1455		if (++yourtm.tm_mon >= MONSPERYEAR) {
1456			yourtm.tm_mon = 0;
1457			if (increment_overflow(&yourtm.tm_year, 1))
1458				return WRONG;
1459		}
1460	}
1461	if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1462		return WRONG;
1463	if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1464		/*
1465		** We can't set tm_sec to 0, because that might push the
1466		** time below the minimum representable time.
1467		** Set tm_sec to 59 instead.
1468		** This assumes that the minimum representable time is
1469		** not in the same minute that a leap second was deleted from,
1470		** which is a safer assumption than using 58 would be.
1471		*/
1472		if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1473			return WRONG;
1474		saved_seconds = yourtm.tm_sec;
1475		yourtm.tm_sec = SECSPERMIN - 1;
1476	} else {
1477		saved_seconds = yourtm.tm_sec;
1478		yourtm.tm_sec = 0;
1479	}
1480	/*
1481	** Divide the search space in half
1482	** (this works whether time_t is signed or unsigned).
1483	*/
1484	bits = TYPE_BIT(time_t) - 1;
1485	/*
1486	** If time_t is signed, then 0 is just above the median,
1487	** assuming two's complement arithmetic.
1488	** If time_t is unsigned, then (1 << bits) is just above the median.
1489	*/
1490	t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1491	for ( ; ; ) {
1492		(*funcp)(&t, offset, &mytm);
1493		dir = tmcomp(&mytm, &yourtm);
1494		if (dir != 0) {
1495			if (bits-- < 0)
1496				return WRONG;
1497			if (bits < 0)
1498				--t; /* may be needed if new t is minimal */
1499			else if (dir > 0)
1500				t -= ((time_t) 1) << bits;
1501			else	t += ((time_t) 1) << bits;
1502			continue;
1503		}
1504		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1505			break;
1506		/*
1507		** Right time, wrong type.
1508		** Hunt for right time, right type.
1509		** It's okay to guess wrong since the guess
1510		** gets checked.
1511		*/
1512		/*
1513		** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1514		*/
1515		sp = (const struct state *)
1516			(((void *) funcp == (void *) localsub) ?
1517			lclptr : gmtptr);
1518#ifdef ALL_STATE
1519		if (sp == NULL)
1520			return WRONG;
1521#endif /* defined ALL_STATE */
1522		for (i = sp->typecnt - 1; i >= 0; --i) {
1523			if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1524				continue;
1525			for (j = sp->typecnt - 1; j >= 0; --j) {
1526				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1527					continue;
1528				newt = t + sp->ttis[j].tt_gmtoff -
1529					sp->ttis[i].tt_gmtoff;
1530				(*funcp)(&newt, offset, &mytm);
1531				if (tmcomp(&mytm, &yourtm) != 0)
1532					continue;
1533				if (mytm.tm_isdst != yourtm.tm_isdst)
1534					continue;
1535				/*
1536				** We have a match.
1537				*/
1538				t = newt;
1539				goto label;
1540			}
1541		}
1542		return WRONG;
1543	}
1544label:
1545	newt = t + saved_seconds;
1546	if ((newt < t) != (saved_seconds < 0))
1547		return WRONG;
1548	t = newt;
1549	(*funcp)(&t, offset, tmp);
1550	*okayp = TRUE;
1551	return t;
1552}
1553
1554static time_t
1555time1(tmp, funcp, offset)
1556struct tm * const	tmp;
1557void (* const		funcp) P((const time_t *, long, struct tm *));
1558const long		offset;
1559{
1560	register time_t			t;
1561	register const struct state *	sp;
1562	register int			samei, otheri;
1563	int				okay;
1564
1565	if (tmp->tm_isdst > 1)
1566		tmp->tm_isdst = 1;
1567	t = time2(tmp, funcp, offset, &okay);
1568#ifdef PCTS
1569	/*
1570	** PCTS code courtesy Grant Sullivan (grant@osf.org).
1571	*/
1572	if (okay)
1573		return t;
1574	if (tmp->tm_isdst < 0)
1575		tmp->tm_isdst = 0;	/* reset to std and try again */
1576#endif /* defined PCTS */
1577#ifndef PCTS
1578	if (okay || tmp->tm_isdst < 0)
1579		return t;
1580#endif /* !defined PCTS */
1581	/*
1582	** We're supposed to assume that somebody took a time of one type
1583	** and did some math on it that yielded a "struct tm" that's bad.
1584	** We try to divine the type they started from and adjust to the
1585	** type they need.
1586	*/
1587	/*
1588	** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1589	*/
1590	sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1591		lclptr : gmtptr);
1592#ifdef ALL_STATE
1593	if (sp == NULL)
1594		return WRONG;
1595#endif /* defined ALL_STATE */
1596	for (samei = sp->typecnt - 1; samei >= 0; --samei) {
1597		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1598			continue;
1599		for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
1600			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1601				continue;
1602			tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1603					sp->ttis[samei].tt_gmtoff;
1604			tmp->tm_isdst = !tmp->tm_isdst;
1605			t = time2(tmp, funcp, offset, &okay);
1606			if (okay)
1607				return t;
1608			tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1609					sp->ttis[samei].tt_gmtoff;
1610			tmp->tm_isdst = !tmp->tm_isdst;
1611		}
1612	}
1613	return WRONG;
1614}
1615
1616time_t
1617mktime(tmp)
1618struct tm * const	tmp;
1619{
1620	time_t mktime_return_value;
1621#ifdef	_THREAD_SAFE
1622	pthread_mutex_lock(&lcl_mutex);
1623#endif
1624	tzset();
1625	mktime_return_value = time1(tmp, localsub, 0L);
1626#ifdef	_THREAD_SAFE
1627	pthread_mutex_unlock(&lcl_mutex);
1628#endif
1629	return(mktime_return_value);
1630}
1631
1632#ifdef STD_INSPIRED
1633
1634time_t
1635timelocal(tmp)
1636struct tm * const	tmp;
1637{
1638	tmp->tm_isdst = -1;	/* in case it wasn't initialized */
1639	return mktime(tmp);
1640}
1641
1642time_t
1643timegm(tmp)
1644struct tm * const	tmp;
1645{
1646	tmp->tm_isdst = 0;
1647	return time1(tmp, gmtsub, 0L);
1648}
1649
1650time_t
1651timeoff(tmp, offset)
1652struct tm * const	tmp;
1653const long		offset;
1654{
1655	tmp->tm_isdst = 0;
1656	return time1(tmp, gmtsub, offset);
1657}
1658
1659#endif /* defined STD_INSPIRED */
1660
1661#ifdef CMUCS
1662
1663/*
1664** The following is supplied for compatibility with
1665** previous versions of the CMUCS runtime library.
1666*/
1667
1668long
1669gtime(tmp)
1670struct tm * const	tmp;
1671{
1672	const time_t	t = mktime(tmp);
1673
1674	if (t == WRONG)
1675		return -1;
1676	return t;
1677}
1678
1679#endif /* defined CMUCS */
1680
1681/*
1682** XXX--is the below the right way to conditionalize??
1683*/
1684
1685#ifdef STD_INSPIRED
1686
1687/*
1688** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1689** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
1690** is not the case if we are accounting for leap seconds.
1691** So, we provide the following conversion routines for use
1692** when exchanging timestamps with POSIX conforming systems.
1693*/
1694
1695static long
1696leapcorr(timep)
1697time_t *	timep;
1698{
1699	register struct state *		sp;
1700	register struct lsinfo *	lp;
1701	register int			i;
1702
1703	sp = lclptr;
1704	i = sp->leapcnt;
1705	while (--i >= 0) {
1706		lp = &sp->lsis[i];
1707		if (*timep >= lp->ls_trans)
1708			return lp->ls_corr;
1709	}
1710	return 0;
1711}
1712
1713time_t
1714time2posix(t)
1715time_t	t;
1716{
1717	tzset();
1718	return t - leapcorr(&t);
1719}
1720
1721time_t
1722posix2time(t)
1723time_t	t;
1724{
1725	time_t	x;
1726	time_t	y;
1727
1728	tzset();
1729	/*
1730	** For a positive leap second hit, the result
1731	** is not unique.  For a negative leap second
1732	** hit, the corresponding time doesn't exist,
1733	** so we return an adjacent second.
1734	*/
1735	x = t + leapcorr(&t);
1736	y = x - leapcorr(&x);
1737	if (y < t) {
1738		do {
1739			x++;
1740			y = x - leapcorr(&x);
1741		} while (y < t);
1742		if (t != y)
1743			return x - 1;
1744	} else if (y > t) {
1745		do {
1746			--x;
1747			y = x - leapcorr(&x);
1748		} while (y > t);
1749		if (t != y)
1750			return x + 1;
1751	}
1752	return x;
1753}
1754
1755#endif /* defined STD_INSPIRED */
1756