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