From 3cfb2ea995c06f39d0e466a1dae01d5d3870d10d Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Wed, 4 Aug 2021 23:48:45 +0800 Subject: [PATCH] [TD-5505]: if there's no space or 'T' delimiter timezone will also be parsed --- src/os/src/detail/osTime.c | 23 +++++++++++++++-------- src/query/tests/unitTest.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/os/src/detail/osTime.c b/src/os/src/detail/osTime.c index 9b5a86cbbb..078b9c62d0 100644 --- a/src/os/src/detail/osTime.c +++ b/src/os/src/detail/osTime.c @@ -86,6 +86,7 @@ static int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec, c static int32_t parseLocaltime(char* timestr, int64_t* time, int32_t timePrec); static int32_t parseLocaltimeWithDst(char* timestr, int64_t* time, int32_t timePrec); static char* forwardToTimeStringEnd(char* str); +static bool checkTzPresent(char *str, int32_t len); static int32_t (*parseLocaltimeFp[]) (char* timestr, int64_t* time, int32_t timePrec) = { parseLocaltime, @@ -96,19 +97,25 @@ int32_t taosGetTimestampSec() { return (int32_t)time(NULL); } int32_t taosParseTime(char* timestr, int64_t* time, int32_t len, int32_t timePrec, int8_t day_light) { /* parse datatime string in with tz */ - char *seg = forwardToTimeStringEnd(timestr); - int32_t seg_len = len - (int32_t)(seg - timestr); if (strnchr(timestr, 'T', len, false) != NULL) { return parseTimeWithTz(timestr, time, timePrec, 'T'); - } else if (strnchr(timestr, ' ', len, false) != NULL && - (strnchr(seg, 'Z', seg_len, false) != NULL || strnchr(seg, 'z', seg_len, false) != NULL || - strnchr(seg, '+', seg_len, false) != NULL || strnchr(seg, '-', seg_len, false) != NULL)) { - return parseTimeWithTz(timestr, time, timePrec, ' '); + } else if (checkTzPresent(timestr, len)) { + return parseTimeWithTz(timestr, time, timePrec, 0); } else { return (*parseLocaltimeFp[day_light])(timestr, time, timePrec); } } +bool checkTzPresent(char *str, int32_t len) { + char *seg = forwardToTimeStringEnd(str); + int32_t seg_len = len - (int32_t)(seg - str); + + return (strnchr(seg, 'Z', seg_len, false) != NULL || + strnchr(seg, 'z', seg_len, false) != NULL || + strnchr(seg, '+', seg_len, false) != NULL || + strnchr(seg, '-', seg_len, false) != NULL); +} + char* forwardToTimeStringEnd(char* str) { int32_t i = 0; int32_t numOfSep = 0; @@ -238,8 +245,8 @@ int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec, char del char* str; if (delim == 'T') { str = strptime(timestr, "%Y-%m-%dT%H:%M:%S", &tm); - } else if (delim == ' ') { - str = strptime(timestr, "%Y-%m-%d%n%H:%M:%S", &tm); + } else if (delim == 0) { + str = strptime(timestr, "%Y-%m-%d %H:%M:%S", &tm); } else { str = NULL; } diff --git a/src/query/tests/unitTest.cpp b/src/query/tests/unitTest.cpp index e898992a40..740a783ca0 100644 --- a/src/query/tests/unitTest.cpp +++ b/src/query/tests/unitTest.cpp @@ -434,6 +434,29 @@ TEST(testCase, parse_time) { taosParseTime(t64, &time1, strlen(t64), TSDB_TIME_PRECISION_MILLI, 0); EXPECT_EQ(time, time1); + // "%Y-%m-%d%H:%M:%S" format with TimeZone appendix is also treated as legal + // and TimeZone will be processed + char t80[] = "2017-4-51:1:2.980"; + char t81[] = "2017-4-52:1:2.98+9:00"; + taosParseTime(t80, &time, strlen(t80), TSDB_TIME_PRECISION_MILLI, 0); + taosParseTime(t81, &time1, strlen(t81), TSDB_TIME_PRECISION_MILLI, 0); + EXPECT_EQ(time, time1); + + char t82[] = "2017-4-52:1:2.98+09:00"; + taosParseTime(t82, &time, strlen(t82), TSDB_TIME_PRECISION_MILLI, 0); + taosParseTime(t81, &time1, strlen(t81), TSDB_TIME_PRECISION_MILLI, 0); + EXPECT_EQ(time, time1); + + char t83[] = "2017-4-52:1:2.98+0900"; + taosParseTime(t83, &time, strlen(t83), TSDB_TIME_PRECISION_MILLI, 0); + taosParseTime(t82, &time1, strlen(t82), TSDB_TIME_PRECISION_MILLI, 0); + EXPECT_EQ(time, time1); + + char t84[] = "2017-4-417:1:2.98Z"; + taosParseTime(t83, &time, strlen(t83), TSDB_TIME_PRECISION_MILLI, 0); + taosParseTime(t84, &time1, strlen(t84), TSDB_TIME_PRECISION_MILLI, 0); + EXPECT_EQ(time, time1); + //////////////////////////////////////////////////////////////////// // illegal timestamp format char t15[] = "2017-12-33 0:0:0"; @@ -498,6 +521,7 @@ TEST(testCase, parse_time) { char t70[] = "2017-12-31 9:0:0.123Z+12:00"; EXPECT_EQ(taosParseTime(t70, &time, strlen(t70), TSDB_TIME_PRECISION_MILLI, 0), -1); + } TEST(testCase, tvariant_convert) { -- GitLab