New file, which is a preview of what should appear
[gnulib.git] / m4 / mktime.m4
1 #serial 1000
2
3 dnl From Paul Eggert
4 dnl Check for a working mktime.
5 dnl This is a preview of what should appear in the next public autoconf release.
6
7 AC_DEFUN(AC_FUNC_MKTIME,
8 [AC_REQUIRE([AC_HEADER_TIME])dnl
9 AC_CHECK_HEADERS(sys/time.h unistd.h)
10 AC_CHECK_FUNCS(alarm)
11 AC_CACHE_CHECK([for working mktime], ac_cv_func_working_mktime,
12 [AC_TRY_RUN(
13 changequote(<<, >>)dnl
14 <</* Test program from Paul Eggert (eggert@twinsun.com)
15    and Tony Leneis (tony@plaza.ds.adp.com).  */
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else
20 # if HAVE_SYS_TIME_H
21 #  include <sys/time.h>
22 # else
23 #  include <time.h>
24 # endif
25 #endif
26
27 #if HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30
31 #if !HAVE_ALARM
32 # define alarm(X) /* empty */
33 #endif
34
35 /* Work around redefinition to rpl_putenv by other config tests.  */
36 #undef putenv
37
38 static time_t time_t_max;
39
40 /* Values we'll use to set the TZ environment variable.  */
41 static const char *const tz_strings[] = {
42   (const char *) 0, "TZ=GMT0", "TZ=JST-9",
43   "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
44 };
45 #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
46
47 /* Fail if mktime fails to convert a date in the spring-forward gap.
48    Based on a problem report from Andreas Jaeger.  */
49 static void
50 spring_forward_gap ()
51 {
52   /* glibc (up to about 1998-10-07) failed this test) */
53   struct tm tm;
54
55   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
56      instead of "TZ=America/Vancouver" in order to detect the bug even
57      on systems that don't support the Olson extension, or don't have the
58      full zoneinfo tables installed.  */
59   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
60
61   tm.tm_year = 98;
62   tm.tm_mon = 3;
63   tm.tm_mday = 5;
64   tm.tm_hour = 2;
65   tm.tm_min = 0;
66   tm.tm_sec = 0;
67   tm.tm_isdst = -1;
68   if (mktime (&tm) == (time_t)-1)
69     exit (1);
70 }
71
72 static void
73 mktime_test (now)
74      time_t now;
75 {
76   struct tm *lt;
77   if ((lt = localtime (&now)) && mktime (lt) != now)
78     exit (1);
79   now = time_t_max - now;
80   if ((lt = localtime (&now)) && mktime (lt) != now)
81     exit (1);
82 }
83
84 static void
85 irix_6_4_bug ()
86 {
87   /* Based on code from Ariel Faigon.  */
88   struct tm tm;
89   tm.tm_year = 96;
90   tm.tm_mon = 3;
91   tm.tm_mday = 0;
92   tm.tm_hour = 0;
93   tm.tm_min = 0;
94   tm.tm_sec = 0;
95   tm.tm_isdst = -1;
96   mktime (&tm);
97   if (tm.tm_mon != 2 || tm.tm_mday != 31)
98     exit (1);
99 }
100
101 static void
102 bigtime_test (j)
103      int j;
104 {
105   struct tm tm;
106   time_t now;
107   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
108   now = mktime (&tm);
109   if (now != (time_t) -1)
110     {
111       struct tm *lt = localtime (&now);
112       if (! (lt
113              && lt->tm_year == tm.tm_year
114              && lt->tm_mon == tm.tm_mon
115              && lt->tm_mday == tm.tm_mday
116              && lt->tm_hour == tm.tm_hour
117              && lt->tm_min == tm.tm_min
118              && lt->tm_sec == tm.tm_sec
119              && lt->tm_yday == tm.tm_yday
120              && lt->tm_wday == tm.tm_wday
121              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
122                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
123         exit (1);
124     }
125 }
126
127 int
128 main ()
129 {
130   time_t t, delta;
131   int i, j;
132
133   /* This test makes some buggy mktime implementations loop.
134      Give up after 60 seconds; a mktime slower than that
135      isn't worth using anyway.  */
136   alarm (60);
137
138   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
139     continue;
140   time_t_max--;
141   delta = time_t_max / 997; /* a suitable prime number */
142   for (i = 0; i < N_STRINGS; i++)
143     {
144       if (tz_strings[i])
145         putenv (tz_strings[i]);
146
147       for (t = 0; t <= time_t_max - delta; t += delta)
148         mktime_test (t);
149       mktime_test ((time_t) 60 * 60);
150       mktime_test ((time_t) 60 * 60 * 24);
151
152       for (j = 1; 0 < j; j *= 2)
153         bigtime_test (j);
154       bigtime_test (j - 1);
155     }
156   irix_6_4_bug ();
157   spring_forward_gap ();
158   exit (0);
159 }
160 >>,
161 changequote([, ])dnl
162 ac_cv_func_working_mktime=yes, ac_cv_func_working_mktime=no,
163 ac_cv_func_working_mktime=no)])
164 if test $ac_cv_func_working_mktime = no; then
165   LIBOBJS="$LIBOBJS mktime.${ac_objext}"
166 fi
167 ])