Add portability wrapper for localtime_r()

This commit is contained in:
João Valverde 2023-09-03 01:35:31 +01:00
parent f475a6d7f2
commit 46f6febd47
4 changed files with 35 additions and 0 deletions

View File

@ -83,6 +83,10 @@ endif()
check_symbol_exists("clock_gettime" "time.h" HAVE_CLOCK_GETTIME)
# Some platforms (macOS pre 10.15) are non-conformant with C11 and lack timespec_get()
check_symbol_exists("timespec_get" "time.h" HAVE_TIMESPEC_GET)
if(NOT MSVC)
check_symbol_exists("localtime_r" "time.h" HAVE_LOCALTIME_R)
check_symbol_exists("tzset" "time.h" HAVE_TZSET)
endif()
check_function_exists("getifaddrs" HAVE_GETIFADDRS)
check_function_exists("issetugid" HAVE_ISSETUGID)
check_function_exists("setresgid" HAVE_SETRESGID)

View File

@ -49,6 +49,12 @@
/* Define to 1 if you have the `timespec_get` function. */
#cmakedefine HAVE_TIMESPEC_GET 1
/* Define to 1 if you have the `localtime_r` function. */
#cmakedefine HAVE_LOCALTIME_R 1
/* Define to 1 if you have the `tzset` function. */
#cmakedefine HAVE_TZSET 1
/* Define to use the MaxMind DB library */
#cmakedefine HAVE_MAXMINDDB 1

View File

@ -250,6 +250,28 @@ char *ws_strptime(const char *restrict s, const char *restrict format,
#endif
}
struct tm *
ws_localtime_r(const time_t *timep, struct tm *result)
{
#if defined(HAVE_LOCALTIME_R)
#ifdef HAVE_TZSET
tzset();
#endif
return localtime_r(timep, result);
#elif defined(_MSC_VER)
errno_t err = localtime_s(result, timep);
if (err == 0)
return result;
return NULL;
#else
struct tm *aux = localtime(timep);
if (aux == NULL)
return NULL;
*result = *aux;
return result;
#endif
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*

View File

@ -71,6 +71,9 @@ struct timespec *ws_clock_get_realtime(struct timespec *ts);
WS_DLL_PUBLIC
char *ws_strptime(const char *s, const char *format, struct tm *tm);
WS_DLL_PUBLIC
struct tm *ws_localtime_r(const time_t *timep, struct tm *result);
#ifdef __cplusplus
}
#endif /* __cplusplus */