include unistd.h
[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_UNISTD_H
30 # include <unistd.h>
31 #endif
32
33 #if !HAVE_ALARM
34 # define alarm(X) /* empty */
35 #endif
36
37 static time_t time_t_max;
38
39 /* Values we'll use to set the TZ environment variable.  */
40 static const char *const tz_strings[] = {
41   (const char *) 0, "GMT0", "JST-9",
42   "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
43 };
44 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
45
46 static void
47 mktime_test (now)
48      time_t now;
49 {
50   struct tm *lt;
51   if ((lt = localtime (&now)) && mktime (lt) != now)
52     exit (1);
53   now = time_t_max - now;
54   if ((lt = localtime (&now)) && mktime (lt) != now)
55     exit (1);
56 }
57
58 static void
59 irix_6_4_bug ()
60 {
61   /* Based on code from Ariel Faigon.  */
62   struct tm tm;
63   tm.tm_year = 96;
64   tm.tm_mon = 3;
65   tm.tm_mday = 0;
66   tm.tm_hour = 0;
67   tm.tm_min = 0;
68   tm.tm_sec = 0;
69   tm.tm_isdst = -1;
70   mktime (&tm);
71   if (tm.tm_mon != 2 || tm.tm_mday != 31)
72     exit (1);
73 }
74
75 static void
76 bigtime_test (j)
77      int j;
78 {
79   struct tm tm;
80   time_t now;
81   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
82   /* This test makes some buggy mktime implementations loop.
83      Give up after 10 seconds.  */
84   alarm (10);
85   now = mktime (&tm);
86   alarm (0);
87   if (now != (time_t) -1)
88     {
89       struct tm *lt = localtime (&now);
90       if (! (lt
91              && lt->tm_year == tm.tm_year
92              && lt->tm_mon == tm.tm_mon
93              && lt->tm_mday == tm.tm_mday
94              && lt->tm_hour == tm.tm_hour
95              && lt->tm_min == tm.tm_min
96              && lt->tm_sec == tm.tm_sec
97              && lt->tm_yday == tm.tm_yday
98              && lt->tm_wday == tm.tm_wday
99              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
100                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
101         exit (1);
102     }
103 }
104
105 int
106 main ()
107 {
108   time_t t, delta;
109   int i, j;
110
111   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
112     continue;
113   time_t_max--;
114   delta = time_t_max / 997; /* a suitable prime number */
115   for (i = 0; i < N_STRINGS; i++)
116     {
117       if (tz_strings[i])
118         putenv (tz_strings[i]);
119
120       for (t = 0; t <= time_t_max - delta; t += delta)
121         mktime_test (t);
122       mktime_test ((time_t) 60 * 60);
123       mktime_test ((time_t) 60 * 60 * 24);
124
125       for (j = 1; 0 < j; j *= 2)
126         bigtime_test (j);
127       bigtime_test (j - 1);
128     }
129   irix_6_4_bug ();
130   exit (0);
131 }
132               >>,
133 changequote([, ])dnl
134              jm_am_cv_func_working_mktime=yes, jm_am_cv_func_working_mktime=no,
135              dnl When crosscompiling, assume mktime is missing or broken.
136              jm_am_cv_func_working_mktime=no)
137   ])
138   if test $jm_am_cv_func_working_mktime = no; then
139     LIBOBJS="$LIBOBJS mktime.o"
140   fi
141 ])