Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / m4 / mktime.m4
blob4b3e399be9f4e1e2c982cb783c891591cc46b11d
1 # serial 30
2 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2018 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.
8 dnl From Jim Meyering.
10 AC_DEFUN([gl_TIME_T_IS_SIGNED],
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
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_CHECK_FUNCS_ONCE([tzset])
35   AC_REQUIRE([gl_MULTIARCH])
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=no
41   fi
42   AC_CACHE_CHECK([for working mktime], [gl_cv_func_working_mktime],
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>
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #endif
54 #if HAVE_DECL_ALARM
55 # include <signal.h>
56 #endif
58 #ifndef TIME_T_IS_SIGNED
59 # define TIME_T_IS_SIGNED 0
60 #endif
62 /* Work around redefinition to rpl_putenv by other config tests.  */
63 #undef putenv
65 static time_t time_t_max;
66 static time_t time_t_min;
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"
73 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
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 ()
80   /* glibc (up to about 1998-10-07) failed this test. */
81   struct tm tm;
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   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
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;
99 static int
100 mktime_test1 (time_t now)
102   struct tm *lt;
103   return ! (lt = localtime (&now)) || mktime (lt) == now;
106 static int
107 mktime_test (time_t now)
109   return (mktime_test1 (now)
110           && mktime_test1 ((time_t) (time_t_max - now))
111           && mktime_test1 ((time_t) (time_t_min + now)));
114 static int
115 irix_6_4_bug ()
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;
130 static int
131 bigtime_test (int j)
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;
156 static int
157 year_2050_test ()
159   /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
160      ignoring leap seconds.  */
161   unsigned long int answer = 2527315200UL;
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;
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   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
177   t = mktime (&tm);
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));
187 main ()
189   int result = 0;
190   time_t t, delta;
191   int i, j;
192   int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
194 #if HAVE_DECL_ALARM
195   /* This test makes some buggy mktime implementations loop.
196      Give up after 60 seconds; a mktime slower than that
197      isn't worth using anyway.  */
198   signal (SIGALRM, SIG_DFL);
199   alarm (60);
200 #endif
202   time_t_max = (! TIME_T_IS_SIGNED
203                 ? (time_t) -1
204                 : ((((time_t) 1 << (sizeof (time_t) * CHAR_BIT - 2)) - 1)
205                    * 2 + 1));
206   time_t_min = (! TIME_T_IS_SIGNED
207                 ? (time_t) 0
208                 : time_t_signed_magnitude
209                 ? ~ (time_t) 0
210                 : ~ time_t_max);
212   delta = time_t_max / 997; /* a suitable prime number */
213   for (i = 0; i < N_STRINGS; i++)
214     {
215       if (tz_strings[i])
216         putenv (tz_strings[i]);
218       for (t = 0; t <= time_t_max - delta && (result & 1) == 0; t += delta)
219         if (! mktime_test (t))
220           result |= 1;
221       if ((result & 2) == 0
222           && ! (mktime_test ((time_t) 1)
223                 && mktime_test ((time_t) (60 * 60))
224                 && mktime_test ((time_t) (60 * 60 * 24))))
225         result |= 2;
227       for (j = 1; (result & 4) == 0; j <<= 1)
228         {
229           if (! bigtime_test (j))
230             result |= 4;
231           if (INT_MAX / 2 < j)
232             break;
233         }
234       if ((result & 8) == 0 && ! bigtime_test (INT_MAX))
235         result |= 8;
236     }
237   if (! irix_6_4_bug ())
238     result |= 16;
239   if (! spring_forward_gap ())
240     result |= 32;
241   if (! year_2050_test ())
242     result |= 64;
243   return result;
244 }]])],
245        [gl_cv_func_working_mktime=yes],
246        [gl_cv_func_working_mktime=no],
247        [case "$host_os" in
248                   # Guess no on native Windows.
249           mingw*) gl_cv_func_working_mktime="guessing no" ;;
250           *)      gl_cv_func_working_mktime="guessing no" ;;
251         esac
252        ])
253     ])
256 dnl Main macro of module 'mktime'.
257 AC_DEFUN([gl_FUNC_MKTIME],
259   AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
260   AC_REQUIRE([AC_CANONICAL_HOST])
261   AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
263   REPLACE_MKTIME=0
264   if test "$gl_cv_func_working_mktime" != yes; then
265     REPLACE_MKTIME=1
266     AC_DEFINE([NEED_MKTIME_WORKING], [1],
267       [Define if the compilation of mktime.c should define 'mktime'
268        with the algorithmic workarounds.])
269   fi
270   case "$host_os" in
271     mingw*)
272       REPLACE_MKTIME=1
273       AC_DEFINE([NEED_MKTIME_WINDOWS], [1],
274         [Define if the compilation of mktime.c should define 'mktime'
275          with the native Windows TZ workaround.])
276       ;;
277   esac
280 dnl Main macro of module 'mktime-internal'.
281 AC_DEFUN([gl_FUNC_MKTIME_INTERNAL], [
282   AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
284   WANT_MKTIME_INTERNAL=0
285   dnl BeOS has __mktime_internal in libc, but other platforms don't.
286   AC_CHECK_FUNC([__mktime_internal],
287     [AC_DEFINE([mktime_internal], [__mktime_internal],
288        [Define to the real name of the mktime_internal function.])
289     ],
290     [dnl mktime works but it doesn't export __mktime_internal,
291      dnl so we need to substitute our own mktime implementation.
292      WANT_MKTIME_INTERNAL=1
293      AC_DEFINE([NEED_MKTIME_INTERNAL], [1],
294        [Define if the compilation of mktime.c should define 'mktime_internal'.])
295     ])
298 # Prerequisites of lib/mktime.c.
299 AC_DEFUN([gl_PREREQ_MKTIME], [:])