Debian Codegraph @

m4/mktime.m4

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