Skip to content

Commit 69ff7a4

Browse files
committed
util: fix compilation with mingw-w64 7.0.0
1 parent 4ced9b5 commit 69ff7a4

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

configure.ac

+17
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,22 @@ if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && te
863863
LDFLAGS="$TEMP_LDFLAGS"
864864
fi
865865

866+
dnl check for gmtime_r(), fallback to gmtime_s() if that is unavailable
867+
dnl fail if neither are available.
868+
AC_MSG_CHECKING(for gmtime_r)
869+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
870+
[[ gmtime_r((const time_t *) nullptr, (struct tm *) nullptr); ]])],
871+
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GMTIME_R, 1, [Define this symbol if gmtime_r is available]) ],
872+
[ AC_MSG_RESULT(no);
873+
AC_MSG_CHECKING(for gmtime_s);
874+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
875+
[[ gmtime_s((struct tm *) nullptr, (const time_t *) nullptr); ]])],
876+
[ AC_MSG_RESULT(yes)],
877+
[ AC_MSG_RESULT(no); AC_MSG_ERROR(Both gmtime_r and gmtime_s are unavailable) ]
878+
)
879+
]
880+
)
881+
866882
# Check for different ways of gathering OS randomness
867883
AC_MSG_CHECKING(for Linux getrandom syscall)
868884
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
@@ -1493,6 +1509,7 @@ AC_SUBST(PROTOBUF_LIBS)
14931509
AC_SUBST(QR_LIBS)
14941510
AC_SUBST(USE_NUM_GMP)
14951511
AC_SUBST(USE_NUM_OPENSSL)
1512+
AC_SUBST(HAVE_GMTIME_R)
14961513
AC_SUBST(HAVE_FDATASYNC)
14971514
AC_SUBST(HAVE_FULLFSYNC)
14981515
AC_SUBST(HAVE_O_CLOEXEC)

src/utiltime.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ std::string DurationToDHMS(int64_t nDurationTime)
9999
std::string FormatISO8601DateTime(int64_t nTime) {
100100
struct tm ts;
101101
time_t time_val = nTime;
102-
#ifdef _MSC_VER
103-
if (gmtime_s(&ts, &time_val) != 0) {
104-
#else
102+
#ifdef HAVE_GMTIME_R
105103
if (gmtime_r(&time_val, &ts) == nullptr) {
104+
#else
105+
if (gmtime_s(&ts, &time_val) != 0) {
106106
#endif
107107
return {};
108108
}
@@ -112,10 +112,10 @@ std::string FormatISO8601DateTime(int64_t nTime) {
112112
std::string FormatISO8601DateTimeForBackup(int64_t nTime) {
113113
struct tm ts;
114114
time_t time_val = nTime;
115-
#ifdef _MSC_VER
116-
if (gmtime_s(&ts, &time_val) != 0) {
117-
#else
115+
#ifdef HAVE_GMTIME_R
118116
if (gmtime_r(&time_val, &ts) == nullptr) {
117+
#else
118+
if (gmtime_s(&ts, &time_val) != 0) {
119119
#endif
120120
return {};
121121
}
@@ -125,10 +125,10 @@ std::string FormatISO8601DateTimeForBackup(int64_t nTime) {
125125
std::string FormatISO8601Date(int64_t nTime) {
126126
struct tm ts;
127127
time_t time_val = nTime;
128-
#ifdef _MSC_VER
129-
if (gmtime_s(&ts, &time_val) != 0) {
130-
#else
128+
#ifdef HAVE_GMTIME_R
131129
if (gmtime_r(&time_val, &ts) == nullptr) {
130+
#else
131+
if (gmtime_s(&ts, &time_val) != 0) {
132132
#endif
133133
return {};
134134
}
@@ -138,10 +138,10 @@ std::string FormatISO8601Date(int64_t nTime) {
138138
std::string FormatISO8601Time(int64_t nTime) {
139139
struct tm ts;
140140
time_t time_val = nTime;
141-
#ifdef _MSC_VER
142-
if (gmtime_s(&ts, &time_val) != 0) {
143-
#else
141+
#ifdef HAVE_GMTIME_R
144142
if (gmtime_r(&time_val, &ts) == nullptr) {
143+
#else
144+
if (gmtime_s(&ts, &time_val) != 0) {
145145
#endif
146146
return {};
147147
}

0 commit comments

Comments
 (0)