revise issue #124

This commit is contained in:
slguan 2019-08-10 23:51:19 +08:00
parent 0da8e9ad2b
commit c571c38bd6
4 changed files with 38 additions and 23 deletions

View File

@ -63,8 +63,8 @@ void httpJsonString(JsonBuf* buf, char* sVal, int len);
void httpJsonOriginString(JsonBuf* buf, char* sVal, int len); void httpJsonOriginString(JsonBuf* buf, char* sVal, int len);
void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int maxLen); void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int maxLen);
void httpJsonInt64(JsonBuf* buf, int64_t num); void httpJsonInt64(JsonBuf* buf, int64_t num);
void httpJsonTimestamp(JsonBuf* buf, int64_t t); void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us);
void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t); void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us);
void httpJsonInt(JsonBuf* buf, int num); void httpJsonInt(JsonBuf* buf, int num);
void httpJsonFloat(JsonBuf* buf, float num); void httpJsonFloat(JsonBuf* buf, float num);
void httpJsonDouble(JsonBuf* buf, double num); void httpJsonDouble(JsonBuf* buf, double num);

View File

@ -260,30 +260,45 @@ void httpJsonInt64(JsonBuf* buf, int64_t num) {
buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%ld", num); buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%ld", num);
} }
void httpJsonTimestamp(JsonBuf* buf, int64_t t) { void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) {
char ts[30] = {0}; char ts[35] = {0};
struct tm *ptm;
int precision = 1000;
if (us) {
precision = 1000000;
}
struct tm* ptm; time_t tt = t / precision;
time_t tt = t / 1000;
ptm = localtime(&tt); 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);
httpJsonString(buf, ts, length + 4);
} }
void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t) { void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us) {
char ts[35] = {0}; char ts[40] = {0};
struct tm *ptm; struct tm *ptm;
time_t tt = t / 1000; int precision = 1000;
ptm = localtime(&tt); if (us) {
int length = (int) strftime(ts, 35, "%Y-%m-%dT%H:%M:%S", ptm); precision = 1000000;
length += snprintf(ts + length, MAX_NUM_STR_SZ, ".%03ld", t % 1000); }
length += (int) strftime(ts + length, 35 - length, "%z", ptm);
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) { void httpJsonInt(JsonBuf* buf, int num) {

View File

@ -135,11 +135,11 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
break; break;
case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_TIMESTAMP:
if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) { 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) { } else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) {
httpJsonInt64(jsonBuf, *((int64_t *)row[i])); httpJsonInt64(jsonBuf, *((int64_t *)row[i]));
} else { } else {
httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i])); httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO);
} }
break; break;
default: default: