Write a script to list Last Sunday of every month in the given year.
#!/usr/bin/tclsh
#
# Write a script to list Last Sunday of every month in the given year.
#
proc lastDay {month year} {
set days [clock format [clock scan "+1 month -1 day" \
-base [clock scan "$month/01/$year"]] -format %d]
}
proc getLastSunday {baseDate} {
set base [clock scan $baseDate -format "%Y%m%d"]
set day_of_week [clock format $base -format %u]
# Sunday may report as 0 or 7.
# If 7, it is not necessary to adjust.
if {$day_of_week != 7} {
set timestamp [clock scan "12:00 last sunday" -base $base]
} else {
set timestamp $base
}
return [clock format $timestamp -format "%Y-%m-%d"]
}
if {$argc >= 1} {
set year [lindex $argv 0]
} else {
set year 2022
puts "Set year to 2022."
}
for {set i 1} {$i <= 12} {incr i} {
set lday [lastDay $i $year]
set ltime [format %04d%02d%02d $year $i $lday]
set sday [getLastSunday $ltime]
puts $sday
}