Skip to content

Commit

Permalink
Fix date format in mosquitto_sub output.
Browse files Browse the repository at this point in the history
Closes #2353. Thanks to Norman Rasmussen.
  • Loading branch information
ralight committed Oct 27, 2021
1 parent 9e5b850 commit 20d2935
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.txt
Expand Up @@ -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
===================
Expand Down
18 changes: 12 additions & 6 deletions client/sub_client_output.c
Expand Up @@ -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];
Expand All @@ -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){
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit 20d2935

Please sign in to comment.