* config/srclist.txt: Temporarily comment out mktime.c until glibc bug
[gnulib.git] / m4 / mktime.m4
1 #serial 8
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.60a 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/lib/autoconf/functions.m4?rev=1.108&root=autoconf>.
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 <stdlib.h>
34
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38
39 #ifndef HAVE_ALARM
40 # define alarm(X) /* empty */
41 #endif
42
43 /* Work around redefinition to rpl_putenv by other config tests.  */
44 #undef putenv
45
46 static time_t time_t_max;
47 static time_t time_t_min;
48
49 /* Values we'll use to set the TZ environment variable.  */
50 static char *tz_strings[] = {
51   (char *) 0, "TZ=GMT0", "TZ=JST-9",
52   "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
53 };
54 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
55
56 /* Return 0 if mktime fails to convert a date in the spring-forward gap.
57    Based on a problem report from Andreas Jaeger.  */
58 static int
59 spring_forward_gap ()
60 {
61   /* glibc (up to about 1998-10-07) failed this test. */
62   struct tm tm;
63
64   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
65      instead of "TZ=America/Vancouver" in order to detect the bug even
66      on systems that don't support the Olson extension, or don't have the
67      full zoneinfo tables installed.  */
68   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
69
70   tm.tm_year = 98;
71   tm.tm_mon = 3;
72   tm.tm_mday = 5;
73   tm.tm_hour = 2;
74   tm.tm_min = 0;
75   tm.tm_sec = 0;
76   tm.tm_isdst = -1;
77   return mktime (&tm) != (time_t) -1;
78 }
79
80 static int
81 mktime_test1 (now)
82      time_t now;
83 {
84   struct tm *lt;
85   return ! (lt = localtime (&now)) || mktime (lt) == now;
86 }
87
88 static int
89 mktime_test (now)
90      time_t now;
91 {
92   return (mktime_test1 (now)
93           && mktime_test1 ((time_t) (time_t_max - now))
94           && mktime_test1 ((time_t) (time_t_min + now)));
95 }
96
97 static int
98 irix_6_4_bug ()
99 {
100   /* Based on code from Ariel Faigon.  */
101   struct tm tm;
102   tm.tm_year = 96;
103   tm.tm_mon = 3;
104   tm.tm_mday = 0;
105   tm.tm_hour = 0;
106   tm.tm_min = 0;
107   tm.tm_sec = 0;
108   tm.tm_isdst = -1;
109   mktime (&tm);
110   return tm.tm_mon == 2 && tm.tm_mday == 31;
111 }
112
113 static int
114 bigtime_test (j)
115      int j;
116 {
117   struct tm tm;
118   time_t now;
119   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
120   now = mktime (&tm);
121   if (now != (time_t) -1)
122     {
123       struct tm *lt = localtime (&now);
124       if (! (lt
125              && lt->tm_year == tm.tm_year
126              && lt->tm_mon == tm.tm_mon
127              && lt->tm_mday == tm.tm_mday
128              && lt->tm_hour == tm.tm_hour
129              && lt->tm_min == tm.tm_min
130              && lt->tm_sec == tm.tm_sec
131              && lt->tm_yday == tm.tm_yday
132              && lt->tm_wday == tm.tm_wday
133              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
134                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
135         return 0;
136     }
137   return 1;
138 }
139
140 static int
141 year_2050_test ()
142 {
143   /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
144      ignoring leap seconds.  */
145   unsigned long int answer = 2527315200UL;
146
147   struct tm tm;
148   time_t t;
149   tm.tm_year = 2050 - 1900;
150   tm.tm_mon = 2 - 1;
151   tm.tm_mday = 1;
152   tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
153   tm.tm_isdst = -1;
154
155   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
156      instead of "TZ=America/Vancouver" in order to detect the bug even
157      on systems that don't support the Olson extension, or don't have the
158      full zoneinfo tables installed.  */
159   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
160
161   t = mktime (&tm);
162
163   /* Check that the result is either a failure, or close enough
164      to the correct answer that we can assume the discrepancy is
165      due to leap seconds.  */
166   return (t == (time_t) -1
167           || (0 < t && answer - 120 <= t && t <= answer + 120));
168 }
169
170 int
171 main ()
172 {
173   time_t t, delta;
174   int i, j;
175
176   /* This test makes some buggy mktime implementations loop.
177      Give up after 60 seconds; a mktime slower than that
178      isn't worth using anyway.  */
179   alarm (60);
180
181   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
182     continue;
183   time_t_max--;
184   if ((time_t) -1 < 0)
185     for (time_t_min = -1; (time_t) (time_t_min * 2) < 0; time_t_min *= 2)
186       continue;
187   delta = time_t_max / 997; /* a suitable prime number */
188   for (i = 0; i < N_STRINGS; i++)
189     {
190       if (tz_strings[i])
191         putenv (tz_strings[i]);
192
193       for (t = 0; t <= time_t_max - delta; t += delta)
194         if (! mktime_test (t))
195           return 1;
196       if (! (mktime_test ((time_t) 1)
197              && mktime_test ((time_t) (60 * 60))
198              && mktime_test ((time_t) (60 * 60 * 24))))
199         return 1;
200
201       for (j = 1; 0 < j; j *= 2)
202         if (! bigtime_test (j))
203           return 1;
204       if (! bigtime_test (j - 1))
205         return 1;
206     }
207   return ! (irix_6_4_bug () && spring_forward_gap () && year_2050_test ());
208 }]])],
209                [ac_cv_func_working_mktime=yes],
210                [ac_cv_func_working_mktime=no],
211                [ac_cv_func_working_mktime=no])])
212 if test $ac_cv_func_working_mktime = no; then
213   AC_LIBOBJ([mktime])
214 fi
215 ])# AC_FUNC_MKTIME
216
217 AC_DEFUN([gl_FUNC_MKTIME],
218 [
219   AC_REQUIRE([AC_FUNC_MKTIME])
220   if test $ac_cv_func_working_mktime = no; then
221     AC_DEFINE(mktime, rpl_mktime,
222       [Define to rpl_mktime if the replacement function should be used.])
223     gl_PREREQ_MKTIME
224   fi
225 ])
226
227 # Prerequisites of lib/mktime.c.
228 AC_DEFUN([gl_PREREQ_MKTIME], [:])