Version 1 of Formatting Functions For Time/Date

Updated 2018-06-08 06:08:28 by CecilWesterhof

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 -1 as default and created a function to convert the -1:

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

Then I created the following three procs.

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

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

proc getTimeStr {{thisTime -1}} {
    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.