← Back to context

Comment by hairtuq

3 months ago

The article leaves out optimizing is_leap_year. Here is an interesting version that is correct (in the Proleptic Gregorian calendar) for 0 <= year <= 102499 and takes only 3 instructions:

    ((year * 1073750999) & 3221352463) <= 126976

How this works is surprisingly complex, I am thinking of writing a whole blog post about it...

I'd read that blog post.

Historical minor quibble - formula correct for 1582 <= year <= 102499 (well, it's actually correct down to 1201, but the gregorian calendar didn't start until oct 1582... and then you get into the quagmire about when each country adopted the gregorian calendar, if they ever did)

  • The GP said the proleptic Gregorian calendar, so they are correct. (Assuming their formula is correct, which I haven't checked.) If they had said, "here's how humans in Western civilization could calculate the leap year of their current calendar back to year zero," then your quibble (and probably others) would be warranted. :-)

What I need for the library is something that works for all values, not being range-limited.