test: for ostime

This commit is contained in:
Shengliang Guan 2024-12-11 15:37:20 +08:00
parent 1a5f61fbe3
commit 98075a7c5a
4 changed files with 121 additions and 71 deletions

View File

@ -31,7 +31,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
//#define TM_YEAR_BASE 1970 //origin
// #define TM_YEAR_BASE 1970 //origin
#define TM_YEAR_BASE 1900 // slguan
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
@ -345,8 +345,7 @@ char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) {
#endif
}
size_t
taosStrfTime(char *s, size_t maxsize, char const *format, struct tm const *t){
size_t taosStrfTime(char *s, size_t maxsize, char const *format, struct tm const *t) {
if (!s || !format || !t) return 0;
return strftime(s, maxsize, format, t);
}
@ -379,7 +378,7 @@ int32_t taosTime(time_t *t) {
if (t == NULL) {
return TSDB_CODE_INVALID_PARA;
}
time_t r = time(t);
time_t r = time(t);
if (r == (time_t)-1) {
return TAOS_SYSTEM_ERROR(errno);
}
@ -433,15 +432,15 @@ time_t taosMktime(struct tm *timep, timezone_t tz) {
return result;
}
int64_t tzw = 0;
#ifdef _MSC_VER
#if _MSC_VER >= 1900
tzw = _timezone;
#endif
#endif
#ifdef _MSC_VER
#if _MSC_VER >= 1900
tzw = _timezone;
#endif
#endif
return user_mktime64(timep->tm_year + 1900, timep->tm_mon + 1, timep->tm_mday, timep->tm_hour, timep->tm_min,
timep->tm_sec, tzw);
#else
time_t r = tz != NULL ? mktime_z(tz, timep) : mktime(timep);
time_t r = (tz != NULL ? mktime_z(tz, timep) : mktime(timep));
if (r == (time_t)-1) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
@ -450,7 +449,7 @@ time_t taosMktime(struct tm *timep, timezone_t tz) {
#endif
}
struct tm *taosGmTimeR(const time_t *timep, struct tm *result){
struct tm *taosGmTimeR(const time_t *timep, struct tm *result) {
if (timep == NULL || result == NULL) {
return NULL;
}
@ -461,7 +460,7 @@ struct tm *taosGmTimeR(const time_t *timep, struct tm *result){
#endif
}
time_t taosTimeGm(struct tm *tmp){
time_t taosTimeGm(struct tm *tmp) {
if (tmp == NULL) {
return -1;
}
@ -530,7 +529,7 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf, int3
}
return result;
#else
res = tz != NULL ? localtime_rz(tz, timep, result): localtime_r(timep, result);
res = (tz != NULL ? localtime_rz(tz, timep, result) : localtime_r(timep, result));
if (res == NULL && buf != NULL) {
(void)snprintf(buf, bufSize, "NaN");
}
@ -544,8 +543,8 @@ int32_t taosGetTimestampSec() { return (int32_t)time(NULL); }
int32_t taosClockGetTime(int clock_id, struct timespec *pTS) {
int32_t code = 0;
#ifdef WINDOWS
LARGE_INTEGER t;
FILETIME f;
LARGE_INTEGER t;
FILETIME f;
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;

View File

@ -750,13 +750,14 @@ int32_t taosSetGlobalTimezone(const char *tz) {
int32_t code = TSDB_CODE_SUCCESS;
uDebug("[tz]set timezone to %s", tz)
#ifdef WINDOWS
char winStr[TD_TIMEZONE_LEN * 2] = {0};
char winStr[TD_TIMEZONE_LEN * 2] = {0};
for (size_t i = 0; i < W_TZ_CITY_NUM; i++) {
if (strcmp(tz_win[i][0], tz) == 0) {
char keyPath[256] = {0};
char keyValue[100] = {0};
DWORD keyValueSize = sizeof(keyValue);
snprintf(keyPath, sizeof(keyPath), "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", tz_win[i][1]);
snprintf(keyPath, sizeof(keyPath), "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s",
tz_win[i][1]);
RegGetValue(HKEY_LOCAL_MACHINE, keyPath, "Display", RRF_RT_ANY, NULL, (PVOID)&keyValue, &keyValueSize);
if (keyValueSize > 0) {
keyValue[4] = (keyValue[4] == '+' ? '-' : '+');
@ -770,7 +771,7 @@ int32_t taosSetGlobalTimezone(const char *tz) {
_putenv(winStr);
_tzset();
#else
code = setenv("TZ", tz, 1);
code = setenv("TZ", tz, 1);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
@ -779,7 +780,7 @@ int32_t taosSetGlobalTimezone(const char *tz) {
tzset();
#endif
time_t tx1 = taosGetTimestampSec();
time_t tx1 = taosGetTimestampSec();
return taosFormatTimezoneStr(tx1, tz, NULL, tsTimezoneStr);
}
@ -797,7 +798,7 @@ int32_t taosGetLocalTimezoneOffset() {
#endif
}
int32_t taosFormatTimezoneStr(time_t t, const char* tz, timezone_t sp, char *outTimezoneStr){
int32_t taosFormatTimezoneStr(time_t t, const char *tz, timezone_t sp, char *outTimezoneStr) {
struct tm tm1;
if (taosLocalTime(&t, &tm1, NULL, 0, sp) == NULL) {
uError("%s failed to get local time: code:%d", __FUNCTION__, errno);
@ -813,16 +814,17 @@ int32_t taosFormatTimezoneStr(time_t t, const char* tz, timezone_t sp, char *out
*/
char str1[TD_TIMEZONE_LEN] = {0};
if (taosStrfTime(str1, sizeof(str1), "%Z", &tm1) == 0){
if (taosStrfTime(str1, sizeof(str1), "%Z", &tm1) == 0) {
uError("failed to get timezone name");
return TSDB_CODE_TIME_ERROR;
}
char str2[TD_TIMEZONE_LEN] = {0};
if (taosStrfTime(str2, sizeof(str2), "%z", &tm1) == 0){
if (taosStrfTime(str2, sizeof(str2), "%z", &tm1) == 0) {
uError("failed to get timezone offset");
return TSDB_CODE_TIME_ERROR;
}
(void)snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %s)", tz, str1, str2);
uDebug("[tz] system timezone:%s", outTimezoneStr);
return 0;
@ -847,7 +849,6 @@ void getTimezoneStr(char *tz) {
goto END;
} while (0);
TdFilePtr pFile = taosOpenFile("/etc/timezone", TD_FILE_READ);
if (pFile == NULL) {
uWarn("[tz] failed to open /etc/timezone, reason:%s", strerror(errno));
@ -876,8 +877,8 @@ int32_t taosGetSystemTimezone(char *outTimezoneStr) {
char value[100] = {0};
char keyPath[100] = {0};
DWORD bufferSize = sizeof(value);
LONG result = RegGetValue(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", "TimeZoneKeyName",
RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize);
LONG result = RegGetValue(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation",
"TimeZoneKeyName", RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize);
if (result != ERROR_SUCCESS) {
return TAOS_SYSTEM_WINAPI_ERROR(result);
}
@ -891,9 +892,9 @@ int32_t taosGetSystemTimezone(char *outTimezoneStr) {
if (result != ERROR_SUCCESS) {
return TAOS_SYSTEM_WINAPI_ERROR(result);
}
if (bufferSize > 0) { // value like (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (UTC, %c%c%c%c%c)", outTimezoneStr,
value[4], value[5], value[6], value[8], value[9]);
if (bufferSize > 0) { // value like (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (UTC, %c%c%c%c%c)", outTimezoneStr, value[4], value[5],
value[6], value[8], value[9]);
}
break;
}
@ -903,7 +904,7 @@ int32_t taosGetSystemTimezone(char *outTimezoneStr) {
#else
char tz[TD_TIMEZONE_LEN] = {0};
getTimezoneStr(tz);
time_t tx1 = taosGetTimestampSec();
time_t tx1 = taosGetTimestampSec();
return taosFormatTimezoneStr(tx1, tz, NULL, outTimezoneStr);
#endif
}

View File

@ -14,20 +14,30 @@ ENDIF()
INCLUDE_DIRECTORIES(${TD_SOURCE_DIR}/src/util/inc)
# osTests
add_executable(osTests "osTests.cpp")
target_link_libraries(osTests os util gtest_main)
add_executable(osAtomicTests "osAtomicTests.cpp")
target_link_libraries(osAtomicTests os util gtest_main)
add_test(
NAME osTests
COMMAND osTests
NAME osAtomicTests
COMMAND osAtomicTests
)
add_executable(osSystemTests "osSystemTests.cpp")
target_link_libraries(osSystemTests os util gtest_main)
if(TD_LINUX)
add_executable(osDirTests "osDirTests.cpp")
target_link_libraries(osDirTests os util gtest_main)
add_test(
NAME osSystemTests
COMMAND osSystemTests
NAME osDirTests
COMMAND osDirTests
)
endif()
if(TD_LINUX)
add_executable(osEnvTests "osEnvTests.cpp")
target_link_libraries(osEnvTests os util gtest_main)
add_test(
NAME osEnvTests
COMMAND osEnvTests
)
endif()
add_executable(osMathTests "osMathTests.cpp")
target_link_libraries(osMathTests os util gtest_main)
@ -36,6 +46,13 @@ add_test(
COMMAND osMathTests
)
add_executable(osSemaphoreTests "osSemaphoreTests.cpp")
target_link_libraries(osSemaphoreTests os util gtest_main)
add_test(
NAME osSemaphoreTests
COMMAND osSemaphoreTests
)
add_executable(osSignalTests "osSignalTests.cpp")
target_link_libraries(osSignalTests os util gtest_main)
add_test(
@ -57,6 +74,20 @@ add_test(
COMMAND osStringTests
)
add_executable(osTests "osTests.cpp")
target_link_libraries(osTests os util gtest_main)
add_test(
NAME osTests
COMMAND osTests
)
add_executable(osSystemTests "osSystemTests.cpp")
target_link_libraries(osSystemTests os util gtest_main)
add_test(
NAME osSystemTests
COMMAND osSystemTests
)
add_executable(osThreadTests "osThreadTests.cpp")
target_link_libraries(osThreadTests os util gtest_main)
add_test(
@ -71,35 +102,3 @@ add_test(
COMMAND osTimeTests
)
if(TD_LINUX)
add_executable(osAtomicTests "osAtomicTests.cpp")
target_link_libraries(osAtomicTests os util gtest_main)
add_test(
NAME osAtomicTests
COMMAND osAtomicTests
)
add_executable(osDirTests "osDirTests.cpp")
target_link_libraries(osDirTests os util gtest_main)
add_test(
NAME osDirTests
COMMAND osDirTests
)
add_executable(osEnvTests "osEnvTests.cpp")
target_link_libraries(osEnvTests os util gtest_main)
add_test(
NAME osEnvTests
COMMAND osEnvTests
)
endif()
add_executable(osSemaphoreTests "osSemaphoreTests.cpp")
target_link_libraries(osSemaphoreTests os util gtest_main)
add_test(
NAME osSemaphoreTests
COMMAND osSemaphoreTests
)

View File

@ -33,7 +33,7 @@ TEST(osTimeTests, taosLocalTime) {
// Test 1: Test when both timep and result are not NULL
time_t timep = 1617531000; // 2021-04-04 18:10:00
struct tm result;
struct tm* local_time = taosLocalTime(&timep, &result, NULL, 0, NULL);
struct tm *local_time = taosLocalTime(&timep, &result, NULL, 0, NULL);
ASSERT_NE(local_time, nullptr);
ASSERT_EQ(local_time->tm_year, 121);
ASSERT_EQ(local_time->tm_mon, 3);
@ -92,4 +92,55 @@ TEST(osTimeTests, taosLocalTime) {
local_time = taosLocalTime(&neg_timep3, &result, NULL, 0, NULL);
ASSERT_EQ(local_time, nullptr);
#endif
}
TEST(osTimeTests, invalidParameter) {
void *retp = NULL;
int32_t reti = 0;
char buf[1024] = {0};
char fmt[1024] = {0};
struct tm tm = {0};
struct timeval tv = {0};
retp = taosStrpTime(buf, fmt, NULL);
EXPECT_EQ(retp, nullptr);
retp = taosStrpTime(NULL, fmt, &tm);
EXPECT_EQ(retp, nullptr);
retp = taosStrpTime(buf, NULL, &tm);
EXPECT_EQ(retp, nullptr);
reti = taosGetTimeOfDay(NULL);
EXPECT_NE(reti, 0);
reti = taosTime(NULL);
EXPECT_NE(reti, 0);
tm.tm_year = 2024;
tm.tm_mon = 10;
tm.tm_mday = 23;
tm.tm_hour = 12;
tm.tm_min = 1;
tm.tm_sec = 0;
tm.tm_isdst = -1;
time_t rett = taosMktime(&tm, NULL);
EXPECT_NE(rett, 0);
retp = taosLocalTime(NULL, &tm, NULL, 0, NULL);
EXPECT_EQ(retp, nullptr);
retp = taosLocalTime(&rett, NULL, NULL, 0, NULL);
EXPECT_EQ(retp, nullptr);
reti = taosSetGlobalTimezone(NULL);
EXPECT_NE(reti, 0);
}
TEST(osTimeTests, user_mktime64) {
int64_t reti = 0;
reti = user_mktime64(2024, 10, 23, 12, 3, 2, 1);
EXPECT_NE(reti, 0);
reti = user_mktime64(2024, 1, 23, 12, 3, 2, 1);
EXPECT_NE(reti, 0);
}