(AC_FUNC_MKTIME): Undefine to avoid syntax errors from m4.
[pspp] / m4 / mktime.m4
1 #serial 1000
2
3 dnl From Paul Eggert
4 dnl Check for a working mktime.
5 dnl This is a preview of what should appear in the next public autoconf release.
6
7 dnl Override any existing definition.
8 undefine([AC_FUNC_MKTIME])
9
10 AC_DEFUN(AC_FUNC_MKTIME,
11 [AC_REQUIRE([AC_HEADER_TIME])dnl
12 AC_CHECK_HEADERS(sys/time.h unistd.h)
13 AC_CHECK_FUNCS(alarm)
14 AC_CACHE_CHECK([for working mktime], ac_cv_func_working_mktime,
15 [AC_TRY_RUN(
16 changequote(<<, >>)dnl
17 <</* Test program from Paul Eggert (eggert@twinsun.com)
18    and Tony Leneis (tony@plaza.ds.adp.com).  */
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #if HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #if !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
43 /* Values we'll use to set the TZ environment variable.  */
44 static const char *const tz_strings[] = {
45   (const char *) 0, "TZ=GMT0", "TZ=JST-9",
46   "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
47 };
48 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
49
50 /* Fail if mktime fails to convert a date in the spring-forward gap.
51    Based on a problem report from Andreas Jaeger.  */
52 static void
53 spring_forward_gap ()
54 {
55   /* glibc (up to about 1998-10-07) failed this test) */
56   struct tm tm;
57
58   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
59      instead of "TZ=America/Vancouver" in order to detect the bug even
60      on systems that don't support the Olson extension, or don't have the
61      full zoneinfo tables installed.  */
62   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
63
64   tm.tm_year = 98;
65   tm.tm_mon = 3;
66   tm.tm_mday = 5;
67   tm.tm_hour = 2;
68   tm.tm_min = 0;
69   tm.tm_sec = 0;
70   tm.tm_isdst = -1;
71   if (mktime (&tm) == (time_t)-1)
72     exit (1);
73 }
74
75 static void
76 mktime_test (now)
77      time_t now;
78 {
79   struct tm *lt;
80   if ((lt = localtime (&now)) && mktime (lt) != now)
81     exit (1);
82   now = time_t_max - now;
83   if ((lt = localtime (&now)) && mktime (lt) != now)
84     exit (1);
85 }
86
87 static void
88 irix_6_4_bug ()
89 {
90   /* Based on code from Ariel Faigon.  */
91   struct tm tm;
92   tm.tm_year = 96;
93   tm.tm_mon = 3;
94   tm.tm_mday = 0;
95   tm.tm_hour = 0;
96   tm.tm_min = 0;
97   tm.tm_sec = 0;
98   tm.tm_isdst = -1;
99   mktime (&tm);
100   if (tm.tm_mon != 2 || tm.tm_mday != 31)
101     exit (1);
102 }
103
104 static void
105 bigtime_test (j)
106      int j;
107 {
108   struct tm tm;
109   time_t now;
110   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
111   now = mktime (&tm);
112   if (now != (time_t) -1)
113     {
114       struct tm *lt = localtime (&now);
115       if (! (lt
116              && lt->tm_year == tm.tm_year
117              && lt->tm_mon == tm.tm_mon
118              && lt->tm_mday == tm.tm_mday
119              && lt->tm_hour == tm.tm_hour
120              && lt->tm_min == tm.tm_min
121              && lt->tm_sec == tm.tm_sec
122              && lt->tm_yday == tm.tm_yday
123              && lt->tm_wday == tm.tm_wday
124              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
125                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
126         exit (1);
127     }
128 }
129
130 int
131 main ()
132 {
133   time_t t, delta;
134   int i, j;
135
136   /* This test makes some buggy mktime implementations loop.
137      Give up after 60 seconds; a mktime slower than that
138      isn't worth using anyway.  */
139   alarm (60);
140
141   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
142     continue;
143   time_t_max--;
144   delta = time_t_max / 997; /* a suitable prime number */
145   for (i = 0; i < N_STRINGS; i++)
146     {
147       if (tz_strings[i])
148         putenv (tz_strings[i]);
149
150       for (t = 0; t <= time_t_max - delta; t += delta)
151         mktime_test (t);
152       mktime_test ((time_t) 60 * 60);
153       mktime_test ((time_t) 60 * 60 * 24);
154
155       for (j = 1; 0 < j; j *= 2)
156         bigtime_test (j);
157       bigtime_test (j - 1);
158     }
159   irix_6_4_bug ();
160   spring_forward_gap ();
161   exit (0);
162 }
163 >>,
164 changequote([, ])dnl
165 ac_cv_func_working_mktime=yes, ac_cv_func_working_mktime=no,
166 ac_cv_func_working_mktime=no)])
167 if test $ac_cv_func_working_mktime = no; then
168   LIBOBJS="$LIBOBJS mktime.${ac_objext}"
169 fi
170 ])