Leap Year Test
Overview
Gregorian leap years satisfy a three-part test: divisible by four, except years divisible by one hundred, unless also divisible by four hundred. The year 1900 failed the exception and had 365 days; 2000 passed the final clause and had 366. The rules exist because the tropical year is about 365.2422 days, so the simple Julian quadrennial leap overshoots by roughly 11 minutes annually — around three days every four centuries. Removing three leap days per four hundred years brings the calendar mean to 365.2425 days, an error of about 27 seconds a year or one day in roughly 3,200 years. Pope Gregory XIII promulgated the correction in 1582, deleting ten accumulated days; Britain and its colonies waited until 1752 and deleted eleven. In code, evaluating the modulo-400 clause first avoids the classic century bug.
Variables
| Symbol | Name | Unit | Description |
|---|---|---|---|
| $y$ | Year | year | The Gregorian calendar year under test. |
| $leap$ | Leap indicator | boolean | True when the year contains 29 February. |
Three Rules in One Expression
A Gregorian year is a leap year when it is divisible by four, except that century years must also be divisible by four hundred:
$$leap = (y \bmod 4 = 0) \land (y \bmod 100 \neq 0 \lor y \bmod 400 = 0)$$
The three clauses handle successively rarer cases. Divisibility by four catches the ordinary leap year; the hundred exception removes three of every four century years; the four-hundred exception restores one of them.
Recent Century Years
| Year | Divisible by 4 | Divisible by 100 | Divisible by 400 | Leap |
|---|---|---|---|---|
| 1900 | yes | yes | no | no |
| 2000 | yes | yes | yes | yes |
| 2024 | yes | no | — | yes |
| 2100 | yes | yes | no | no |
The year 2000 satisfied the four-hundred exception, so a generation of software written after 1970 was never tested against a non-leap century year. The next such year is 2100.
Why the Corrections Exist
The tropical year is about 365.2422 days. A plain four-year rule assumes 365.25, overshooting by roughly 11 minutes annually — about three days per four centuries. Removing three leap days per four hundred years brings the average calendar year to 365.2425 days, an error of about one day in 3,200 years.
Derivation & History
The Julian calendar of 46 BCE used the four-year rule alone and so drifted against the equinoxes by about one day per 128 years. By the sixteenth century the vernal equinox had moved ten days from its nominal 21 March, displacing the computation of Easter. The 1582 reform of Pope Gregory XIII deleted those ten days and added the century exceptions, choosing to suppress three leap days per four centuries because 400 is the smallest convenient cycle that reduces the residual error to a day per several millennia.
Worked Examples
Testing 1900 and 2000
- 1900 mod 4 = 0, so the first clause holds
- 1900 mod 100 = 0, so the second clause requires the 400 test
- 1900 mod 400 = 300 ≠ 0, so 1900 is not a leap year
- 2000 mod 400 = 0, so the exception is satisfied and 2000 is a leap year
Result: 1900 has 365 days; 2000 has 366
Edge Cases & Limitations
The century trap: code that tests only divisibility by four returns an incorrect result for 1900 and will do so again for 2100, a class of defect that lay dormant through the whole of the twentieth century's software history.
Proleptic dates: applying the Gregorian rule to years before 1582 gives dates that no contemporary calendar used, so historical records require an explicit calendar declaration.
Leap seconds are unrelated: they correct for irregularities in Earth's rotation and are announced individually rather than following any divisibility rule.
The rule is not exact: the residual error accumulates to roughly one day every 3,200 years, and no further correction has been legislated.
Real-World Applications
Every date library implements this test, and it governs any calculation that spans a February — payroll accruals, interest day counts, subscription renewals and age arithmetic. Financial day-count conventions such as actual/365 depend on it directly, and scheduling systems use it to decide whether an annual event falling on 29 February moves to 28 February or to 1 March.