diff --git a/include/common/ttime.h b/include/common/ttime.h index 306b5105d0..1dfa609064 100644 --- a/include/common/ttime.h +++ b/include/common/ttime.h @@ -118,6 +118,13 @@ int32_t taosChar2Ts(const char* format, SArray** formats, const char* tsStr, int void 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); +/// @brief get offset seconds from zero timezone to input timezone +/// for +XX timezone, the offset to zero is negative value +/// @param tzStr timezonestr, eg: +0800, -0830, -08 +/// @param offset seconds, eg: +08 offset -28800, -01 offset 3600 +/// @return 0 success, other fail +int32_t offsetOfTimezone(char* tzStr, int64_t* offset); + #ifdef __cplusplus } #endif diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index a701c88a24..227de7f5fc 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -194,6 +194,14 @@ int32_t parseTimezone(char* str, int64_t* tzOffset) { return 0; } +int32_t offsetOfTimezone(char* tzStr, int64_t* offset) { + if (tzStr && (tzStr[0] == 'z' || tzStr[0] == 'Z')) { + *offset = 0; + return 0; + } + return parseTimezone(tzStr, offset); +} + /* * rfc3339 format: * 2013-04-12T15:52:01+08:00 diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index e7c6297f44..6144ebd340 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -1083,6 +1083,15 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam * memmove(fraction, fraction + TSDB_TIME_PRECISION_SEC_DIGITS, TSDB_TIME_PRECISION_SEC_DIGITS); } + // trans current timezone's unix ts to dest timezone + // offset = delta from dest timezone to zero + // delta from zero to current timezone = 3600 * (cur)tsTimezone + int64_t offset = 0; + if (0 != offsetOfTimezone(tz, &offset)) { + goto _end; + } + timeVal -= offset + 3600 * ((int64_t)tsTimezone); + struct tm tmInfo; int32_t len = 0; diff --git a/tests/system-test/2-query/To_iso8601.py b/tests/system-test/2-query/To_iso8601.py index 92aacbb350..160473ffce 100644 --- a/tests/system-test/2-query/To_iso8601.py +++ b/tests/system-test/2-query/To_iso8601.py @@ -30,17 +30,16 @@ class TDTestCase: tdSql.query(f'select to_iso8601(ts) from {self.ntbname}') for i in range(self.rowNum): tdSql.checkEqual(tdSql.queryResult[i][0],f'2022-01-01T00:00:00.00{i}{time_zone}') - timezone_list = ['+0000','+0100','+0200','+0300','+0330','+0400','+0500','+0530','+0600','+0700','+0800','+0900','+1000','+1100','+1200',\ - '+00','+01','+02','+03','+04','+05','+06','+07','+08','+09','+10','+11','+12',\ - '+00:00','+01:00','+02:00','+03:00','+03:30','+04:00','+05:00','+05:30','+06:00','+07:00','+08:00','+09:00','+10:00','+11:00','+12:00',\ - '-0000','-0100','-0200','-0300','-0400','-0500','-0600','-0700','-0800','-0900','-1000','-1100','-1200',\ - '-00','-01','-02','-03','-04','-05','-06','-07','-08','-09','-10','-11','-12',\ - '-00:00','-01:00','-02:00','-03:00','-04:00','-05:00','-06:00','-07:00','-08:00','-09:00','-10:00','-11:00','-12:00',\ - 'z','Z'] - for j in timezone_list: - tdSql.query(f'select to_iso8601(ts,"{j}") from {self.ntbname}') - for i in range(self.rowNum): - tdSql.checkEqual(tdSql.queryResult[i][0],f'2022-01-01T00:00:00.00{i}{j}') + + tz_list = ['+0000','+0530', '+00', '+06', '+00:00', '+12:00', '-0000', '-0900', '-00', '-05', '-00:00', '-03:00','z', 'Z'] + res_list = ['2021-12-31T16:00:00.000+0000', '2021-12-31T21:30:00.000+0530', '2021-12-31T16:00:00.000+00', '2021-12-31T22:00:00.000+06',\ + '2021-12-31T16:00:00.000+00:00', '2022-01-01T04:00:00.000+12:00','2021-12-31T16:00:00.000-0000','2021-12-31T07:00:00.000-0900',\ + '2021-12-31T16:00:00.000-00', '2021-12-31T11:00:00.000-05','2021-12-31T16:00:00.000-00:00','2021-12-31T13:00:00.000-03:00',\ + '2021-12-31T16:00:00.000z', '2021-12-31T16:00:00.000Z'] + for i in range(len(tz_list)): + tdSql.query(f'select to_iso8601(ts,"{tz_list[i]}") from {self.ntbname} where c1==1') + tdSql.checkEqual(tdSql.queryResult[0][0],res_list[i]) + error_param_list = [0,100.5,'a','!'] for i in error_param_list: tdSql.error(f'select to_iso8601(ts,"{i}") from {self.ntbname}')