logoalt Hacker News

silisiliyesterday at 6:47 PM4 repliesview on HN

At this risk of me feeling stupid, could you briefly explain the benefit of this?


Replies

kccqzyyesterday at 6:51 PM

I just added a link to the code with a brief comment. Basically, it simplifies the leap year date calculation. If February is the last month of the year, then the possibly-existing leap day is the last day of the year. If you do it the normal way your calculations for March through December need to know whether February is a leap year. Now none of that is needed. You don’t even need explicit code to calculate whether a given year is a leap year: it’s implicit in the constants 146097, 36524, and 1461.

show 2 replies
da_chickenyesterday at 6:48 PM

It's easy to know what day of the year it is because leap days are at the end.

3eb7988a1663yesterday at 7:36 PM

Not so relevant, but some fun history, the Roman calendar did start in March, so tacking on the leap years was done at the finale. This also meant that the root of the words - the "oct" in october means 8 was also the eighth month of the year.

zimpenfishyesterday at 9:05 PM

As well as the leap year stuff people have mentioned, there was something else that I've got a vague memory of (from an old SciAm article, IIRC, which was about using March as the first month for calculations) which pointed out that if you use March as 0, you can multiple the month number by (I forget exactly what but it was around 30.4ish?) and, if you round the fraction up, you get the day number of the start of that month and it all works out correctly for the right 31-30-31 etc sequence.