Jan Rybar ef095b
From 7eade2544e1c45bc516744aeaccc45df1d8f42df Mon Sep 17 00:00:00 2001
Jan Rybar ef095b
From: Kyle Walker <kwalker@redhat.com>
Jan Rybar ef095b
Date: Tue, 11 Feb 2020 14:30:39 -0500
Jan Rybar ef095b
Subject: [PATCH] whattime: Refactor the pretty-print evaluation
Jan Rybar ef095b
Jan Rybar ef095b
This avoids rounding errors such as in the case of 364 days of uptime which
Jan Rybar ef095b
results in only output that prints at the hour and below.
Jan Rybar ef095b
---
Jan Rybar ef095b
 proc/whattime.c | 15 ++++++++++++---
Jan Rybar ef095b
 1 file changed, 12 insertions(+), 3 deletions(-)
Jan Rybar ef095b
Jan Rybar ef095b
diff --git a/proc/whattime.c b/proc/whattime.c
Jan Rybar ef095b
index c223cad..3e1b65c 100644
Jan Rybar ef095b
--- a/proc/whattime.c
Jan Rybar ef095b
+++ b/proc/whattime.c
Jan Rybar ef095b
@@ -69,9 +69,18 @@ char *sprint_uptime(int human_readable) {
Jan Rybar ef095b
 
Jan Rybar ef095b
   if (human_readable) {
Jan Rybar ef095b
     updecades = (int) uptime_secs / (60*60*24*365*10);
Jan Rybar ef095b
-    upyears = ((int) uptime_secs / (60*60*24*365)) % 10;
Jan Rybar ef095b
-    upweeks = ((int) uptime_secs / (60*60*24*7)) % 52;
Jan Rybar ef095b
-    updays = ((int) uptime_secs / (60*60*24)) % 7;
Jan Rybar ef095b
+    if (updecades)
Jan Rybar ef095b
+        uptime_secs -= updecades * (60*60*24*365*10);
Jan Rybar ef095b
+
Jan Rybar ef095b
+    upyears = (int) uptime_secs / (60*60*24*365);
Jan Rybar ef095b
+    if (upyears)
Jan Rybar ef095b
+        uptime_secs -= upyears * (60*60*24*365);
Jan Rybar ef095b
+
Jan Rybar ef095b
+    upweeks = (int) uptime_secs / (60*60*24*7);
Jan Rybar ef095b
+    if (upweeks)
Jan Rybar ef095b
+        uptime_secs -= upweeks * (60*60*24*7);
Jan Rybar ef095b
+
Jan Rybar ef095b
+    updays = (int) uptime_secs / (60*60*24);
Jan Rybar ef095b
   }
Jan Rybar ef095b
   else
Jan Rybar ef095b
     updays = (int) uptime_secs / (60*60*24);
Jan Rybar ef095b
-- 
Jan Rybar ef095b
2.24.1
Jan Rybar ef095b