mktime.m4 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # serial 37
  2. dnl Copyright (C) 2002-2003, 2005-2007, 2009-2023 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_TIME_T_IS_SIGNED],
  9. [
  10. AC_CACHE_CHECK([whether time_t is signed],
  11. [gl_cv_time_t_is_signed],
  12. [AC_COMPILE_IFELSE(
  13. [AC_LANG_PROGRAM([[#include <time.h>
  14. char time_t_signed[(time_t) -1 < 0 ? 1 : -1];]])],
  15. [gl_cv_time_t_is_signed=yes],
  16. [gl_cv_time_t_is_signed=no])])
  17. if test $gl_cv_time_t_is_signed = yes; then
  18. AC_DEFINE([TIME_T_IS_SIGNED], [1], [Define to 1 if time_t is signed.])
  19. fi
  20. ])
  21. dnl Test whether mktime works. Set gl_cv_func_working_mktime.
  22. AC_DEFUN([gl_FUNC_MKTIME_WORKS],
  23. [
  24. AC_REQUIRE([gl_TIME_T_IS_SIGNED])
  25. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  26. dnl We don't use AC_FUNC_MKTIME any more, because it is no longer maintained
  27. dnl in Autoconf and because it invokes AC_LIBOBJ.
  28. AC_CHECK_HEADERS_ONCE([unistd.h])
  29. AC_CHECK_DECLS_ONCE([alarm])
  30. AC_REQUIRE([gl_MULTIARCH])
  31. AC_CACHE_CHECK([for working mktime], [gl_cv_func_working_mktime],
  32. [if test $APPLE_UNIVERSAL_BUILD = 1; then
  33. # A universal build on Apple Mac OS X platforms.
  34. # The test result would be 'yes' in 32-bit mode and 'no' in 64-bit mode.
  35. # But we need a configuration result that is valid in both modes.
  36. gl_cv_func_working_mktime="guessing no"
  37. else
  38. AC_RUN_IFELSE(
  39. [AC_LANG_SOURCE(
  40. [[/* Test program from Paul Eggert and Tony Leneis. */
  41. #include <limits.h>
  42. #include <stdlib.h>
  43. #include <time.h>
  44. #ifdef HAVE_UNISTD_H
  45. # include <unistd.h>
  46. #endif
  47. #if HAVE_DECL_ALARM
  48. # include <signal.h>
  49. #endif
  50. ]GL_MDA_DEFINES[
  51. #ifndef TIME_T_IS_SIGNED
  52. # define TIME_T_IS_SIGNED 0
  53. #endif
  54. static time_t time_t_max;
  55. static time_t time_t_min;
  56. /* Values we'll use to set the TZ environment variable. */
  57. static char *tz_strings[] = {
  58. (char *) 0, "TZ=GMT0", "TZ=JST-9",
  59. "TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"
  60. };
  61. #define N_STRINGS (sizeof (tz_strings) / sizeof (tz_strings[0]))
  62. /* Return 0 if mktime fails to convert a date in the spring-forward gap.
  63. Based on a problem report from Andreas Jaeger. */
  64. static int
  65. spring_forward_gap ()
  66. {
  67. /* glibc (up to about 1998-10-07) failed this test. */
  68. struct tm tm;
  69. /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
  70. instead of "TZ=America/Vancouver" in order to detect the bug even
  71. on systems that don't support the Olson extension, or don't have the
  72. full zoneinfo tables installed. */
  73. if (putenv ("TZ=PST8PDT,M4.1.0,M10.5.0") != 0)
  74. return -1;
  75. tm.tm_year = 98;
  76. tm.tm_mon = 3;
  77. tm.tm_mday = 5;
  78. tm.tm_hour = 2;
  79. tm.tm_min = 0;
  80. tm.tm_sec = 0;
  81. tm.tm_isdst = -1;
  82. return mktime (&tm) != (time_t) -1;
  83. }
  84. static int
  85. mktime_test1 (time_t now)
  86. {
  87. struct tm *lt;
  88. return ! (lt = localtime (&now)) || mktime (lt) == now;
  89. }
  90. static int
  91. mktime_test (time_t now)
  92. {
  93. return (mktime_test1 (now)
  94. && mktime_test1 ((time_t) (time_t_max - now))
  95. && mktime_test1 ((time_t) (time_t_min + now)));
  96. }
  97. static int
  98. irix_6_4_bug ()
  99. {
  100. /* Based on code from Ariel Faigon. */
  101. struct tm tm;
  102. tm.tm_year = 96;
  103. tm.tm_mon = 3;
  104. tm.tm_mday = 0;
  105. tm.tm_hour = 0;
  106. tm.tm_min = 0;
  107. tm.tm_sec = 0;
  108. tm.tm_isdst = -1;
  109. mktime (&tm);
  110. return tm.tm_mon == 2 && tm.tm_mday == 31;
  111. }
  112. static int
  113. bigtime_test (int j)
  114. {
  115. struct tm tm;
  116. time_t now;
  117. tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = j;
  118. now = mktime (&tm);
  119. if (now != (time_t) -1)
  120. {
  121. struct tm *lt = localtime (&now);
  122. if (! (lt
  123. && lt->tm_year == tm.tm_year
  124. && lt->tm_mon == tm.tm_mon
  125. && lt->tm_mday == tm.tm_mday
  126. && lt->tm_hour == tm.tm_hour
  127. && lt->tm_min == tm.tm_min
  128. && lt->tm_sec == tm.tm_sec
  129. && lt->tm_yday == tm.tm_yday
  130. && lt->tm_wday == tm.tm_wday
  131. && ((lt->tm_isdst < 0 ? -1 : 0 < lt->tm_isdst)
  132. == (tm.tm_isdst < 0 ? -1 : 0 < tm.tm_isdst))))
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. static int
  138. year_2050_test ()
  139. {
  140. /* The correct answer for 2050-02-01 00:00:00 in Pacific time,
  141. ignoring leap seconds. */
  142. unsigned long int answer = 2527315200UL;
  143. struct tm tm;
  144. time_t t;
  145. tm.tm_year = 2050 - 1900;
  146. tm.tm_mon = 2 - 1;
  147. tm.tm_mday = 1;
  148. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  149. tm.tm_isdst = -1;
  150. /* Use the portable POSIX.1 specification "TZ=PST8PDT,M4.1.0,M10.5.0"
  151. instead of "TZ=America/Vancouver" in order to detect the bug even
  152. on systems that don't support the Olson extension, or don't have the
  153. full zoneinfo tables installed. */
  154. if (putenv ("TZ=PST8PDT,M4.1.0,M10.5.0") != 0)
  155. return -1;
  156. t = mktime (&tm);
  157. /* Check that the result is either a failure, or close enough
  158. to the correct answer that we can assume the discrepancy is
  159. due to leap seconds. */
  160. return (t == (time_t) -1
  161. || (0 < t && answer - 120 <= t && t <= answer + 120));
  162. }
  163. static int
  164. indiana_test ()
  165. {
  166. if (putenv ("TZ=America/Indiana/Indianapolis") != 0)
  167. return -1;
  168. struct tm tm;
  169. tm.tm_year = 1986 - 1900; tm.tm_mon = 4 - 1; tm.tm_mday = 28;
  170. tm.tm_hour = 16; tm.tm_min = 24; tm.tm_sec = 50; tm.tm_isdst = 0;
  171. time_t std = mktime (&tm);
  172. if (! (std == 515107490 || std == 515107503))
  173. return 1;
  174. /* This platform supports TZDB, either without or with leap seconds.
  175. Return true if GNU Bug#48085 is absent. */
  176. tm.tm_isdst = 1;
  177. time_t dst = mktime (&tm);
  178. return std - dst == 60 * 60;
  179. }
  180. int
  181. main ()
  182. {
  183. int result = 0;
  184. time_t t, delta;
  185. int i, j;
  186. int time_t_signed_magnitude = (time_t) ~ (time_t) 0 < (time_t) -1;
  187. #if HAVE_DECL_ALARM
  188. /* This test makes some buggy mktime implementations loop.
  189. Give up after 60 seconds; a mktime slower than that
  190. isn't worth using anyway. */
  191. signal (SIGALRM, SIG_DFL);
  192. alarm (60);
  193. #endif
  194. time_t_max = (! TIME_T_IS_SIGNED
  195. ? (time_t) -1
  196. : ((((time_t) 1 << (sizeof (time_t) * CHAR_BIT - 2)) - 1)
  197. * 2 + 1));
  198. time_t_min = (! TIME_T_IS_SIGNED
  199. ? (time_t) 0
  200. : time_t_signed_magnitude
  201. ? ~ (time_t) 0
  202. : ~ time_t_max);
  203. delta = time_t_max / 997; /* a suitable prime number */
  204. for (i = 0; i < N_STRINGS; i++)
  205. {
  206. if (tz_strings[i])
  207. putenv (tz_strings[i]);
  208. for (t = 0; t <= time_t_max - delta && (result & 1) == 0; t += delta)
  209. if (! mktime_test (t))
  210. result |= 1;
  211. if ((result & 2) == 0
  212. && ! (mktime_test ((time_t) 1)
  213. && mktime_test ((time_t) (60 * 60))
  214. && mktime_test ((time_t) (60 * 60 * 24))))
  215. result |= 2;
  216. for (j = 1; (result & 4) == 0; j <<= 1)
  217. {
  218. if (! bigtime_test (j))
  219. result |= 4;
  220. if (INT_MAX / 2 < j)
  221. break;
  222. }
  223. if ((result & 8) == 0 && ! bigtime_test (INT_MAX))
  224. result |= 8;
  225. }
  226. if (! irix_6_4_bug ())
  227. result |= 16;
  228. if (! spring_forward_gap ())
  229. result |= 32;
  230. if (! year_2050_test () || ! indiana_test ())
  231. result |= 64;
  232. return result;
  233. }]])],
  234. [gl_cv_func_working_mktime=yes],
  235. [gl_cv_func_working_mktime=no],
  236. [case "$host_os" in
  237. # Guess no on native Windows.
  238. mingw*) gl_cv_func_working_mktime="guessing no" ;;
  239. *) gl_cv_func_working_mktime="$gl_cross_guess_normal" ;;
  240. esac
  241. ])
  242. fi
  243. ])
  244. ])
  245. dnl Main macro of module 'mktime'.
  246. AC_DEFUN([gl_FUNC_MKTIME],
  247. [
  248. AC_REQUIRE([gl_TIME_H_DEFAULTS])
  249. AC_REQUIRE([AC_CANONICAL_HOST])
  250. AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
  251. REPLACE_MKTIME=0
  252. if test "$gl_cv_func_working_mktime" != yes; then
  253. REPLACE_MKTIME=1
  254. AC_DEFINE([NEED_MKTIME_WORKING], [1],
  255. [Define if the compilation of mktime.c should define 'mktime'
  256. with the algorithmic workarounds.])
  257. fi
  258. case "$host_os" in
  259. mingw*)
  260. REPLACE_MKTIME=1
  261. AC_DEFINE([NEED_MKTIME_WINDOWS], [1],
  262. [Define if the compilation of mktime.c should define 'mktime'
  263. with the native Windows TZ workaround.])
  264. ;;
  265. esac
  266. ])
  267. dnl Main macro of module 'mktime-internal'.
  268. AC_DEFUN([gl_FUNC_MKTIME_INTERNAL], [
  269. AC_REQUIRE([gl_FUNC_MKTIME_WORKS])
  270. WANT_MKTIME_INTERNAL=0
  271. dnl BeOS has __mktime_internal in libc, but other platforms don't.
  272. AC_CHECK_FUNC([__mktime_internal],
  273. [AC_DEFINE([mktime_internal], [__mktime_internal],
  274. [Define to the real name of the mktime_internal function.])
  275. ],
  276. [dnl mktime works but it doesn't export __mktime_internal,
  277. dnl so we need to substitute our own mktime implementation.
  278. WANT_MKTIME_INTERNAL=1
  279. AC_DEFINE([NEED_MKTIME_INTERNAL], [1],
  280. [Define if the compilation of mktime.c should define 'mktime_internal'.])
  281. ])
  282. ])
  283. # Prerequisites of lib/mktime.c.
  284. AC_DEFUN([gl_PREREQ_MKTIME], [:])