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