(jm_AM_FUNC_MKTIME): New file and macro.
[gnulib.git] / m4 / mktime.m4
1 #serial 1000
2
3 dnl Just like mktime.m4 from automake-1.3b, but with an additional test.
4 dnl Renamed to have jm_ prefix and to use jm_ prefix on cache variable names.
5
6 AC_DEFUN(jm_AM_FUNC_MKTIME,
7 [AC_REQUIRE([AC_HEADER_TIME])dnl
8  AC_CHECK_HEADERS(sys/time.h unistd.h)
9  AC_CHECK_FUNCS(alarm)
10  AC_CACHE_CHECK([for working mktime], jm_am_cv_func_working_mktime,
11   [AC_TRY_RUN(
12 changequote(<<, >>)dnl
13 <</* Test program from Paul Eggert (eggert@twinsun.com)
14    and Tony Leneis (tony@plaza.ds.adp.com).  */
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
17 # include <time.h>
18 #else
19 # if HAVE_SYS_TIME_H
20 #  include <sys/time.h>
21 # else
22 #  include <time.h>
23 # endif
24 #endif
25
26 #if HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29
30 #if !HAVE_ALARM
31 # define alarm(X) /* empty */
32 #endif
33
34 /* Work around redefinition to rpl_putenv by other config tests.  */
35 #undef putenv
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, "TZ=GMT0", "TZ=JST-9",
42   "TZ=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 /* Fail if mktime fails to convert a date in the spring-forward gap.
47    Based on a problem report from Andreas Jaeger.  */
48 static void
49 spring_forward_gap ()
50 {
51   /* glibc (up to about 1998-10-07) failed this test) */
52   struct tm tm;
53   putenv ("TZ=America/Vancouver");
54   tm.tm_year = 98;
55   tm.tm_mon = 3;
56   tm.tm_mday = 5;
57   tm.tm_hour = 2;
58   tm.tm_min = 0;
59   tm.tm_sec = 0;
60   tm.tm_isdst = -1;
61   if (mktime (&tm) == (time_t)-1)
62     exit (1);
63 }
64
65 static void
66 mktime_test (now)
67      time_t now;
68 {
69   struct tm *lt;
70   if ((lt = localtime (&now)) && mktime (lt) != now)
71     exit (1);
72   now = time_t_max - now;
73   if ((lt = localtime (&now)) && mktime (lt) != now)
74     exit (1);
75 }
76
77 static void
78 irix_6_4_bug ()
79 {
80   /* Based on code from Ariel Faigon.  */
81   struct tm tm;
82   tm.tm_year = 96;
83   tm.tm_mon = 3;
84   tm.tm_mday = 0;
85   tm.tm_hour = 0;
86   tm.tm_min = 0;
87   tm.tm_sec = 0;
88   tm.tm_isdst = -1;
89   mktime (&tm);
90   if (tm.tm_mon != 2 || tm.tm_mday != 31)
91     exit (1);
92 }
93
94 static void
95 bigtime_test (j)
96      int j;
97 {
98   struct tm tm;
99   time_t now;
100   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
101   /* This test makes some buggy mktime implementations loop.
102      Give up after 10 seconds.  */
103   alarm (10);
104   now = mktime (&tm);
105   alarm (0);
106   if (now != (time_t) -1)
107     {
108       struct tm *lt = localtime (&now);
109       if (! (lt
110              && lt->tm_year == tm.tm_year
111              && lt->tm_mon == tm.tm_mon
112              && lt->tm_mday == tm.tm_mday
113              && lt->tm_hour == tm.tm_hour
114              && lt->tm_min == tm.tm_min
115              && lt->tm_sec == tm.tm_sec
116              && lt->tm_yday == tm.tm_yday
117              && lt->tm_wday == tm.tm_wday
118              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
119                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
120         exit (1);
121     }
122 }
123
124 int
125 main ()
126 {
127   time_t t, delta;
128   int i, j;
129
130   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
131     continue;
132   time_t_max--;
133   delta = time_t_max / 997; /* a suitable prime number */
134   for (i = 0; i < N_STRINGS; i++)
135     {
136       if (tz_strings[i])
137         putenv (tz_strings[i]);
138
139       for (t = 0; t <= time_t_max - delta; t += delta)
140         mktime_test (t);
141       mktime_test ((time_t) 60 * 60);
142       mktime_test ((time_t) 60 * 60 * 24);
143
144       for (j = 1; 0 < j; j *= 2)
145         bigtime_test (j);
146       bigtime_test (j - 1);
147     }
148   irix_6_4_bug ();
149   spring_forward_gap ();
150   exit (0);
151 }
152               >>,
153 changequote([, ])dnl
154              jm_am_cv_func_working_mktime=yes, jm_am_cv_func_working_mktime=no,
155              dnl When crosscompiling, assume mktime is missing or broken.
156              jm_am_cv_func_working_mktime=no)
157   ])
158   if test $jm_am_cv_func_working_mktime = no; then
159     LIBOBJS="$LIBOBJS mktime.o"
160   fi
161 ])