Here’s the code to check if the date is a holiday.
# Memorial Day – (Last Monday in may)
# Labor Day – (First Monday in September)
# Thanksgiving – (4th Thursday in November)
# New Year’s Day – (January 1st)
# Independence Day – (July 4th)
# Christmas Eve – (December 24th)
# Christmas Day – (December 25th)
proc vmc_holidays { year } {
set res {}
# Memorial day, labor day, Thanksgiving
foreach { month dow lim ind } {
05 1 31 end
09 1 30 0
11 4 30 3
} {
set s2 {}
# get the numeric day of the week for the first of the month
set s [clock format [clock scan [format “%s%s01” $year $month]] -format %w]
# find the day of month of the first monday or thurs
set day [expr (7+$dow-$s)%7+1]
while { $day <= $lim } {
# add the date of each matching day of the week in the month to a list
lappend s2 [format “%s%s%02d” $year $month $day]
incr day 7
}
# pick out just date specified by “ind” above
lappend res [lindex $s2 $ind]
}
# new years, independence, christmas & eve, new years observed
foreach x { 0101 0704 1225 1231 } {
set d “$year$x”
set s [clock scan $d]
set dow [clock format $s -format %a]
if { $x eq “1231” && $dow ne “Fri” } {
# new years day observed on Fri, 12/31
continue
}
if { $x eq “1225” } {
# compute where christmas and christmas eve fall
if { $dow eq “Sat” } {
lappend res [clock format [clock add $s -2 days] -format %Y%m%d]
set s [clock add $s -1 day]
} elseif { $dow eq “Sun” } {
lappend res [clock format [clock add $s -2 days] -format %Y%m%d]
set s [clock add $s 1 day]
} elseif { $dow eq “Mon” } {
lappend res [clock format [clock add $s 1 days] -format %Y%m%d]
} else {
lappend res [clock format [clock add $s -1 days] -format %Y%m%d]
}
} elseif { $dow eq “Sat” } {
if { $x eq “0101” } {
# this was observed last year
continue
}
set s [clock add $s -1 day]
} elseif { $dow eq “Sun” } {
set s [clock add $s 1 day]
}
lappend res [clock format $s -format %Y%m%d]
}
return [lsort $res]
}
proc is_vmc_holiday { date } {
# see if the first 8 digits of the date or date/time stamp are in the list of holidays generated by the year portion of the date
return [expr [lsearch -exact [vmc_holidays [string range $date 0 3]] [string range $date 0 7]] >= 0]
}
-
This reply was modified 6 months ago by David Barr.