Version 8 of ClockDemo

Updated 2015-03-12 00:21:11 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


}

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

  set Prog(title)   "ClockDemo"
  set Prog(version) "v0.05"
  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"
  }


 ### 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; show0} }
  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,t1:change time, x,y, show0,show1,  e:exit"
  }

  proc t1 {} { 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 .}

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

 ### EOF ###

Output

ClockDemo v0.05
--
clock seconds: 567951255          TimeDate: 1987-12-31  13:14:15

Formatting codes for date:
%B  December                      Name of the month, full name,    localized
%b  Dec                           Name of the month, abbreviation, localized
%C  19                            Century, Indo-Arabic numerals
%D  12/31/1987                    Date, as in %m/%d/%Y
%d  31                            Number of the day of the month, two decimal digits
%e  31                            Number of the day of the month, one (with a leading blank) or two decimal digits
%G  1987                          Four-digit year number for use with the week-based ISO8601 calendar
%g  87                            Two-digit year number, for use with the week-based ISO8601 calendar
%h  Dec                           Name of the month, as in %b
%J  2447161                       Julian Day Number, since 1 January, 4713 BCE.
%j  365                           Day of the year (001-366), three-digits
%m  12                            Number of the month (01-12) with two digits
%N  12                            Number of the month (1-12) one (with a leading blank) or two digits
%P  pm                            Part of the day, am or pm, localized
%p  PM                            Part of the day, AM or PM, localized
%U  52                            Ordinal number of the week of the year (00-53). (obsolete)
%V  53                            Number of the ISO8601 week as a two digit number (01-53).
%W  52                            Week number (00-53) within the year
%x  12/31/1987                    Date, localized
%Y  1987                          Year, four digits (1938-2037)
%y  87                            Year, two digits  (1938-2037)

Formatting codes for time:
%+  Thu Dec 31 13:14:15 CET 1987  TimeDate, as in %a %b %e %H:%M:%S %Z %Y
%c  Thu Dec 31 13:14:15 1987      Date and time of day, localized
%H  13                            Hour of the day (00-23), on a 24-hour clock.
%I  01                            Hour of the day (12-11), on a 12-hour clock.
%k  13                            Hour of the day (00-23), one- or two-digits, 24-hour clock.
%l   1                            Hour of the day (12-11), one- or two-digits, 12-hour clock.
%M  14                            Number of the minute of the hour (00-59) with two digits.
%R  13:14                         Time of the day, on a 24-hour clock, localized
%r  01:14:15 pm                   Time of the day, on a 12-hour clock, localized
%S  15                            Second of the minute (00-59).
%s  567951255                     Clockseconds as decimal integer
%T  13:14:15                      Time, as in %H:%M:%S.
%X  13:14:15                      Time, localized

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).

Some questions:

}