How to create a leap year helper variable

What is a leap year and how do we take account of it in models? You can see the mildly fascinating background here. The key part for us is the below:

By adding an extra day every four years, our calendar years stay adjusted to the sidereal year, but that’s not quite right either. Some simple math will show that over four years the difference between the calendar years and the sidereal year is not exactly 24 hours. Instead, it’s 23.262222 hours. Rounding strikes again! By adding a leap day every four years, we actually make the calendar longer by over 44 minutes. Over time, these extra 44+ minutes would also cause the seasons to drift in our calendar. For this reason, not every four years is a leap year. The rule is that if the year is divisible by 100 and not divisible by 400, leap year is skipped.

Using this we can create a leap year helper variable:

if year % 4 = 0 and (year % 100 != 0 or year % 400 = 0) then 366 else 365 will return the number of days in the year for each period. You can replace with then 1 else 0 if you want a leap year flag.

Using this you can model leap years more accurately and take into account the extra day!

Happy modelling!

2 Likes