summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMark Stapp <mjs@voltanet.io>2020-03-05 20:06:46 +0100
committerMark Stapp <mjs@voltanet.io>2020-03-09 16:12:32 +0100
commitd0636ead31ccfc9f6c27d02617d5965e3111d4ad (patch)
treef1c383042125411fc44acd65febdf43ce1f1de17 /lib
parentMerge pull request #5919 from qlyoung/fix-vrrp-mvl-uaf (diff)
downloadfrr-d0636ead31ccfc9f6c27d02617d5965e3111d4ad.tar.xz
frr-d0636ead31ccfc9f6c27d02617d5965e3111d4ad.zip
lib, *: add a common time interval formatting api
Add a common api that formats a time interval into a string with different output for short and longer intervals. We do this in several places, for cli/ui output. Signed-off-by: Mark Stapp <mjs@voltanet.io>
Diffstat (limited to 'lib')
-rw-r--r--lib/monotime.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/monotime.h b/lib/monotime.h
index e246f177d..dda763784 100644
--- a/lib/monotime.h
+++ b/lib/monotime.h
@@ -112,6 +112,26 @@ static inline char *time_to_string(time_t ts, char *buf)
return ctime_r(&tbuf, 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)
+{
+ struct tm tm;
+
+ gmtime_r(&t, &tm);
+
+ if (t < ONE_DAY_SECOND)
+ snprintf(buf, buflen, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
+ tm.tm_sec);
+ else if (t < ONE_WEEK_SECOND)
+ snprintf(buf, buflen, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
+ tm.tm_min);
+ else
+ snprintf(buf, buflen, "%02dw%dd%02dh", tm.tm_yday / 7,
+ tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
+ return buf;
+}
+
#ifdef __cplusplus
}
#endif