Temporarily add a copy of this file from the automake
[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)
12  AC_CACHE_CHECK([for working mktime], jm_am_cv_func_working_mktime,
13   [AC_TRY_RUN(
14 changequote(<<, >>)dnl
15 <</* Test program from Paul Eggert (eggert@twinsun.com)
16    and Tony Leneis (tony@plaza.ds.adp.com).  */
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27
28 static time_t time_t_max;
29
30 /* Values we'll use to set the TZ environment variable.  */
31 static const char *const tz_strings[] = {
32   NULL, "GMT0", "JST-9", "EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
33 };
34 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
35
36 static void
37 mktime_test (now)
38      time_t now;
39 {
40   if (mktime (localtime (&now)) != now)
41     exit (1);
42   now = time_t_max - now;
43   if (mktime (localtime (&now)) != now)
44     exit (1);
45 }
46
47 static void
48 irix_6_4_bug ()
49 {
50   /* Based on code from Ariel Faigon.  */
51   struct tm tm;
52   tm.tm_year = 96;
53   tm.tm_mon = 3;
54   tm.tm_mday = 0;
55   tm.tm_hour = 0;
56   tm.tm_min = 0;
57   tm.tm_sec = 0;
58   tm.tm_isdst = -1;
59   mktime (&tm);
60   if (tm.tm_mon != 2 || tm.tm_mday != 31)
61     exit (1);
62 }
63
64 int
65 main ()
66 {
67   time_t t, delta;
68   int i;
69
70   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
71     continue;
72   time_t_max--;
73   delta = time_t_max / 997; /* a suitable prime number */
74   for (i = 0; i < N_STRINGS; i++)
75     {
76       if (tz_strings[i])
77         putenv (tz_strings[i]);
78
79       for (t = 0; t <= time_t_max - delta; t += delta)
80         mktime_test (t);
81       mktime_test ((time_t) 60 * 60);
82       mktime_test ((time_t) 60 * 60 * 24);
83     }
84   irix_6_4_bug ();
85   exit (0);
86 }
87               >>,
88 changequote([, ])dnl
89              jm_am_cv_func_working_mktime=yes, jm_am_cv_func_working_mktime=no,
90              dnl When crosscompiling, assume mktime is missing or broken.
91              jm_am_cv_func_working_mktime=no)
92   ])
93   if test $jm_am_cv_func_working_mktime = no; then
94     LIBOBJS="$LIBOBJS mktime.o"
95   fi
96 ])