year2038.m4 4.8 KB

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