diff --git a/configure.ac b/configure.ac index e31531730a..25a5017431 100644 --- a/configure.ac +++ b/configure.ac @@ -913,6 +913,22 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([ ) LDFLAGS="$TEMP_LDFLAGS" +dnl check for gmtime_r(), fallback to gmtime_s() if that is unavailable +dnl fail if neither are available. +AC_MSG_CHECKING(for gmtime_r) +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[ gmtime_r((const time_t *) nullptr, (struct tm *) nullptr); ]])], + [ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GMTIME_R, 1, [Define this symbol if gmtime_r is available]) ], + [ AC_MSG_RESULT(no); + AC_MSG_CHECKING(for gmtime_s); + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[ gmtime_s((struct tm *) nullptr, (const time_t *) nullptr); ]])], + [ AC_MSG_RESULT(yes)], + [ AC_MSG_RESULT(no); AC_MSG_ERROR(Both gmtime_r and gmtime_s are unavailable) ] + ) + ] +) + # Check for different ways of gathering OS randomness AC_MSG_CHECKING(for Linux getrandom syscall) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include @@ -1574,6 +1590,7 @@ AC_SUBST(HAVE_BUILTIN_PREFETCH) AC_SUBST(HAVE_MM_PREFETCH) AC_SUBST(HAVE_STRONG_GETAUXVAL) AC_SUBST(HAVE_WEAK_GETAUXVAL) +AC_SUBST(HAVE_GMTIME_R) AC_CONFIG_FILES([Makefile src/Makefile doc/man/Makefile share/setup.nsi share/qt/Info.plist test/config.ini]) AC_CONFIG_FILES([contrib/devtools/split-debug.sh],[chmod +x contrib/devtools/split-debug.sh]) AM_COND_IF([HAVE_DOXYGEN], [AC_CONFIG_FILES([doc/Doxyfile])]) diff --git a/src/util/time.cpp b/src/util/time.cpp index 44f45249f1..d768065cd6 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -76,10 +76,10 @@ int64_t GetSystemTimeInSeconds() std::string FormatISO8601DateTime(int64_t nTime) { struct tm ts; time_t time_val = nTime; -#ifdef _MSC_VER - gmtime_s(&ts, &time_val); -#else +#ifdef HAVE_GMTIME_R gmtime_r(&time_val, &ts); +#else + gmtime_s(&ts, &time_val); #endif return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec); } @@ -87,10 +87,10 @@ std::string FormatISO8601DateTime(int64_t nTime) { std::string FormatISO8601Date(int64_t nTime) { struct tm ts; time_t time_val = nTime; -#ifdef _MSC_VER - gmtime_s(&ts, &time_val); -#else +#ifdef HAVE_GMTIME_R gmtime_r(&time_val, &ts); +#else + gmtime_s(&ts, &time_val); #endif return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday); } @@ -98,10 +98,10 @@ std::string FormatISO8601Date(int64_t nTime) { std::string FormatISO8601Time(int64_t nTime) { struct tm ts; time_t time_val = nTime; -#ifdef _MSC_VER - gmtime_s(&ts, &time_val); -#else +#ifdef HAVE_GMTIME_R gmtime_r(&time_val, &ts); +#else + gmtime_s(&ts, &time_val); #endif return strprintf("%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec); }