From 3bdef853b2b03a3df53be9fc64a5073ddcb38f32 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Fri, 24 Mar 2023 10:29:36 +0800 Subject: [PATCH 1/5] fix: fix windows handling localtime_s error --- source/os/src/osTime.c | 7 +++++++ tools/shell/src/shellEngine.c | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 685693a709..c0f4bc7d3e 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -33,6 +33,7 @@ #include //#define TM_YEAR_BASE 1970 //origin #define TM_YEAR_BASE 1900 // slguan +#define _MAX__TIME64_T 0x793406fffi64 // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) // until 00:00:00 January 1, 1970 @@ -444,6 +445,9 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result) { result->tm_yday = 0; result->tm_isdst = 0; } else { + if (*timep > _MAX__TIME64_T) { + return NULL; + } localtime_s(result, timep); } #else @@ -500,6 +504,9 @@ struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst) result->tm_yday = 0; result->tm_isdst = 0; } else { + if (*timep > _MAX__TIME64_T) { + return NULL; + } localtime_s(result, timep); } #elif defined(LINUX) diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index 3080b15b8c..e732d42082 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -291,7 +291,10 @@ char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) { } struct tm ptm = {0}; - taosLocalTime(&tt, &ptm); + if (taosLocalTime(&tt, &ptm) == NULL) { + sprintf(buf, "%s", "NaN"); + return buf; + } size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); if (precision == TSDB_TIME_PRECISION_NANO) { From 6f119ef4d0c70946842410e5b7e367e77bd290b8 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Fri, 24 Mar 2023 11:24:04 +0800 Subject: [PATCH 2/5] change print format --- tools/shell/src/shellEngine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index e732d42082..a87ba16267 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -292,7 +292,7 @@ char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) { struct tm ptm = {0}; if (taosLocalTime(&tt, &ptm) == NULL) { - sprintf(buf, "%s", "NaN"); + sprintf(buf, "NaN"); return buf; } size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); From 223ae4cc7e1b2da71d4d0aa2c9ed1d4d38375f72 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Fri, 24 Mar 2023 14:50:56 +0800 Subject: [PATCH 3/5] fix --- source/os/src/osTime.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index c0f4bc7d3e..f0ab91f9c5 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -33,7 +33,6 @@ #include //#define TM_YEAR_BASE 1970 //origin #define TM_YEAR_BASE 1900 // slguan -#define _MAX__TIME64_T 0x793406fffi64 // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) // until 00:00:00 January 1, 1970 @@ -419,7 +418,9 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result) { LARGE_INTEGER offset; struct tm tm1; time_t tt = 0; - localtime_s(&tm1, &tt); + if (localtime_s(&tm1, &tt) != 0 ) { + return NULL; + } ss.wYear = tm1.tm_year + 1900; ss.wMonth = tm1.tm_mon + 1; ss.wDay = tm1.tm_mday; @@ -445,10 +446,9 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result) { result->tm_yday = 0; result->tm_isdst = 0; } else { - if (*timep > _MAX__TIME64_T) { + if (localtime_s(result, timep) != 0) { return NULL; } - localtime_s(result, timep); } #else localtime_r(timep, result); From b06e9e10313d085168470362d3b6731dbe3ce7af Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Fri, 24 Mar 2023 14:50:56 +0800 Subject: [PATCH 4/5] fix --- source/os/src/osTime.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index f0ab91f9c5..28be12e908 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -478,7 +478,9 @@ struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst) LARGE_INTEGER offset; struct tm tm1; time_t tt = 0; - localtime_s(&tm1, &tt); + if (localtime_s(&tm1, &tt) != 0) { + return NULL; + } ss.wYear = tm1.tm_year + 1900; ss.wMonth = tm1.tm_mon + 1; ss.wDay = tm1.tm_mday; @@ -504,10 +506,9 @@ struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst) result->tm_yday = 0; result->tm_isdst = 0; } else { - if (*timep > _MAX__TIME64_T) { + if (localtime_s(result, timep) != 0) { return NULL; } - localtime_s(result, timep); } #elif defined(LINUX) time_t secsMin = 60, secsHour = 3600, secsDay = 3600 * 24; From 9788532a0ab81e05e36e5b340ceb3ff239ec2ad1 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Fri, 24 Mar 2023 14:50:56 +0800 Subject: [PATCH 5/5] fix --- source/os/src/osTime.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 28be12e908..5d5bff8c48 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -413,6 +413,8 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result) { } #ifdef WINDOWS if (*timep < 0) { + return NULL; + // TODO: bugs in following code SYSTEMTIME ss, s; FILETIME ff, f; LARGE_INTEGER offset; @@ -473,6 +475,8 @@ struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst) } #ifdef WINDOWS if (*timep < 0) { + return NULL; + // TODO: bugs in following code SYSTEMTIME ss, s; FILETIME ff, f; LARGE_INTEGER offset;