Home | History | Annotate | Line # | Download | only in m4
      1  1.1  christos # mktime.m4 serial 5
      2  1.1  christos dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc.
      3  1.1  christos dnl This file is free software; the Free Software Foundation
      4  1.1  christos dnl gives unlimited permission to copy and/or distribute it,
      5  1.1  christos dnl with or without modifications, as long as this notice is preserved.
      6  1.1  christos 
      7  1.1  christos dnl From Jim Meyering.
      8  1.1  christos 
      9  1.1  christos # Redefine AC_FUNC_MKTIME, to fix a bug in Autoconf 2.57 and earlier.
     10  1.1  christos # This redefinition can be removed once a new version of Autoconf comes out.
     11  1.1  christos # The redefinition is taken from
     12  1.1  christos # <http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/autoconf/autoconf/lib/autoconf/functions.m4?rev=1.78>.
     13  1.1  christos # AC_FUNC_MKTIME
     14  1.1  christos # --------------
     15  1.1  christos AC_DEFUN([AC_FUNC_MKTIME],
     16  1.1  christos [AC_REQUIRE([AC_HEADER_TIME])dnl
     17  1.1  christos AC_CHECK_HEADERS(stdlib.h sys/time.h unistd.h)
     18  1.1  christos AC_CHECK_FUNCS(alarm)
     19  1.1  christos AC_CACHE_CHECK([for working mktime], ac_cv_func_working_mktime,
     20  1.1  christos [AC_RUN_IFELSE([AC_LANG_SOURCE(
     21  1.1  christos [[/* Test program from Paul Eggert and Tony Leneis.  */
     22  1.1  christos #if TIME_WITH_SYS_TIME
     23  1.1  christos # include <sys/time.h>
     24  1.1  christos # include <time.h>
     25  1.1  christos #else
     26  1.1  christos # if HAVE_SYS_TIME_H
     27  1.1  christos #  include <sys/time.h>
     28  1.1  christos # else
     29  1.1  christos #  include <time.h>
     30  1.1  christos # endif
     31  1.1  christos #endif
     32  1.1  christos 
     33  1.1  christos #if HAVE_STDLIB_H
     34  1.1  christos # include <stdlib.h>
     35  1.1  christos #endif
     36  1.1  christos 
     37  1.1  christos #if HAVE_UNISTD_H
     38  1.1  christos # include <unistd.h>
     39  1.1  christos #endif
     40  1.1  christos 
     41  1.1  christos #if !HAVE_ALARM
     42  1.1  christos # define alarm(X) /* empty */
     43  1.1  christos #endif
     44  1.1  christos 
     45  1.1  christos /* Work around redefinition to rpl_putenv by other config tests.  */
     46  1.1  christos #undef putenv
     47  1.1  christos 
     48  1.1  christos static time_t time_t_max;
     49  1.1  christos static time_t time_t_min;
     50  1.1  christos 
     51  1.1  christos /* Values we'll use to set the TZ environment variable.  */
     52  1.1  christos static char *tz_strings[] = {
     53  1.1  christos   (char *) 0, "TZ=GMT0", "TZ=JST-9",
     54  1.1  christos   "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
     55  1.1  christos };
     56  1.1  christos #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
     57  1.1  christos 
     58  1.1  christos /* Fail if mktime fails to convert a date in the spring-forward gap.
     59  1.1  christos    Based on a problem report from Andreas Jaeger.  */
     60  1.1  christos static void
     61  1.1  christos spring_forward_gap ()
     62  1.1  christos {
     63  1.1  christos   /* glibc (up to about 1998-10-07) failed this test. */
     64  1.1  christos   struct tm tm;
     65  1.1  christos 
     66  1.1  christos   /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
     67  1.1  christos      instead of "TZ=America/Vancouver" in order to detect the bug even
     68  1.1  christos      on systems that don't support the Olson extension, or don't have the
     69  1.1  christos      full zoneinfo tables installed.  */
     70  1.1  christos   putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
     71  1.1  christos 
     72  1.1  christos   tm.tm_year = 98;
     73  1.1  christos   tm.tm_mon = 3;
     74  1.1  christos   tm.tm_mday = 5;
     75  1.1  christos   tm.tm_hour = 2;
     76  1.1  christos   tm.tm_min = 0;
     77  1.1  christos   tm.tm_sec = 0;
     78  1.1  christos   tm.tm_isdst = -1;
     79  1.1  christos   if (mktime (&tm) == (time_t)-1)
     80  1.1  christos     exit (1);
     81  1.1  christos }
     82  1.1  christos 
     83  1.1  christos static void
     84  1.1  christos mktime_test1 (now)
     85  1.1  christos      time_t now;
     86  1.1  christos {
     87  1.1  christos   struct tm *lt;
     88  1.1  christos   if ((lt = localtime (&now)) && mktime (lt) != now)
     89  1.1  christos     exit (1);
     90  1.1  christos }
     91  1.1  christos 
     92  1.1  christos static void
     93  1.1  christos mktime_test (now)
     94  1.1  christos      time_t now;
     95  1.1  christos {
     96  1.1  christos   mktime_test1 (now);
     97  1.1  christos   mktime_test1 ((time_t) (time_t_max - now));
     98  1.1  christos   mktime_test1 ((time_t) (time_t_min + now));
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos static void
    102  1.1  christos irix_6_4_bug ()
    103  1.1  christos {
    104  1.1  christos   /* Based on code from Ariel Faigon.  */
    105  1.1  christos   struct tm tm;
    106  1.1  christos   tm.tm_year = 96;
    107  1.1  christos   tm.tm_mon = 3;
    108  1.1  christos   tm.tm_mday = 0;
    109  1.1  christos   tm.tm_hour = 0;
    110  1.1  christos   tm.tm_min = 0;
    111  1.1  christos   tm.tm_sec = 0;
    112  1.1  christos   tm.tm_isdst = -1;
    113  1.1  christos   mktime (&tm);
    114  1.1  christos   if (tm.tm_mon != 2 || tm.tm_mday != 31)
    115  1.1  christos     exit (1);
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos static void
    119  1.1  christos bigtime_test (j)
    120  1.1  christos      int j;
    121  1.1  christos {
    122  1.1  christos   struct tm tm;
    123  1.1  christos   time_t now;
    124  1.1  christos   tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
    125  1.1  christos   now = mktime (&tm);
    126  1.1  christos   if (now != (time_t) -1)
    127  1.1  christos     {
    128  1.1  christos       struct tm *lt = localtime (&now);
    129  1.1  christos       if (! (lt
    130  1.1  christos 	     && lt->tm_year == tm.tm_year
    131  1.1  christos 	     && lt->tm_mon == tm.tm_mon
    132  1.1  christos 	     && lt->tm_mday == tm.tm_mday
    133  1.1  christos 	     && lt->tm_hour == tm.tm_hour
    134  1.1  christos 	     && lt->tm_min == tm.tm_min
    135  1.1  christos 	     && lt->tm_sec == tm.tm_sec
    136  1.1  christos 	     && lt->tm_yday == tm.tm_yday
    137  1.1  christos 	     && lt->tm_wday == tm.tm_wday
    138  1.1  christos 	     && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
    139  1.1  christos 		  == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
    140  1.1  christos 	exit (1);
    141  1.1  christos     }
    142  1.1  christos }
    143  1.1  christos 
    144  1.1  christos int
    145  1.1  christos main ()
    146  1.1  christos {
    147  1.1  christos   time_t t, delta;
    148  1.1  christos   int i, j;
    149  1.1  christos 
    150  1.1  christos   /* This test makes some buggy mktime implementations loop.
    151  1.1  christos      Give up after 60 seconds; a mktime slower than that
    152  1.1  christos      isn't worth using anyway.  */
    153  1.1  christos   alarm (60);
    154  1.1  christos 
    155  1.1  christos   for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
    156  1.1  christos     continue;
    157  1.1  christos   time_t_max--;
    158  1.1  christos   if ((time_t) -1 < 0)
    159  1.1  christos     for (time_t_min = -1; (time_t) (time_t_min * 2) < 0; time_t_min *= 2)
    160  1.1  christos       continue;
    161  1.1  christos   delta = time_t_max / 997; /* a suitable prime number */
    162  1.1  christos   for (i = 0; i < N_STRINGS; i++)
    163  1.1  christos     {
    164  1.1  christos       if (tz_strings[i])
    165  1.1  christos 	putenv (tz_strings[i]);
    166  1.1  christos 
    167  1.1  christos       for (t = 0; t <= time_t_max - delta; t += delta)
    168  1.1  christos 	mktime_test (t);
    169  1.1  christos       mktime_test ((time_t) 1);
    170  1.1  christos       mktime_test ((time_t) (60 * 60));
    171  1.1  christos       mktime_test ((time_t) (60 * 60 * 24));
    172  1.1  christos 
    173  1.1  christos       for (j = 1; 0 < j; j *= 2)
    174  1.1  christos 	bigtime_test (j);
    175  1.1  christos       bigtime_test (j - 1);
    176  1.1  christos     }
    177  1.1  christos   irix_6_4_bug ();
    178  1.1  christos   spring_forward_gap ();
    179  1.1  christos   exit (0);
    180  1.1  christos }]])],
    181  1.1  christos 	       [ac_cv_func_working_mktime=yes],
    182  1.1  christos 	       [ac_cv_func_working_mktime=no],
    183  1.1  christos 	       [ac_cv_func_working_mktime=no])])
    184  1.1  christos if test $ac_cv_func_working_mktime = no; then
    185  1.1  christos   AC_LIBOBJ([mktime])
    186  1.1  christos fi
    187  1.1  christos ])# AC_FUNC_MKTIME
    188  1.1  christos 
    189  1.1  christos AC_DEFUN([gl_FUNC_MKTIME],
    190  1.1  christos [
    191  1.1  christos   AC_REQUIRE([AC_FUNC_MKTIME])
    192  1.1  christos   if test $ac_cv_func_working_mktime = no; then
    193  1.1  christos     AC_DEFINE(mktime, rpl_mktime,
    194  1.1  christos       [Define to rpl_mktime if the replacement function should be used.])
    195  1.1  christos     gl_PREREQ_MKTIME
    196  1.1  christos   fi
    197  1.1  christos ])
    198  1.1  christos 
    199  1.1  christos # Prerequisites of lib/mktime.c.
    200  1.1  christos AC_DEFUN([gl_PREREQ_MKTIME], [:])
    201