feat:[TD-32642] add timezone for connection support

This commit is contained in:
wangmm0220 2024-11-29 00:32:29 +08:00
parent ea9c5d3ce6
commit 5ead3c68b1
3 changed files with 55 additions and 21 deletions

View File

@ -177,9 +177,41 @@ TEST(timezoneCase, alter_timezone_Test) {
execQuery(pConn, "alter local 'timezone Asia/Kolkata'");
check_timezone(pConn, "show local variables", "Asia/Kolkata");
execQuery(pConn, "alter local 'timezone Asia/Shanghai'");
check_timezone(pConn, "show local variables", "Asia/Shanghai");
execQueryFail(pConn, "alter dnode 1 'timezone Asia/Kolkata'");
}
char *tz_test[] = {
"2023-09-16 17:00:00+",
"2023-09-16 17:00:00+8:",
"2023-09-16 17:00:00+:30",
"2023-09-16 17:00:00+:-30",
"2023-09-16 17:00:00++:-30",
"2023-09-16 17:00:00+:",
"2023-09-16 17:00:00+8f:",
"2023-09-16 17:00:00+080:",
"2023-09-16 17:00:00+080",
"2023-09-16 17:00:00+a",
"2023-09-16 17:00:00+09:",
"2023-09-16 17:00:00+09:a",
"2023-09-16 17:00:00+09:abc",
"2023-09-16 17:00:00+09:001",
};
void do_insert_failed(){
TAOS* pConn = getConnWithGlobalOption("UTC-8");
for (unsigned int i = 0; i < sizeof (tz_test) / sizeof (tz_test[0]); ++i){
char sql[1024] = {0};
(void)snprintf(sql, sizeof(sql), "insert into db1.ctb1 values('%s', '%s', 1)", tz_test[i], tz_test[i]);
execQueryFail(pConn, sql);
}
taos_close(pConn);
}
struct insert_params
{
const char *tz;
@ -227,7 +259,7 @@ TEST(timezoneCase, insert_with_timezone_Test) {
execQuery(pConn1, "create table db1.ctb1 using db1.stb tags(\"2023-09-16 17:00:00+05:00\", \"2023-09-16 17:00:00\", 1)");
execQuery(pConn1, "create table db1.ctb2 using db1.stb tags(1732178775000, 1732178775000, 1)");
execQuery(pConn1, "insert into db1.ntb values(1732178775133, 1732178775133, 1)");
execQuery(pConn1, "insert into db1.ctb1 values(1732178775133, 1732178775133, 1)");
execQuery(pConn1, "insert into db1.ctb1 values(1732178775133, 1732178775133, 1)"); //2024-11-21 10:46:15.133+02:00
execQuery(pConn1, "insert into db1.ctb2 values(1732178775133, 1732178775133, 1)");
/*
@ -251,6 +283,7 @@ TEST(timezoneCase, insert_with_timezone_Test) {
do_select(params2[i]);
}
do_insert_failed();
/*
* 4. test NULL timezone, use default timezone UTC-8
*/
@ -284,7 +317,7 @@ TEST(timezoneCase, func_timezone_Test) {
execQuery(pConn, "insert into db1.ntb values(1704142800000, '2024-01-01 23:00:00', 1)"); // 2024-01-01 23:00:00+0200
// test timezone
check_sql_result(pConn, "select timezone()", "UTC-test (UTC, +0200)");
check_sql_result(pConn, "select timezone()", "UTC-2 (UTC, +0200)");
// test timetruncate
check_sql_result(pConn, "select TO_ISO8601(TIMETRUNCATE('2024-01-01 23:00:00', 1d, 0))", "2024-01-01T02:00:00.000+0200");

View File

@ -130,8 +130,15 @@ int32_t parseFraction(char* str, char** end, int32_t timePrec, int64_t* pFractio
TAOS_RETURN(TSDB_CODE_SUCCESS);
}
#define PARSE(str,len,result) \
if (len > 2 || len < 1) {\
TAOS_RETURN(TSDB_CODE_INVALID_PARA);\
}\
result = strnatoi(str, len);
int32_t parseTimezone(char* str, int64_t* tzOffset) {
int64_t hour = 0;
int64_t minute = 0;
int32_t i = 0;
if (str[i] != '+' && str[i] != '-') {
@ -152,30 +159,24 @@ int32_t parseTimezone(char* str, int64_t* tzOffset) {
char* sep = strchr(&str[i], ':');
if (sep != NULL) {
int32_t len = (int32_t)(sep - &str[i]);
int32_t hourSize = (int32_t)(sep - &str[i]);
PARSE(&str[i], hourSize, hour);
hour = strnatoi(&str[i], len);
i += len + 1;
i += hourSize + 1;
size_t minSize = strlen(&str[i]);
PARSE(&str[i], minSize, minute);
if (minute > 59 || minute < 0) {
TAOS_RETURN(TSDB_CODE_INVALID_PARA);
}
} else {
hour = strnatoi(&str[i], 2);
i += 2;
size_t hourSize = strlen(&str[i]);
PARSE(&str[i], hourSize, hour)
}
if (hour > 13 || hour < 0) {
TAOS_RETURN(TSDB_CODE_INVALID_PARA);
}
// return error if there're illegal charaters after min(2 Digits)
char* minStr = &str[i];
if (minStr[1] != '\0' && minStr[2] != '\0') {
TAOS_RETURN(TSDB_CODE_INVALID_PARA);
}
int64_t minute = strnatoi(&str[i], 2);
if (minute > 59 || minute < 0) {
TAOS_RETURN(TSDB_CODE_INVALID_PARA);
}
if (str[0] == '+') {
*tzOffset = -(hour * 3600 + minute * 60);
} else {

View File

@ -238,16 +238,16 @@ iconv_t taosAcquireConv(int32_t *idx, ConvType type, void* charsetCxt) {
charsetCxt = tsCharsetCxt;
}
SConvInfo *info = (SConvInfo *)charsetCxt;
if (info->gConvMaxNum[type] <= 0) {
if (info == NULL) {
*idx = -1;
if (type == M2C) {
iconv_t c = iconv_open(DEFAULT_UNICODE_ENCODEC, info->charset);
iconv_t c = iconv_open(DEFAULT_UNICODE_ENCODEC, "UTF-8");
if ((iconv_t)-1 == c) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return c;
} else {
iconv_t c = iconv_open(info->charset, DEFAULT_UNICODE_ENCODEC);
iconv_t c = iconv_open("UTF-8", DEFAULT_UNICODE_ENCODEC);
if ((iconv_t)-1 == c) {
terrno = TAOS_SYSTEM_ERROR(errno);
}