#!/usr/bin/perl use strict; # print &DOW ('20040712'), "\n"; #---------------------------------------------------------------------- # DOW: # Find the day of the week for any given date using Zeller's congruence. # There's a Pascal version here (if it helps any) : # http://atlas.csd.net/~cgadd/knowbase/DATETIME0027.HTM # Validation of the input date could be more anal. # Parameters: # $Date YYYYMMDD # Return: # -1 Parameter error # 0 -> 6 Day of week (Unix style) # #---------------------------------------------------------------------- sub DOW { my $Date = shift; # YYYYMMDD my $Century; # Century part of the year my $Day; # Day part of date my $Holder; # my $Month; # Month part of date my $Year; # Year in century part of date if (length ($Date) != 8) { return -1; } $Century = int (substr ($Date, 0, 2)); $Year = int (substr ($Date, 2, 2)); $Month = int (substr ($Date, 4, 2)); $Day = int (substr ($Date, 6, 2)); if ($Day < 1 || $Day > 31 || $Month < 1 || $Month > 12) { return -1; } if ($Month < 3) { $Month += 12; if ($Year > 0) { $Year--; } else { $Year = 99; $Century--; } } # # Zeller's black magic, first described it in "Acta Mathematica" #7, # Stockhold, 1887 # $Holder = $Day + int ((($Month + 1) * 26) / 10) + $Year + int ($Year / 4) + int ($Century / 4) - $Century - $Century; while ($Holder < 0) { $Holder += 7; } $Holder = $Holder % 7; # # Wrap Saturday to be the last day and start from zero. # if ($Holder == 0) { $Holder = 7; } $Holder--; return $Holder; }