From efde801b7e94f7a558f6bb04e3a553b9ce4bafce Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 24 Dec 2024 14:26:07 +0800 Subject: [PATCH] fix:[TD-33285]gmtime return error in windows --- source/os/src/osTime.c | 3 ++- source/os/test/osTimeTests.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 75cb2b91a2..3bab3e7e25 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -454,7 +454,8 @@ struct tm *taosGmTimeR(const time_t *timep, struct tm *result) { return NULL; } #ifdef WINDOWS - return gmtime_s(result, timep); + errno_t code = gmtime_s(result, timep); + return (code == 0) ? result : NULL; #else return gmtime_r(timep, result); #endif diff --git a/source/os/test/osTimeTests.cpp b/source/os/test/osTimeTests.cpp index 1d34587ad8..d1ce654a76 100644 --- a/source/os/test/osTimeTests.cpp +++ b/source/os/test/osTimeTests.cpp @@ -94,6 +94,35 @@ TEST(osTimeTests, taosLocalTime) { #endif } +TEST(osTimeTests, taosGmTimeR) { + // Test 1: Test when both timep and result are not NULL + time_t timep = 1617531000; // 2021-04-04 18:10:00 + struct tm tmInfo; + ASSERT_NE(taosGmTimeR(&timep, &tmInfo), nullptr); + + char buf[128]; + taosStrfTime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tmInfo); + ASSERT_STREQ(buf, "2021-04-04T10:10:00"); +} + +TEST(osTimeTests, taosTimeGm) { + char *timestr= "2021-04-04T18:10:00"; + struct tm tm = {0}; + + taosStrpTime(timestr, "%Y-%m-%dT%H:%M:%S", &tm); + int64_t seconds = taosTimeGm(&tm); + ASSERT_EQ(seconds, 1617559800); +} + +TEST(osTimeTests, taosMktime) { + char *timestr= "2021-04-04T18:10:00"; + struct tm tm = {0}; + + taosStrpTime(timestr, "%Y-%m-%dT%H:%M:%S", &tm); + time_t seconds = taosMktime(&tm, NULL); + ASSERT_EQ(seconds, 1617531000); +} + TEST(osTimeTests, invalidParameter) { void *retp = NULL; int32_t reti = 0;