X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=m4%2Fmktime.m4;h=cec7bc3ffd84ba5be088af7f6e8a5bf06db03125;hb=3ac9552429c5a500717e1d3d129c32f7d0ea37a3;hp=5314c635008ea26c7aa4c3e97e24f3681e3eabef;hpb=f0ba8702dcd5810a92c9b428032efb760bd4c100;p=gnulib.git diff --git a/m4/mktime.m4 b/m4/mktime.m4 index 5314c6350..cec7bc3ff 100644 --- a/m4/mktime.m4 +++ b/m4/mktime.m4 @@ -1,33 +1,32 @@ -#serial 1000 - -dnl Just like mktime.m4 from automake-1.3b, but with an additional test. - -undefine([AM_FUNC_MKTIME]) -AC_DEFUN(AM_FUNC_MKTIME, -[AC_REQUIRE([AC_HEADER_TIME])dnl - AC_CHECK_HEADERS(sys/time.h unistd.h) - AC_CHECK_FUNCS(alarm) - AC_CACHE_CHECK([for working mktime], am_cv_func_working_mktime, - [AC_TRY_RUN( -changequote(<<, >>)dnl -< -# include -#else -# if HAVE_SYS_TIME_H -# include -# else -# include -# endif -#endif +#serial 12 +dnl Copyright (C) 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +dnl From Jim Meyering. -#if HAVE_UNISTD_H +# Redefine AC_FUNC_MKTIME, to fix a bug in Autoconf 2.61a and earlier. +# This redefinition can be removed once a new version of Autoconf is assumed. +# The redefinition is taken from +# . +# AC_FUNC_MKTIME +# -------------- +AC_DEFUN([AC_FUNC_MKTIME], +[AC_CHECK_HEADERS_ONCE(unistd.h) +AC_CHECK_FUNCS_ONCE(alarm) +AC_CACHE_CHECK([for working mktime], ac_cv_func_working_mktime, +[AC_RUN_IFELSE([AC_LANG_SOURCE( +[[/* Test program from Paul Eggert and Tony Leneis. */ +#include +#include +#include + +#ifdef HAVE_UNISTD_H # include #endif -#if !HAVE_ALARM +#ifndef HAVE_ALARM # define alarm(X) /* empty */ #endif @@ -35,22 +34,29 @@ changequote(<<, >>)dnl #undef putenv static time_t time_t_max; +static time_t time_t_min; /* Values we'll use to set the TZ environment variable. */ -static const char *const tz_strings[] = { - (const char *) 0, "TZ=GMT0", "TZ=JST-9", +static char *tz_strings[] = { + (char *) 0, "TZ=GMT0", "TZ=JST-9", "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00" }; #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0])) -/* Fail if mktime fails to convert a date in the spring-forward gap. +/* Return 0 if mktime fails to convert a date in the spring-forward gap. Based on a problem report from Andreas Jaeger. */ -static void +static int spring_forward_gap () { - /* glibc (up to about 1998-10-07) failed this test) */ + /* glibc (up to about 1998-10-07) failed this test. */ struct tm tm; - putenv ("TZ=America/Vancouver"); + + /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0" + instead of "TZ=America/Vancouver" in order to detect the bug even + on systems that don't support the Olson extension, or don't have the + full zoneinfo tables installed. */ + putenv ("TZ=PST8PDT,M4.1.0,M10.5.0"); + tm.tm_year = 98; tm.tm_mon = 3; tm.tm_mday = 5; @@ -58,23 +64,27 @@ spring_forward_gap () tm.tm_min = 0; tm.tm_sec = 0; tm.tm_isdst = -1; - if (mktime (&tm) == (time_t)-1) - exit (1); + return mktime (&tm) != (time_t) -1; } -static void -mktime_test (now) +static int +mktime_test1 (now) time_t now; { struct tm *lt; - if ((lt = localtime (&now)) && mktime (lt) != now) - exit (1); - now = time_t_max - now; - if ((lt = localtime (&now)) && mktime (lt) != now) - exit (1); + return ! (lt = localtime (&now)) || mktime (lt) == now; } -static void +static int +mktime_test (now) + time_t now; +{ + return (mktime_test1 (now) + && mktime_test1 ((time_t) (time_t_max - now)) + && mktime_test1 ((time_t) (time_t_min + now))); +} + +static int irix_6_4_bug () { /* Based on code from Ariel Faigon. */ @@ -87,22 +97,17 @@ irix_6_4_bug () tm.tm_sec = 0; tm.tm_isdst = -1; mktime (&tm); - if (tm.tm_mon != 2 || tm.tm_mday != 31) - exit (1); + return tm.tm_mon == 2 && tm.tm_mday == 31; } -static void +static int bigtime_test (j) int j; { struct tm tm; time_t now; tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j; - /* This test makes some buggy mktime implementations loop. - Give up after 10 seconds. */ - alarm (10); now = mktime (&tm); - alarm (0); if (now != (time_t) -1) { struct tm *lt = localtime (&now); @@ -117,8 +122,39 @@ bigtime_test (j) && lt->tm_wday == tm.tm_wday && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst) == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst)))) - exit (1); + return 0; } + return 1; +} + +static int +year_2050_test () +{ + /* The correct answer for 2050-02-01 00:00:00 in Pacific time, + ignoring leap seconds. */ + unsigned long int answer = 2527315200UL; + + struct tm tm; + time_t t; + tm.tm_year = 2050 - 1900; + tm.tm_mon = 2 - 1; + tm.tm_mday = 1; + tm.tm_hour = tm.tm_min = tm.tm_sec = 0; + tm.tm_isdst = -1; + + /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0" + instead of "TZ=America/Vancouver" in order to detect the bug even + on systems that don't support the Olson extension, or don't have the + full zoneinfo tables installed. */ + putenv ("TZ=PST8PDT,M4.1.0,M10.5.0"); + + t = mktime (&tm); + + /* Check that the result is either a failure, or close enough + to the correct answer that we can assume the discrepancy is + due to leap seconds. */ + return (t == (time_t) -1 + || (0 < t && answer - 120 <= t && t <= answer + 120)); } int @@ -127,9 +163,20 @@ main () time_t t, delta; int i, j; - for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2) - continue; - time_t_max--; + /* This test makes some buggy mktime implementations loop. + Give up after 60 seconds; a mktime slower than that + isn't worth using anyway. */ + alarm (60); + + for (;;) + { + t = (time_t_max << 1) + 1; + if (t <= time_t_max) + break; + time_t_max = t; + } + time_t_min = - ((time_t) ~ (time_t) 0 == (time_t) -1) - time_t_max; + delta = time_t_max / 997; /* a suitable prime number */ for (i = 0; i < N_STRINGS; i++) { @@ -137,25 +184,41 @@ main () putenv (tz_strings[i]); for (t = 0; t <= time_t_max - delta; t += delta) - mktime_test (t); - mktime_test ((time_t) 60 * 60); - mktime_test ((time_t) 60 * 60 * 24); + if (! mktime_test (t)) + return 1; + if (! (mktime_test ((time_t) 1) + && mktime_test ((time_t) (60 * 60)) + && mktime_test ((time_t) (60 * 60 * 24)))) + return 1; - for (j = 1; 0 < j; j *= 2) - bigtime_test (j); - bigtime_test (j - 1); + for (j = 1; ; j <<= 1) + if (! bigtime_test (j)) + return 1; + else if (INT_MAX / 2 < j) + break; + if (! bigtime_test (INT_MAX)) + return 1; } - irix_6_4_bug (); - spring_forward_gap (); - exit (0); -} - >>, -changequote([, ])dnl - am_cv_func_working_mktime=yes, am_cv_func_working_mktime=no, - dnl When crosscompiling, assume mktime is missing or broken. - am_cv_func_working_mktime=no) - ]) - if test $am_cv_func_working_mktime = no; then - LIBOBJS="$LIBOBJS mktime.o" + return ! (irix_6_4_bug () && spring_forward_gap () && year_2050_test ()); +}]])], + [ac_cv_func_working_mktime=yes], + [ac_cv_func_working_mktime=no], + [ac_cv_func_working_mktime=no])]) +if test $ac_cv_func_working_mktime = no; then + AC_LIBOBJ([mktime]) +fi +])# AC_FUNC_MKTIME + +AC_DEFUN([gl_FUNC_MKTIME], +[ + AC_FUNC_MKTIME + dnl Note: AC_FUNC_MKTIME does AC_LIBOBJ(mktime). + if test $ac_cv_func_working_mktime = no; then + AC_DEFINE(mktime, rpl_mktime, + [Define to rpl_mktime if the replacement function should be used.]) + gl_PREREQ_MKTIME fi ]) + +# Prerequisites of lib/mktime.c. +AC_DEFUN([gl_PREREQ_MKTIME], [:])