mktime.m4 6.2 KB

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