From 4d765003dd7d2636b67508fe357a45e811670e16 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Wed, 28 Oct 2020 11:46:20 +0100 Subject: [PATCH] rpc: adds json_add_timeiso helper --- common/json.c | 16 ++++++++++++++++ common/json.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/common/json.c b/common/json.c index 2c7fac185..d4b111921 100644 --- a/common/json.c +++ b/common/json.c @@ -827,6 +827,22 @@ void json_add_time(struct json_stream *result, const char *fieldname, json_add_string(result, fieldname, timebuf); } +void json_add_timeiso(struct json_stream *result, + const char *fieldname, + struct timeabs *time) +{ + char iso8601_msec_fmt[sizeof("YYYY-mm-ddTHH:MM:SS.%03dZ")]; + char iso8601_s[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ")]; + + strftime(iso8601_msec_fmt, sizeof(iso8601_msec_fmt), + "%FT%T.%%03dZ", gmtime(&time->ts.tv_sec)); + snprintf(iso8601_s, sizeof(iso8601_s), + iso8601_msec_fmt, (int) time->ts.tv_nsec / 1000000); + + json_add_string(result, fieldname, iso8601_s); +} + + void json_add_tok(struct json_stream *result, const char *fieldname, const jsmntok_t *tok, const char *buffer) { diff --git a/common/json.h b/common/json.h index c1374a89c..c9c2f6613 100644 --- a/common/json.h +++ b/common/json.h @@ -201,6 +201,11 @@ void json_add_timeabs(struct json_stream *result, const char *fieldname, void json_add_time(struct json_stream *result, const char *fieldname, struct timespec ts); +/* Add ISO_8601 timestamp string, i.e. "2019-09-07T15:50+01:00" */ +void json_add_timeiso(struct json_stream *result, + const char *fieldname, + struct timeabs *time); + /* Add any json token */ void json_add_tok(struct json_stream *result, const char *fieldname, const jsmntok_t *tok, const char *buffer);