* m4/mktime.m4 (AC_FUNC_MKTIME): Sync from Autoconf.
[pspp] / m4 / mktime.m4
1 #serial 9
2 dnl Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl From Jim Meyering.
8
9 # Redefine AC_FUNC_MKTIME, to fix a bug in Autoconf 2.61a and earlier.
10 # This redefinition can be removed once a new version of Autoconf is assumed.
11 # The redefinition is taken from
12 # <http://cvs.sv.gnu.org/viewcvs/*checkout*/autoconf/autoconf/lib/autoconf/functions.m4?rev=1.119>.
13 # AC_FUNC_MKTIME
14 # --------------
15 AC_DEFUN([AC_FUNC_MKTIME],
16 [AC_REQUIRE([AC_HEADER_TIME])dnl
17 AC_CHECK_HEADERS_ONCE(sys/time.h unistd.h)
18 AC_CHECK_FUNCS_ONCE(alarm)
19 AC_CACHE_CHECK([for working mktime], ac_cv_func_working_mktime,
20 [AC_RUN_IFELSE([AC_LANG_SOURCE(
21 [[/* Test program from Paul Eggert and Tony Leneis.  */
22 #ifdef TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # ifdef HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <limits.h>
34 #include <stdlib.h>
35
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39
40 #ifndef HAVE_ALARM
41 # define alarm(X) /* empty */
42 #endif
43
44 /* Work around redefinition to rpl_putenv by other config tests.  */
45 #undef putenv
46
47 static time_t time_t_max;
48 static time_t time_t_min;
49
50 /* Values we'll use to set the TZ environment variable.  */
51 static char *tz_strings[] = {
52   (char *) 0, "TZ=GMT0", "TZ=JST-9",
53   "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
54 };
55 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
56
57 /* Return 0 if mktime fails to convert a date in the spring-forward gap.
58    Based on a problem report from Andreas Jaeger.  */
59 static int
60 spring_forward_gap ()
61 {
62   /* glibc (up to about 1998-10-07) failed this test. */
63   struct tm tm;
64
65   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
66      instead of "TZ=America/Vancouver" in order to detect the bug even
67      on systems that don't support the Olson extension, or don't have the
68      full zoneinfo tables installed.  */
69   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
70
71   tm.tm_year = 98;
72   tm.tm_mon = 3;
73   tm.tm_mday = 5;
74   tm.tm_hour = 2;
75   tm.tm_min = 0;
76   tm.tm_sec = 0;
77   tm.tm_isdst = -1;
78   return mktime (&tm) != (time_t) -1;
79 }
80
81 static int
82 mktime_test1 (now)
83      time_t now;
84 {
85   struct tm *lt;
86   return ! (lt = localtime (&now)) || mktime (lt) == now;
87 }
88
89 static int
90 mktime_test (now)
91      time_t now;
92 {
93   return (mktime_test1 (now)
94           && mktime_test1 ((time_t) (time_t_max - now))
95           && mktime_test1 ((time_t) (time_t_min + now)));
96 }
97
98 static int
99 irix_6_4_bug ()
100 {
101   /* Based on code from Ariel Faigon.  */
102   struct tm tm;
103   tm.tm_year = 96;
104   tm.tm_mon = 3;
105   tm.tm_mday = 0;
106   tm.tm_hour = 0;
107   tm.tm_min = 0;
108   tm.tm_sec = 0;
109   tm.tm_isdst = -1;
110   mktime (&tm);
111   return tm.tm_mon == 2 && tm.tm_mday == 31;
112 }
113
114 static int
115 bigtime_test (j)
116      int j;
117 {
118   struct tm tm;
119   time_t now;
120   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
121   now = mktime (&tm);
122   if (now != (time_t) -1)
123     {
124       struct tm *lt = localtime (&now);
125       if (! (lt
126              && lt->tm_year == tm.tm_year
127              && lt->tm_mon == tm.tm_mon
128              && lt->tm_mday == tm.tm_mday
129              && lt->tm_hour == tm.tm_hour
130              && lt->tm_min == tm.tm_min
131              && lt->tm_sec == tm.tm_sec
132              && lt->tm_yday == tm.tm_yday
133              && lt->tm_wday == tm.tm_wday
134              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
135                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
136         return 0;
137     }
138   return 1;
139 }
140
141 static int
142 year_2050_test ()
143 {
144   /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
145      ignoring leap seconds.  */
146   unsigned long int answer = 2527315200UL;
147
148   struct tm tm;
149   time_t t;
150   tm.tm_year = 2050 - 1900;
151   tm.tm_mon = 2 - 1;
152   tm.tm_mday = 1;
153   tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
154   tm.tm_isdst = -1;
155
156   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
157      instead of "TZ=America/Vancouver" in order to detect the bug even
158      on systems that don't support the Olson extension, or don't have the
159      full zoneinfo tables installed.  */
160   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
161
162   t = mktime (&tm);
163
164   /* Check that the result is either a failure, or close enough
165      to the correct answer that we can assume the discrepancy is
166      due to leap seconds.  */
167   return (t == (time_t) -1
168           || (0 < t && answer - 120 <= t && t <= answer + 120));
169 }
170
171 int
172 main ()
173 {
174   time_t t, delta;
175   int i, j;
176
177   /* This test makes some buggy mktime implementations loop.
178      Give up after 60 seconds; a mktime slower than that
179      isn't worth using anyway.  */
180   alarm (60);
181
182   for (;;)
183     {
184       t = (time_t_max << 1) + 1;
185       if (t <= time_t_max)
186         break;
187       time_t_max = t;
188     }
189   time_t_min = - ((time_t) ~ (time_t) 0 == (time_t) -1) - time_t_max;
190
191   delta = time_t_max / 997; /* a suitable prime number */
192   for (i = 0; i < N_STRINGS; i++)
193     {
194       if (tz_strings[i])
195         putenv (tz_strings[i]);
196
197       for (t = 0; t <= time_t_max - delta; t += delta)
198         if (! mktime_test (t))
199           return 1;
200       if (! (mktime_test ((time_t) 1)
201              && mktime_test ((time_t) (60 * 60))
202              && mktime_test ((time_t) (60 * 60 * 24))))
203         return 1;
204
205       for (j = 1; ; j <<= 1)
206         if (! bigtime_test (j))
207           return 1;
208         else if (INT_MAX / 2 < j)
209           break;
210       if (! bigtime_test (INT_MAX))
211         return 1;
212     }
213   return ! (irix_6_4_bug () && spring_forward_gap () && year_2050_test ());
214 }]])],
215                [ac_cv_func_working_mktime=yes],
216                [ac_cv_func_working_mktime=no],
217                [ac_cv_func_working_mktime=no])])
218 if test $ac_cv_func_working_mktime = no; then
219   AC_LIBOBJ([mktime])
220 fi
221 ])# AC_FUNC_MKTIME
222
223 AC_DEFUN([gl_FUNC_MKTIME],
224 [
225   AC_REQUIRE([AC_FUNC_MKTIME])
226   if test $ac_cv_func_working_mktime = no; then
227     AC_DEFINE(mktime, rpl_mktime,
228       [Define to rpl_mktime if the replacement function should be used.])
229     gl_PREREQ_MKTIME
230   fi
231 ])
232
233 # Prerequisites of lib/mktime.c.
234 AC_DEFUN([gl_PREREQ_MKTIME], [:])