1171568Sscottl/*
2211095Sdes * Copyright (c) 1987, 1989 Regents of the University of California.
3171568Sscottl * All rights reserved.
4171568Sscottl *
5171568Sscottl * This code is derived from software contributed to Berkeley by
6171568Sscottl * Arthur David Olson of the National Cancer Institute.
7171568Sscottl *
8171568Sscottl * Redistribution and use in source and binary forms, with or without
9171568Sscottl * modification, are permitted provided that the following conditions
10171568Sscottl * are met:
11171568Sscottl * 1. Redistributions of source code must retain the above copyright
12171568Sscottl *    notice, this list of conditions and the following disclaimer.
13171568Sscottl * 2. Redistributions in binary form must reproduce the above copyright
14171568Sscottl *    notice, this list of conditions and the following disclaimer in the
15171568Sscottl *    documentation and/or other materials provided with the distribution.
16171568Sscottl * 3. All advertising materials mentioning features or use of this software
17171568Sscottl *    must display the following acknowledgement:
18171568Sscottl *	This product includes software developed by the University of
19171568Sscottl *	California, Berkeley and its contributors.
20171568Sscottl * 4. Neither the name of the University nor the names of its contributors
21171568Sscottl *    may be used to endorse or promote products derived from this software
22171568Sscottl *    without specific prior written permission.
23171568Sscottl *
24171568Sscottl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25171568Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26171568Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27171568Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28171568Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29171568Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30171568Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31171568Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32171568Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33171568Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34171568Sscottl * SUCH DAMAGE.  */
35171568Sscottl
36171568Sscottl/*static char *sccsid = "from: @(#)ctime.c	5.26 (Berkeley) 2/23/91";*/
37171568Sscottl
38171568Sscottl/*
39171568Sscottl * This implementation of mktime is lifted straight from the NetBSD (BSD 4.4)
40171568Sscottl * version.  I modified it slightly to divorce it from the internals of the
41171568Sscottl * ctime library.  Thus this version can't use details of the internal
42171568Sscottl * timezone state file to figure out strange unnormalized struct tm values,
43171568Sscottl * as might result from someone doing date math on the tm struct then passing
44171568Sscottl * it to mktime.
45171568Sscottl *
46171568Sscottl * It just does as well as it can at normalizing the tm input, then does a
47171568Sscottl * binary search of the time space using the system's localtime() function.
48171568Sscottl *
49171568Sscottl * The original binary search was defective in that it didn't consider the
50171568Sscottl * setting of tm_isdst when comparing tm values, causing the search to be
51171568Sscottl * flubbed for times near the dst/standard time changeover.  The original
52211095Sdes * code seems to make up for this by grubbing through the timezone info
53171568Sscottl * whenever the binary search barfed.  Since I don't have that luxury in
54171568Sscottl * portable code, I have to take care of tm_isdst in the comparison routine.
55171568Sscottl * This requires knowing how many minutes offset dst is from standard time.
56171568Sscottl *
57171568Sscottl * So, if you live somewhere in the world where dst is not 60 minutes offset,
58171568Sscottl * and your vendor doesn't supply mktime(), you'll have to edit this variable
59171568Sscottl * by hand.  Sorry about that.
60171568Sscottl */
61171568Sscottl
62171568Sscottl#include <config.h>
63171568Sscottl#include "ntp_machine.h"
64171568Sscottl
65171568Sscottl#if !defined(HAVE_MKTIME) || ( !defined(HAVE_TIMEGM) && defined(WANT_TIMEGM) )
66171568Sscottl
67171568Sscottl#if SIZEOF_TIME_T >= 8
68171568Sscottl#error libntp supplied mktime()/timegm() do not support 64-bit time_t
69171568Sscottl#endif
70171568Sscottl
71171568Sscottl#ifndef DSTMINUTES
72171568Sscottl#define DSTMINUTES 60
73171568Sscottl#endif
74171568Sscottl
75171568Sscottl#define FALSE 0
76171568Sscottl#define TRUE 1
77171568Sscottl
78171568Sscottl/* some constants from tzfile.h */
79171568Sscottl#define SECSPERMIN      60
80171568Sscottl#define MINSPERHOUR     60
81171568Sscottl#define HOURSPERDAY     24
82171568Sscottl#define DAYSPERWEEK     7
83171568Sscottl#define DAYSPERNYEAR    365
84171568Sscottl#define DAYSPERLYEAR    366
85171568Sscottl#define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
86171568Sscottl#define SECSPERDAY      ((long) SECSPERHOUR * HOURSPERDAY)
87171568Sscottl#define MONSPERYEAR     12
88171568Sscottl#define TM_YEAR_BASE    1900
89171568Sscottl#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
90171568Sscottl
91171568Sscottlstatic int	mon_lengths[2][MONSPERYEAR] = {
92171568Sscottl	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
93171568Sscottl	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
94171568Sscottl};
95171568Sscottl
96171568Sscottlstatic int	year_lengths[2] = {
97171568Sscottl	DAYSPERNYEAR, DAYSPERLYEAR
98171568Sscottl};
99171568Sscottl
100171568Sscottl/*
101171568Sscottl** Adapted from code provided by Robert Elz, who writes:
102171568Sscottl**	The "best" way to do mktime I think is based on an idea of Bob
103171568Sscottl**	Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
104171568Sscottl**	It does a binary search of the time_t space.  Since time_t's are
105171568Sscottl**	just 32 bits, its a max of 32 iterations (even at 64 bits it
106171568Sscottl**	would still be very reasonable).
107171568Sscottl*/
108171568Sscottl
109171568Sscottl#ifndef WRONG
110171568Sscottl#define WRONG	(-1)
111171568Sscottl#endif /* !defined WRONG */
112171568Sscottl
113171568Sscottlstatic void
114171568Sscottlnormalize(
115171568Sscottl	int * tensptr,
116171568Sscottl	int * unitsptr,
117171568Sscottl	int	base
118171568Sscottl	)
119171568Sscottl{
120171568Sscottl	if (*unitsptr >= base) {
121171568Sscottl		*tensptr += *unitsptr / base;
122171568Sscottl		*unitsptr %= base;
123171568Sscottl	} else if (*unitsptr < 0) {
124171568Sscottl		--*tensptr;
125171568Sscottl		*unitsptr += base;
126171568Sscottl		if (*unitsptr < 0) {
127171568Sscottl			*tensptr -= 1 + (-*unitsptr) / base;
128171568Sscottl			*unitsptr = base - (-*unitsptr) % base;
129171568Sscottl		}
130171568Sscottl	}
131171568Sscottl}
132171568Sscottl
133171568Sscottlstatic struct tm *
134171568Sscottlmkdst(
135171568Sscottl	struct tm *	tmp
136171568Sscottl	)
137171568Sscottl{
138171568Sscottl    /* jds */
139171568Sscottl    static struct tm tmbuf;
140171568Sscottl
141171568Sscottl    tmbuf = *tmp;
142171568Sscottl    tmbuf.tm_isdst = 1;
143171568Sscottl    tmbuf.tm_min += DSTMINUTES;
144171568Sscottl    normalize(&tmbuf.tm_hour, &tmbuf.tm_min, MINSPERHOUR);
145171568Sscottl    return &tmbuf;
146171568Sscottl}
147171568Sscottl
148171568Sscottlstatic int
149171568Sscottltmcomp(
150171568Sscottl	register struct tm * atmp,
151171568Sscottl	register struct tm * btmp
152211095Sdes	)
153171568Sscottl{
154171568Sscottl	register int	result;
155171568Sscottl
156171568Sscottl	/* compare down to the same day */
157171568Sscottl
158171568Sscottl	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
159171568Sscottl	    (result = (atmp->tm_mon - btmp->tm_mon)) == 0)
160171568Sscottl	    result = (atmp->tm_mday - btmp->tm_mday);
161171568Sscottl
162171568Sscottl	if(result != 0)
163171568Sscottl	    return result;
164171568Sscottl
165171568Sscottl	/* get rid of one-sided dst bias */
166171568Sscottl
167171568Sscottl	if(atmp->tm_isdst == 1 && !btmp->tm_isdst)
168171568Sscottl	    btmp = mkdst(btmp);
169171568Sscottl	else if(btmp->tm_isdst == 1 && !atmp->tm_isdst)
170171568Sscottl	    atmp = mkdst(atmp);
171171568Sscottl
172171568Sscottl	/* compare the rest of the way */
173171568Sscottl
174171568Sscottl	if ((result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
175171568Sscottl	    (result = (atmp->tm_min - btmp->tm_min)) == 0)
176171568Sscottl	    result = atmp->tm_sec - btmp->tm_sec;
177171568Sscottl	return result;
178171568Sscottl}
179171568Sscottl
180171568Sscottl
181171568Sscottlstatic time_t
182171568Sscottltime2(
183171568Sscottl	struct tm *	tmp,
184171568Sscottl	int * 		okayp,
185171568Sscottl	int		usezn
186171568Sscottl	)
187171568Sscottl{
188171568Sscottl	register int			dir;
189171568Sscottl	register int			bits;
190171568Sscottl	register int			i;
191171568Sscottl	register int			saved_seconds;
192171568Sscottl	time_t				t;
193171568Sscottl	struct tm			yourtm, mytm;
194171568Sscottl
195171568Sscottl	*okayp = FALSE;
196171568Sscottl	yourtm = *tmp;
197171568Sscottl	if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
198171568Sscottl		normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
199171568Sscottl	normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
200171568Sscottl	normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
201171568Sscottl	normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
202171568Sscottl	while (yourtm.tm_mday <= 0) {
203171568Sscottl		--yourtm.tm_year;
204171568Sscottl		yourtm.tm_mday +=
205			year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
206	}
207	for ( ; ; ) {
208		i = mon_lengths[isleap(yourtm.tm_year +
209			TM_YEAR_BASE)][yourtm.tm_mon];
210		if (yourtm.tm_mday <= i)
211			break;
212		yourtm.tm_mday -= i;
213		if (++yourtm.tm_mon >= MONSPERYEAR) {
214			yourtm.tm_mon = 0;
215			++yourtm.tm_year;
216		}
217	}
218	saved_seconds = yourtm.tm_sec;
219	yourtm.tm_sec = 0;
220	/*
221	** Calculate the number of magnitude bits in a time_t
222	** (this works regardless of whether time_t is
223	** signed or unsigned, though lint complains if unsigned).
224	*/
225	for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
226		;
227	/*
228	** If time_t is signed, then 0 is the median value,
229	** if time_t is unsigned, then 1 << bits is median.
230	*/
231	t = (t < 0) ? 0 : ((time_t) 1 << bits);
232	for ( ; ; ) {
233		if (usezn)
234			mytm = *localtime(&t);
235		else
236			mytm = *gmtime(&t);
237		dir = tmcomp(&mytm, &yourtm);
238		if (dir != 0) {
239			if (bits-- < 0)
240				return WRONG;
241			if (bits < 0)
242				--t;
243			else if (dir > 0)
244				t -= (time_t) 1 << bits;
245			else	t += (time_t) 1 << bits;
246			continue;
247		}
248		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
249			break;
250
251		return WRONG;
252	}
253	t += saved_seconds;
254	if (usezn)
255		*tmp = *localtime(&t);
256	else
257		*tmp = *gmtime(&t);
258	*okayp = TRUE;
259	return t;
260}
261#else
262int mktime_bs;
263#endif /* !HAVE_MKTIME || !HAVE_TIMEGM */
264
265#ifndef HAVE_MKTIME
266static time_t
267time1(
268	struct tm * tmp
269	)
270{
271	register time_t			t;
272	int				okay;
273
274	if (tmp->tm_isdst > 1)
275		tmp->tm_isdst = 1;
276	t = time2(tmp, &okay, 1);
277	if (okay || tmp->tm_isdst < 0)
278		return t;
279
280	return WRONG;
281}
282
283time_t
284mktime(
285	struct tm * tmp
286	)
287{
288	return time1(tmp);
289}
290#endif /* !HAVE_MKTIME */
291
292#ifdef WANT_TIMEGM
293#ifndef HAVE_TIMEGM
294time_t
295timegm(
296	struct tm * tmp
297	)
298{
299	register time_t			t;
300	int				okay;
301
302	tmp->tm_isdst = 0;
303	t = time2(tmp, &okay, 0);
304	if (okay || tmp->tm_isdst < 0)
305		return t;
306
307	return WRONG;
308}
309#endif /* !HAVE_TIMEGM */
310#endif /* WANT_TIMEGM */
311