A long while back, after finding these:
http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
http://www.smart.net/~mmontes/ushols.html
I put together the following subs. Please note that I add or subtract 3 in the conversion routines. This allows a modulo function to return the day of the week with 0 equaling Sunday. As in:
$DoW = $gdate % 7;
I humbly submit the following for review...
#===================================================================
# d2g() Convert year, month, and day to date factor G.
sub d2g {
my($y, $m, $d) = @_;
my $g;
$m = ($m + 9) % 12;
$y = $y - int($m/10);
$g = 365*$y + int($y/4) - int($y/100) + int($y/400) + int(($m*306
++ 5)/10) + ($d - 1);
#-------
$g += 3;
return $g;
}
#====================================================================
# g2d() Convert date factor G to year, month, and day.
sub g2d {
my($g) = @_;
my($y, $m, $d, $M, $D);
#-----------------------------------
$g -= 3;
#-------
$y = int((10000*$g + 14780)/3652425);
$D = $g - (365*$y + int($y/4) - int($y/100) + int($y/400));
if ( $D < 0 ) {
$y = $y - 1;
$D = $g - (365*$y + int($y/4) - int($y/100) + int($y/400));
}
$M = int((100*$D + 52)/3060);
$m = ($M + 2)%12 + 1;
$y = $y + int(($M + 2)/12);
$d = $D - int(($M*306 + 5)/10) + 1;
return ($y, $m, $d);
}
#====================================================================
# ValidYMD() Returns true if given year, month, and day is valid.
sub ValidYMD {
my($y, $m, $d) = @_;
my($Y, $M, $D) = g2d( d2g($y, $m, $d) );
return $y == $Y && $m == $M && $d == $D;
}