summaryrefslogtreecommitdiff
path: root/lib/monotime.h
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@opensourcerouting.org>2022-01-13 07:51:54 +0100
committerDavid Lamparter <equinox@opensourcerouting.org>2022-01-14 13:33:57 +0100
commit2c76ba433f2b0d0b180bca20ddc2f28751dd9b70 (patch)
tree87aa3f67c878c8809f590e4414dc6f9131caf8e7 /lib/monotime.h
parent2c5b4d80efc507f7244303d959f99af8061dacf3 (diff)
lib: add time formatting printfrr exts
Refer to docs in doc/developer for details. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/monotime.h')
-rw-r--r--lib/monotime.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/monotime.h b/lib/monotime.h
index dda763784f..15b6933955 100644
--- a/lib/monotime.h
+++ b/lib/monotime.h
@@ -25,6 +25,9 @@
extern "C" {
#endif
+struct fbuf;
+struct printfrr_eargs;
+
#ifndef TIMESPEC_TO_TIMEVAL
/* should be in sys/time.h on BSD & Linux libcs */
#define TIMESPEC_TO_TIMEVAL(tv, ts) \
@@ -42,6 +45,31 @@ extern "C" {
} while (0)
#endif
+/* Linux/glibc is sadly missing these timespec helpers */
+#ifndef timespecadd
+#define timespecadd(tsp, usp, vsp) \
+ do { \
+ (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
+ (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
+ if ((vsp)->tv_nsec >= 1000000000L) { \
+ (vsp)->tv_sec++; \
+ (vsp)->tv_nsec -= 1000000000L; \
+ } \
+ } while (0)
+#endif
+
+#ifndef timespecsub
+#define timespecsub(tsp, usp, vsp) \
+ do { \
+ (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
+ (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
+ if ((vsp)->tv_nsec < 0) { \
+ (vsp)->tv_sec--; \
+ (vsp)->tv_nsec += 1000000000L; \
+ } \
+ } while (0)
+#endif
+
static inline time_t monotime(struct timeval *tvo)
{
struct timespec ts;
@@ -132,6 +160,53 @@ static inline const char *frrtime_to_interval(time_t t, char *buf,
return buf;
}
+enum {
+ /* n/a - input was seconds precision, don't print any fractional */
+ TIMEFMT_SECONDS = (1 << 0),
+ /* caller is directly invoking printfrr_time and has pre-specified
+ * I/Iu/Is/M/Mu/Ms/R/Ru/Rs (for printing timers)
+ */
+ TIMEFMT_PRESELECT = (1 << 1),
+ /* don't print any output - this is needed for invoking printfrr_time
+ * from another printfrr extensions to skip over flag characters
+ */
+ TIMEFMT_SKIP = (1 << 2),
+ /* use spaces in appropriate places */
+ TIMEFMT_SPACE = (1 << 3),
+
+ /* input interpretations: */
+ TIMEFMT_REALTIME = (1 << 8),
+ TIMEFMT_MONOTONIC = (1 << 9),
+ TIMEFMT_SINCE = (1 << 10),
+ TIMEFMT_UNTIL = (1 << 11),
+
+ TIMEFMT_ABSOLUTE = TIMEFMT_REALTIME | TIMEFMT_MONOTONIC,
+ TIMEFMT_ANCHORS = TIMEFMT_SINCE | TIMEFMT_UNTIL,
+
+ /* calendaric formats: */
+ TIMEFMT_ISO8601 = (1 << 16),
+
+ /* interval formats: */
+ /* 't' - use [t]raditional 3-block format */
+ TIMEFMT_BASIC = (1 << 24),
+ /* 'm' - select mm:ss */
+ TIMEFMT_MMSS = (1 << 25),
+ /* 'h' - select hh:mm:ss */
+ TIMEFMT_HHMMSS = (1 << 26),
+ /* 'd' - print as decimal number of seconds */
+ TIMEFMT_DECIMAL = (1 << 27),
+ /* 'mx'/'hx' - replace zero value with "--:--" or "--:--:--" */
+ TIMEFMT_DASHES = (1 << 31),
+
+ /* helpers for reference */
+ TIMEFMT_TIMER_DEADLINE =
+ TIMEFMT_PRESELECT | TIMEFMT_MONOTONIC | TIMEFMT_UNTIL,
+ TIMEFMT_TIMER_INTERVAL = TIMEFMT_PRESELECT,
+};
+
+extern ssize_t printfrr_time(struct fbuf *buf, struct printfrr_eargs *ea,
+ const struct timespec *ts, unsigned int flags);
+
#ifdef __cplusplus
}
#endif