On Fri, Sep 18, 2020 at 03:59:05PM -0500, Scott Cheloha wrote:
> 
> [...]
> 
> - An HH:MM:SS format uptime is useful in top(1).  It's also more
>   visually consistent with the local timestamp printed on the line
>   above it, so it is easier to read at a glance.
> 
> - The variable printing of "days" is annoying.  I would rather it
>   just told me "0 days" if it had been less than one day.  It sucks
>   when the information you want moves around or isn't shown at all.
>   It's clever, sure, but I'd rather it be consistent.
> 
> This patch changes the uptime format string to "up D days HH:MM:SS".
> The format string does not vary with the elapsed uptime.  There is no
> inclusion/omission of the plural suffix depending on whether days is
> equal to one.
> 
> [...]

Whoops, forgot about this one.  September 18, 2020.  What a time to be
alive.

Let's try this again.  98 week bump.

To recap, this patch makes the uptime formatting in top(1) produce
more constant-width results.  The formatting is now always:

        up D days HH:MM:SS

so only the day-count changes size.  The day-count is also always
printed: if the machine has not been up for a full day it prints

        up 0 days HH:MM:SS

For example, the upper lines on the top(1) running on my machine
currently look like this:

load averages:  0.29,  0.29,  0.27                 jetsam.attlocal.net 18:12:16
82 processes: 81 idle, 1 on processor                        up 3 days 07:14:01

I have been running with this for almost two years and I love it.
I would like to commit it.

The only feedback I got when I originally posted this was that the
output formatting would no longer be the same as uptime(1)'s.  I don't
think that matters very much.  The person who offered the feedback
didn't think it mattered either, they were just hypothesizing
objections.

ok?

Index: display.c
===================================================================
RCS file: /cvs/src/usr.bin/top/display.c,v
retrieving revision 1.65
diff -u -p -r1.65 display.c
--- display.c   26 Aug 2020 16:21:28 -0000      1.65
+++ display.c   7 Aug 2022 23:14:25 -0000
@@ -208,31 +208,28 @@ display_init(struct statics * statics)
        return (display_lines);
 }
 
+/*
+ * Print the time elapsed since the system booted.
+ */
 static void
 format_uptime(char *buf, size_t buflen)
 {
-       time_t uptime;
-       int days, hrs, mins;
        struct timespec boottime;
+       time_t uptime;
+       unsigned int days, hrs, mins, secs;
+
+       if (clock_gettime(CLOCK_BOOTTIME, &boottime) == -1)
+               err(1, "clock_gettime");
 
-       /*
-        * Print how long system has been up.
-        */
-       if (clock_gettime(CLOCK_BOOTTIME, &boottime) != -1) {
-               uptime = boottime.tv_sec;
-               uptime += 30;
-               days = uptime / (3600 * 24);
-               uptime %= (3600 * 24);
-               hrs = uptime / 3600;
-               uptime %= 3600;
-               mins = uptime / 60;
-               if (days > 0)
-                       snprintf(buf, buflen, "up %d day%s, %2d:%02d",
-                           days, days > 1 ? "s" : "", hrs, mins);
-               else
-                       snprintf(buf, buflen, "up %2d:%02d",
-                           hrs, mins);
-       }
+       uptime = boottime.tv_sec;
+       days = uptime / (3600 * 24);
+       uptime %= (3600 * 24);
+       hrs = uptime / 3600;
+       uptime %= 3600;
+       mins = uptime / 60;
+       secs = uptime % 60;
+       snprintf(buf, buflen, "up %u days %02u:%02u:%02u",
+           days, hrs, mins, secs);
 }
 
 

Reply via email to