From dae0016bb27ae70922a89d1a44b866f9229a3c1c Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 13 Apr 2019 03:01:31 +0200 Subject: [PATCH] times times times --- client/cmdlft55xx.c | 17 +++++++++++------ client/cmdmain.c | 17 +++++++++-------- client/util.c | 21 +++++++++++++++------ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c index 82dc68c8a..a5a262cce 100644 --- a/client/cmdlft55xx.c +++ b/client/cmdlft55xx.c @@ -6,8 +6,8 @@ //----------------------------------------------------------------------------- // Low frequency T55xx commands //----------------------------------------------------------------------------- -/* Ensure localtime_r is available even with -std=c99; must be included before - */ + +// ensure localtime_r is available even with -std=c99; must be included before #if !defined(_WIN32) #define _POSIX_C_SOURCE 200112L #endif @@ -1205,10 +1205,15 @@ static int CmdT55xxReadTrace(const char *Cmd) { si += 5; data.dw = PackBits(si, 15, DemodBuffer); - struct tm *tm = NULL; - time_t now = time(NULL); - localtime_r(&now, tm); - if (data.year > tm->tm_year - 110) + struct tm *ct, tm_buf; + time_t now = time(NULL); +#if defined(_WIN32) + ct = localtime_s(&tm_buf, &now) == 0 ? &tm_buf : NULL; +#else + ct = localtime_r(&now, &tm_buf); +#endif + + if (data.year > ct->tm_year - 110) data.year += 2000; else data.year += 2010; diff --git a/client/cmdmain.c b/client/cmdmain.c index 615ec72e2..db91b9c19 100644 --- a/client/cmdmain.c +++ b/client/cmdmain.c @@ -9,8 +9,7 @@ // Main command parser entry point //----------------------------------------------------------------------------- -/* Ensure gmtime_r is available even with -std=c99; must be included before - */ +// ensure gmtime_r is available even with -std=c99; must be included before #if !defined(_WIN32) #define _POSIX_C_SOURCE 200112L #endif @@ -19,13 +18,15 @@ static int CmdHelp(const char *Cmd); static int CmdRem(const char *Cmd) { - char buf[22]; - - memset(buf, 0x00, sizeof(buf)); - struct tm *curTime = NULL; + char buf[22] = {0}; + struct tm *ct, tm_buf; time_t now = time(NULL); - gmtime_r(&now, curTime); - strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", curTime); // ISO8601 +#if defined(_WIN32) + ct = gmtime_s(&tm_buf, &now) == 0 ? &tm_buf : NULL; +#else + ct = gmtime_r(&now, &tm_buf); +#endif + strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", ct); // ISO8601 PrintAndLogEx(NORMAL, "%s remark: %s", buf, Cmd); return 0; } diff --git a/client/util.c b/client/util.c index 9f600f85a..a71f79ffd 100644 --- a/client/util.c +++ b/client/util.c @@ -7,6 +7,12 @@ //----------------------------------------------------------------------------- // utilities //----------------------------------------------------------------------------- + +// ensure gmtime_r is available even with -std=c99; must be included before + #if !defined(_WIN32) +#define _POSIX_C_SOURCE 200112L +#endif + #include "util.h" #define UTIL_BUFFER_SIZE_SPRINT 4097 @@ -92,12 +98,15 @@ void AddLogUint64(const char *fn, const char *data, const uint64_t value) { } void AddLogCurrentDT(const char *fn) { - char buf[20]; - memset(buf, 0x00, sizeof(buf)); - struct tm *curTime; - time_t now = time(0); - curTime = gmtime(&now); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", curTime); + char buf[20] = {0}; + struct tm *ct, tm_buf; + time_t now = time(NULL); +#if defined(_WIN32) + ct = gmtime_s(&tm_buf, &now) == 0 ? &tm_buf : NULL; +#else + ct = gmtime_r(&now, &tm_buf); +#endif + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ct); AddLogLine(fn, "\nanticollision: ", buf); }