clock format [clock seconds] -format "%A %B %d, %Y %I:%M %p"produces "Wednesday November 05, 2003 03:48 PM".
Today I was writing code to run only on Sunday. Again as with most languages, one of the format options is to represent the day of the week as an integer. Languages vary somewhat on this, sometimes Sunday=0 and sometimes Sunday=1. This is all well documented and good programmers will put helpful comments to remind future readers of their code.
Tcl has a very powerful command "clock scan" that lets you create new dates using a wide variety of date formats, e.g. "2003-11-05", "tomorrow", "2 days ago"... or "Sunday". So, rather than hoping that I properly remember that Sunday is 0 or 1, I did the following:
Internally, tcl will turnset weekday [clock format [clock seconds] -format "%w"] set sunday [clock format [clock scan "Sunday"] -format "%w"] if {$weekday == $sunday} { puts "yes, it's Sunday" } else { puts "no, it isn't Sunday" }
[clock scan "Sunday"]
into a date representing midnight next Sunday which is a reasonable guess as to what somebody would want. For my use here, I don't really care if it's last Sunday, next Sunday or Super Bowl Sunday. I just care that it's a Sunday.
No comments:
Post a Comment