4
0

mktime.m4 5.7 KB

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