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