This is one for the pedants, and those who just plain like to be different. Which includes me, I'm afraid.

It's a clock utility, which displays the time and date in a nice, clean format which is correct with respect to both numerical and ASCII collation ordering ('YYYYMMDD.hhmmss'), and also displays the time of day as a decimal fraction of the day since midnight. Not that that's an inherently useful thing in itself, but it does satisfy the aesthetic criteria that it be all of simple, consistent and correct, something which the 12-hour and 24-hour clock systems can't claim. And if it's there whenever you glance at the time, you may end up subconsciously osmosing an understanding of the time in decimal.

It's in tk, so should run on any of the supported platforms (I've tried it on Linux, Solaris and Windows 98. Oh, and Windows 2000)

Cut and paste, Share and enjoy:


#!/usr/bin/env wish
# clock.tk
# Display the date and time in conventional format, and the time of
# day in decimal

# Set background to your preference. The default background colour
# given by wish is far, far too bright for my eyes...
set bg \#8c8c90


# Date and current time without all that annoying syntax
# Though it's useful to mark the mod-6,s mod-3s and mod-4s is some way?
proc date_time { } {
  set date_time [clock format [clock seconds] -format "%Y%m%d.%H%M%S"]
  return $date_time
}

# Get current time as a decimal fraction of the day.
proc dec_time { } {

  # To save making multiple calls to clock, or faffing about with
  # seconds-past-the-epoch, local time and leap-seconds, make the date
  # format an expression for the number of seconds since midnight.
  # note the hack that each % substitution has a space before it: this
  # is because the default date format gives a leading '0' if the item
  # is in the range 0..9; since we want to evaluate this expression,
  # we have to trim this off otherwise expr will interpret it as an
  # octal number, and have a fit when it gets to 08.
  set secs_expr [
    clock format [clock seconds] -format "( %H * 60 + %M ) * 60 + %S"
  ]
  regsub -all " 0" $secs_expr "" secs_expr

  # Number of seconds into the day.
  set secs [expr $secs_expr]
  
  # Fraction of the day multiplied up by 1e5. Factored down by 10 on
  # each side here to stay in 2^32 range...
  set frac [expr 1000 * $secs / 864]
  return [format "0.%05d" $frac]
}

# Periodically update decimal time display
proc dectime_update { widget } {
  $widget configure -text [dec_time]
  after [expr [expr 24 * 60 * 60 / 100]] dectime_update $widget
}

# Periodically update conventional time display
proc date_update { widget } {
  $widget configure -text [date_time]
  after [expr 1000] date_update $widget
}

# Create a widget to display a clock time
proc clockwidget { widget } {
  global bg
  pack [
    label $widget \
      -bg "$bg" \
      -font [ font create \
                -family lucidatypewriter \
		-size 10 -weight normal \
		-slant roman ]
  ] -side left -ipadx 8 -ipady 6 -fill both -expand 1
  return $widget
}

# Create the display:
# .f: frame to contain both clocks.
#  +.datetime: date/time widget
#  +.dectime: decimal time widget
pack [frame .f -borderwidth 1 -bg $bg ] -fill both -expand 1
date_update [clockwidget .f.datetime]
dectime_update [clockwidget .f.dectime]

Log in or register to write something here or to contact authors.