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