From 51849220bdc1e0164df4d31d1a673e0f28801e42 Mon Sep 17 00:00:00 2001 From: xywang Date: Mon, 30 Aug 2021 02:54:33 +0800 Subject: [PATCH 1/2] [TD-6408]: fixed json error when timestamp was before Epoch --- src/plugins/http/src/httpJson.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index 3c72b795ee..b9067f0639 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -275,7 +275,7 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { quot = t / 1000; fractionLen = 5; format = ".%03" PRId64; - mod = t % 1000; + mod = ((t) % 1000 + 1000) % 1000; break; } @@ -283,7 +283,7 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { quot = t / 1000000; fractionLen = 8; format = ".%06" PRId64; - mod = t % 1000000; + mod = ((t) % 1000000 + 1000000) % 1000000; break; } @@ -291,7 +291,7 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { quot = t / 1000000000; fractionLen = 11; format = ".%09" PRId64; - mod = t % 1000000000; + mod = ((t) % 1000000000 + 1000000000) % 1000000000; break; } @@ -322,7 +322,7 @@ void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { quot = t / 1000; fractionLen = 5; format = ".%03" PRId64; - mod = t % 1000; + mod = ((t) % 1000 + 1000) % 1000; break; } @@ -330,7 +330,7 @@ void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { quot = t / 1000000; fractionLen = 8; format = ".%06" PRId64; - mod = t % 1000000; + mod = ((t) % 1000000 + 1000000) % 1000000; break; } @@ -338,7 +338,7 @@ void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { quot = t / 1000000000; fractionLen = 11; format = ".%09" PRId64; - mod = t % 1000000000; + mod = ((t) % 1000000000 + 1000000000) % 1000000000; break; } From 6912568b8af57942a6f67157e70facc95fae3289 Mon Sep 17 00:00:00 2001 From: xywang Date: Mon, 30 Aug 2021 03:37:58 +0800 Subject: [PATCH 2/2] [TD-6408]: one second more than actual timestamp --- src/plugins/http/src/httpJson.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index b9067f0639..86e0f2f40b 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -272,26 +272,35 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { switch (timePrecision) { case TSDB_TIME_PRECISION_MILLI: { + mod = ((t) % 1000 + 1000) % 1000; + if (t < 0 && mod != 0) { + t -= 1000; + } quot = t / 1000; fractionLen = 5; format = ".%03" PRId64; - mod = ((t) % 1000 + 1000) % 1000; break; } case TSDB_TIME_PRECISION_MICRO: { + mod = ((t) % 1000000 + 1000000) % 1000000; + if (t < 0 && mod != 0) { + t -= 1000000; + } quot = t / 1000000; fractionLen = 8; format = ".%06" PRId64; - mod = ((t) % 1000000 + 1000000) % 1000000; break; } case TSDB_TIME_PRECISION_NANO: { + mod = ((t) % 1000000000 + 1000000000) % 1000000000; + if (t < 0 && mod != 0) { + t -= 1000000000; + } quot = t / 1000000000; fractionLen = 11; format = ".%09" PRId64; - mod = ((t) % 1000000000 + 1000000000) % 1000000000; break; } @@ -319,26 +328,35 @@ void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { switch (timePrecision) { case TSDB_TIME_PRECISION_MILLI: { + mod = ((t) % 1000 + 1000) % 1000; + if (t < 0 && mod != 0) { + t -= 1000; + } quot = t / 1000; fractionLen = 5; format = ".%03" PRId64; - mod = ((t) % 1000 + 1000) % 1000; break; } case TSDB_TIME_PRECISION_MICRO: { + mod = ((t) % 1000000 + 1000000) % 1000000; + if (t < 0 && mod != 0) { + t -= 1000000; + } quot = t / 1000000; fractionLen = 8; format = ".%06" PRId64; - mod = ((t) % 1000000 + 1000000) % 1000000; break; } case TSDB_TIME_PRECISION_NANO: { + mod = ((t) % 1000000000 + 1000000000) % 1000000000; + if (t < 0 && mod != 0) { + t -= 1000000000; + } quot = t / 1000000000; fractionLen = 11; format = ".%09" PRId64; - mod = ((t) % 1000000000 + 1000000000) % 1000000000; break; }