Calendar Functions (simpler lea

karl at haddock karl at haddock
Thu Sep 18 23:54:00 AEST 1986


vianet!devine (Bob Devine) writes:
>Dave Lewis writes:
>>   if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
>
>While this works, it is overkill.  Unless you believe that your
>code will make it to the year 2100, a simple test for divisibility
>by 4 is sufficient.

It looks like you're assuming the test is for the current year.  For a
general-purpose algorithm (as per the original request), Dave's code is
correct.  If the input is a 32-bit signed integer measuring seconds since
1970 (a common implementation of time_t), then the range is contained in
(1900,2100) and the simpler algorithm works -- but this is probably not a
good assumption to make; an unsigned long will reach past 2100.

>If you really want an algorithm for all years,
>you then need to also test for years divisible by 4000...

I believe the quadrimillennium correction was never officially adopted.
It's not accounted for in cal(1).

Karl W. Z. Heuer (ima!haddock!karl; karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list