Import upstream version 1.26
[debian/tar] / m4 / mktime.m4
1 # serial 19
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   int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
169   int time_t_signed = ! ((time_t) 0 < (time_t) -1);
170
171   /* This test makes some buggy mktime implementations loop.
172      Give up after 60 seconds; a mktime slower than that
173      isn't worth using anyway.  */
174   alarm (60);
175
176   time_t_max = (! time_t_signed
177                 ? (time_t) -1
178                 : ((((time_t) 1 << (sizeof (time_t) * CHAR_BIT - 2)) - 1)
179                    * 2 + 1));
180   time_t_min = (! time_t_signed
181                 ? (time_t) 0
182                 : time_t_signed_magnitude
183                 ? ~ (time_t) 0
184                 : ~ time_t_max);
185
186   delta = time_t_max / 997; /* a suitable prime number */
187   for (i = 0; i < N_STRINGS; i++)
188     {
189       if (tz_strings[i])
190         putenv (tz_strings[i]);
191
192       for (t = 0; t <= time_t_max - delta; t += delta)
193         if (! mktime_test (t))
194           result |= 1;
195       if (! (mktime_test ((time_t) 1)
196              && mktime_test ((time_t) (60 * 60))
197              && mktime_test ((time_t) (60 * 60 * 24))))
198         result |= 2;
199
200       for (j = 1; ; j <<= 1)
201         if (! bigtime_test (j))
202           result |= 4;
203         else if (INT_MAX / 2 < j)
204           break;
205       if (! bigtime_test (INT_MAX))
206         result |= 8;
207     }
208   if (! irix_6_4_bug ())
209     result |= 16;
210   if (! spring_forward_gap ())
211     result |= 32;
212   if (! year_2050_test ())
213     result |= 64;
214   return result;
215 }]])],
216                [ac_cv_func_working_mktime=yes],
217                [ac_cv_func_working_mktime=no],
218                [ac_cv_func_working_mktime=no])])
219 if test $ac_cv_func_working_mktime = no; then
220   AC_LIBOBJ([mktime])
221 fi
222 ])# AC_FUNC_MKTIME
223
224 AC_DEFUN([gl_FUNC_MKTIME],
225 [
226   AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
227   AC_FUNC_MKTIME
228   dnl Note: AC_FUNC_MKTIME does AC_LIBOBJ([mktime]).
229   if test $ac_cv_func_working_mktime = no; then
230     REPLACE_MKTIME=1
231     gl_PREREQ_MKTIME
232   else
233     REPLACE_MKTIME=0
234   fi
235 ])
236
237 # Prerequisites of lib/mktime.c.
238 AC_DEFUN([gl_PREREQ_MKTIME],
239 [
240   AC_REQUIRE([AC_C_INLINE])
241 ])