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