aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--configure.ac17
-rw-r--r--src/util/time.cpp12
2 files changed, 23 insertions, 6 deletions
diff --git a/configure.ac b/configure.ac
index 1f85dd3a99..4cfd5c9274 100644
--- a/configure.ac
+++ b/configure.ac
@@ -906,6 +906,22 @@ if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && te
LDFLAGS="$TEMP_LDFLAGS"
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
AC_MSG_CHECKING(for Linux getrandom syscall)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
@@ -1596,6 +1612,7 @@ AC_SUBST(EVENT_LIBS)
AC_SUBST(EVENT_PTHREADS_LIBS)
AC_SUBST(ZMQ_LIBS)
AC_SUBST(QR_LIBS)
+AC_SUBST(HAVE_GMTIME_R)
AC_SUBST(HAVE_FDATASYNC)
AC_SUBST(HAVE_FULLFSYNC)
AC_SUBST(HAVE_O_CLOEXEC)
diff --git a/src/util/time.cpp b/src/util/time.cpp
index 14937b985e..0938ff36a6 100644
--- a/src/util/time.cpp
+++ b/src/util/time.cpp
@@ -78,10 +78,10 @@ int64_t GetSystemTimeInSeconds()
std::string FormatISO8601DateTime(int64_t nTime) {
struct tm ts;
time_t time_val = nTime;
-#ifdef _MSC_VER
- if (gmtime_s(&ts, &time_val) != 0) {
-#else
+#ifdef HAVE_GMTIME_R
if (gmtime_r(&time_val, &ts) == nullptr) {
+#else
+ if (gmtime_s(&ts, &time_val) != 0) {
#endif
return {};
}
@@ -91,10 +91,10 @@ std::string FormatISO8601DateTime(int64_t nTime) {
std::string FormatISO8601Date(int64_t nTime) {
struct tm ts;
time_t time_val = nTime;
-#ifdef _MSC_VER
- if (gmtime_s(&ts, &time_val) != 0) {
-#else
+#ifdef HAVE_GMTIME_R
if (gmtime_r(&time_val, &ts) == nullptr) {
+#else
+ if (gmtime_s(&ts, &time_val) != 0) {
#endif
return {};
}