Loading AI tools
Issue resulting in miscalculation of leap years From Wikipedia, the free encyclopedia
The leap year problem (also known as the leap year bug or the leap day bug) is a problem for both digital (computer-related) and non-digital documentation and data storage situations which results from errors in the calculation of which years are leap years, or from manipulating dates without regard to the difference between leap years and common years.
Leap year bugs typically fall into two categories, based on the amount of impact they may have in real-world usage:[1]
The following Python code is an example of a Category 1 leap year bug. It will work properly until today
becomes February 29. Then, it will attempt to create a February 29 of a common year, which does not exist. The date
constructor will raise a ValueError
with the message "day is out of range for month".[2]
from datetime import date
today = date.today()
later = today.replace(year = today.year + 1)
The following Windows C++ code is an example of a Category 1 leap year bug. It will work properly until the current date becomes February 29 of a leap year. Then, it will modify st
to represent February 29 of a common year, a date which does not actually exist. Passing st
to any function that accepts a SYSTEMTIME
struct as a parameter will likely fail.
For example, the SystemTimeToFileTime
call shown here will return an error code. Since that return value is unchecked (which is extremely common), this will result in ft
being left uninitialized.[3]
SYSTEMTIME st;
FILETIME ft;
GetSystemTime(&st);
st.wYear++;
SystemTimeToFileTime(&st, &ft);
The following .NET C# code is an example of a Category 1 leap year bug. It will work properly until dt
becomes February 29. Then, it will attempt to create a February 29 of a common year, which does not exist. The DateTime
constructor will throw an ArgumentOutOfRangeException
.[4]
DateTime dt = DateTime.Now;
DateTime result = new DateTime(dt.Year + 1, dt.Month, dt.Day);
The following JavaScript code is an example of a Category 2 leap year bug. It will work properly until dt
becomes February 29, such as on 2020-02-29. Then it will attempt to set the year to 2021. Since 2021-02-29 doesn't exist, the Date
object will roll forward to the next valid date, which is 2021-03-01.[5]
var dt = new Date();
dt.setFullYear(dt.getFullYear() + 1);
The following code is an example of a leap year bug that is seen in many languages. It may cause either a Category 1 or Category 2 impact, depending on what the result is used for. It incorrectly assumes that a leap year occurs exactly every four years.[6]
bool isLeapYear = year % 4 == 0;
The correct leap year algorithm is explained at Leap Year Algorithm[dead link].
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.