[conspire] clock and ... Re: Beat the heat: Come to CABAL

Michael Paoli Michael.Paoli at cal.berkeley.edu
Wed Sep 18 19:27:26 PDT 2019


> From: "Rick Moen" <rick at linuxmafia.com>
> Subject: Re: [conspire] Beat the heat:  Come to CABAL
> Date: Sat, 14 Sep 2019 13:03:38 -0700

... And a lovely time was had by all!  :-)
I brought frozen fruit pops - mango, coconut, and strawberry.
May not have been hot enough to "require" them ;-)
But they were yummy in any case ...
along with lots of other yummy delectables.

Someone asked about about a wee bit 'o clock display
script/program I wrote, which I'd left running displaying
on a screen/window.  Anyway, they'd seen something on
The Internet, roughly like - as I (partially) recall:

while true
do
      maybe clears screen first - or after - or not
      something or another that displays the time
done

And among other things the effectively said it burns lots of CPU.
And I essentially remarks, not at all surprisingly so, as there's
nothing in the loop to delay it from proceeding to the next iteration
of the loop - so essentially notwithstanding waiting on I/O (notably
output - but local screen is pretty fast on that), or being preempted
by other tasks, it'll burn as much (at least of a single thread of) of
a CPU as the OS will give to it - not very efficient ... and particularly
wasteful too if that time information being displayed only has granularity
to the second - then why refresh that on the display thousands or more
times per second?

As I oft say of The Internet - yes, lots of good information there,
but also, about 20% of it is anywhere from rather incomplete or flawed,
to downright totally incorrect and/or dangerous.

Anyway, my wee 'lil program/script ... I just added fair bit more comments
to make it fair bit more comprehensible.  Most 'o the rest of it is
left as exercise for the the interested readers.

$ (cd ~/bin && expand -t 4 < bclock)
#!/bin/sh

# display a big ASCII clock, update every second, and reasonably
# feasibly close to on the second.

# vi(1) :se tabstop=4

# Use temporary file for tracking sub-second accuracy
# our filesystem for that happens to support that.
t=$(mktemp) || exit

trap "rm '$t'; trap - 0; exit" 0 1 2 3 15 # cleanup when we're done

while :
do
     set -- $(date +%r) &&
     clear && # clear screen and
     banner "$1" # display HH:MM:SS large
     >"$t" # "touch" our temporary file

     # Examine our mtime on the file, determine any fractional part
     # beyond the second
     over=$(stat -c '%y' "$t" | sed -ne 's/^[^.]*\(\.[0-9][0-9]*\).*$/\1/p')

     # If we got over (above), subtract that from 1,
     # that's then the fractional part of a second we should wait to
     # almost exactly land us atop the turning over of the next second
     [ -n "$over" ] && sleep=$(echo 1-"$over" | bc -l)
     # If we didn't get that calculated fractional bit, fallback to 1
     [ -n "$sleep" ] || sleep=1

     # Wait (sleep) our calculated (or fallback of 1) second(s) -
     # nomially a franctional part of a second.
     sleep "$sleep"

     # Alternative commented out code, that would wait until about the
     # roll-over of the 1/4 minute - essentially each 15 seconds
     # s=$(expr 15 - \( $(date +%S) % 15 \))
     # [ "$s" -ne 0 ] || s=15
     # sleep "$s"
done
$

And, semi-random hint, in case one might not have it installed:
$ type banner
banner is hashed (/usr/bin/banner)
$ dpkg -S /usr/bin/banner
sysvbanner: /usr/bin/banner
$ man banner | col -b | expand | grep '^[       ]*[^    ]'
BANNER(1)                   User's Reference Manual                  BANNER(1)
NAME
        banner - print large banner
SYNOPSIS
        banner text
DESCRIPTION
        banner prints out the first 10 characters of text in large letters.
SEE ALSO
        banner(6).
Debian                         February 4, 1997                      BANNER(1)
$
The above, also not to be confused with (what some might also have installed):
printerbanner
wbanner
/usr/games/{,bin/}banner




More information about the conspire mailing list