Re: Y2k compliance

Tim Smith (
Sat, 5 Dec 1998 17:58:34 -0800 (PST)

On Sat, 5 Dec 1998, Rik van Riel wrote:
> Oww, I don't believe this!
> #define leapyear(year) (!(year % 4) && ((year % 100) || \
> !(year % 400)))
> I hope this matter is settled now, once and for all
> (well, until we need to define a new calender due to
> the earth's rotational speed slowing down).

You've got it right mathematically, but your C code is asking for trouble.
Someone is likely to do this:

struct tm * tp;
if ( leapyear(1900+tp->tm_year) )

which won't work. (% has higher precedence that +). Throw in parens around
all the occurences of "year" in the definition, and you'll be much happier.

BTW, if the leap year test is not in an inner loop somewhere, so it doesn't
have to be blazingly fast, I think one can make a good case for letting the
standard library figure it out for you:

int leapyear(int year)
struct tm t = {0,0,0,29,1,1900+year,0,0,0};
return t.tm_mon == 1;

--Tim Smith

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
Please read the FAQ at