From 3113697ef7746b43adc6eceb22ab547ca2f47950 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Mon, 5 Aug 2024 16:57:40 +0200 Subject: [PATCH] include/timehead.h: fix hacky macro localtime_*() fallbacks for WIN32 builds [#2583, #1611] Partially inspired by /mingw64/include/time.h NOTE: Unlike linux+mingw cross-builds, the semi-native ones on Windows with MSYS2 do not enforce _POSIX_THREAD_SAFE_FUNCTIONS and so can lack the optional declarations in the file above. Signed-off-by: Jim Klimov --- include/timehead.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/timehead.h b/include/timehead.h index 984f8ff56b..0eeea4bf9b 100644 --- a/include/timehead.h +++ b/include/timehead.h @@ -46,8 +46,10 @@ char * strptime(const char *buf, const char *fmt, struct tm *tm); #if !(defined HAVE_LOCALTIME_R && HAVE_LOCALTIME_R) && !(defined HAVE_DECL_LOCALTIME_R && HAVE_DECL_LOCALTIME_R) # if (defined HAVE_LOCALTIME_S && HAVE_LOCALTIME_S) || (defined HAVE_DECL_LOCALTIME_S && HAVE_DECL_LOCALTIME_S) -/* A bit of a silly trick, but should help on MSYS2 builds it seems */ -# define localtime_r(timer, buf) localtime_s(timer, buf) +/* A bit of a silly trick, but should help on MSYS2 builds it seems + * errno_t localtime_s(struct tm *_Tm, const time_t *_Time) + */ +# define localtime_r(timer, buf) (localtime_s(buf, timer) ? NULL : buf) # else # include /* memcpy */ static inline struct tm *localtime_r( const time_t *timer, struct tm *buf ) { @@ -61,7 +63,8 @@ static inline struct tm *localtime_r( const time_t *timer, struct tm *buf ) { #if !(defined HAVE_GMTIME_R && HAVE_GMTIME_R) && !(defined HAVE_DECL_GMTIME_R && HAVE_DECL_GMTIME_R) # if (defined HAVE_GMTIME_S && HAVE_GMTIME_S) || (defined HAVE_DECL_GMTIME_S && HAVE_DECL_GMTIME_S) -# define gmtime_r(timer, buf) gmtime_s(timer, buf) +/* See comment above */ +# define gmtime_r(timer, buf) (gmtime_s(buf, timer) ? NULL : buf) # else # include /* memcpy */ static inline struct tm *gmtime_r( const time_t *timer, struct tm *buf ) {