xref: /netbsd-src/external/bsd/ntp/dist/libntp/mktime.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: mktime.c,v 1.1.1.1 2009/12/13 16:55:03 kardel Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1989 Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Arthur David Olson of the National Cancer Institute.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.  */
37 
38 /*static char *sccsid = "from: @(#)ctime.c	5.26 (Berkeley) 2/23/91";*/
39 
40 /*
41  * This implementation of mktime is lifted straight from the NetBSD (BSD 4.4)
42  * version.  I modified it slightly to divorce it from the internals of the
43  * ctime library.  Thus this version can't use details of the internal
44  * timezone state file to figure out strange unnormalized struct tm values,
45  * as might result from someone doing date math on the tm struct then passing
46  * it to mktime.
47  *
48  * It just does as well as it can at normalizing the tm input, then does a
49  * binary search of the time space using the system's localtime() function.
50  *
51  * The original binary search was defective in that it didn't consider the
52  * setting of tm_isdst when comparing tm values, causing the search to be
53  * flubbed for times near the dst/standard time changeover.  The original
54  * code seems to make up for this by grubbing through the timezone info
55  * whenever the binary search barfed.  Since I don't have that luxury in
56  * portable code, I have to take care of tm_isdst in the comparison routine.
57  * This requires knowing how many minutes offset dst is from standard time.
58  *
59  * So, if you live somewhere in the world where dst is not 60 minutes offset,
60  * and your vendor doesn't supply mktime(), you'll have to edit this variable
61  * by hand.  Sorry about that.
62  */
63 
64 #include "ntp_machine.h"
65 
66 #if !defined(HAVE_MKTIME) || !defined(HAVE_TIMEGM)
67 
68 #ifndef DSTMINUTES
69 #define DSTMINUTES 60
70 #endif
71 
72 #define FALSE 0
73 #define TRUE 1
74 
75 /* some constants from tzfile.h */
76 #define SECSPERMIN      60
77 #define MINSPERHOUR     60
78 #define HOURSPERDAY     24
79 #define DAYSPERWEEK     7
80 #define DAYSPERNYEAR    365
81 #define DAYSPERLYEAR    366
82 #define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
83 #define SECSPERDAY      ((long) SECSPERHOUR * HOURSPERDAY)
84 #define MONSPERYEAR     12
85 #define TM_YEAR_BASE    1900
86 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
87 
88 static int	mon_lengths[2][MONSPERYEAR] = {
89 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
90 	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
91 };
92 
93 static int	year_lengths[2] = {
94 	DAYSPERNYEAR, DAYSPERLYEAR
95 };
96 
97 /*
98 ** Adapted from code provided by Robert Elz, who writes:
99 **	The "best" way to do mktime I think is based on an idea of Bob
100 **	Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
101 **	It does a binary search of the time_t space.  Since time_t's are
102 **	just 32 bits, its a max of 32 iterations (even at 64 bits it
103 **	would still be very reasonable).
104 */
105 
106 #ifndef WRONG
107 #define WRONG	(-1)
108 #endif /* !defined WRONG */
109 
110 static void
111 normalize(
112 	int * tensptr,
113 	int * unitsptr,
114 	int	base
115 	)
116 {
117 	if (*unitsptr >= base) {
118 		*tensptr += *unitsptr / base;
119 		*unitsptr %= base;
120 	} else if (*unitsptr < 0) {
121 		--*tensptr;
122 		*unitsptr += base;
123 		if (*unitsptr < 0) {
124 			*tensptr -= 1 + (-*unitsptr) / base;
125 			*unitsptr = base - (-*unitsptr) % base;
126 		}
127 	}
128 }
129 
130 static struct tm *
131 mkdst(
132 	struct tm *	tmp
133 	)
134 {
135     /* jds */
136     static struct tm tmbuf;
137 
138     tmbuf = *tmp;
139     tmbuf.tm_isdst = 1;
140     tmbuf.tm_min += DSTMINUTES;
141     normalize(&tmbuf.tm_hour, &tmbuf.tm_min, MINSPERHOUR);
142     return &tmbuf;
143 }
144 
145 static int
146 tmcomp(
147 	register struct tm * atmp,
148 	register struct tm * btmp
149 	)
150 {
151 	register int	result;
152 
153 	/* compare down to the same day */
154 
155 	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
156 	    (result = (atmp->tm_mon - btmp->tm_mon)) == 0)
157 	    result = (atmp->tm_mday - btmp->tm_mday);
158 
159 	if(result != 0)
160 	    return result;
161 
162 	/* get rid of one-sided dst bias */
163 
164 	if(atmp->tm_isdst == 1 && !btmp->tm_isdst)
165 	    btmp = mkdst(btmp);
166 	else if(btmp->tm_isdst == 1 && !atmp->tm_isdst)
167 	    atmp = mkdst(atmp);
168 
169 	/* compare the rest of the way */
170 
171 	if ((result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
172 	    (result = (atmp->tm_min - btmp->tm_min)) == 0)
173 	    result = atmp->tm_sec - btmp->tm_sec;
174 	return result;
175 }
176 
177 
178 static time_t
179 time2(
180 	struct tm *	tmp,
181 	int * 		okayp,
182 	int		usezn
183 	)
184 {
185 	register int			dir;
186 	register int			bits;
187 	register int			i;
188 	register int			saved_seconds;
189 	time_t				t;
190 	struct tm			yourtm, mytm;
191 
192 	*okayp = FALSE;
193 	yourtm = *tmp;
194 	if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
195 		normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
196 	normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
197 	normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
198 	normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
199 	while (yourtm.tm_mday <= 0) {
200 		--yourtm.tm_year;
201 		yourtm.tm_mday +=
202 			year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
203 	}
204 	for ( ; ; ) {
205 		i = mon_lengths[isleap(yourtm.tm_year +
206 			TM_YEAR_BASE)][yourtm.tm_mon];
207 		if (yourtm.tm_mday <= i)
208 			break;
209 		yourtm.tm_mday -= i;
210 		if (++yourtm.tm_mon >= MONSPERYEAR) {
211 			yourtm.tm_mon = 0;
212 			++yourtm.tm_year;
213 		}
214 	}
215 	saved_seconds = yourtm.tm_sec;
216 	yourtm.tm_sec = 0;
217 	/*
218 	** Calculate the number of magnitude bits in a time_t
219 	** (this works regardless of whether time_t is
220 	** signed or unsigned, though lint complains if unsigned).
221 	*/
222 	for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
223 		;
224 	/*
225 	** If time_t is signed, then 0 is the median value,
226 	** if time_t is unsigned, then 1 << bits is median.
227 	*/
228 	t = (t < 0) ? 0 : ((time_t) 1 << bits);
229 	for ( ; ; ) {
230 		if (usezn)
231 	        	mytm = *localtime(&t);
232 		else
233 	        	mytm = *gmtime(&t);
234 		dir = tmcomp(&mytm, &yourtm);
235 		if (dir != 0) {
236 			if (bits-- < 0)
237 				return WRONG;
238 			if (bits < 0)
239 				--t;
240 			else if (dir > 0)
241 				t -= (time_t) 1 << bits;
242 			else	t += (time_t) 1 << bits;
243 			continue;
244 		}
245 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
246 			break;
247 
248 		return WRONG;
249 	}
250 	t += saved_seconds;
251 	if (usezn)
252 		*tmp = *localtime(&t);
253 	else
254 		*tmp = *gmtime(&t);
255 	*okayp = TRUE;
256 	return t;
257 }
258 #else
259 int mktime_bs;
260 #endif /* !HAVE_MKTIME || !HAVE_TIMEGM */
261 
262 #ifndef HAVE_MKTIME
263 static time_t
264 time1(
265 	struct tm * tmp
266 	)
267 {
268 	register time_t			t;
269 	int				okay;
270 
271 	if (tmp->tm_isdst > 1)
272 		tmp->tm_isdst = 1;
273 	t = time2(tmp, &okay, 1);
274 	if (okay || tmp->tm_isdst < 0)
275 		return t;
276 
277 	return WRONG;
278 }
279 
280 time_t
281 mktime(
282 	struct tm * tmp
283 	)
284 {
285 	return time1(tmp);
286 }
287 #endif /* !HAVE_MKTIME */
288 
289 #ifndef HAVE_TIMEGM
290 time_t
291 timegm(
292 	struct tm * tmp
293 	)
294 {
295 	register time_t			t;
296 	int				okay;
297 
298 	tmp->tm_isdst = 0;
299 	t = time2(tmp, &okay, 0);
300 	if (okay || tmp->tm_isdst < 0)
301 		return t;
302 
303 	return WRONG;
304 }
305 #endif /* !HAVE_TIMEGM */
306