]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
last: don't show negative time
authorcoastal-hiker <coastal-hiker@users.noreply.github.com>
Mon, 26 Jun 2017 16:40:28 +0000 (18:40 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 10 Jul 2017 08:02:38 +0000 (10:02 +0200)
Under strange circumstances, the output of command 'last reboot'
showed the last time as a negative time, with both the hours and the
mins value having a minus sign. Example, taken from my workstation:

$last reboot
[...]
reboot   system boot  4.4.0-79-generic Wed Jun 14 09:20 - 07:33  (-1:-47)
[...]

I am aware this should happen only infrequently. Nevertheless, I
propose a more robust behaviour: show a minus sign only for the most
significant value (days or hours) and show the rest always as
positive. In the special case of ((secs < 0) && (secs >= -59)), print
mins as "-00".

Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/last.c

index 1218a3018d15c7eeedc6e681ae66d4c0370c5dbd..ad70b38fb14ab7b42d353f6d61e7688cc9fd6c3e 100644 (file)
@@ -435,7 +435,7 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
                errx(EXIT_FAILURE, _("preallocation size exceeded"));
 
        /* log-out time */
-       secs  = logout_time - utmp_time;
+       secs  = logout_time - utmp_time; /* Under strange circumstances, secs < 0 can happen */
        mins  = (secs / 60) % 60;
        hours = (secs / 3600) % 24;
        days  = secs / 86400;
@@ -454,9 +454,13 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
                        sprintf(length, "running");
                }
        } else if (days) {
-               sprintf(length, "(%d+%02d:%02d)", days, hours, mins);
+               sprintf(length, "(%d+%02d:%02d)", days, abs(hours), abs(mins)); /* hours and mins always shown as positive (w/o minus sign!) even if secs < 0 */
+       } else if (hours) {
+               sprintf(length, " (%02d:%02d)", hours, abs(mins));  /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
+       } else if (secs > 0) {
+               sprintf(length, " (%02d:%02d)", hours, mins); 
        } else {
-               sprintf(length, " (%02d:%02d)", hours, mins);
+               sprintf(length, " (-00:%02d)", abs(mins));  /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
        }
 
        switch(what) {