diff options
author | Donatas Abraitis <donatas@opensourcerouting.org> | 2024-12-20 16:12:12 +0100 |
---|---|---|
committer | Donatas Abraitis <donatas@opensourcerouting.org> | 2024-12-20 16:12:12 +0100 |
commit | b3e1ceaf88afa66fbdb1bcebde8d9ffcf3039bcf (patch) | |
tree | a820f5f4272a519249f88ec366a2d5766d8eaf37 /lib | |
parent | Merge pull request #17670 from pguibert6WIND/rpki_json_missing (diff) | |
download | frr-b3e1ceaf88afa66fbdb1bcebde8d9ffcf3039bcf.tar.xz frr-b3e1ceaf88afa66fbdb1bcebde8d9ffcf3039bcf.zip |
lib: Add a wrapper for time_to_string() to print time in JSON outputs
newline is not expected to be printed in JSON outputs, e.g.:
```
"lastUpdate":{"epoch":1734490463,"string":"Wed Dec 18 04:54:23 2024\n"
```
Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/monotime.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/monotime.h b/lib/monotime.h index f7ae1bbbe..5e1bfe754 100644 --- a/lib/monotime.h +++ b/lib/monotime.h @@ -129,6 +129,22 @@ static inline char *time_to_string(time_t ts, char *buf) return ctime_r(&tbuf, buf); } +/* A wrapper for time_to_string() which removes newline at the end. + * This is needed for JSON outputs, where newline is not expected. + */ +static inline char *time_to_string_json(time_t ts, char *buf) +{ + size_t len; + + time_to_string(ts, buf); + len = strlen(buf); + + if (len && buf[len - 1] == '\n') + buf[len - 1] = '\0'; + + return buf; +} + /* Convert interval to human-friendly string, used in cli output e.g. */ static inline const char *frrtime_to_interval(time_t t, char *buf, size_t buflen) |