diff --git a/ChangeLog.txt b/ChangeLog.txt index bdc53bfc..cd051b76 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -46,6 +46,7 @@ Clients: easily set for MQTT v5 clients. - Add `--random-filter` to mosquitto_sub, to allow only a certain proportion of received messages to be printed. +- mosquitto_sub %j and %J timestamps are now in a ISO 8601 compatible format. 1.6.9 - 20200227 diff --git a/client/sub_client_output.c b/client/sub_client_output.c index e764ef21..6b4c6519 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -208,8 +208,9 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti #endif -static int json_print(const struct mosquitto_message *message, const mosquitto_property *properties, const struct tm *ti, bool escaped, bool pretty) +static int json_print(const struct mosquitto_message *message, const mosquitto_property *properties, const struct tm *ti, int ns, bool escaped, bool pretty) { + char buf[100]; #ifdef WITH_CJSON cJSON *root; cJSON *tmp; @@ -221,7 +222,11 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p return MOSQ_ERR_NOMEM; } - tmp = cJSON_CreateNumber(time(NULL)); + strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.000000Z%z", ti); + snprintf(&buf[strlen("2020-05-06T21:48:00.")], 9, "%06d", ns/1000); + buf[strlen("2020-05-06T21:48:00.000000")] = 'Z'; + + tmp = cJSON_CreateStringReference(buf); if(tmp == NULL){ cJSON_Delete(root); return MOSQ_ERR_NOMEM; @@ -304,10 +309,12 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p return MOSQ_ERR_SUCCESS; #else - char buf[100]; + strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.000000Z%z", ti); + snprintf(&buf[strlen("2020-05-06T21:48:00.")], 9, "%06d", ns/1000); + buf[strlen("2020-05-06T21:48:00.000000")] = 'Z'; - strftime(buf, 100, "%s", ti); - printf("{\"tst\":%s,\"topic\":\"%s\",\"qos\":%d,\"retain\":%d,\"payloadlen\":%d,", buf, message->topic, message->qos, message->retain, message->payloadlen); + tmp = cJSON_CreateStringReference(buf); + printf("{\"tst\":\"%s\",\"topic\":\"%s\",\"qos\":%d,\"retain\":%d,\"payloadlen\":%d,", buf, message->topic, message->qos, message->retain, message->payloadlen); if(message->qos > 0){ printf("\"mid\":%d,", message->mid); } @@ -403,7 +410,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit return; } } - if(json_print(message, properties, ti, true, lcfg->pretty) != MOSQ_ERR_SUCCESS){ + if(json_print(message, properties, ti, ns, true, lcfg->pretty) != MOSQ_ERR_SUCCESS){ err_printf(lcfg, "Error: Out of memory.\n"); return; } @@ -416,7 +423,7 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit return; } } - rc = json_print(message, properties, ti, false, lcfg->pretty); + rc = json_print(message, properties, ti, ns, false, lcfg->pretty); if(rc == MOSQ_ERR_NOMEM){ err_printf(lcfg, "Error: Out of memory.\n"); return; diff --git a/man/mosquitto_rr.1.xml b/man/mosquitto_rr.1.xml index 683e212f..b4f25ccb 100644 --- a/man/mosquitto_rr.1.xml +++ b/man/mosquitto_rr.1.xml @@ -728,13 +728,13 @@ JSON output of message parameters and timestamp, with a quoted and escaped payload. For example - {"tst":1470825369,"topic":"greeting","qos":0,"retain":0,"payload":"hello + {"tst":"2020-05-06T22:12:00.000000+0100","topic":"greeting","qos":0,"retain":0,"payload":"hello world"} JSON output of message parameters and timestamp, with a non-quoted and non-escaped payload - this means the payload must itself be valid JSON. For example: - {"tst":1470825369,"topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}. + {"tst":"2020-05-06T22:12:00.000000+0100","topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}. If the payload is not valid JSON, then the error message "Error: Message payload is not valid JSON on topic <topic>" will be printed to stderr. ISO-8601 format date and time, e.g. 2016-08-10T09:47:38+0100 diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml index 841851cc..8419197b 100644 --- a/man/mosquitto_sub.1.xml +++ b/man/mosquitto_sub.1.xml @@ -857,13 +857,13 @@ mosquitto_sub -t 'bbc/#' -T bbc/bbc1 --remove-retained JSON output of message parameters and timestamp, with a quoted and escaped payload. For example - {"tst":1470825369,"topic":"greeting","qos":0,"retain":0,"payload":"hello + {"tst":"2020-05-06T22:12:00.000000+0100","topic":"greeting","qos":0,"retain":0,"payload":"hello world"} JSON output of message parameters and timestamp, with a non-quoted and non-escaped payload - this means the payload must itself be valid JSON. For example: - {"tst":1470825369,"topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}. + {"tst":"2020-05-06T22:12:00.000000+0100","topic":"foo","qos":0,"retain":0,"payload":{"temperature":27.0,"humidity":57}}. If the payload is not valid JSON, then the error message "Error: Message payload is not valid JSON on topic <topic>" will be printed to stderr.