calculating leap years

Bob Devine devine at vianet.UUCP
Tue Oct 7 12:00:34 AEST 1986


  First I need to correct myself.  I wrote that a test for divisiblity
of the year by 4000 should be used accommodate the difference between
the Gregorian calendar and the true tropical year.  Wrong.  There is
currently not a leap year correction accepted beyond the 400-year rule
even though a straight line extrapolation for the Gregorian minus the
tropical year leads to an extra leap day in about 3000 years.  At the
level of difference (365.2425 - (approx.)365.2422), there is too much
"noise" caused by the varying earth rotation.  Thanks to Henry
Spencer for making me double check.

  That doesn't mean it wouldn't be better to use the rule of tossing
out one leap year every 128 years (=365.24219) instead of three every
four hundred years....


  In reply to fgd3 at jc3b21.UUCP (Fabbian G. Dufoe) who writes:
> It is particularly galling to see a correct algorithm criticized as
> overkill when it is as simple and short as the above code segment.  There
> may be a justification for writing code that only works part of the time if
> the fix is costly and difficult.  However, it should be a general rule that
> an algorithm which works in all cases is preferred over one that only works
> in most cases.
> Instead of panning someone's code because he has written it more correctly,
> one should adopt the improved algorithm with gratitude.

  You are missing the point: I wrote that doing the simple check for
divisibility by four is sufficient for most programs.  I would bet that
99% of programs don't need to handle dates with precision outside of
+/-20 years from the current date.  Geneology programs might want more.
Astronomical programs almost certainly do.  In the same sense, not all
arithmetic calculations need be done in double precision.

  That algorithm for the Gregorian leap years DOES NOT work in all cases.
It does only after a country has adopted that calendar.  Japan, for
instance, didn't adopt it until 1873.  USSR did so in 1918.  [I wonder if
that means its "October Revolution" really falls in the Gregorian November 
because of the 13 days its calendar 'lost'?  I'll check.]

  I'll post to mod.sources a program to correctly handle leap years for
a large range of years.  It will handle the problem of different countries
switching calendars (somewhat).  Expect it in a coupl'a weeks.

Bob Devine



More information about the Comp.lang.c mailing list