From 931a40985c730403ad8b77ff994f170ac07d4a5e Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 4 Dec 2024 10:53:33 +0800 Subject: [PATCH] feat:[TD-32642] add timezone support in windows --- contrib/CMakeLists.txt | 19 +++++++++++++------ include/os/osTime.h | 2 ++ source/common/src/tdatablock.c | 2 +- source/common/src/ttime.c | 4 ++-- source/libs/function/src/builtins.c | 2 +- source/libs/scalar/src/sclfunc.c | 2 +- source/os/CMakeLists.txt | 15 ++++++++++----- source/os/src/osTime.c | 6 ++++++ source/os/src/osTimezone.c | 4 ++-- source/util/src/tlog.c | 2 +- utils/test/c/tmqSim.c | 2 +- utils/tsim/src/simExe.c | 2 +- 12 files changed, 41 insertions(+), 21 deletions(-) diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index af05b2c47c..ef7acb1f34 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -665,16 +665,23 @@ MESSAGE(STATUS "timezone file path: " ${TZ_OUTPUT_PATH}) if(NOT ${TD_WINDOWS}) execute_process( - COMMAND make CFLAGS+=-fPIC TZDIR=${TZ_OUTPUT_PATH}/ clean libtz.a + COMMAND make TZDIR=${TZ_OUTPUT_PATH}/ tzdir.h WORKING_DIRECTORY "${TD_CONTRIB_DIR}/tz" ) -# set(TZ_SRC_DIR "${TD_SOURCE_DIR}/source/os/src/timezone") -# file(MAKE_DIRECTORY ${TZ_SRC_DIR}) -# file(COPY ${TD_CONTRIB_DIR}/tz/private.h ${TD_CONTRIB_DIR}/tz/tzdir.h ${TD_CONTRIB_DIR}/tz/tzfile.h -# ${TD_CONTRIB_DIR}/tz/localtime.c ${TD_CONTRIB_DIR}/tz/strftime.c -# DESTINATION ${TZ_SRC_DIR}) + set(TZ_SRC_DIR "${TD_SOURCE_DIR}/source/os/src/timezone") + file(MAKE_DIRECTORY ${TZ_SRC_DIR}) + file(COPY ${TD_CONTRIB_DIR}/tz/private.h ${TD_CONTRIB_DIR}/tz/tzdir.h ${TD_CONTRIB_DIR}/tz/tzfile.h + ${TD_CONTRIB_DIR}/tz/localtime.c ${TD_CONTRIB_DIR}/tz/strftime.c + DESTINATION ${TZ_SRC_DIR}) endif(NOT ${TD_WINDOWS}) + +#if(NOT ${TD_WINDOWS}) +# execute_process( +# COMMAND make CFLAGS+=-fPIC CFLAGS+=-g TZDIR=${TZ_OUTPUT_PATH} clean libtz.a +# WORKING_DIRECTORY "${TD_CONTRIB_DIR}/tz" +# ) +#endif(NOT ${TD_WINDOWS}) # ================================================================================================ # Build test # ================================================================================================ diff --git a/include/os/osTime.h b/include/os/osTime.h index 723574a91c..69ae8f5e2e 100644 --- a/include/os/osTime.h +++ b/include/os/osTime.h @@ -24,6 +24,7 @@ extern "C" { // When you want to use this feature, you should find or add the same function in the following section. #ifndef ALLOW_FORBID_FUNC #define strptime STRPTIME_FUNC_TAOS_FORBID +#define strftime STRFTIME_FUNC_TAOS_FORBID #define gettimeofday GETTIMEOFDAY_FUNC_TAOS_FORBID #define localtime LOCALTIME_FUNC_TAOS_FORBID #define localtime_s LOCALTIMES_FUNC_TAOS_FORBID @@ -92,6 +93,7 @@ static FORCE_INLINE int64_t taosGetMonoTimestampMs() { } char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm); +size_t taosStrfTime(char *s, size_t maxsize, char const *format, struct tm const *t); struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf, int32_t bufSize, timezone_t tz); struct tm *taosGmTimeR(const time_t *timep, struct tm *result); time_t taosTimeGm(struct tm *tmp); diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 3b3c4b72d9..bd18c9ceb9 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -2499,7 +2499,7 @@ static int32_t formatTimestamp(char* buf, size_t cap, int64_t val, int precision TSDB_CHECK_CODE(code, lino, _end); } - size_t pos = strftime(buf, cap, "%Y-%m-%d %H:%M:%S", &ptm); + size_t pos = taosStrfTime(buf, cap, "%Y-%m-%d %H:%M:%S", &ptm); if (pos == 0) { code = TSDB_CODE_OUT_OF_BUFFER; TSDB_CHECK_CODE(code, lino, _end); diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index db3f7fec32..dd9a3b1b16 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -923,9 +923,9 @@ int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precisio if (NULL == taosLocalTime(", &ptm, buf, bufLen, NULL)) { TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); } - int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", &ptm); + int32_t length = (int32_t)taosStrfTime(ts, 40, "%Y-%m-%dT%H:%M:%S", &ptm); length += tsnprintf(ts + length, fractionLen, format, mod); - length += (int32_t)strftime(ts + length, 40 - length, "%z", &ptm); + length += (int32_t)taosStrfTime(ts + length, 40 - length, "%z", &ptm); tstrncpy(buf, ts, bufLen); TAOS_RETURN(TSDB_CODE_SUCCESS); diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index 4e54c12e40..f71ba0187f 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -195,7 +195,7 @@ static int32_t addTimezoneParam(SNodeList* pList, timezone_t tz) { } struct tm tmInfo; if (taosLocalTime(&t, &tmInfo, buf, sizeof(buf), tz) != NULL) { - (void)strftime(buf, sizeof(buf), "%z", &tmInfo); + (void)taosStrfTime(buf, sizeof(buf), "%z", &tmInfo); } int32_t len = (int32_t)strlen(buf); diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index efec336589..3fac8d2718 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -2249,7 +2249,7 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam * goto _end; } - int32_t len = (int32_t)strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tmInfo); + int32_t len = (int32_t)taosStrfTime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tmInfo); len += tsnprintf(buf + len, fractionLen, format, mod); diff --git a/source/os/CMakeLists.txt b/source/os/CMakeLists.txt index a0f9e5b87a..4602f291f3 100644 --- a/source/os/CMakeLists.txt +++ b/source/os/CMakeLists.txt @@ -1,5 +1,10 @@ aux_source_directory(src OS_SRC) -add_library(os STATIC ${OS_SRC}) +if(NOT ${TD_WINDOWS}) + aux_source_directory(src/timezone OS_TZ) + add_library(os STATIC ${OS_SRC} ${OS_TZ}) +else() + add_library(os STATIC ${OS_SRC}) +endif(NOT ${TD_WINDOWS}) target_include_directories( os PUBLIC "${TD_SOURCE_DIR}/include/os" @@ -70,10 +75,10 @@ IF (JEMALLOC_ENABLED) target_link_libraries(os PUBLIC -L${CMAKE_BINARY_DIR}/build/lib -ljemalloc) ENDIF () -if(NOT ${TD_WINDOWS}) - find_library(tz libtz.a "${TD_SOURCE_DIR}/contrib/tz") - target_link_libraries(os PUBLIC ${tz}) -endif(NOT ${TD_WINDOWS}) +#if(NOT ${TD_WINDOWS}) +# find_library(tz libtz.a "${TD_SOURCE_DIR}/contrib/tz") +# target_link_libraries(os PUBLIC ${tz}) +#endif(NOT ${TD_WINDOWS}) if(${BUILD_TEST}) add_subdirectory(test) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 2720eab56e..b39624c44c 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -345,6 +345,12 @@ 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){ + if (!s || !format || !t) return 0; + return strftime(s, maxsize, format, t); +} + int32_t taosGetTimeOfDay(struct timeval *tv) { if (tv == NULL) { return TSDB_CODE_INVALID_PARA; diff --git a/source/os/src/osTimezone.c b/source/os/src/osTimezone.c index d47c0ac663..395c1cbb82 100644 --- a/source/os/src/osTimezone.c +++ b/source/os/src/osTimezone.c @@ -813,13 +813,13 @@ int32_t taosFormatTimezoneStr(time_t t, const char* tz, timezone_t sp, char *out */ char str1[TD_TIMEZONE_LEN] = {0}; - if (strftime(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 (strftime(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; } diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 31054d40ed..73771aee09 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -163,7 +163,7 @@ static int32_t getDay(char *buf, int32_t bufSize) { } struct tm tmInfo; if (taosLocalTime(&t, &tmInfo, buf, bufSize, NULL) != NULL) { - TAOS_UNUSED(strftime(buf, bufSize, "%Y-%m-%d", &tmInfo)); + TAOS_UNUSED(taosStrfTime(buf, bufSize, "%Y-%m-%d", &tmInfo)); } return 0; } diff --git a/utils/test/c/tmqSim.c b/utils/test/c/tmqSim.c index bb0d813ace..1ed9af00f2 100644 --- a/utils/test/c/tmqSim.c +++ b/utils/test/c/tmqSim.c @@ -475,7 +475,7 @@ static char* shellFormatTimestamp(char* buf, int32_t bufSize, int64_t val, int32 if (taosLocalTime(&tt, &ptm, buf, bufSize, NULL) == NULL) { return buf; } - size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); + size_t pos = taosStrfTime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); if (precision == TSDB_TIME_PRECISION_NANO) { sprintf(buf + pos, ".%09d", ms); diff --git a/utils/tsim/src/simExe.c b/utils/tsim/src/simExe.c index 2d269bdea2..a9772addbb 100644 --- a/utils/tsim/src/simExe.c +++ b/utils/tsim/src/simExe.c @@ -800,7 +800,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { if (taosLocalTime(&tt, &tp, timeStr, sizeof(timeStr), NULL) == NULL) { break; } - strftime(timeStr, 64, "%y-%m-%d %H:%M:%S", &tp); + taosStrfTime(timeStr, 64, "%y-%m-%d %H:%M:%S", &tp); if (precision == TSDB_TIME_PRECISION_MILLI) { sprintf(value, "%s.%03d", timeStr, (int32_t)(*((int64_t *)row[i]) % 1000)); } else if (precision == TSDB_TIME_PRECISION_MICRO) {