diff --git a/src/modules/http/inc/httpJson.h b/src/modules/http/inc/httpJson.h index 91c39a1ac2..403c20b083 100644 --- a/src/modules/http/inc/httpJson.h +++ b/src/modules/http/inc/httpJson.h @@ -63,8 +63,8 @@ void httpJsonString(JsonBuf* buf, char* sVal, int len); void httpJsonOriginString(JsonBuf* buf, char* sVal, int len); void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int maxLen); void httpJsonInt64(JsonBuf* buf, int64_t num); -void httpJsonTimestamp(JsonBuf* buf, int64_t t); -void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t); +void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us); +void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us); void httpJsonInt(JsonBuf* buf, int num); void httpJsonFloat(JsonBuf* buf, float num); void httpJsonDouble(JsonBuf* buf, double num); diff --git a/src/modules/http/inc/restJson.h b/src/modules/http/inc/restJson.h index e16c148d7c..7cff21d190 100644 --- a/src/modules/http/inc/restJson.h +++ b/src/modules/http/inc/restJson.h @@ -40,8 +40,8 @@ #define REST_JSON_AFFECT_ROWS_LEN 13 #define REST_TIMESTAMP_FMT_LOCAL_STRING 0 -#define REST_TIMESTAMP_FMT_TIMESTAMP 1 -#define REST_TIMESTAMP_FMT_UTC_STRING 2 +#define REST_TIMESTAMP_FMT_TIMESTAMP 1 +#define REST_TIMESTAMP_FMT_UTC_STRING 2 void restBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int affect_rows); diff --git a/src/modules/http/src/httpJson.c b/src/modules/http/src/httpJson.c index 189d6c8f30..26e539bfdb 100644 --- a/src/modules/http/src/httpJson.c +++ b/src/modules/http/src/httpJson.c @@ -260,30 +260,45 @@ void httpJsonInt64(JsonBuf* buf, int64_t num) { buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%ld", num); } -void httpJsonTimestamp(JsonBuf* buf, int64_t t) { - char ts[30] = {0}; +void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) { + char ts[35] = {0}; + struct tm *ptm; + int precision = 1000; + if (us) { + precision = 1000000; + } - struct tm* ptm; - time_t tt = t / 1000; + time_t tt = t / precision; ptm = localtime(&tt); - int length = (int)strftime(ts, 30, "%Y-%m-%d %H:%M:%S", ptm); + int length = (int) strftime(ts, 35, "%Y-%m-%d %H:%M:%S", ptm); + if (us) { + length += snprintf(ts + length, 8, ".%06ld", t % precision); + } else { + length += snprintf(ts + length, 5, ".%03ld", t % precision); + } - snprintf(ts+length, MAX_NUM_STR_SZ, ".%03ld", t % 1000); - - httpJsonString(buf, ts, length + 4); + httpJsonString(buf, ts, length); } -void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t) { - char ts[35] = {0}; - +void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us) { + char ts[40] = {0}; struct tm *ptm; - time_t tt = t / 1000; - ptm = localtime(&tt); - int length = (int) strftime(ts, 35, "%Y-%m-%dT%H:%M:%S", ptm); - length += snprintf(ts + length, MAX_NUM_STR_SZ, ".%03ld", t % 1000); - length += (int) strftime(ts + length, 35 - length, "%z", ptm); + int precision = 1000; + if (us) { + precision = 1000000; + } - httpJsonString(buf, ts, length + 4); + time_t tt = t / precision; + ptm = localtime(&tt); + int length = (int) strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", ptm); + if (us) { + length += snprintf(ts + length, 8, ".%06ld", t % precision); + } else { + length += snprintf(ts + length, 5, ".%03ld", t % precision); + } + length += (int) strftime(ts + length, 40 - length, "%z", ptm); + + httpJsonString(buf, ts, length); } void httpJsonInt(JsonBuf* buf, int num) { diff --git a/src/modules/http/src/restJson.c b/src/modules/http/src/restJson.c index e9315fa32b..8aa0ac5069 100644 --- a/src/modules/http/src/restJson.c +++ b/src/modules/http/src/restJson.c @@ -135,11 +135,11 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, break; case TSDB_DATA_TYPE_TIMESTAMP: if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) { - httpJsonTimestamp(jsonBuf, *((int64_t *)row[i])); + httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO); } else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) { httpJsonInt64(jsonBuf, *((int64_t *)row[i])); } else { - httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i])); + httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO); } break; default: