Debian Codegraph @

m4/mktime.m4

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