diff --git a/src/common/src/texpr.c b/src/common/src/texpr.c index 70d64b8ce48a0e52941764d151be968d03c95c92..a7b16b80198dccab080e0a90dc528fbf47b6440b 100644 --- a/src/common/src/texpr.c +++ b/src/common/src/texpr.c @@ -1817,18 +1817,44 @@ void vectorTimeFunc(int16_t functionId, tExprOperandInfo *pInputs, int32_t numIn bool hasFraction = false; NUM_TO_STRING(pInputs[0].type, inputData[0], sizeof(fraction), fraction); int32_t tsDigits = strlen(fraction); - if (tsDigits > 10) { - hasFraction = true; - memmove(fraction, fraction, 10); - } char buf[64] = {0}; int64_t timeVal; GET_TYPED_DATA(timeVal, int64_t, pInputs[0].type, inputData[0]); + if (tsDigits > TSDB_TIME_PRECISION_SEC_DIGITS) { + if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) { + timeVal = timeVal / 1000; + } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) { + timeVal = timeVal / (1000 * 1000); + } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) { + timeVal = timeVal / (1000 * 1000 * 1000); + } else { + assert(0); + } + hasFraction = true; + memmove(fraction, fraction + TSDB_TIME_PRECISION_SEC_DIGITS, TSDB_TIME_PRECISION_SEC_DIGITS); + } + struct tm *tmInfo = localtime(&timeVal); strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", tmInfo); int32_t len = (int32_t)strlen(buf); + if (hasFraction) { + int32_t fracLen = strlen(fraction) + 1; + char *tzInfo = strchr(buf, '+'); + if (tzInfo) { + memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo)); + } else { + tzInfo = strchr(buf, '-'); + memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo)); + } + + char tmp[32]; + sprintf(tmp, ".%s", fraction); + memcpy(tzInfo, tmp, fracLen); + len += fracLen; + } + memcpy(((char*)varDataVal(outputData)), buf, len); varDataSetLen(outputData, len); diff --git a/src/inc/taosdef.h b/src/inc/taosdef.h index e0f92fd123d8c75c8e390d68c726f1cf3814db4f..16607d27024763b1c650828198d0d7faa67c421e 100644 --- a/src/inc/taosdef.h +++ b/src/inc/taosdef.h @@ -104,6 +104,11 @@ extern const int32_t TYPE_BYTES[16]; #define TSDB_TIME_PRECISION_MICRO_STR "us" #define TSDB_TIME_PRECISION_NANO_STR "ns" +#define TSDB_TIME_PRECISION_SEC_DIGITS 10 +#define TSDB_TIME_PRECISION_MILLI_DIGITS 13 +#define TSDB_TIME_PRECISION_MICRO_DIGITS 16 +#define TSDB_TIME_PRECISION_NANO_DIGITS 19 + #define TSDB_TICK_PER_SECOND(precision) ((int64_t)((precision)==TSDB_TIME_PRECISION_MILLI ? 1e3L : ((precision)==TSDB_TIME_PRECISION_MICRO ? 1e6L : 1e9L))) #define T_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)