Skip to content

Commit

Permalink
rpc: adds json_add_timeiso helper
Browse files Browse the repository at this point in the history
  • Loading branch information
m-schmoock authored and niftynei committed Nov 6, 2020
1 parent 67f4970 commit 4d76500
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions common/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 5 additions & 0 deletions common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4d76500

Please sign in to comment.