2 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2020 Free Software Foundation,
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.
10 AC_DEFUN([gl_TIME_T_IS_SIGNED],
12 AC_CACHE_CHECK([whether time_t is signed],
13 [gl_cv_time_t_is_signed],
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.])
24 dnl Test whether mktime works. Set gl_cv_func_working_mktime.
25 AC_DEFUN([gl_FUNC_MKTIME_WORKS],
27 AC_REQUIRE([gl_TIME_T_IS_SIGNED])
28 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
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"
44 [[/* Test program from Paul Eggert and Tony Leneis. */
59 #ifndef TIME_T_IS_SIGNED
60 # define TIME_T_IS_SIGNED 0
63 static time_t time_t_max;
64 static time_t time_t_min;
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"
71 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
73 /* Return 0 if mktime fails to convert a date in the spring-forward gap.
74 Based on a problem report from Andreas Jaeger. */
78 /* glibc (up to about 1998-10-07) failed this test. */
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 putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
94 return mktime (&tm) != (time_t) -1;
98 mktime_test1 (time_t now)
101 return ! (lt = localtime (&now)) || mktime (lt) == now;
105 mktime_test (time_t now)
107 return (mktime_test1 (now)
108 && mktime_test1 ((time_t) (time_t_max - now))
109 && mktime_test1 ((time_t) (time_t_min + now)));
115 /* Based on code from Ariel Faigon. */
125 return tm.tm_mon == 2 && tm.tm_mday == 31;
133 tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
135 if (now != (time_t) -1)
137 struct tm *lt = localtime (&now);
139 && lt->tm_year == tm.tm_year
140 && lt->tm_mon == tm.tm_mon
141 && lt->tm_mday == tm.tm_mday
142 && lt->tm_hour == tm.tm_hour
143 && lt->tm_min == tm.tm_min
144 && lt->tm_sec == tm.tm_sec
145 && lt->tm_yday == tm.tm_yday
146 && lt->tm_wday == tm.tm_wday
147 && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
148 == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
157 /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
158 ignoring leap seconds. */
159 unsigned long int answer = 2527315200UL;
163 tm.tm_year = 2050 - 1900;
166 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
169 /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
170 instead of "TZ=America/Vancouver" in order to detect the bug even
171 on systems that don't support the Olson extension, or don't have the
172 full zoneinfo tables installed. */
173 putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
177 /* Check that the result is either a failure, or close enough
178 to the correct answer that we can assume the discrepancy is
179 due to leap seconds. */
180 return (t == (time_t) -1
181 || (0 < t && answer - 120 <= t && t <= answer + 120));
190 int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
193 /* This test makes some buggy mktime implementations loop.
194 Give up after 60 seconds; a mktime slower than that
195 isn't worth using anyway. */
196 signal (SIGALRM, SIG_DFL);
200 time_t_max = (! TIME_T_IS_SIGNED
202 : ((((time_t) 1 << (sizeof (time_t) * CHAR_BIT - 2)) - 1)
204 time_t_min = (! TIME_T_IS_SIGNED
206 : time_t_signed_magnitude
210 delta = time_t_max / 997; /* a suitable prime number */
211 for (i = 0; i < N_STRINGS; i++)
214 putenv (tz_strings[i]);
216 for (t = 0; t <= time_t_max - delta && (result & 1) == 0; t += delta)
217 if (! mktime_test (t))
219 if ((result & 2) == 0
220 && ! (mktime_test ((time_t) 1)
221 && mktime_test ((time_t) (60 * 60))
222 && mktime_test ((time_t) (60 * 60 * 24))))
225 for (j = 1; (result & 4) == 0; j <<= 1)
227 if (! bigtime_test (j))
232 if ((result & 8) == 0 && ! bigtime_test (INT_MAX))
235 if (! irix_6_4_bug ())
237 if (! spring_forward_gap ())
239 if (! year_2050_test ())
243 [gl_cv_func_working_mktime=yes],
244 [gl_cv_func_working_mktime=no],
246 # Guess no on native Windows.
247 mingw*) gl_cv_func_working_mktime="guessing no" ;;
248 *) gl_cv_func_working_mktime="$gl_cross_guess_normal" ;;
255 dnl Main macro of module 'mktime'.
256 AC_DEFUN([gl_FUNC_MKTIME],
258 AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
259 AC_REQUIRE([AC_CANONICAL_HOST])
260 AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
263 if test "$gl_cv_func_working_mktime" != yes; then
265 AC_DEFINE([NEED_MKTIME_WORKING], [1],
266 [Define if the compilation of mktime.c should define 'mktime'
267 with the algorithmic workarounds.])
272 AC_DEFINE([NEED_MKTIME_WINDOWS], [1],
273 [Define if the compilation of mktime.c should define 'mktime'
274 with the native Windows TZ workaround.])
279 dnl Main macro of module 'mktime-internal'.
280 AC_DEFUN([gl_FUNC_MKTIME_INTERNAL], [
281 AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
283 WANT_MKTIME_INTERNAL=0
284 dnl BeOS has __mktime_internal in libc, but other platforms don't.
285 AC_CHECK_FUNC([__mktime_internal],
286 [AC_DEFINE([mktime_internal], [__mktime_internal],
287 [Define to the real name of the mktime_internal function.])
289 [dnl mktime works but it doesn't export __mktime_internal,
290 dnl so we need to substitute our own mktime implementation.
291 WANT_MKTIME_INTERNAL=1
292 AC_DEFINE([NEED_MKTIME_INTERNAL], [1],
293 [Define if the compilation of mktime.c should define 'mktime_internal'.])
297 # Prerequisites of lib/mktime.c.
298 AC_DEFUN([gl_PREREQ_MKTIME], [:])