From b1620387ba654983f660dedf09a59c602fbabf89 Mon Sep 17 00:00:00 2001 From: dmiller Date: Sun, 19 Jan 2020 16:37:37 +0000 Subject: [PATCH] Add thread-safe gmtime equivalent to nbase --- nbase/nbase.h | 1 + nbase/nbase_time.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/nbase/nbase.h b/nbase/nbase.h index 9298e0214..2078448e6 100644 --- a/nbase/nbase.h +++ b/nbase/nbase.h @@ -458,6 +458,7 @@ void usleep(unsigned long usec); /* localtime is not thread safe. This will use a thread safe alternative on * supported platforms. */ int n_localtime(const time_t *timer, struct tm *result); +int n_gmtime(const time_t *timer, struct tm *result); int n_ctime(char *buffer, size_t bufsz, const time_t *timer); /***************** String functions -- See nbase_str.c ******************/ diff --git a/nbase/nbase_time.c b/nbase/nbase_time.c index 90130e04a..57637202e 100644 --- a/nbase/nbase_time.c +++ b/nbase/nbase_time.c @@ -165,6 +165,10 @@ int n_localtime(const time_t *timer, struct tm *result) { return localtime_s(result, timer); } +int n_gmtime(const time_t *timer, struct tm *result) { + return gmtime_s(result, timer); +} + int n_ctime(char *buffer, size_t bufsz, const time_t *timer) { return ctime_s(buffer, bufsz, timer); } @@ -184,6 +188,14 @@ int n_localtime(const time_t *timer, struct tm *result) { return 0; } +int n_gmtime(const time_t *timer, struct tm *result) { + struct tm *tmp = gmtime_s(timer, result); + if (!tmp) { + return errno; + } + return 0; +} + int n_ctime(char *buffer, size_t bufsz, const time_t *timer) { return ctime_s(buffer, bufsz, timer); } @@ -200,6 +212,14 @@ int n_localtime(const time_t *timer, struct tm *result) { return 0; } +int n_gmtime(const time_t *timer, struct tm *result) { + struct tm *tmp = gmtime_r(timer, result); + if (!tmp) { + return errno; + } + return 0; +} + int n_ctime(char *buffer, size_t bufsz, const time_t *timer) { char *tmp = ctime_r(timer, buffer); if (!tmp) { @@ -223,6 +243,15 @@ int n_localtime(const time_t *timer, struct tm *result) { return 0; } +int n_gmtime(const time_t *timer, struct tm *result) { + struct tm *tmp = gmtime(timer); // lgtm[cpp/potentially-dangerous-function] + if (tmp) + *result = *tmp; + else + return errno; + return 0; +} + int n_ctime(char *buffer, size_t bufsz, const time_t *timer) { char *tmp = ctime(timer); // lgtm[cpp/potentially-dangerous-function] if (tmp)