Skip to content

Commit a46484c

Browse files
Empactfanquake
andcommitted
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 <[email protected]>
1 parent 41fa292 commit a46484c

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

configure.ac

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,22 @@ if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && te
906906
LDFLAGS="$TEMP_LDFLAGS"
907907
fi
908908

909+
dnl check for gmtime_r(), fallback to gmtime_s() if that is unavailable
910+
dnl fail if neither are available.
911+
AC_MSG_CHECKING(for gmtime_r)
912+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
913+
[[ gmtime_r((const time_t *) nullptr, (struct tm *) nullptr); ]])],
914+
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GMTIME_R, 1, [Define this symbol if gmtime_r is available]) ],
915+
[ AC_MSG_RESULT(no);
916+
AC_MSG_CHECKING(for gmtime_s);
917+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
918+
[[ gmtime_s((struct tm *) nullptr, (const time_t *) nullptr); ]])],
919+
[ AC_MSG_RESULT(yes)],
920+
[ AC_MSG_RESULT(no); AC_MSG_ERROR(Both gmtime_r and gmtime_s are unavailable) ]
921+
)
922+
]
923+
)
924+
909925
dnl Check for different ways of gathering OS randomness
910926
AC_MSG_CHECKING(for Linux getrandom syscall)
911927
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
@@ -1596,6 +1612,7 @@ AC_SUBST(EVENT_LIBS)
15961612
AC_SUBST(EVENT_PTHREADS_LIBS)
15971613
AC_SUBST(ZMQ_LIBS)
15981614
AC_SUBST(QR_LIBS)
1615+
AC_SUBST(HAVE_GMTIME_R)
15991616
AC_SUBST(HAVE_FDATASYNC)
16001617
AC_SUBST(HAVE_FULLFSYNC)
16011618
AC_SUBST(HAVE_O_CLOEXEC)

src/util/time.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ int64_t GetSystemTimeInSeconds()
7878
std::string FormatISO8601DateTime(int64_t nTime) {
7979
struct tm ts;
8080
time_t time_val = nTime;
81-
#ifdef _MSC_VER
82-
if (gmtime_s(&ts, &time_val) != 0) {
83-
#else
81+
#ifdef HAVE_GMTIME_R
8482
if (gmtime_r(&time_val, &ts) == nullptr) {
83+
#else
84+
if (gmtime_s(&ts, &time_val) != 0) {
8585
#endif
8686
return {};
8787
}
@@ -91,10 +91,10 @@ std::string FormatISO8601DateTime(int64_t nTime) {
9191
std::string FormatISO8601Date(int64_t nTime) {
9292
struct tm ts;
9393
time_t time_val = nTime;
94-
#ifdef _MSC_VER
95-
if (gmtime_s(&ts, &time_val) != 0) {
96-
#else
94+
#ifdef HAVE_GMTIME_R
9795
if (gmtime_r(&time_val, &ts) == nullptr) {
96+
#else
97+
if (gmtime_s(&ts, &time_val) != 0) {
9898
#endif
9999
return {};
100100
}

0 commit comments

Comments
 (0)