Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:[TD-32642] add timezone logic #28933

Open
wants to merge 7 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmake/cmake.define
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ ELSE()
SET(TAOS_LIB_PLATFORM_SPEC taos)
ENDIF()

IF(NOT DEFINED TZ_OUTPUT_PATH)
SET(TZ_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/share/timezone)
ENDIF()
MESSAGE(STATUS "timezone output path: " ${TZ_OUTPUT_PATH})


# build TSZ by default
IF("${TSZ_ENABLED}" MATCHES "false")
set(VAR_TSZ "" CACHE INTERNAL "global variant empty")
Expand Down
15 changes: 15 additions & 0 deletions cmake/tz_CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# timezone
ExternalProject_Add(tz
GIT_REPOSITORY https://github.com/eggert/tz.git
GIT_TAG main
SOURCE_DIR "${TD_CONTRIB_DIR}/tz"
BINARY_DIR ""
CONFIGURE_COMMAND ""
#BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
GIT_SHALLOW true
GIT_PROGRESS true
BUILD_COMMAND ""
)

12 changes: 12 additions & 0 deletions contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ cat("${TD_SUPPORT_DIR}/zlib_CMakeLists.txt.in" ${CONTRIB_TMP_FILE})
# cJson
cat("${TD_SUPPORT_DIR}/cjson_CMakeLists.txt.in" ${CONTRIB_TMP_FILE})

cat("${TD_SUPPORT_DIR}/tz_CMakeLists.txt.in" ${CONTRIB_TMP_FILE})

# xz
# cat("${TD_SUPPORT_DIR}/xz_CMakeLists.txt.in" ${CONTRIB_TMP_FILE})

Expand Down Expand Up @@ -651,6 +653,16 @@ if(${TD_LINUX} AND ${BUILD_WITH_S3})
add_subdirectory(azure-cmake EXCLUDE_FROM_ALL)
endif()

execute_process(
COMMAND make TZDIR=${TZ_OUTPUT_PATH}/ clean all posix_only
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})
# ================================================================================================
# Build test
# ================================================================================================
Expand Down
17 changes: 17 additions & 0 deletions include/client/taos.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ typedef enum {
TSDB_MAX_OPTIONS
} TSDB_OPTION;

typedef enum {
TSDB_OPTION_CONNECTION_CHARSET, // charset, Same as the scope supported by the system
TSDB_OPTION_CONNECTION_TIMEZONE, // timezone, Same as the scope supported by the system
TSDB_OPTION_CONNECTION_USER_IP, // user ip
TSDB_OPTION_CONNECTION_USER_APP, // user app
TSDB_MAX_CONNECTION_OPTIONS = 100
} TSDB_OPTION_CONNECTION;

typedef enum {
TSDB_OPTION_CONNECT_CHARSET,
TSDB_OPTION_CONNECT_TIMEZONE,
TSDB_OPTION_CONNECT_IP,
TSDB_OPTION_CONNECT_APP_NAME,
TSDB_MAX_CONNECT_OPTIONS
} TSDB_OPTION_CONNECT;

typedef enum {
TSDB_SML_UNKNOWN_PROTOCOL = 0,
TSDB_SML_LINE_PROTOCOL = 1,
Expand Down Expand Up @@ -174,6 +190,7 @@ typedef struct TAOS_STMT_OPTIONS {

DLL_EXPORT void taos_cleanup(void);
DLL_EXPORT int taos_options(TSDB_OPTION option, const void *arg, ...);
DLL_EXPORT int taos_options_connection(TAOS *taos, TSDB_OPTION_CONNECTION option, const void *arg, ...);
DLL_EXPORT setConfRet taos_set_config(const char *config);
DLL_EXPORT int taos_init(void);
DLL_EXPORT TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port);
Expand Down
1 change: 1 addition & 0 deletions include/common/tmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -3420,6 +3420,7 @@ typedef struct {
SAppHbReq app;
SQueryHbReqBasic* query;
SHashObj* info; // hash<Skv.key, Skv>
char name[TSDB_APP_NAME_LEN];
} SClientHbReq;

typedef struct {
Expand Down
28 changes: 7 additions & 21 deletions include/common/ttime.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,7 @@ static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) {
* precision == TSDB_TIME_PRECISION_MILLI, it returns timestamp in millisecond.
* precision == TSDB_TIME_PRECISION_NANO, it returns timestamp in nanosecond.
*/
static FORCE_INLINE int64_t taosGetTimestampToday(int32_t precision) {
int64_t factor = (precision == TSDB_TIME_PRECISION_MILLI) ? 1000
: (precision == TSDB_TIME_PRECISION_MICRO) ? 1000000
: 1000000000;
time_t t;
(void) taosTime(&t);
struct tm tm;
(void) taosLocalTime(&t, &tm, NULL, 0);
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;

return (int64_t)taosMktime(&tm) * factor;
}
int64_t taosGetTimestampToday(int32_t precision, timezone_t tz);

int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision);

Expand All @@ -82,13 +69,12 @@ int32_t taosTimeCountIntervalForFill(int64_t skey, int64_t ekey, int64_t interva
int32_t parseAbsoluteDuration(const char* token, int32_t tokenlen, int64_t* ts, char* unit, int32_t timePrecision);
int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision, bool negativeAllow);

int32_t taosParseTime(const char* timestr, int64_t* pTime, int32_t len, int32_t timePrec, int8_t dayligth);
void deltaToUtcInitOnce();
int32_t taosParseTime(const char* timestr, int64_t* pTime, int32_t len, int32_t timePrec, timezone_t tz);
char getPrecisionUnit(int32_t precision);

int64_t convertTimePrecision(int64_t ts, int32_t fromPrecision, int32_t toPrecision);
int32_t convertTimeFromPrecisionToUnit(int64_t time, int32_t fromPrecision, char toUnit, int64_t* pRes);
int32_t convertStringToTimestamp(int16_t type, char* inputData, int64_t timePrec, int64_t* timeVal);
int32_t convertStringToTimestamp(int16_t type, char* inputData, int64_t timePrec, int64_t* timeVal, timezone_t tz);
int32_t getDuration(int64_t val, char unit, int64_t* result, int32_t timePrecision);

int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t ts, int32_t precision);
Expand All @@ -98,8 +84,8 @@ struct STm {
int64_t fsec; // in NANOSECOND
};

int32_t taosTs2Tm(int64_t ts, int32_t precision, struct STm* tm);
int32_t taosTm2Ts(struct STm* tm, int64_t* ts, int32_t precision);
int32_t taosTs2Tm(int64_t ts, int32_t precision, struct STm* tm, timezone_t tz);
int32_t taosTm2Ts(struct STm* tm, int64_t* ts, int32_t precision, timezone_t tz);

/// @brief convert a timestamp to a formatted string
/// @param format the timestamp format, must null terminated
Expand All @@ -108,15 +94,15 @@ int32_t taosTm2Ts(struct STm* tm, int64_t* ts, int32_t precision);
/// formats array; If not NULL, [formats] will be used instead of [format] to skip parse formats again.
/// @param out output buffer, should be initialized by memset
/// @notes remember to free the generated formats
int32_t taosTs2Char(const char* format, SArray** formats, int64_t ts, int32_t precision, char* out, int32_t outLen);
int32_t taosTs2Char(const char* format, SArray** formats, int64_t ts, int32_t precision, char* out, int32_t outLen, timezone_t tz);
/// @brief convert a formatted timestamp string to a timestamp
/// @param format must null terminated
/// @param [in, out] formats, see taosTs2Char
/// @param tsStr must null terminated
/// @retval 0 for success, otherwise error occured
/// @notes remember to free the generated formats even when error occured
int32_t taosChar2Ts(const char* format, SArray** formats, const char* tsStr, int64_t* ts, int32_t precision, char* errMsg,
int32_t errMsgLen);
int32_t errMsgLen, timezone_t tz);

int32_t TEST_ts2char(const char* format, int64_t ts, int32_t precision, char* out, int32_t outLen);
int32_t TEST_char2ts(const char* format, int64_t* ts, int32_t precision, const char* tsStr);
Expand Down
1 change: 1 addition & 0 deletions include/libs/function/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ struct SScalarParam {
void *param; // other parameter, such as meta handle from vnode, to extract table name/tag value
int32_t numOfRows;
int32_t numOfQualified; // number of qualified elements in the final results
timezone_t tz;
};

#define cleanupResultRowEntry(p) p->initialized = false
Expand Down
9 changes: 7 additions & 2 deletions include/libs/nodes/querynodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ typedef struct SValueNode {
double d;
char* p;
} datum;
int64_t typeData;
int8_t unit;
int64_t typeData;
int8_t unit;
timezone_t tz;
} SValueNode;

typedef struct SLeftValueNode {
Expand Down Expand Up @@ -159,6 +160,7 @@ typedef struct SOperatorNode {
EOperatorType opType;
SNode* pLeft;
SNode* pRight;
timezone_t tz;
} SOperatorNode;

typedef struct SLogicConditionNode {
Expand Down Expand Up @@ -191,6 +193,8 @@ typedef struct SFunctionNode {
int32_t originalFuncId;
ETrimType trimType;
bool dual; // whether select stmt without from stmt, true for without.
// char timezone[TD_TIMEZONE_LEN];
timezone_t tz;
} SFunctionNode;

typedef struct STableNode {
Expand Down Expand Up @@ -399,6 +403,7 @@ typedef struct SCaseWhenNode {
SNode* pCase;
SNode* pElse;
SNodeList* pWhenThenList;
timezone_t tz;
} SCaseWhenNode;

typedef struct SWindowOffsetNode {
Expand Down
1 change: 1 addition & 0 deletions include/libs/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ typedef struct SParseContext {
int8_t biMode;
SArray* pSubMetaList;
setQueryFn setQueryFp;
timezone_t timezone;
} SParseContext;

int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery);
Expand Down
5 changes: 2 additions & 3 deletions include/os/osSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#define epoll_create EPOLL_CREATE_FUNC_TAOS_FORBID
#define epoll_ctl EPOLL_CTL_FUNC_TAOS_FORBID
#define epoll_wait EPOLL_WAIT_FUNC_TAOS_FORBID
#define inet_addr INET_ADDR_FUNC_TAOS_FORBID
#define inet_ntoa INET_NTOA_FUNC_TAOS_FORBID
#endif

Expand Down Expand Up @@ -55,10 +54,11 @@
#define __BIG_ENDIAN BIG_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __PDP_ENDIAN PDP_ENDIAN

#include <winsock2.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#if defined(_TD_DARWIN_64)
#include <osEok.h>
Expand Down Expand Up @@ -163,7 +163,6 @@ int32_t taosBlockSIGPIPE();
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t *ip);
int32_t taosGetFqdn(char *);
void tinet_ntoa(char *ipstr, uint32_t ip);
uint32_t ip2uint(const char *const ip_addr);
int32_t taosIgnSIGPIPE();
const char *taosInetNtoa(struct in_addr ipInt, char *dstStr, int32_t len);

Expand Down
10 changes: 7 additions & 3 deletions include/os/osTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern "C" {
#define MILLISECOND_PER_SECOND ((int64_t)1000LL)
#endif

#include "osTimezone.h"
#define MILLISECOND_PER_MINUTE (MILLISECOND_PER_SECOND * 60)
#define MILLISECOND_PER_HOUR (MILLISECOND_PER_MINUTE * 60)
#define MILLISECOND_PER_DAY (MILLISECOND_PER_HOUR * 24)
Expand Down Expand Up @@ -91,13 +92,16 @@ static FORCE_INLINE int64_t taosGetMonoTimestampMs() {
}

char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm);
struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf, int32_t bufSize);
struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst);
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);
int32_t taosTime(time_t *t);
time_t taosMktime(struct tm *timep);
time_t taosMktime(struct tm *timep, timezone_t tz);
int64_t user_mktime64(const uint32_t year, const uint32_t mon, const uint32_t day, const uint32_t hour,
const uint32_t min, const uint32_t sec, int64_t time_zone);

//struct tm *taosLocalTimeRz(timezone_t state, const time_t *timep, struct tm *result);
//time_t taosMktimeRz(timezone_t state, struct tm *timep);
#ifdef __cplusplus
}
#endif
Expand Down
18 changes: 10 additions & 8 deletions include/os/osTimezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
extern "C" {
#endif

// If the error is in a third-party library, place this header file under the third-party library header file.
// When you want to use this feature, you should find or add the same function in the following section.
#ifndef ALLOW_FORBID_FUNC
#define tzset TZSET_FUNC_TAOS_FORBID
#endif

enum TdTimezone {
TdWestZone12 = -12,
TdWestZone11,
Expand Down Expand Up @@ -54,9 +48,17 @@ enum TdTimezone {
TdEastZone12
};

int32_t taosGetSystemTimezone(char *outTimezone, enum TdTimezone *tsTimezone);
int32_t taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight, enum TdTimezone *tsTimezone);
typedef struct state *timezone_t;
struct tm *localtime_rz(timezone_t , time_t const *, struct tm *);
time_t mktime_z(timezone_t, struct tm *);
timezone_t tzalloc(char const *);
void tzfree(timezone_t);

void getTimezoneStr(char *tz);
int32_t taosGetSystemTimezone(char *outTimezone);
int32_t taosSetGlobalTimezone(const char *tz);
int32_t taosFormatTimezoneStr(time_t t, const char* tzStr, timezone_t sp, char *outTimezoneStr);
int32_t taosIsValidateTimezone(const char *tz);
#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions include/util/taoserror.h
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,9 @@ int32_t taosGetErrSize();
#define TSDB_CODE_AUDIT_FAIL_SEND_AUDIT_RECORD TAOS_DEF_ERROR_CODE(0, 0x6101)
#define TSDB_CODE_AUDIT_FAIL_GENERATE_JSON TAOS_DEF_ERROR_CODE(0, 0x6102)

//TIMEZONE
#define TSDB_CODE_INVALID_TIMEZONE TAOS_DEF_ERROR_CODE(0, 0x6200)

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion include/util/tconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ typedef enum {
CFG_STYPE_APOLLO_URL,
CFG_STYPE_ARG_LIST,
CFG_STYPE_TAOS_OPTIONS,
CFG_STYPE_ALTER_CMD,
CFG_STYPE_ALTER_CLIENT_CMD,
CFG_STYPE_ALTER_SERVER_CMD,
} ECfgSrcType;

typedef enum {
Expand Down
5 changes: 0 additions & 5 deletions include/util/tutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ int32_t taosHexStrToByteArray(char hexstr[], char bytes[]);
int32_t tintToHex(uint64_t val, char hex[]);
int32_t titoa(uint64_t val, size_t radix, char str[]);

char *taosIpStr(uint32_t ipInt);
uint32_t ip2uint(const char *const ip_addr);
void taosIp2String(uint32_t ip, char *str);
void taosIpPort2String(uint32_t ip, uint16_t port, char *str);

void *tmemmem(const char *haystack, int hlen, const char *needle, int nlen);

int32_t parseCfgReal(const char *str, float *out);
Expand Down
4 changes: 4 additions & 0 deletions packaging/tools/make_install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ if not exist %target_dir%\\cfg (
if not exist %target_dir%\\include (
mkdir %target_dir%\\include
)
if not exist %target_dir%\\share (
mkdir %target_dir%\\share
)
if not exist %target_dir%\\driver (
mkdir %target_dir%\\driver
)
Expand All @@ -67,6 +70,7 @@ copy %binary_dir%\\build\\lib\\taos.lib %target_dir%\\driver > nul
copy %binary_dir%\\build\\lib\\taos_static.lib %target_dir%\\driver > nul
copy %binary_dir%\\build\\lib\\taos.dll %target_dir%\\driver > nul
copy %binary_dir%\\build\\bin\\taos.exe %target_dir% > nul
xcopy %binary_dir%\\build\\share\\* %target_dir%\\share > nul
if exist %binary_dir%\\build\\bin\\taosBenchmark.exe (
copy %binary_dir%\\build\\bin\\taosBenchmark.exe %target_dir% > nul
)
Expand Down
9 changes: 9 additions & 0 deletions packaging/tools/make_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ else
installDir="/usr/local/taos"
fi
fi

timezone_dir="/usr/local/share/timezone"
install_main_dir=${installDir}
bin_dir="${installDir}/bin"
cfg_dir="${installDir}/cfg"
Expand Down Expand Up @@ -385,6 +387,11 @@ function install_header() {
${csudo}chmod 644 ${install_main_dir}/include/*
}

function install_timezone(){
${csudo}mkdir -p ${timezone_dir}
${csudo}cp -rf ${binary_dir}/build/share/timezone/* ${timezone_dir} && ${csudo}chmod 644 ${timezone_dir}/*
}

function install_config() {
if [ ! -f ${cfg_install_dir}/${configFile} ]; then
${csudo}mkdir -p ${cfg_install_dir}
Expand Down Expand Up @@ -641,6 +648,7 @@ function update_TDengine() {
install_log
install_header
install_lib
install_timezone
# install_connector
install_examples
install_bin
Expand Down Expand Up @@ -706,6 +714,7 @@ function install_TDengine() {
install_log
install_header
install_lib
install_timezone
# install_connector
install_examples
install_bin
Expand Down
Loading
Loading