Date functionality

Stephen Clamage steve at taumet.com
Mon Aug 13 01:59:39 AEST 1990


It has been pointed out that there was a potential error in my
(and others') posting of Zeller's Congruence as follows:

The day of the week (0 = Sunday) is given by
  W = ((26*M - 2) / 10 + D + Y + Y/4 + C/4 - 2*C) % 7
  where
	C is the century
	Y is the last two digits of the year
	D is the day of the month
	M is a special month number, where Jan and Feb are taken as
	  month 11 and 12 of the previous year
  and each division is truncating division, and cannot be combined.

The algorithm uses the modulo operator '%' which is not the same
as the '%' operator in C, which is instead a remainder operator.
The result in C of the '%' operator on a negative value is
implemenation-dependent, even in ANSI C.  So we must ensure that
we do not apply '%' to a negative number.

The properties of modulo arithmetic are such that we may apply it
individually to each term of a sum.  To guarantee that our sum
is positive, we can find (2*C)%7, which is subtracted instead of 2*C,
and add an additional 7 before the final %7.

  W = ((26*M - 2) / 10 + D + Y + Y/4 + C/4 - (2*C)%7 + 7) % 7

As has also been noted:
1.  The year 1752 had a funny calendar, so use only for year >= 1753.
    (September had 11 days removed because the calendar had fallen
    that far out of sync with the true seasons.)
2.  Jan and Feb are treated as in the previous year, so in this case
    subtract 1 from the year.  Compute the century after computing
    the year. (For Mar - Dec, just subtract 2 from the month.)
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list