Debian Codegraph @

m4/mktime.m4

raw
1 # serial 37
2 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2022 Free Software Foundation,
3 dnl Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7
8 dnl From Jim Meyering.
9
10 AC_DEFUN([gl_TIME_T_IS_SIGNED],
11 [
12 AC_CACHE_CHECK([whether time_t is signed],
13 [gl_cv_time_t_is_signed],
14 [AC_COMPILE_IFELSE(
15 [AC_LANG_PROGRAM([[#include <time.h>
16 char time_t_signed[(time_t) -1 < 0 ? 1 : -1];]])],
17 [gl_cv_time_t_is_signed=yes],
18 [gl_cv_time_t_is_signed=no])])
19 if test $gl_cv_time_t_is_signed = yes; then
20 AC_DEFINE([TIME_T_IS_SIGNED], [1], [Define to 1 if time_t is signed.])
21 fi
22 ])
23
24 dnl Test whether mktime works. Set gl_cv_func_working_mktime.
25 AC_DEFUN([gl_FUNC_MKTIME_WORKS],
26 [
27 AC_REQUIRE([gl_TIME_T_IS_SIGNED])
28 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
29
30 dnl We don't use AC_FUNC_MKTIME any more, because it is no longer maintained
31 dnl in Autoconf and because it invokes AC_LIBOBJ.
32 AC_CHECK_HEADERS_ONCE([unistd.h])
33 AC_CHECK_DECLS_ONCE([alarm])
34 AC_REQUIRE([gl_MULTIARCH])
35 AC_CACHE_CHECK([for working mktime], [gl_cv_func_working_mktime],
36 [if test $APPLE_UNIVERSAL_BUILD = 1; then
37 # A universal build on Apple Mac OS X platforms.
38 # The test result would be 'yes' in 32-bit mode and 'no' in 64-bit mode.
39 # But we need a configuration result that is valid in both modes.
40 gl_cv_func_working_mktime="guessing no"
41 else
42 AC_RUN_IFELSE(
43 [AC_LANG_SOURCE(
44 [[/* Test program from Paul Eggert and Tony Leneis. */
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <time.h>
48
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52
53 #if HAVE_DECL_ALARM
54 # include <signal.h>
55 #endif
56
57 ]GL_MDA_DEFINES[
58
59 #ifndef TIME_T_IS_SIGNED
60 # define TIME_T_IS_SIGNED 0
61 #endif
62
63 static time_t time_t_max;
64 static time_t time_t_min;
65
66 /* Values we'll use to set the TZ environment variable. */
67 static char *tz_strings[] = {
68 (char *) 0, "TZ=GMT0", "TZ=JST-9",
69 "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
70 };
71 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
72
73 /* Return 0 if mktime fails to convert a date in the spring-forward gap.
74 Based on a problem report from Andreas Jaeger. */
75 static int
76 spring_forward_gap ()
77 {
78 /* glibc (up to about 1998-10-07) failed this test. */
79 struct tm tm;
80
81 /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
82 instead of "TZ=America/Vancouver" in order to detect the bug even
83 on systems that don't support the Olson extension, or don't have the
84 full zoneinfo tables installed. */
85 if (putenv ("TZ=PST8PDT,M4.1.0,M10.5.0") != 0)
86 return -1;
87
88 tm.tm_year = 98;
89 tm.tm_mon = 3;
90 tm.tm_mday = 5;
91 tm.tm_hour = 2;
92 tm.tm_min = 0;
93 tm.tm_sec = 0;
94 tm.tm_isdst = -1;
95 return mktime (&tm) != (time_t) -1;
96 }
97
98 static int
99 mktime_test1 (time_t now)
100 {
101 struct tm *lt;
102 return ! (lt = localtime (&now)) || mktime (lt) == now;
103 }
104
105 static int
106 mktime_test (time_t now)
107 {
108 return (mktime_test1 (now)
109 && mktime_test1 ((time_t) (time_t_max - now))
110 && mktime_test1 ((time_t) (time_t_min + now)));
111 }
112
113 static int
114 irix_6_4_bug ()
115 {
116 /* Based on code from Ariel Faigon. */
117 struct tm tm;
118 tm.tm_year = 96;
119 tm.tm_mon = 3;
120 tm.tm_mday = 0;
121 tm.tm_hour = 0;
122 tm.tm_min = 0;
123 tm.tm_sec = 0;
124 tm.tm_isdst = -1;
125 mktime (&tm);
126 return tm.tm_mon == 2 && tm.tm_mday == 31;
127 }
128
129 static int
130 bigtime_test (int j)
131 {
132 struct tm tm;
133 time_t now;
134 tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
135 now = mktime (&tm);
136 if (now != (time_t) -1)
137 {
138 struct tm *lt = localtime (&now);
139 if (! (lt
140 && lt->tm_year == tm.tm_year
141 && lt->tm_mon == tm.tm_mon
142 && lt->tm_mday == tm.tm_mday
143 && lt->tm_hour == tm.tm_hour
144 && lt->tm_min == tm.tm_min
145 && lt->tm_sec == tm.tm_sec
146 && lt->tm_yday == tm.tm_yday
147 && lt->tm_wday == tm.tm_wday
148 && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
149 == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
150 return 0;
151 }
152 return 1;
153 }
154
155 static int
156 year_2050_test ()
157 {
158 /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
159 ignoring leap seconds. */
160 unsigned long int answer = 2527315200UL;
161
162 struct tm tm;
163 time_t t;
164 tm.tm_year = 2050 - 1900;
165 tm.tm_mon = 2 - 1;
166 tm.tm_mday = 1;
167 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
168 tm.tm_isdst = -1;
169
170 /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
171 instead of "TZ=America/Vancouver" in order to detect the bug even
172 on systems that don't support the Olson extension, or don't have the
173 full zoneinfo tables installed. */
174 if (putenv ("TZ=PST8PDT,M4.1.0,M10.5.0") != 0)
175 return -1;
176
177 t = mktime (&tm);
178
179 /* Check that the result is either a failure, or close enough
180 to the correct answer that we can assume the discrepancy is
181 due to leap seconds. */
182 return (t == (time_t) -1
183 || (0 < t && answer - 120 <= t && t <= answer + 120));
184 }
185
186 static int
187 indiana_test ()
188 {
189 if (putenv ("TZ=America/Indiana/Indianapolis") != 0)
190 return -1;
191 struct tm tm;
192 tm.tm_year = 1986 - 1900; tm.tm_mon = 4 - 1; tm.tm_mday = 28;
193 tm.tm_hour = 16; tm.tm_min = 24; tm.tm_sec = 50; tm.tm_isdst = 0;
194 time_t std = mktime (&tm);
195 if (! (std == 515107490 || std == 515107503))
196 return 1;
197
198 /* This platform supports TZDB, either without or with leap seconds.
199 Return true if GNU Bug#48085 is absent. */
200 tm.tm_isdst = 1;
201 time_t dst = mktime (&tm);
202 return std - dst == 60 * 60;
203 }
204
205 int
206 main ()
207 {
208 int result = 0;
209 time_t t, delta;
210 int i, j;
211 int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
212
213 #if HAVE_DECL_ALARM
214 /* This test makes some buggy mktime implementations loop.
215 Give up after 60 seconds; a mktime slower than that
216 isn't worth using anyway. */
217 signal (SIGALRM, SIG_DFL);
218 alarm (60);
219 #endif
220
221 time_t_max = (! TIME_T_IS_SIGNED
222 ? (time_t) -1
223 : ((((time_t) 1 << (sizeof (time_t) * CHAR_BIT - 2)) - 1)
224 * 2 + 1));
225 time_t_min = (! TIME_T_IS_SIGNED
226 ? (time_t) 0
227 : time_t_signed_magnitude
228 ? ~ (time_t) 0
229 : ~ time_t_max);
230
231 delta = time_t_max / 997; /* a suitable prime number */
232 for (i = 0; i < N_STRINGS; i++)
233 {
234 if (tz_strings[i])
235 putenv (tz_strings[i]);
236
237 for (t = 0; t <= time_t_max - delta && (result & 1) == 0; t += delta)
238 if (! mktime_test (t))
239 result |= 1;
240 if ((result & 2) == 0
241 && ! (mktime_test ((time_t) 1)
242 && mktime_test ((time_t) (60 * 60))
243 && mktime_test ((time_t) (60 * 60 * 24))))
244 result |= 2;
245
246 for (j = 1; (result & 4) == 0; j <<= 1)
247 {
248 if (! bigtime_test (j))
249 result |= 4;
250 if (INT_MAX / 2 < j)
251 break;
252 }
253 if ((result & 8) == 0 && ! bigtime_test (INT_MAX))
254 result |= 8;
255 }
256 if (! irix_6_4_bug ())
257 result |= 16;
258 if (! spring_forward_gap ())
259 result |= 32;
260 if (! year_2050_test () || ! indiana_test ())
261 result |= 64;
262 return result;
263 }]])],
264 [gl_cv_func_working_mktime=yes],
265 [gl_cv_func_working_mktime=no],
266 [case "$host_os" in
267 # Guess no on native Windows.
268 mingw*) gl_cv_func_working_mktime="guessing no" ;;
269 *) gl_cv_func_working_mktime="$gl_cross_guess_normal" ;;
270 esac
271 ])
272 fi
273 ])
274 ])
275
276 dnl Main macro of module 'mktime'.
277 AC_DEFUN([gl_FUNC_MKTIME],
278 [
279 AC_REQUIRE([gl_TIME_H_DEFAULTS])
280 AC_REQUIRE([AC_CANONICAL_HOST])
281 AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
282
283 REPLACE_MKTIME=0
284 if test "$gl_cv_func_working_mktime" != yes; then
285 REPLACE_MKTIME=1
286 AC_DEFINE([NEED_MKTIME_WORKING], [1],
287 [Define if the compilation of mktime.c should define 'mktime'
288 with the algorithmic workarounds.])
289 fi
290 case "$host_os" in
291 mingw*)
292 REPLACE_MKTIME=1
293 AC_DEFINE([NEED_MKTIME_WINDOWS], [1],
294 [Define if the compilation of mktime.c should define 'mktime'
295 with the native Windows TZ workaround.])
296 ;;
297 esac
298 ])
299
300 dnl Main macro of module 'mktime-internal'.
301 AC_DEFUN([gl_FUNC_MKTIME_INTERNAL], [
302 AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
303
304 WANT_MKTIME_INTERNAL=0
305 dnl BeOS has __mktime_internal in libc, but other platforms don't.
306 AC_CHECK_FUNC([__mktime_internal],
307 [AC_DEFINE([mktime_internal], [__mktime_internal],
308 [Define to the real name of the mktime_internal function.])
309 ],
310 [dnl mktime works but it doesn't export __mktime_internal,
311 dnl so we need to substitute our own mktime implementation.
312 WANT_MKTIME_INTERNAL=1
313 AC_DEFINE([NEED_MKTIME_INTERNAL], [1],
314 [Define if the compilation of mktime.c should define 'mktime_internal'.])
315 ])
316 ])
317
318 # Prerequisites of lib/mktime.c.
319 AC_DEFUN([gl_PREREQ_MKTIME], [:])