build: Detect gmtime_* definitions via configure

This improves the portability of the codebase and fixes compilation
with mingw-w64 7.0+.

Co-authored-by: fanquake <fanquake@gmail.com>
pull/764/head
Ben Woosley 5 years ago committed by fanquake
parent 41fa2926d8
commit a46484c8b3
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

@ -906,6 +906,22 @@ if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && te
LDFLAGS="$TEMP_LDFLAGS" LDFLAGS="$TEMP_LDFLAGS"
fi fi
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 <ctime>]],
[[ 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 <ctime>]],
[[ 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) ]
)
]
)
dnl Check for different ways of gathering OS randomness dnl Check for different ways of gathering OS randomness
AC_MSG_CHECKING(for Linux getrandom syscall) AC_MSG_CHECKING(for Linux getrandom syscall)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
@ -1596,6 +1612,7 @@ AC_SUBST(EVENT_LIBS)
AC_SUBST(EVENT_PTHREADS_LIBS) AC_SUBST(EVENT_PTHREADS_LIBS)
AC_SUBST(ZMQ_LIBS) AC_SUBST(ZMQ_LIBS)
AC_SUBST(QR_LIBS) AC_SUBST(QR_LIBS)
AC_SUBST(HAVE_GMTIME_R)
AC_SUBST(HAVE_FDATASYNC) AC_SUBST(HAVE_FDATASYNC)
AC_SUBST(HAVE_FULLFSYNC) AC_SUBST(HAVE_FULLFSYNC)
AC_SUBST(HAVE_O_CLOEXEC) AC_SUBST(HAVE_O_CLOEXEC)

@ -78,10 +78,10 @@ int64_t GetSystemTimeInSeconds()
std::string FormatISO8601DateTime(int64_t nTime) { std::string FormatISO8601DateTime(int64_t nTime) {
struct tm ts; struct tm ts;
time_t time_val = nTime; time_t time_val = nTime;
#ifdef _MSC_VER #ifdef HAVE_GMTIME_R
if (gmtime_s(&ts, &time_val) != 0) {
#else
if (gmtime_r(&time_val, &ts) == nullptr) { if (gmtime_r(&time_val, &ts) == nullptr) {
#else
if (gmtime_s(&ts, &time_val) != 0) {
#endif #endif
return {}; return {};
} }
@ -91,10 +91,10 @@ std::string FormatISO8601DateTime(int64_t nTime) {
std::string FormatISO8601Date(int64_t nTime) { std::string FormatISO8601Date(int64_t nTime) {
struct tm ts; struct tm ts;
time_t time_val = nTime; time_t time_val = nTime;
#ifdef _MSC_VER #ifdef HAVE_GMTIME_R
if (gmtime_s(&ts, &time_val) != 0) {
#else
if (gmtime_r(&time_val, &ts) == nullptr) { if (gmtime_r(&time_val, &ts) == nullptr) {
#else
if (gmtime_s(&ts, &time_val) != 0) {
#endif #endif
return {}; return {};
} }

Loading…
Cancel
Save