(bigtime_test): New function from Paul Eggert to detect SunOS4.1.4's
[pspp] / m4 / mktime.m4
1 #serial 103
2 dnl FIXME: when this goes back into automake, reset this to a small number
3
4 dnl From Jim Meyering.
5 dnl FIXME: this should migrate into libit.
6
7 dnl FIXME: when this goes back into automake, remove all jm_ prefixes
8
9 AC_DEFUN(jm_AM_FUNC_MKTIME,
10 [AC_REQUIRE([AC_HEADER_TIME])dnl
11  AC_CHECK_HEADERS(sys/time.h unistd.h)
12  AC_CHECK_FUNCS(alarm)
13  AC_CACHE_CHECK([for working mktime], jm_am_cv_func_working_mktime,
14   [AC_TRY_RUN(
15 changequote(<<, >>)dnl
16 <</* Test program from Paul Eggert (eggert@twinsun.com)
17    and Tony Leneis (tony@plaza.ds.adp.com).  */
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #if !HAVE_ALARM
30 # define alarm(X) /* empty */
31 #endif
32
33 static time_t time_t_max;
34
35 /* Values we'll use to set the TZ environment variable.  */
36 static const char *const tz_strings[] = {
37   (const char *) 0, "GMT0", "JST-9",
38   "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
39 };
40 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
41
42 static void
43 mktime_test (now)
44      time_t now;
45 {
46   struct tm *lt;
47   if ((lt = localtime (&now)) && mktime (lt) != now)
48     exit (1);
49   now = time_t_max - now;
50   if ((lt = localtime (&now)) && mktime (lt) != now)
51     exit (1);
52 }
53
54 static void
55 irix_6_4_bug ()
56 {
57   /* Based on code from Ariel Faigon.  */
58   struct tm tm;
59   tm.tm_year = 96;
60   tm.tm_mon = 3;
61   tm.tm_mday = 0;
62   tm.tm_hour = 0;
63   tm.tm_min = 0;
64   tm.tm_sec = 0;
65   tm.tm_isdst = -1;
66   mktime (&tm);
67   if (tm.tm_mon != 2 || tm.tm_mday != 31)
68     exit (1);
69 }
70
71 static void
72 bigtime_test (j)
73      int j;
74 {
75   struct tm tm;
76   time_t now;
77   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
78   /* This test makes some buggy mktime implementations loop.
79      Give up after 10 seconds.  */
80   alarm (10);
81   now = mktime (&tm);
82   alarm (0);
83   if (now != (time_t) -1)
84     {
85       struct tm *lt = localtime (&now);
86       if (! (lt
87              && lt->tm_year == tm.tm_year
88              && lt->tm_mon == tm.tm_mon
89              && lt->tm_mday == tm.tm_mday
90              && lt->tm_hour == tm.tm_hour
91              && lt->tm_min == tm.tm_min
92              && lt->tm_sec == tm.tm_sec
93              && lt->tm_yday == tm.tm_yday
94              && lt->tm_wday == tm.tm_wday
95              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
96                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
97         exit (1);
98     }
99 }
100
101 int
102 main ()
103 {
104   time_t t, delta;
105   int i, j;
106
107   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
108     continue;
109   time_t_max--;
110   delta = time_t_max / 997; /* a suitable prime number */
111   for (i = 0; i < N_STRINGS; i++)
112     {
113       if (tz_strings[i])
114         putenv (tz_strings[i]);
115
116       for (t = 0; t <= time_t_max - delta; t += delta)
117         mktime_test (t);
118       mktime_test ((time_t) 60 * 60);
119       mktime_test ((time_t) 60 * 60 * 24);
120
121       for (j = 1; 0 < j; j *= 2)
122         bigtime_test (j);
123       bigtime_test (j - 1);
124     }
125   irix_6_4_bug ();
126   exit (0);
127 }
128               >>,
129 changequote([, ])dnl
130              jm_am_cv_func_working_mktime=yes, jm_am_cv_func_working_mktime=no,
131              dnl When crosscompiling, assume mktime is missing or broken.
132              jm_am_cv_func_working_mktime=no)
133   ])
134   if test $jm_am_cv_func_working_mktime = no; then
135     LIBOBJS="$LIBOBJS mktime.o"
136   fi
137 ])