Zeller's Congruence Script Bombs

Quote:

> This program uses Zeller's Congruence to calculate the Day of the Week.

> I grabbed it off the net a while back. It has been working fine,

> but I now see that it bombs on January and February--can anyone help?

> ....

> sub weekday {

> local($y,$m,$c,$yy,$z);

> $y = $year;

> $m = ($month + 10) % 12;

^^^^^^^^^^^^^^^^^^^^^^^^ WRONG!

Quote:

> $y-- if ($m > 10);

> $c = int ( $y / 100 );

> $yy = $year % 100;

> $z = ( int ( (26*$m - 2)/10) + $day + $yy + int($yy/4) +

> int ($c/4) - 2 * $c ) % 7;

> return $wday[$z];

> }

The problem is in the computation of the adjusted month. Zellers

Congruence is based the old calendar, where the year begins in March

(hence September was the nineth month and October the tenth).

Thus, January and February are considered to be the eleventh and

twelfth months of the preceding year.

Unfortunately, the expression above yields the numbers 11 and 0 for

January and February, rather than 11 and 12 as it should. The

following expression should yield the correct results:

$m = ($month + 9) %+12 - 1;

It would appear that the original author forgot to make the month

zero-based before doing modulo arithmetic on it. I.e., the

expression is really:

$m = (($month - 1) + 10) % 12 + 1;

The rest of the algorithm appears to be correct.

--

Gary Puckering Cognos Incorporated

VOICE: (613) 738-1338 x3374 P.O. Box 9707