From 4c096bd36c2029e07a2d18cfe15d6ab5360882e3 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 30 Oct 2022 10:31:59 +0100 Subject: [PATCH] uptime: break time down into more understandable units 80 seconds -> 1 minute, 20 seconds for example 647 seconds would be 10 minutes, 47 seconds and more... --- apps/src/uptime.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/apps/src/uptime.c b/apps/src/uptime.c index 4a5a652e..673ff47c 100644 --- a/apps/src/uptime.c +++ b/apps/src/uptime.c @@ -1,10 +1,38 @@ #include #include +#define VALUE_SINGULAR_AT_ONE(v) v, v == 1 ? "" : "s" + int main() { struct timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); // On Luna, CLOCK_MONOTONIC starts at boot. - printf("up for %ld seconds\n", tp.tv_sec); + struct tm* time = gmtime( + &tp.tv_sec); // just splitting the value into seconds, minutes, hours, days... not the best way to do it but ok. + time->tm_year -= 70; + + if (time->tm_year) + { + printf("up for %d year%s, %d day%s, %d hour%s, %d minute%s, %d second%s\n", + VALUE_SINGULAR_AT_ONE(time->tm_year), VALUE_SINGULAR_AT_ONE(time->tm_yday), + VALUE_SINGULAR_AT_ONE(time->tm_hour), VALUE_SINGULAR_AT_ONE(time->tm_min), + VALUE_SINGULAR_AT_ONE(time->tm_sec)); + } + else if (time->tm_yday) + { + printf("up for %d day%s, %d hour%s, %d minute%s, %d second%s\n", VALUE_SINGULAR_AT_ONE(time->tm_yday), + VALUE_SINGULAR_AT_ONE(time->tm_hour), VALUE_SINGULAR_AT_ONE(time->tm_min), + VALUE_SINGULAR_AT_ONE(time->tm_sec)); + } + else if (time->tm_hour) + { + printf("up for %d hour%s, %d minute%s, %d second%s\n", VALUE_SINGULAR_AT_ONE(time->tm_hour), + VALUE_SINGULAR_AT_ONE(time->tm_min), VALUE_SINGULAR_AT_ONE(time->tm_sec)); + } + else if (time->tm_min) + printf("up for %d minute%s, %d second%s\n", VALUE_SINGULAR_AT_ONE(time->tm_min), + VALUE_SINGULAR_AT_ONE(time->tm_sec)); + else + printf("up for %d second%s\n", VALUE_SINGULAR_AT_ONE(time->tm_sec)); } \ No newline at end of file