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