Subject: fun with ksh, showing the current time, and daylight savings time transitions....
To: NetBSD User's Discussion List <netbsd-users@NetBSD.ORG>
From: Greg A. Woods <>
List: netbsd-users
Date: 10/27/2002 14:34:24
For many many years now I've been putting the current time in my Ksh
prompt using everything but the "trap" and "kill" commands below.  I
added those today because I finally got around to debugging why I had to
re-source my ~/.kshrc file after every DST transition.  I'm not sure
I'll keep the "trap" and "kill" commands as their presence means I'll
have an extra shell and sleep command running for every window I have
open, and the trap may have side effects for other child processes that
I've not yet encountered in just a short bit of testing.  However they
do make it possible for you to almost always have the current local time
in your shell prompt without any manual intervention no matter how long
you stay logged in!  ;-)

# initialize SECONDS to the number of seconds since the last
# local wall-clock midnight hour
alias set_secs_to_midnight='SECONDS="$(date '"'"'+3600*%H+60*%M+%S'"'"')"'

# We wouldn't normally have to reset $SECONDS since our calculations
# for hours and minutes only need to find the remainder values for the
# seconds/unit since any epoch.  However wall-clock time will shift
# when daylight savings time transitions happen.  Resetting $SECONDS
# once per hour is probably overkill but will do the job -- it only
# really needs to be done once a day at most in order to catch any
# number of daylight savings time transitions.
# The first one in the loop should only sleep to the top of the hour,
# but calculating that would really be overkill!
# The extra sub-shell is to prevent the main shell from tracking the
# kill job...
trap 'SECONDS="$(date '+3600*%H+60*%M+%S')"; ( { sleep 3600; kill -14 $$; } & )' 14

# start the cycle
kill -14 $$

# expressions to calculate hours, minutes, and seconds since midnight
# given the number of $SECONDS (we ignore the number of days that may
# have passed since that midnight hour)

# We'll be wanting zero-filled 2-digit expansion for our hours and
# minutes variables
typeset -Z2 _h _m

# a magic expression that evaluates the above expressions to set the two
# variables we've configured specially above, and then expands those two
# variables in a standard "HH:MM" 24-hr format to show the current time.

PS1="$_time $PS1"

								Greg A. Woods

+1 416 218-0098;            <>;           <>
Planix, Inc. <>; VE3TCP; Secrets of the Weird <>