year2038.m4 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # year2038.m4 serial 8
  2. dnl Copyright (C) 2017-2022 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 Attempt to ensure that 'time_t' can go past the year 2038 and that
  7. dnl the functions 'time', 'stat', etc. work with post-2038 timestamps.
  8. m4_ifdef([AC_SYS_YEAR2038], [
  9. AC_DEFUN([gl_YEAR2038_EARLY])
  10. AC_DEFUN([gl_YEAR2038], [AC_SYS_YEAR2038])
  11. AC_DEFUN([gl_YEAR2038_BODY], [_AC_SYS_YEAR2038])
  12. ], [
  13. AC_DEFUN([gl_YEAR2038_EARLY],
  14. [
  15. AC_REQUIRE([AC_CANONICAL_HOST])
  16. case "$host_os" in
  17. mingw*)
  18. AC_DEFINE([__MINGW_USE_VC2005_COMPAT], [1],
  19. [For 64-bit time_t on 32-bit mingw.])
  20. ;;
  21. esac
  22. ])
  23. # gl_YEAR2038_TEST_INCLUDES
  24. # -------------------------
  25. AC_DEFUN([gl_YEAR2038_TEST_INCLUDES],
  26. [[
  27. #include <time.h>
  28. /* Check that time_t can represent 2**32 - 1 correctly. */
  29. #define LARGE_TIME_T \\
  30. ((time_t) (((time_t) 1 << 30) - 1 + 3 * ((time_t) 1 << 30)))
  31. int verify_time_t_range[(LARGE_TIME_T / 65537 == 65535
  32. && LARGE_TIME_T % 65537 == 0)
  33. ? 1 : -1];
  34. ]])
  35. # gl_YEAR2038_BODY(REQUIRE-YEAR2038-SAFE)
  36. -----------------------------------------
  37. AC_DEFUN([gl_YEAR2038_BODY],
  38. [
  39. AC_ARG_ENABLE([year2038],
  40. [ --disable-year2038 omit support for timestamps past the year 2038])
  41. AS_IF([test "$enable_year2038" != no],
  42. [
  43. dnl On many systems, time_t is already a 64-bit type.
  44. dnl On those systems where time_t is still 32-bit, it requires kernel
  45. dnl and libc support to make it 64-bit. For glibc 2.34 and later on Linux,
  46. dnl defining _TIME_BITS=64 and _FILE_OFFSET_BITS=64 is needed on x86 and ARM.
  47. dnl
  48. dnl On native Windows, the system include files define types __time32_t
  49. dnl and __time64_t. By default, time_t is an alias of
  50. dnl - __time32_t on 32-bit mingw,
  51. dnl - __time64_t on 64-bit mingw and on MSVC (since MSVC 8).
  52. dnl But when compiling with -D__MINGW_USE_VC2005_COMPAT, time_t is an
  53. dnl alias of __time64_t.
  54. dnl And when compiling with -D_USE_32BIT_TIME_T, time_t is an alias of
  55. dnl __time32_t.
  56. AC_CACHE_CHECK([for time_t past the year 2038], [gl_cv_type_time_t_y2038],
  57. [AC_COMPILE_IFELSE(
  58. [AC_LANG_SOURCE([gl_YEAR2038_TEST_INCLUDES])],
  59. [gl_cv_type_time_t_y2038=yes], [gl_cv_type_time_t_y2038=no])
  60. ])
  61. if test "$gl_cv_type_time_t_y2038" = no; then
  62. AC_CACHE_CHECK([for 64-bit time_t with _TIME_BITS=64],
  63. [gl_cv_type_time_t_bits_macro],
  64. [AC_COMPILE_IFELSE(
  65. [AC_LANG_SOURCE([[#define _TIME_BITS 64
  66. #define _FILE_OFFSET_BITS 64
  67. ]gl_YEAR2038_TEST_INCLUDES])],
  68. [gl_cv_type_time_t_bits_macro=yes],
  69. [gl_cv_type_time_t_bits_macro=no])
  70. ])
  71. if test "$gl_cv_type_time_t_bits_macro" = yes; then
  72. AC_DEFINE([_TIME_BITS], [64],
  73. [Number of bits in a timestamp, on hosts where this is settable.])
  74. dnl AC_SYS_LARGFILE also defines this; it's OK if we do too.
  75. AC_DEFINE([_FILE_OFFSET_BITS], [64],
  76. [Number of bits in a file offset, on hosts where this is settable.])
  77. gl_cv_type_time_t_y2038=yes
  78. fi
  79. fi
  80. if test $gl_cv_type_time_t_y2038 = no; then
  81. AC_COMPILE_IFELSE(
  82. [AC_LANG_SOURCE(
  83. [[#ifdef _USE_32BIT_TIME_T
  84. int ok;
  85. #else
  86. error fail
  87. #endif
  88. ]])],
  89. [AC_MSG_FAILURE(
  90. [The 'time_t' type stops working after January 2038.
  91. Remove _USE_32BIT_TIME_T from the compiler flags.])],
  92. [# If not cross-compiling and $1 says we should check,
  93. # and 'touch' works with a large timestamp, then evidently wider time_t
  94. # is desired and supported, so fail and ask the builder to fix the
  95. # problem. Otherwise, just warn the builder.
  96. m4_ifval([$1],
  97. [if test $cross_compiling = no \
  98. && TZ=UTC0 touch -t 210602070628.15 conftest.time 2>/dev/null; then
  99. case `TZ=UTC0 LC_ALL=C ls -l conftest.time 2>/dev/null` in
  100. *'Feb 7 2106'* | *'Feb 7 17:10'*)
  101. AC_MSG_FAILURE(
  102. [The 'time_t' type stops working after January 2038,
  103. and your system appears to support a wider 'time_t'.
  104. Try configuring with 'CC="${CC} -m64"'.
  105. To build with a 32-bit time_t anyway (not recommended),
  106. configure with '--disable-year2038'.]);;
  107. esac
  108. rm -f conftest.time
  109. fi])
  110. if test "$gl_warned_about_y2038" != yes; then
  111. AC_MSG_WARN(
  112. [The 'time_t' type stops working after January 2038,
  113. and this package needs a wider 'time_t' type
  114. if there is any way to access timestamps after that.
  115. Configure with 'CC="${CC} -m64"' perhaps?])
  116. gl_warned_about_y2038=yes
  117. fi
  118. ])
  119. fi])
  120. ])
  121. AC_DEFUN([gl_YEAR2038],
  122. [
  123. gl_YEAR2038_BODY([require-year2038-safe])
  124. ])
  125. ]) # m4_ifndef AC_SYS_YEAR2038