Merge branch 'master' into comment-cache
[emacs.git] / m4 / mktime.m4
blobd594ddc58be24cdc665168474a8b65b83723455c
1 # serial 27
2 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2017 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 AC_DEFUN([gl_FUNC_MKTIME],
26   AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
27   AC_REQUIRE([gl_TIME_T_IS_SIGNED])
29   dnl We don't use AC_FUNC_MKTIME any more, because it is no longer maintained
30   dnl in Autoconf and because it invokes AC_LIBOBJ.
31   AC_CHECK_HEADERS_ONCE([unistd.h])
32   AC_CHECK_DECLS_ONCE([alarm])
33   AC_CHECK_FUNCS_ONCE([tzset])
34   AC_REQUIRE([gl_MULTIARCH])
35   if test $APPLE_UNIVERSAL_BUILD = 1; then
36     # A universal build on Apple Mac OS X platforms.
37     # The test result would be 'yes' in 32-bit mode and 'no' in 64-bit mode.
38     # But we need a configuration result that is valid in both modes.
39     gl_cv_func_working_mktime=no
40   fi
41   AC_CACHE_CHECK([for working mktime], [gl_cv_func_working_mktime],
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>
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
53 #if HAVE_DECL_ALARM
54 # include <signal.h>
55 #endif
57 /* Work around redefinition to rpl_putenv by other config tests.  */
58 #undef putenv
60 static time_t time_t_max;
61 static time_t time_t_min;
63 /* Values we'll use to set the TZ environment variable.  */
64 static char *tz_strings[] = {
65   (char *) 0, "TZ=GMT0", "TZ=JST-9",
66   "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
68 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
70 /* Return 0 if mktime fails to convert a date in the spring-forward gap.
71    Based on a problem report from Andreas Jaeger.  */
72 static int
73 spring_forward_gap ()
75   /* glibc (up to about 1998-10-07) failed this test. */
76   struct tm tm;
78   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
79      instead of "TZ=America/Vancouver" in order to detect the bug even
80      on systems that don't support the Olson extension, or don't have the
81      full zoneinfo tables installed.  */
82   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
84   tm.tm_year = 98;
85   tm.tm_mon = 3;
86   tm.tm_mday = 5;
87   tm.tm_hour = 2;
88   tm.tm_min = 0;
89   tm.tm_sec = 0;
90   tm.tm_isdst = -1;
91   return mktime (&tm) != (time_t) -1;
94 static int
95 mktime_test1 (time_t now)
97   struct tm *lt;
98   return ! (lt = localtime (&now)) || mktime (lt) == now;
101 static int
102 mktime_test (time_t now)
104   return (mktime_test1 (now)
105           && mktime_test1 ((time_t) (time_t_max - now))
106           && mktime_test1 ((time_t) (time_t_min + now)));
109 static int
110 irix_6_4_bug ()
112   /* Based on code from Ariel Faigon.  */
113   struct tm tm;
114   tm.tm_year = 96;
115   tm.tm_mon = 3;
116   tm.tm_mday = 0;
117   tm.tm_hour = 0;
118   tm.tm_min = 0;
119   tm.tm_sec = 0;
120   tm.tm_isdst = -1;
121   mktime (&tm);
122   return tm.tm_mon == 2 && tm.tm_mday == 31;
125 static int
126 bigtime_test (int j)
128   struct tm tm;
129   time_t now;
130   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
131   now = mktime (&tm);
132   if (now != (time_t) -1)
133     {
134       struct tm *lt = localtime (&now);
135       if (! (lt
136              && lt->tm_year == tm.tm_year
137              && lt->tm_mon == tm.tm_mon
138              && lt->tm_mday == tm.tm_mday
139              && lt->tm_hour == tm.tm_hour
140              && lt->tm_min == tm.tm_min
141              && lt->tm_sec == tm.tm_sec
142              && lt->tm_yday == tm.tm_yday
143              && lt->tm_wday == tm.tm_wday
144              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
145                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
146         return 0;
147     }
148   return 1;
151 static int
152 year_2050_test ()
154   /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
155      ignoring leap seconds.  */
156   unsigned long int answer = 2527315200UL;
158   struct tm tm;
159   time_t t;
160   tm.tm_year = 2050 - 1900;
161   tm.tm_mon = 2 - 1;
162   tm.tm_mday = 1;
163   tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
164   tm.tm_isdst = -1;
166   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
167      instead of "TZ=America/Vancouver" in order to detect the bug even
168      on systems that don't support the Olson extension, or don't have the
169      full zoneinfo tables installed.  */
170   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
172   t = mktime (&tm);
174   /* Check that the result is either a failure, or close enough
175      to the correct answer that we can assume the discrepancy is
176      due to leap seconds.  */
177   return (t == (time_t) -1
178           || (0 < t && answer - 120 <= t && t <= answer + 120));
182 main ()
184   int result = 0;
185   time_t t, delta;
186   int i, j;
187   int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
189 #if HAVE_DECL_ALARM
190   /* This test makes some buggy mktime implementations loop.
191      Give up after 60 seconds; a mktime slower than that
192      isn't worth using anyway.  */
193   signal (SIGALRM, SIG_DFL);
194   alarm (60);
195 #endif
197   time_t_max = (! TIME_T_IS_SIGNED
198                 ? (time_t) -1
199                 : ((((time_t) 1 << (sizeof (time_t) * CHAR_BIT - 2)) - 1)
200                    * 2 + 1));
201   time_t_min = (! TIME_T_IS_SIGNED
202                 ? (time_t) 0
203                 : time_t_signed_magnitude
204                 ? ~ (time_t) 0
205                 : ~ time_t_max);
207   delta = time_t_max / 997; /* a suitable prime number */
208   for (i = 0; i < N_STRINGS; i++)
209     {
210       if (tz_strings[i])
211         putenv (tz_strings[i]);
213       for (t = 0; t <= time_t_max - delta && (result & 1) == 0; t += delta)
214         if (! mktime_test (t))
215           result |= 1;
216       if ((result & 2) == 0
217           && ! (mktime_test ((time_t) 1)
218                 && mktime_test ((time_t) (60 * 60))
219                 && mktime_test ((time_t) (60 * 60 * 24))))
220         result |= 2;
222       for (j = 1; (result & 4) == 0; j <<= 1)
223         {
224           if (! bigtime_test (j))
225             result |= 4;
226           if (INT_MAX / 2 < j)
227             break;
228         }
229       if ((result & 8) == 0 && ! bigtime_test (INT_MAX))
230         result |= 8;
231     }
232   if (! irix_6_4_bug ())
233     result |= 16;
234   if (! spring_forward_gap ())
235     result |= 32;
236   if (! year_2050_test ())
237     result |= 64;
238   return result;
239 }]])],
240        [gl_cv_func_working_mktime=yes],
241        [gl_cv_func_working_mktime=no],
242        [gl_cv_func_working_mktime=no])
243     ])
245   if test $gl_cv_func_working_mktime = no; then
246     REPLACE_MKTIME=1
247   else
248     REPLACE_MKTIME=0
249   fi
252 AC_DEFUN([gl_FUNC_MKTIME_INTERNAL], [
253   AC_REQUIRE([gl_FUNC_MKTIME])
254   if test $REPLACE_MKTIME = 0; then
255     dnl BeOS has __mktime_internal in libc, but other platforms don't.
256     AC_CHECK_FUNC([__mktime_internal],
257       [AC_DEFINE([mktime_internal], [__mktime_internal],
258          [Define to the real name of the mktime_internal function.])
259       ],
260       [dnl mktime works but it doesn't export __mktime_internal,
261        dnl so we need to substitute our own mktime implementation.
262        REPLACE_MKTIME=1
263       ])
264   fi
267 # Prerequisites of lib/mktime.c.
268 AC_DEFUN([gl_PREREQ_MKTIME], [:])