From 1b5a4492106f1aab2c6c8c12f2cdb23d6da45003 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 29 Jul 2021 09:41:47 +0800 Subject: [PATCH] add support for parsing timestamp with both letter 'T' and whitespace as timezone indicator --- src/os/src/detail/osTime.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/os/src/detail/osTime.c b/src/os/src/detail/osTime.c index 847d484d9e..8db3fed701 100644 --- a/src/os/src/detail/osTime.c +++ b/src/os/src/detail/osTime.c @@ -82,7 +82,7 @@ void deltaToUtcInitOnce() { } static int64_t parseFraction(char* str, char** end, int32_t timePrec); -static int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec); +static int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec, char delim); static int32_t parseLocaltime(char* timestr, int64_t* time, int32_t timePrec); static int32_t parseLocaltimeWithDst(char* timestr, int64_t* time, int32_t timePrec); @@ -96,7 +96,9 @@ 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 */ if (strnchr(timestr, 'T', len, false) != NULL) { - return parseTimeWithTz(timestr, time, timePrec); + return parseTimeWithTz(timestr, time, timePrec, 'T'); + } else if (strnchr(timestr, ' ', len, false) != NULL) { + return parseTimeWithTz(timestr, time, timePrec, ' '); } else { return (*parseLocaltimeFp[day_light])(timestr, time, timePrec); } @@ -213,14 +215,23 @@ int32_t parseTimezone(char* str, int64_t* tzOffset) { * 2013-04-12T15:52:01+0800 * 2013-04-12T15:52:01.123+0800 */ -int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec) { +int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec, char delim) { int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000); int64_t tzOffset = 0; struct tm tm = {0}; - char* str = strptime(timestr, "%Y-%m-%dT%H:%M:%S", &tm); + + 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 { + str = NULL; + } + if (str == NULL) { return -1; } -- GitLab