]> git.cworth.org Git - tar/blob - m4/mktime.m4
Imported Upstream version 1.24
[tar] / m4 / mktime.m4
1 # serial 16
2 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2010 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   time_t t, delta;
166   int i, j;
167
168   /* This test makes some buggy mktime implementations loop.
169      Give up after 60 seconds; a mktime slower than that
170      isn't worth using anyway.  */
171   alarm (60);
172
173   for (;;)
174     {
175       t = (time_t_max << 1) + 1;
176       if (t <= time_t_max)
177         break;
178       time_t_max = t;
179     }
180   time_t_min = - ((time_t) ~ (time_t) 0 == (time_t) -1) - time_t_max;
181
182   delta = time_t_max / 997; /* a suitable prime number */
183   for (i = 0; i < N_STRINGS; i++)
184     {
185       if (tz_strings[i])
186         putenv (tz_strings[i]);
187
188       for (t = 0; t <= time_t_max - delta; t += delta)
189         if (! mktime_test (t))
190           return 1;
191       if (! (mktime_test ((time_t) 1)
192              && mktime_test ((time_t) (60 * 60))
193              && mktime_test ((time_t) (60 * 60 * 24))))
194         return 1;
195
196       for (j = 1; ; j <<= 1)
197         if (! bigtime_test (j))
198           return 1;
199         else if (INT_MAX / 2 < j)
200           break;
201       if (! bigtime_test (INT_MAX))
202         return 1;
203     }
204   return ! (irix_6_4_bug () && spring_forward_gap () && year_2050_test ());
205 }]])],
206                [ac_cv_func_working_mktime=yes],
207                [ac_cv_func_working_mktime=no],
208                [ac_cv_func_working_mktime=no])])
209 if test $ac_cv_func_working_mktime = no; then
210   AC_LIBOBJ([mktime])
211 fi
212 ])# AC_FUNC_MKTIME
213
214 AC_DEFUN([gl_FUNC_MKTIME],
215 [
216   AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
217   AC_FUNC_MKTIME
218   dnl Note: AC_FUNC_MKTIME does AC_LIBOBJ([mktime]).
219   if test $ac_cv_func_working_mktime = no; then
220     REPLACE_MKTIME=1
221     gl_PREREQ_MKTIME
222   else
223     REPLACE_MKTIME=0
224   fi
225 ])
226
227 # Prerequisites of lib/mktime.c.
228 AC_DEFUN([gl_PREREQ_MKTIME],
229 [
230   AC_REQUIRE([AC_C_INLINE])
231 ])