package Weekday; use strict; use vars qw(@ISA @EXPORT_OK); use Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(&day_of_week); =head2 day_of_week($year, $month, $date) Return the day of the week (0 == Sunday ... 6 == Saturday) for the given date. C<$year> should have four digits; C<$month> and C<$date> both start at 1, not zero. Algorithm is from the CRC Handbook of Mathematics, 30th Ed, page 738. =cut sub day_of_week { my ($year, $month, $date) = @_; $month -= 2; if ($month <= 0) { -- $year; $month += 12; } my $cent = int($year/100); $year %= 100; # print STDERR "year=$year, cent=$cent, date=$date, month=$month\n"; my $dow = ($date + int(2.6*$month - 0.2) - 2*$cent + $year + int($year/4) + int($cent/4)); $dow += 7 while $dow < 0; $dow %= 7; return $dow; } 1;