Merge jrn/db/vcs-svn-housekeeping:vcs-svn into master
[svn-fe.git] / compat / mkgmtime.c
blob97322a763ad29cf2b2d4616c5ac098851a824430
1 /* mkgmtime.c - make time corresponding to a GMT timeval struct
2 * $Id: mkgmtime.c,v 1.10 2003/10/22 18:50:12 rjs3 Exp $
4 * Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * 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
15 * the documentation and/or other materials provided with the
16 * distribution.
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any other legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
24 * 5000 Forbes Avenue
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
30 * acknowledgment:
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 * Copyright (c) 1987, 1989, 1993
45 * The Regents of the University of California. All rights reserved.
47 * This code is derived from software contributed to Berkeley by
48 * Arthur David Olson of the National Cancer Institute.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. All advertising materials mentioning features or use of this software
59 * must display the following acknowledgement:
60 * This product includes software developed by the University of
61 * California, Berkeley and its contributors.
62 * 4. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * SUCH DAMAGE.
80 * Adapted from code provided by Robert Elz, who writes:
81 * The "best" way to do mktime I think is based on an idea of Bob
82 * Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
83 * It does a binary search of the time_t space. Since time_t's are
84 * just 32 bits, its a max of 32 iterations (even at 64 bits it
85 * would still be very reasonable).
88 #include "mkgmtime.h"
90 #ifndef WRONG
91 #define WRONG (-1)
92 #endif /* !defined WRONG */
94 static int tmcomp(const struct tm *const atmp, const struct tm *const btmp)
96 register int result;
98 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
99 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
100 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
101 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
102 (result = (atmp->tm_min - btmp->tm_min)) == 0)
103 result = atmp->tm_sec - btmp->tm_sec;
104 return result;
107 time_t mkgmtime(struct tm *const tmp)
109 register int dir;
110 register int bits;
111 register int saved_seconds;
112 time_t t;
113 struct tm yourtm, *mytm;
115 yourtm = *tmp;
116 saved_seconds = yourtm.tm_sec;
117 yourtm.tm_sec = 0;
119 * Calculate the number of magnitude bits in a time_t
120 * (this works regardless of whether time_t is
121 * signed or unsigned, though lint complains if unsigned).
123 for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
126 * If time_t is signed, then 0 is the median value,
127 * if time_t is unsigned, then 1 << bits is median.
129 t = (t < 0) ? 0 : ((time_t) 1 << bits);
131 /* Some gmtime() implementations are broken and will return
132 * NULL for time_ts larger than 40 bits even on 64-bit platforms
133 * so we'll just cap it at 40 bits
135 if(bits > 40) bits = 40;
137 for ( ; ; ) {
138 mytm = gmtime(&t);
140 if(!mytm) return WRONG;
142 dir = tmcomp(mytm, &yourtm);
143 if (dir != 0) {
144 if (bits-- < 0)
145 return WRONG;
146 if (bits < 0)
147 --t;
148 else if (dir > 0)
149 t -= (time_t) 1 << bits;
150 else
151 t += (time_t) 1 << bits;
152 continue;
154 break;
156 t += saved_seconds;
157 return t;