Formatting Functions For Time/Date

Created by CecilWesterhof.

I created some procs to easily get some date/time formats.

The first is because I miss the %F format in clock.

# Clock format which recognises %F
proc clockFrmt {time format} {
    clock format $time -format [string map {%F {%Y-%m-%d}} $format]

But I find it also handy as a shorter form for clock with format. ;-)

I want as default the current time. This is not really possible. That is why I use NOW as default and created a function to convert the NOW:

# When a proc has no value for thisTime convert it to current time.
proc convertDefaultTime {thisTime} {
    if {$thisTime == NOW} {
        return [clock seconds]
    return $thisTime

Martyn Smith For me I would use the default value NOW and not -1 it makes the code easier to read.

Done, thanks for the tip.

Then I created the following three procs.

proc getDateStr {{thisTime NOW}} {
    set thisTime [convertDefaultTime $thisTime]
    clockFrmt $thisTime "%F"

proc getDateTimeStr {{thisTime NOW}} {
    set thisTime [convertDefaultTime $thisTime]
    clockFrmt $thisTime "%FT%T"

proc getTimeStr {{thisTime NOW}} {
    set thisTime [convertDefaultTime $thisTime]
    clockFrmt $thisTime "%T"

An example usage:

proc showMessage {message {onlyTime False} {display True}} {
    if {$display} {
        if {$onlyTime} {
            set moment [getTimeStr]
        } else {
            set moment [getDateTimeStr]
        puts [format "%s: %s" $moment $message]

As always: comments, tips and questions are appreciated.