From 4a315c923067dea8fb985e5791cd118a256ec209 Mon Sep 17 00:00:00 2001 From: ed neville Date: Wed, 29 Mar 2023 18:19:36 +0100 Subject: Store sec and usec in terms of usec Rather than attempt to convert usec to sec with division, add both together and then divide. This reduced the rounding errors on my system. --- src/uptime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uptime.c b/src/uptime.c index 6d6e739..91e89c7 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -44,12 +44,12 @@ static void print_uptime_since() /* Get the current time and convert it to a double */ if (gettimeofday(&tim, NULL) != 0) xerr(EXIT_FAILURE, "gettimeofday"); - now = tim.tv_sec + (tim.tv_usec / 1000000.0); + now = (tim.tv_sec * 1000000.0) + tim.tv_usec; /* Get the uptime and calculate when that was */ if (procps_uptime(&uptime_secs, &idle_secs) < 0) xerr(EXIT_FAILURE, _("Cannot get system uptime")); - up_since_secs = (time_t) ((now - uptime_secs) + 0.5); + up_since_secs = (time_t) ((now/1000000.0) - uptime_secs); /* Show this */ if ((up_since = localtime(&up_since_secs)) == NULL) -- cgit v1.2.1