aboutsummaryrefslogtreecommitdiffstats
path: root/src/vty/vty.c
diff options
context:
space:
mode:
authorAlexander Couzens <lynxis@fe80.eu>2021-09-05 23:08:59 +0200
committerAlexander Couzens <lynxis@fe80.eu>2021-09-23 13:12:34 +0200
commit06929166de7fe181d82d6a7225ffdede7a90120d (patch)
tree03aa01dac504128615b70d6638a1ab81dfd23a82 /src/vty/vty.c
parent53b4bbb955efa32220f4b546b75b414c773e3fe0 (diff)
vty: add vty_out_uptime() print the uptime to the vty
vty_out_uptime() calculates the time difference to a given timespec and print it in a human readable format (days, hours, minutes, seconds) to the vty. Related: OS#5028 Change-Id: I264a3f49096b96646e0a1f5366623ac20d860793
Diffstat (limited to 'src/vty/vty.c')
-rw-r--r--src/vty/vty.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/vty/vty.c b/src/vty/vty.c
index 1ad84f53..3a549b43 100644
--- a/src/vty/vty.c
+++ b/src/vty/vty.c
@@ -66,6 +66,7 @@
#include <osmocom/vty/command.h>
#include <osmocom/vty/buffer.h>
#include <osmocom/core/talloc.h>
+#include <osmocom/core/timer.h>
#include <osmocom/core/utils.h>
#ifndef MAXPATHLEN
@@ -338,6 +339,25 @@ int vty_out_newline(struct vty *vty)
return 0;
}
+/*! calculates the time difference of a give timespec to the current time
+ * and prints in a human readable format (days, hours, minutes, seconds).
+ */
+int vty_out_uptime(struct vty *vty, const struct timespec *starttime)
+{
+ struct timespec now;
+ struct timespec uptime;
+
+ osmo_clock_gettime(CLOCK_MONOTONIC, &now);
+ timespecsub(&now, starttime, &uptime);
+
+ int d = uptime.tv_sec / (3600 * 24);
+ int h = uptime.tv_sec / 3600 % 24;
+ int m = uptime.tv_sec / 60 % 60;
+ int s = uptime.tv_sec % 60;
+
+ return vty_out(vty, "%dd %dh %dm %ds", d, h, m, s);
+}
+
/*! return the current index of a given VTY */
void *vty_current_index(struct vty *vty)
{