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 46e298f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Expand Up @@ -118,6 +118,7 @@ Clients:
that the pipe has closed and disconnect. Closes #2164.
- Fix `mosquitto_pub -l` quitting if a message publication is attempted when
the broker is temporarily unavailable. Closes #2187.
- Fix date format in mosquitto_sub output. Closes #2353.


2.0.10 - 2021-04-03
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 46e298f

Please sign in to comment.