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