Fix date format in mosquitto_sub output.

Closes #2353. Thanks to Norman Rasmussen.
This commit is contained in:
Roger A. Light 2021-10-27 15:51:12 +01:00
parent 9e5b850181
commit 20d2935d81
2 changed files with 15 additions and 6 deletions

View File

@ -17,6 +17,9 @@ Client library:
- Initialise sockpairR/W to invalid in `mosquitto_reinitialise()` to avoid
closing invalid sockets in `mosquitto_destroy()` on error. Closes #2326.
Clients:
- Fix date format in mosquitto_sub output. Closes #2353.
2.0.12 - 2021-08-31
===================

View File

@ -248,6 +248,16 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti
#endif
static void format_time_8601(const struct tm *ti, int ns, char *buf, size_t len)
{
char c;
strftime(buf, len, "%Y-%m-%dT%H:%M:%S.000000%z", ti);
c = buf[strlen("2020-05-06T21:48:00.000000")];
snprintf(&buf[strlen("2020-05-06T21:48:00.")], 9, "%06d", ns/1000);
buf[strlen("2020-05-06T21:48:00.000000")] = c;
}
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];
@ -262,9 +272,7 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p
return MOSQ_ERR_NOMEM;
}
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';
format_time_8601(ti, ns, buf, sizeof(buf));
tmp = cJSON_CreateStringReference(buf);
if(tmp == NULL){
@ -367,9 +375,7 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p
UNUSED(properties);
UNUSED(pretty);
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';
format_time_8601(ti, ns, buf, sizeof(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){