(AM_FUNC_MKTIME): Run tests for each of a few values
[gnulib.git] / m4 / mktime.m4
1 #serial 2
2
3 dnl From Jim Meyering.
4 dnl FIXME: this should migrate into libit.
5
6 AC_DEFUN(AM_FUNC_MKTIME,
7 [AC_REQUIRE([AC_HEADER_TIME])dnl
8  AC_CHECK_HEADERS(sys/time.h)
9  AC_CACHE_CHECK([for working mktime], am_cv_func_working_mktime,
10   [AC_TRY_RUN(
11 changequote(<<, >>)dnl
12 <</* Test program from Paul Eggert (eggert@twinsun.com)
13    and Tony Leneis (tony@plaza.ds.adp.com).  */
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
16 # include <time.h>
17 #else
18 # if HAVE_SYS_TIME_H
19 #  include <sys/time.h>
20 # else
21 #  include <time.h>
22 # endif
23 #endif
24
25 static time_t time_t_max;
26
27 /* Values we'll use to set the TZ environment variable.  */
28 static const char *const tz_strings[] = {
29   NULL, "GMT0", "JST-9", "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
30 };
31 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
32
33 static void
34 mktime_test (now)
35      time_t now;
36 {
37   if (mktime (localtime (&now)) != now)
38     exit (1);
39   now = time_t_max - now;
40   if (mktime (localtime (&now)) != now)
41     exit (1);
42 }
43
44 int
45 main ()
46 {
47   time_t t, delta;
48   int i;
49
50   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
51     continue;
52   time_t_max--;
53   delta = time_t_max / 997; /* a suitable prime number */
54   for (i = 0; i < N_STRINGS; i++)
55     {
56       if (tz_strings[i])
57         putenv (tz_strings[i]);
58
59       for (t = 0; t <= time_t_max - delta; t += delta)
60         mktime_test (t);
61       mktime_test ((time_t) 60 * 60);
62       mktime_test ((time_t) 60 * 60 * 24);
63     }
64   exit (0);
65 }
66               >>,
67 changequote([, ])dnl
68              am_cv_func_working_mktime=yes, am_cv_func_working_mktime=no,
69              dnl When crosscompiling, assume mktime is missing or broken.
70              am_cv_func_working_mktime=no)
71   ])
72   if test $am_cv_func_working_mktime = no; then
73     LIBOBJS="$LIBOBJS mktime.o"
74   fi
75 ])