Sync from coreutils.
[gnulib.git] / m4 / mktime.m4
1 #serial 7
2 dnl Copyright (C) 2002, 2003, 2005 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.57 and earlier.
10 # This redefinition can be removed once a new version of Autoconf comes out.
11 # The redefinition is taken from
12 # <http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/autoconf/autoconf/lib/autoconf/functions.m4?rev=1.78>.
13 # AC_FUNC_MKTIME
14 # --------------
15 AC_DEFUN([AC_FUNC_MKTIME],
16 [AC_REQUIRE([AC_HEADER_TIME])dnl
17 AC_CHECK_HEADERS_ONCE(sys/time.h)
18 AC_CHECK_FUNCS_ONCE(alarm)
19 AC_CACHE_CHECK([for working mktime], ac_cv_func_working_mktime,
20 [AC_RUN_IFELSE([AC_LANG_SOURCE(
21 [[/* Test program from Paul Eggert and Tony Leneis.  */
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #if !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 /* Fail if mktime fails to convert a date in the spring-forward gap.
54    Based on a problem report from Andreas Jaeger.  */
55 static void
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   if (mktime (&tm) == (time_t)-1)
75     exit (1);
76 }
77
78 static void
79 mktime_test1 (now)
80      time_t now;
81 {
82   struct tm *lt;
83   if ((lt = localtime (&now)) && mktime (lt) != now)
84     exit (1);
85 }
86
87 static void
88 mktime_test (now)
89      time_t now;
90 {
91   mktime_test1 (now);
92   mktime_test1 ((time_t) (time_t_max - now));
93   mktime_test1 ((time_t) (time_t_min + now));
94 }
95
96 static void
97 irix_6_4_bug ()
98 {
99   /* Based on code from Ariel Faigon.  */
100   struct tm tm;
101   tm.tm_year = 96;
102   tm.tm_mon = 3;
103   tm.tm_mday = 0;
104   tm.tm_hour = 0;
105   tm.tm_min = 0;
106   tm.tm_sec = 0;
107   tm.tm_isdst = -1;
108   mktime (&tm);
109   if (tm.tm_mon != 2 || tm.tm_mday != 31)
110     exit (1);
111 }
112
113 static void
114 bigtime_test (j)
115      int j;
116 {
117   struct tm tm;
118   time_t now;
119   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
120   now = mktime (&tm);
121   if (now != (time_t) -1)
122     {
123       struct tm *lt = localtime (&now);
124       if (! (lt
125              && lt->tm_year == tm.tm_year
126              && lt->tm_mon == tm.tm_mon
127              && lt->tm_mday == tm.tm_mday
128              && lt->tm_hour == tm.tm_hour
129              && lt->tm_min == tm.tm_min
130              && lt->tm_sec == tm.tm_sec
131              && lt->tm_yday == tm.tm_yday
132              && lt->tm_wday == tm.tm_wday
133              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
134                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
135         exit (1);
136     }
137 }
138
139 int
140 main ()
141 {
142   time_t t, delta;
143   int i, j;
144
145   /* This test makes some buggy mktime implementations loop.
146      Give up after 60 seconds; a mktime slower than that
147      isn't worth using anyway.  */
148   alarm (60);
149
150   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
151     continue;
152   time_t_max--;
153   if ((time_t) -1 < 0)
154     for (time_t_min = -1; (time_t) (time_t_min * 2) < 0; time_t_min *= 2)
155       continue;
156   delta = time_t_max / 997; /* a suitable prime number */
157   for (i = 0; i < N_STRINGS; i++)
158     {
159       if (tz_strings[i])
160         putenv (tz_strings[i]);
161
162       for (t = 0; t <= time_t_max - delta; t += delta)
163         mktime_test (t);
164       mktime_test ((time_t) 1);
165       mktime_test ((time_t) (60 * 60));
166       mktime_test ((time_t) (60 * 60 * 24));
167
168       for (j = 1; 0 < j; j *= 2)
169         bigtime_test (j);
170       bigtime_test (j - 1);
171     }
172   irix_6_4_bug ();
173   spring_forward_gap ();
174   exit (0);
175 }]])],
176                [ac_cv_func_working_mktime=yes],
177                [ac_cv_func_working_mktime=no],
178                [ac_cv_func_working_mktime=no])])
179 if test $ac_cv_func_working_mktime = no; then
180   AC_LIBOBJ([mktime])
181 fi
182 ])# AC_FUNC_MKTIME
183
184 AC_DEFUN([gl_FUNC_MKTIME],
185 [
186   AC_REQUIRE([AC_FUNC_MKTIME])
187   if test $ac_cv_func_working_mktime = no; then
188     AC_DEFINE(mktime, rpl_mktime,
189       [Define to rpl_mktime if the replacement function should be used.])
190     gl_PREREQ_MKTIME
191   fi
192 ])
193
194 # Prerequisites of lib/mktime.c.
195 AC_DEFUN([gl_PREREQ_MKTIME], [:])