mktime.m4 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # serial 25
  2. dnl Copyright (C) 2002-2003, 2005-2007, 2009-2015 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. AC_DEFUN([gl_FUNC_MKTIME],
  9. [
  10. AC_REQUIRE([gl_HEADER_TIME_H_DEFAULTS])
  11. dnl We don't use AC_FUNC_MKTIME any more, because it is no longer maintained
  12. dnl in Autoconf and because it invokes AC_LIBOBJ.
  13. AC_CHECK_HEADERS_ONCE([unistd.h])
  14. AC_CHECK_DECLS_ONCE([alarm])
  15. AC_REQUIRE([gl_MULTIARCH])
  16. if test $APPLE_UNIVERSAL_BUILD = 1; then
  17. # A universal build on Apple Mac OS X platforms.
  18. # The test result would be 'yes' in 32-bit mode and 'no' in 64-bit mode.
  19. # But we need a configuration result that is valid in both modes.
  20. gl_cv_func_working_mktime=no
  21. fi
  22. AC_CACHE_CHECK([for working mktime], [gl_cv_func_working_mktime],
  23. [AC_RUN_IFELSE(
  24. [AC_LANG_SOURCE(
  25. [[/* Test program from Paul Eggert and Tony Leneis. */
  26. #include <limits.h>
  27. #include <stdlib.h>
  28. #include <time.h>
  29. #ifdef HAVE_UNISTD_H
  30. # include <unistd.h>
  31. #endif
  32. #if HAVE_DECL_ALARM
  33. # include <signal.h>
  34. #endif
  35. /* Work around redefinition to rpl_putenv by other config tests. */
  36. #undef putenv
  37. static time_t time_t_max;
  38. 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]))
  45. /* Return 0 if mktime fails to convert a date in the spring-forward gap.
  46. Based on a problem report from Andreas Jaeger. */
  47. static int
  48. spring_forward_gap ()
  49. {
  50. /* glibc (up to about 1998-10-07) failed this test. */
  51. struct tm tm;
  52. /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
  53. instead of "TZ=America/Vancouver" in order to detect the bug even
  54. on systems that don't support the Olson extension, or don't have the
  55. full zoneinfo tables installed. */
  56. putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
  57. tm.tm_year = 98;
  58. tm.tm_mon = 3;
  59. tm.tm_mday = 5;
  60. tm.tm_hour = 2;
  61. tm.tm_min = 0;
  62. tm.tm_sec = 0;
  63. tm.tm_isdst = -1;
  64. return mktime (&tm) != (time_t) -1;
  65. }
  66. static int
  67. mktime_test1 (time_t now)
  68. {
  69. struct tm *lt;
  70. return ! (lt = localtime (&now)) || mktime (lt) == now;
  71. }
  72. static int
  73. mktime_test (time_t now)
  74. {
  75. return (mktime_test1 (now)
  76. && mktime_test1 ((time_t) (time_t_max - now))
  77. && mktime_test1 ((time_t) (time_t_min + now)));
  78. }
  79. static int
  80. irix_6_4_bug ()
  81. {
  82. /* Based on code from Ariel Faigon. */
  83. struct tm tm;
  84. tm.tm_year = 96;
  85. tm.tm_mon = 3;
  86. tm.tm_mday = 0;
  87. tm.tm_hour = 0;
  88. tm.tm_min = 0;
  89. tm.tm_sec = 0;
  90. tm.tm_isdst = -1;
  91. mktime (&tm);
  92. return tm.tm_mon == 2 && tm.tm_mday == 31;
  93. }
  94. static int
  95. bigtime_test (int j)
  96. {
  97. struct tm tm;
  98. time_t now;
  99. tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
  100. now = mktime (&tm);
  101. if (now != (time_t) -1)
  102. {
  103. struct tm *lt = localtime (&now);
  104. if (! (lt
  105. && lt->tm_year == tm.tm_year
  106. && lt->tm_mon == tm.tm_mon
  107. && lt->tm_mday == tm.tm_mday
  108. && lt->tm_hour == tm.tm_hour
  109. && lt->tm_min == tm.tm_min
  110. && lt->tm_sec == tm.tm_sec
  111. && lt->tm_yday == tm.tm_yday
  112. && lt->tm_wday == tm.tm_wday
  113. && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
  114. == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. static int
  120. year_2050_test ()
  121. {
  122. /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
  123. ignoring leap seconds. */
  124. unsigned long int answer = 2527315200UL;
  125. struct tm tm;
  126. time_t t;
  127. tm.tm_year = 2050 - 1900;
  128. tm.tm_mon = 2 - 1;
  129. tm.tm_mday = 1;
  130. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  131. tm.tm_isdst = -1;
  132. /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
  133. instead of "TZ=America/Vancouver" in order to detect the bug even
  134. on systems that don't support the Olson extension, or don't have the
  135. full zoneinfo tables installed. */
  136. putenv ("TZ=PST8PDT,M4.1.0,M10.5.0");
  137. t = mktime (&tm);
  138. /* Check that the result is either a failure, or close enough
  139. to the correct answer that we can assume the discrepancy is
  140. due to leap seconds. */
  141. return (t == (time_t) -1
  142. || (0 < t && answer - 120 <= t && t <= answer + 120));
  143. }
  144. int
  145. main ()
  146. {
  147. int result = 0;
  148. time_t t, delta;
  149. int i, j;
  150. int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
  151. int time_t_signed = ! ((time_t) 0 < (time_t) -1);
  152. #if HAVE_DECL_ALARM
  153. /* This test makes some buggy mktime implementations loop.
  154. Give up after 60 seconds; a mktime slower than that
  155. isn't worth using anyway. */
  156. signal (SIGALRM, SIG_DFL);
  157. alarm (60);
  158. #endif
  159. time_t_max = (! time_t_signed
  160. ? (time_t) -1
  161. : ((((time_t) 1 << (sizeof (time_t) * CHAR_BIT - 2)) - 1)
  162. * 2 + 1));
  163. time_t_min = (! time_t_signed
  164. ? (time_t) 0
  165. : time_t_signed_magnitude
  166. ? ~ (time_t) 0
  167. : ~ time_t_max);
  168. delta = time_t_max / 997; /* a suitable prime number */
  169. for (i = 0; i < N_STRINGS; i++)
  170. {
  171. if (tz_strings[i])
  172. putenv (tz_strings[i]);
  173. for (t = 0; t <= time_t_max - delta && (result & 1) == 0; t += delta)
  174. if (! mktime_test (t))
  175. result |= 1;
  176. if ((result & 2) == 0
  177. && ! (mktime_test ((time_t) 1)
  178. && mktime_test ((time_t) (60 * 60))
  179. && mktime_test ((time_t) (60 * 60 * 24))))
  180. result |= 2;
  181. for (j = 1; (result & 4) == 0; j <<= 1)
  182. {
  183. if (! bigtime_test (j))
  184. result |= 4;
  185. if (INT_MAX / 2 < j)
  186. break;
  187. }
  188. if ((result & 8) == 0 && ! bigtime_test (INT_MAX))
  189. result |= 8;
  190. }
  191. if (! irix_6_4_bug ())
  192. result |= 16;
  193. if (! spring_forward_gap ())
  194. result |= 32;
  195. if (! year_2050_test ())
  196. result |= 64;
  197. return result;
  198. }]])],
  199. [gl_cv_func_working_mktime=yes],
  200. [gl_cv_func_working_mktime=no],
  201. [gl_cv_func_working_mktime=no])
  202. ])
  203. if test $gl_cv_func_working_mktime = no; then
  204. REPLACE_MKTIME=1
  205. else
  206. REPLACE_MKTIME=0
  207. fi
  208. ])
  209. AC_DEFUN([gl_FUNC_MKTIME_INTERNAL], [
  210. AC_REQUIRE([gl_FUNC_MKTIME])
  211. if test $REPLACE_MKTIME = 0; then
  212. dnl BeOS has __mktime_internal in libc, but other platforms don't.
  213. AC_CHECK_FUNC([__mktime_internal],
  214. [AC_DEFINE([mktime_internal], [__mktime_internal],
  215. [Define to the real name of the mktime_internal function.])
  216. ],
  217. [dnl mktime works but it doesn't export __mktime_internal,
  218. dnl so we need to substitute our own mktime implementation.
  219. REPLACE_MKTIME=1
  220. ])
  221. fi
  222. ])
  223. # Prerequisites of lib/mktime.c.
  224. AC_DEFUN([gl_PREREQ_MKTIME], [:])