Version 3 of ClockDemo

Updated 2015-03-08 21:58:04 by miha

if 0 {

General Notes

A demo for all the format groups recognized by the clock scan and clock format commands.

References to programs and pages here :

Notes about working with tcl

  • As I already ranted, the Doku/Help has not enough useful examples.
    So, this demo is how such an example-program could look like, with almost everything copied/pasted directly from "Tcl/Tk Documentation > TclCmd > clock".

Program


}

 # ClockDemo04.tcl - Mike Hase - 2015-03-03
 # http://wiki.tcl.tk/41210

 #package require Tk

  set Prog(title)   "ClockDemo"
  set Prog(version) "v0.04"
  set Prog(date)    "2015-03-03"


 ### clock format-codes:

  # Code Type: Description
  array set fTab {        
    %a  "Wd: Day of the week,   abbreviation, localized"
    %A  "Wd: Day of the week,   full name,    localized"

    %b  "Dm: Name of the month, abbreviation, localized"
    %B  "Dm: Name of the month, full name,    localized"

    %c  "TD: Date and time of day, localized"

    %C  "Dy: Century, Indo-Arabic numerals"

    %d  "Dm: Number of the day of the month, two decimal digits"
    %D  "D_: Date, as in %m/%d/%Y"

    %e  "Dd: Number of the day of the month, one (with a leading blank) or two decimal digits"

    %g  "Dy: Two-digit year number, for use with the week-based ISO8601 calendar"
    %G  "Dy: Four-digit year number for use with the week-based ISO8601 calendar"

    %h  "Dm: Name of the month, as in %b"

    %H  "Th: Hour of the day (00-23), on a 24-hour clock."
    %I  "Th: Hour of the day (12-11), on a 12-hour clock."

    %j  "Dd: Day of the year (001-366), three-digits"
    %J  "D_: Julian Day Number, since 1 January, 4713 BCE."

    %k  "Th: Hour of the day (00-23), one- or two-digits, 24-hour clock."
    %l  "Th: Hour of the day (12-11), one- or two-digits, 12-hour clock."

    %m  "Dm: Number of the month (01-12) with two digits"
    %M  "Tm: Number of the minute of the hour (00-59) with two digits."

    %N  "Dm: Number of the month (1-12) one (with a leading blank) or two digits"
    %p  "D_: Part of the day, AM or PM, localized"
    %P  "D_: Part of the day, am or pm, localized"

    %Q  "X_: (internal use:)"

    %r  "T_: Time of the day, on a 12-hour clock, localized"
    %R  "T_: Time of the day, on a 24-hour clock, localized"

    %s  "T_: Clockseconds as decimal integer"
    %S  "Ts: Second of the minute (00-59)."

    %T  "T_: Time, as in %H:%M:%S."

    %u  "Wd: Number of the day of the week (1=Monday, 7=Sunday)"
    %U  "Dw: Ordinal number of the week of the year (00-53). (obsolete)"

    %V  "Dw: Number of the ISO8601 week as a two digit number (01-53)."
    %w  "Wd: Day of the week, ordinal number  (Sunday=0; Saturday=6)."
    %W  "Dw: Week number (00-53) within the year"

    %x  "D_: Date, localized"
    %X  "T_: Time, localized"

    %y  "Dy: Year, two digits  (1938-2037)"
    %Y  "Dy: Year, four digits (1938-2037)"

    %z  "Z_: Timezone, in hours and minutes east (+hhmm) or west (-hhmm) of Greenwich."
    %Z  "Z_: Timezone as name, localized"

    %+  "TD: TimeDate, as in %a %b %e %H:%M:%S %Z %Y"
  }
  # %f  "X_: (test)"


 ### Display:

  proc show0 {} {
    global cs td  
    set td [clock format $cs  -format "%Y-%m-%d  %H:%M:%S" ]
    puts "--"
    puts "clock seconds: $cs          TimeDate: $td"
  }

  proc show1 {} {
    global cs td  fTab

    puts "\nAll codes, unsorted:"
    foreach {fc descr} [array get fTab] {
      set res [clock format $cs  -format $fc ]
      puts [format "%-3s %-28s  %s" $fc $res $descr]
    }

  }

  proc show2 { select title } {
    global cs td  fTab

    puts "\n$title:"

    set res "xx"
    set i 0
    foreach fc [lsort -dict [array names fTab] ] {
      incr i 1
      set descr $fTab($fc)
      set type  [string range $descr 0 0]
      set desc1 [string range $descr 4 end]
      if {$type == $select} {
        set res [clock format $cs  -format $fc ]
        puts [format "%-3s %-28s  %s" $fc $res $desc1]
      }
    }
  }


 ### Time:

  proc now {} {
    global cs td  fTab

    set cs [clock seconds]
    set td [clock format $cs  -format "%Y-%m-%d  %H:%M:%S" ]
  }


 ### Time/Repeat/Wait:

  proc every {ms body} {
    if 1 $body
    after $ms [list after idle [info level 0]]
  }
  proc run  {} {every 10000 {now; u} }
  proc stop {} {foreach id [after info] {after cancel $id}; }


 ### Test+Debug:

  proc ?    {} { help }
  proc help {} {
    puts "argv0 : $::argv0"
    puts "argv  : $::argv"
    puts "tcl/tk: $::tcl_patchLevel / $::tk_patchLevel"
    puts "now,t:change time, x,y, show0,show1,  e:exit"
  }

  proc t  {} { set ::cs [clock scan {1987-12-31 13:14:15} -format {%Y-%m-%d %H:%M:%S}] }
  proc x  {} { show0; show1 }
  proc y  {} { 
    show0
    show2 "D" "Formatting codes for date"
    show2 "T" "Formatting codes for time"
  }

  proc e  {} { exit }



 ### Main:

   wm title . "$Prog(title) $Prog(version)"
   puts       "$Prog(title) $Prog(version)"

   bind .  <F2>     { console show }
   catch {console show}        ;##
   catch {wm withdraw .}

   t
  #now
   show0
  #show1
   show2 "D" "Formatting codes for date"
   show2 "T" "Formatting codes for time"

 ### EOF ###

Remarks

if 0 {

MiHa 2015-03-03 - It looks like there is no formatting code for the total minutes of the day (0-1440).
Same for seconds (0-86400).
Also, is there an easy way to format long strings to fit into table-columns ?

}