Why is there a single space before the output of `uptime`?












0















Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime and it got me wondering why it was there in the first place.



An example from a Debian Stretch EC2 instance:



admin@ip-88-88-88-88:~$ uptime
22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
admin@ip-88-88-88-88:~$


I thought maybe it was padding, but for what? A three-digit hour for the clock?










share|improve this question



























    0















    Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime and it got me wondering why it was there in the first place.



    An example from a Debian Stretch EC2 instance:



    admin@ip-88-88-88-88:~$ uptime
    22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
    admin@ip-88-88-88-88:~$


    I thought maybe it was padding, but for what? A three-digit hour for the clock?










    share|improve this question

























      0












      0








      0








      Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime and it got me wondering why it was there in the first place.



      An example from a Debian Stretch EC2 instance:



      admin@ip-88-88-88-88:~$ uptime
      22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
      admin@ip-88-88-88-88:~$


      I thought maybe it was padding, but for what? A three-digit hour for the clock?










      share|improve this question














      Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime and it got me wondering why it was there in the first place.



      An example from a Debian Stretch EC2 instance:



      admin@ip-88-88-88-88:~$ uptime
      22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
      admin@ip-88-88-88-88:~$


      I thought maybe it was padding, but for what? A three-digit hour for the clock?







      linux debian uptime






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      smitellismitelli

      15318




      15318






















          3 Answers
          3






          active

          oldest

          votes


















          1














          Because the code explicitly puts it there:



          pos = sprintf(buf, " %02d:%02d:%02d ",
          realtime->tm_hour, realtime->tm_min, realtime->tm_sec);


          The uptime executable calls a function called print_uptime(). This function lives in proc/whattime.c in the procps sources. The function just outputs a string built by sprint_uptime() in the same C source file which constructs the output string bit by bit.



          The first bit that is added to the string is added with an initial space, as shown above.



          The space has been there since at least 2002 in the procps implementation of uptime.



          Note that uptime -p does not output a space:



          $ uptime -p
          up 8 weeks, 8 hours, 41 minutes

          $ uptime --version
          uptime from procps-ng 3.3.12


          As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.



          The uptime utility on BSD systems does not follow the same output format:



          $ uptime
          11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00





          share|improve this answer

































            0














            Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c



            As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:



            fprintftime (stdout, _(" %H:%M:%S  "), tmn, 0, 0);

            printf (_("up %2d:%02d, "), uphours, upmins);

            printf (ngettext ("%lu user", "%lu users", select_plural (entries)),

            printf (_(", load average: %.2f"), avg[0]);





            share|improve this answer
























            • @jsotola No, the spaces are explicit in the calls to printf().

              – Kusalananda
              5 hours ago



















            0














            It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.



            if (tim >= 60) {
            printf("%3d:", tim/60);
            didhrs++;
            } else {
            printf(" ");
            }
            tim %= 60;
            if (tim > 0 || didhrs) {
            printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
            } else {
            printf(" ");
            }


            The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.






            share|improve this answer


























            • It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.

              – JdeBP
              3 hours ago











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507831%2fwhy-is-there-a-single-space-before-the-output-of-uptime%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Because the code explicitly puts it there:



            pos = sprintf(buf, " %02d:%02d:%02d ",
            realtime->tm_hour, realtime->tm_min, realtime->tm_sec);


            The uptime executable calls a function called print_uptime(). This function lives in proc/whattime.c in the procps sources. The function just outputs a string built by sprint_uptime() in the same C source file which constructs the output string bit by bit.



            The first bit that is added to the string is added with an initial space, as shown above.



            The space has been there since at least 2002 in the procps implementation of uptime.



            Note that uptime -p does not output a space:



            $ uptime -p
            up 8 weeks, 8 hours, 41 minutes

            $ uptime --version
            uptime from procps-ng 3.3.12


            As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.



            The uptime utility on BSD systems does not follow the same output format:



            $ uptime
            11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00





            share|improve this answer






























              1














              Because the code explicitly puts it there:



              pos = sprintf(buf, " %02d:%02d:%02d ",
              realtime->tm_hour, realtime->tm_min, realtime->tm_sec);


              The uptime executable calls a function called print_uptime(). This function lives in proc/whattime.c in the procps sources. The function just outputs a string built by sprint_uptime() in the same C source file which constructs the output string bit by bit.



              The first bit that is added to the string is added with an initial space, as shown above.



              The space has been there since at least 2002 in the procps implementation of uptime.



              Note that uptime -p does not output a space:



              $ uptime -p
              up 8 weeks, 8 hours, 41 minutes

              $ uptime --version
              uptime from procps-ng 3.3.12


              As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.



              The uptime utility on BSD systems does not follow the same output format:



              $ uptime
              11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00





              share|improve this answer




























                1












                1








                1







                Because the code explicitly puts it there:



                pos = sprintf(buf, " %02d:%02d:%02d ",
                realtime->tm_hour, realtime->tm_min, realtime->tm_sec);


                The uptime executable calls a function called print_uptime(). This function lives in proc/whattime.c in the procps sources. The function just outputs a string built by sprint_uptime() in the same C source file which constructs the output string bit by bit.



                The first bit that is added to the string is added with an initial space, as shown above.



                The space has been there since at least 2002 in the procps implementation of uptime.



                Note that uptime -p does not output a space:



                $ uptime -p
                up 8 weeks, 8 hours, 41 minutes

                $ uptime --version
                uptime from procps-ng 3.3.12


                As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.



                The uptime utility on BSD systems does not follow the same output format:



                $ uptime
                11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00





                share|improve this answer















                Because the code explicitly puts it there:



                pos = sprintf(buf, " %02d:%02d:%02d ",
                realtime->tm_hour, realtime->tm_min, realtime->tm_sec);


                The uptime executable calls a function called print_uptime(). This function lives in proc/whattime.c in the procps sources. The function just outputs a string built by sprint_uptime() in the same C source file which constructs the output string bit by bit.



                The first bit that is added to the string is added with an initial space, as shown above.



                The space has been there since at least 2002 in the procps implementation of uptime.



                Note that uptime -p does not output a space:



                $ uptime -p
                up 8 weeks, 8 hours, 41 minutes

                $ uptime --version
                uptime from procps-ng 3.3.12


                As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.



                The uptime utility on BSD systems does not follow the same output format:



                $ uptime
                11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 5 hours ago

























                answered 6 hours ago









                KusalanandaKusalananda

                137k17258426




                137k17258426

























                    0














                    Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c



                    As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:



                    fprintftime (stdout, _(" %H:%M:%S  "), tmn, 0, 0);

                    printf (_("up %2d:%02d, "), uphours, upmins);

                    printf (ngettext ("%lu user", "%lu users", select_plural (entries)),

                    printf (_(", load average: %.2f"), avg[0]);





                    share|improve this answer
























                    • @jsotola No, the spaces are explicit in the calls to printf().

                      – Kusalananda
                      5 hours ago
















                    0














                    Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c



                    As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:



                    fprintftime (stdout, _(" %H:%M:%S  "), tmn, 0, 0);

                    printf (_("up %2d:%02d, "), uphours, upmins);

                    printf (ngettext ("%lu user", "%lu users", select_plural (entries)),

                    printf (_(", load average: %.2f"), avg[0]);





                    share|improve this answer
























                    • @jsotola No, the spaces are explicit in the calls to printf().

                      – Kusalananda
                      5 hours ago














                    0












                    0








                    0







                    Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c



                    As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:



                    fprintftime (stdout, _(" %H:%M:%S  "), tmn, 0, 0);

                    printf (_("up %2d:%02d, "), uphours, upmins);

                    printf (ngettext ("%lu user", "%lu users", select_plural (entries)),

                    printf (_(", load average: %.2f"), avg[0]);





                    share|improve this answer













                    Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c



                    As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:



                    fprintftime (stdout, _(" %H:%M:%S  "), tmn, 0, 0);

                    printf (_("up %2d:%02d, "), uphours, upmins);

                    printf (ngettext ("%lu user", "%lu users", select_plural (entries)),

                    printf (_(", load average: %.2f"), avg[0]);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 5 hours ago









                    maxxvwmaxxvw

                    18818




                    18818













                    • @jsotola No, the spaces are explicit in the calls to printf().

                      – Kusalananda
                      5 hours ago



















                    • @jsotola No, the spaces are explicit in the calls to printf().

                      – Kusalananda
                      5 hours ago

















                    @jsotola No, the spaces are explicit in the calls to printf().

                    – Kusalananda
                    5 hours ago





                    @jsotola No, the spaces are explicit in the calls to printf().

                    – Kusalananda
                    5 hours ago











                    0














                    It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.



                    if (tim >= 60) {
                    printf("%3d:", tim/60);
                    didhrs++;
                    } else {
                    printf(" ");
                    }
                    tim %= 60;
                    if (tim > 0 || didhrs) {
                    printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
                    } else {
                    printf(" ");
                    }


                    The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.






                    share|improve this answer


























                    • It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.

                      – JdeBP
                      3 hours ago
















                    0














                    It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.



                    if (tim >= 60) {
                    printf("%3d:", tim/60);
                    didhrs++;
                    } else {
                    printf(" ");
                    }
                    tim %= 60;
                    if (tim > 0 || didhrs) {
                    printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
                    } else {
                    printf(" ");
                    }


                    The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.






                    share|improve this answer


























                    • It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.

                      – JdeBP
                      3 hours ago














                    0












                    0








                    0







                    It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.



                    if (tim >= 60) {
                    printf("%3d:", tim/60);
                    didhrs++;
                    } else {
                    printf(" ");
                    }
                    tim %= 60;
                    if (tim > 0 || didhrs) {
                    printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
                    } else {
                    printf(" ");
                    }


                    The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.






                    share|improve this answer















                    It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.



                    if (tim >= 60) {
                    printf("%3d:", tim/60);
                    didhrs++;
                    } else {
                    printf(" ");
                    }
                    tim %= 60;
                    if (tim > 0 || didhrs) {
                    printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
                    } else {
                    printf(" ");
                    }


                    The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 1 hour ago

























                    answered 4 hours ago









                    Mark PlotnickMark Plotnick

                    18.6k24067




                    18.6k24067













                    • It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.

                      – JdeBP
                      3 hours ago



















                    • It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.

                      – JdeBP
                      3 hours ago

















                    It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.

                    – JdeBP
                    3 hours ago





                    It has been strftime() with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps was using getopt() and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.

                    – JdeBP
                    3 hours ago


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507831%2fwhy-is-there-a-single-space-before-the-output-of-uptime%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    サソリ

                    広島県道265号伴広島線

                    Setup Asymptote in Texstudio