Code

Add the timegm Gnulib module
[nagiosplug.git] / gl / m4 / mktime.m4
1 # serial 14
2 dnl Copyright (C) 2002-2003, 2005-2007, 2009 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 dnl From Jim Meyering.
9 # Redefine AC_FUNC_MKTIME, to fix a bug in Autoconf 2.61a and earlier.
10 # This redefinition can be removed once a new version of Autoconf is assumed.
11 # The redefinition is taken from
12 # <http://cvs.sv.gnu.org/viewcvs/*checkout*/autoconf/autoconf/lib/autoconf/functions.m4?rev=1.119>.
13 # AC_FUNC_MKTIME
14 # --------------
15 AC_DEFUN([AC_FUNC_MKTIME],
16 [AC_CHECK_HEADERS_ONCE([unistd.h])
17 AC_CHECK_FUNCS_ONCE([alarm])
18 AC_CACHE_CHECK([for working mktime], [ac_cv_func_working_mktime],
19 [AC_RUN_IFELSE([AC_LANG_SOURCE(
20 [[/* Test program from Paul Eggert and Tony Leneis.  */
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <time.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
29 #ifndef HAVE_ALARM
30 # define alarm(X) /* empty */
31 #endif
33 /* Work around redefinition to rpl_putenv by other config tests.  */
34 #undef putenv
36 static time_t time_t_max;
37 static time_t time_t_min;
39 /* Values we'll use to set the TZ environment variable.  */
40 static char *tz_strings[] = {
41   (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]))
46 /* Return 0 if mktime fails to convert a date in the spring-forward gap.
47    Based on a problem report from Andreas Jaeger.  */
48 static int
49 spring_forward_gap ()
50 {
51   /* glibc (up to about 1998-10-07) failed this test. */
52   struct tm tm;
54   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
55      instead of "TZ=America/Vancouver" in order to detect the bug even
56      on systems that don't support the Olson extension, or don't have the
57      full zoneinfo tables installed.  */
58   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
60   tm.tm_year = 98;
61   tm.tm_mon = 3;
62   tm.tm_mday = 5;
63   tm.tm_hour = 2;
64   tm.tm_min = 0;
65   tm.tm_sec = 0;
66   tm.tm_isdst = -1;
67   return mktime (&tm) != (time_t) -1;
68 }
70 static int
71 mktime_test1 (time_t now)
72 {
73   struct tm *lt;
74   return ! (lt = localtime (&now)) || mktime (lt) == now;
75 }
77 static int
78 mktime_test (time_t now)
79 {
80   return (mktime_test1 (now)
81           && mktime_test1 ((time_t) (time_t_max - now))
82           && mktime_test1 ((time_t) (time_t_min + now)));
83 }
85 static int
86 irix_6_4_bug ()
87 {
88   /* Based on code from Ariel Faigon.  */
89   struct tm tm;
90   tm.tm_year = 96;
91   tm.tm_mon = 3;
92   tm.tm_mday = 0;
93   tm.tm_hour = 0;
94   tm.tm_min = 0;
95   tm.tm_sec = 0;
96   tm.tm_isdst = -1;
97   mktime (&tm);
98   return tm.tm_mon == 2 && tm.tm_mday == 31;
99 }
101 static int
102 bigtime_test (int j)
104   struct tm tm;
105   time_t now;
106   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
107   now = mktime (&tm);
108   if (now != (time_t) -1)
109     {
110       struct tm *lt = localtime (&now);
111       if (! (lt
112              && lt->tm_year == tm.tm_year
113              && lt->tm_mon == tm.tm_mon
114              && lt->tm_mday == tm.tm_mday
115              && lt->tm_hour == tm.tm_hour
116              && lt->tm_min == tm.tm_min
117              && lt->tm_sec == tm.tm_sec
118              && lt->tm_yday == tm.tm_yday
119              && lt->tm_wday == tm.tm_wday
120              && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
121                   == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
122         return 0;
123     }
124   return 1;
127 static int
128 year_2050_test ()
130   /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
131      ignoring leap seconds.  */
132   unsigned long int answer = 2527315200UL;
134   struct tm tm;
135   time_t t;
136   tm.tm_year = 2050 - 1900;
137   tm.tm_mon = 2 - 1;
138   tm.tm_mday = 1;
139   tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
140   tm.tm_isdst = -1;
142   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
143      instead of "TZ=America/Vancouver" in order to detect the bug even
144      on systems that don't support the Olson extension, or don't have the
145      full zoneinfo tables installed.  */
146   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
148   t = mktime (&tm);
150   /* Check that the result is either a failure, or close enough
151      to the correct answer that we can assume the discrepancy is
152      due to leap seconds.  */
153   return (t == (time_t) -1
154           || (0 < t && answer - 120 <= t && t <= answer + 120));
157 int
158 main ()
160   time_t t, delta;
161   int i, j;
163   /* This test makes some buggy mktime implementations loop.
164      Give up after 60 seconds; a mktime slower than that
165      isn't worth using anyway.  */
166   alarm (60);
168   for (;;)
169     {
170       t = (time_t_max << 1) + 1;
171       if (t <= time_t_max)
172         break;
173       time_t_max = t;
174     }
175   time_t_min = - ((time_t) ~ (time_t) 0 == (time_t) -1) - time_t_max;
177   delta = time_t_max / 997; /* a suitable prime number */
178   for (i = 0; i < N_STRINGS; i++)
179     {
180       if (tz_strings[i])
181         putenv (tz_strings[i]);
183       for (t = 0; t <= time_t_max - delta; t += delta)
184         if (! mktime_test (t))
185           return 1;
186       if (! (mktime_test ((time_t) 1)
187              && mktime_test ((time_t) (60 * 60))
188              && mktime_test ((time_t) (60 * 60 * 24))))
189         return 1;
191       for (j = 1; ; j <<= 1)
192         if (! bigtime_test (j))
193           return 1;
194         else if (INT_MAX / 2 < j)
195           break;
196       if (! bigtime_test (INT_MAX))
197         return 1;
198     }
199   return ! (irix_6_4_bug () && spring_forward_gap () && year_2050_test ());
200 }]])],
201                [ac_cv_func_working_mktime=yes],
202                [ac_cv_func_working_mktime=no],
203                [ac_cv_func_working_mktime=no])])
204 if test $ac_cv_func_working_mktime = no; then
205   AC_LIBOBJ([mktime])
206 fi
207 ])# AC_FUNC_MKTIME
209 AC_DEFUN([gl_FUNC_MKTIME],
211   AC_FUNC_MKTIME
212   dnl Note: AC_FUNC_MKTIME does AC_LIBOBJ([mktime]).
213   if test $ac_cv_func_working_mktime = no; then
214     AC_DEFINE([mktime], [rpl_mktime],
215       [Define to rpl_mktime if the replacement function should be used.])
216     gl_PREREQ_MKTIME
217   fi
218 ])
220 # Prerequisites of lib/mktime.c.
221 AC_DEFUN([gl_PREREQ_MKTIME],
223   AC_REQUIRE([AC_C_INLINE])
224 ])