Leap year problem explained

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.

Categories

Leap year bugs typically fall into two categories, based on the amount of impact they may have in real-world usage:[1]

  1. Those that lead to error conditions, such as exceptions, error return codes, uninitialized variables, or endless loops
  2. Those that lead to incorrect data, such as off-by-one problems in range queries or aggregation

Examples

Python

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 datetoday = date.todaylater = today.replace(year = today.year + 1)

Windows C++

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);

Microsoft C#

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);

JavaScript

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);

Bad leap year algorithm (many languages)

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.

Occurrences

See also

Notes and References

  1. Web site: Johnson-Pint . Matt . What are some examples of leap year bugs? . Stack Overflow . 5 February 2020.
  2. Web site: Johnson-Pint . Matt . Python - Replacing the year . Stack Overflow . 29 February 2020.
  3. Web site: Johnson-Pint . Matt . Win32 / C++ SYSTEMTIME struct manipulation . Stack Overflow . 5 February 2020.
  4. Web site: Johnson-Pint . Matt . .NET / C# - Construction from date parts . Stack Overflow . 5 February 2020.
  5. Web site: Johnson-Pint . Matt . JavaScript - Adding Year(s) . Stack Overflow . 5 February 2020.
  6. Web site: Johnson-Pint . Matt . Determining if a Year is a Leap Year . Stack Overflow . 5 February 2020.
  7. https://docs.microsoft.com/office/troubleshoot/excel/wrongly-assumes-1900-is-leap-year Excel incorrectly assumes that the year 1900 is a leap year
  8. http://www.ecma-international.org/publications/standards/Ecma-376.htm Standard ECMA-376 / Open Office XML File Formats
  9. http://standards.iso.org/ittf/PubliclyAvailableStandards/index.html ISO/IEC 29500 / Open Office XML File Formats
  10. Towler . Jim . Leap-Year software bug gives "Million-dollar glitch" . The RISKS Digest . 7 January 1997 . 18 . 74 . ACM Committee on Computers and Public Policy . 5 February 2020.
  11. Web site: The last bite of the bug . BBC News . 5 January 2001.
  12. News: 7-Eleven Systems Hit by Y2k-like Glitch . 10 March 2023.
  13. News: 1 January 2001 . Y2K Bug Hits Norway's Railroad At End Of Year . en . 10 March 2023.
  14. Web site: Home - Microsoft Answers . Forums.zune.net . 2011-07-27 . dead . https://web.archive.org/web/20090830061109/http://forums.zune.net/412486/ShowPost.aspx . August 30, 2009 .
  15. Web site: John Herrman . 30GB Zunes Failing Everywhere, All At Once . Gizmodo.com . 2008-12-31 . 2011-07-27.
  16. Web site: Geere . Duncan . BREAKING: Zunes worldwide hit by mystery crash : Tech Digest . Techdigest.tv . 31 December 2008. 2011-07-27.
  17. Web site: Zune 30 FAQ. December 31, 2008. Microsoft. January 1, 2009. January 2, 2009. https://web.archive.org/web/20090102065939/http://www.zune.net/en-us/support/zune30.htm. dead.
  18. Web site: A lesson on infinite loops. January 3, 2009. AeroXperience. Bryant. Zadegan. January 5, 2009.
  19. News: Sony fixes PS3 leap year bug. Metro. English. 2 March 2010. 10 October 2019.
  20. News: TomTom sat-nav devices hit by GPS 'leap year bug' . BBC News . 3 April 2012 . 5 February 2020.
  21. Web site: Johnson-Pint . Matt . List of 2016 Leap Day Bugs . Code of Matt . 29 February 2016 . 5 February 2020.
  22. Web site: Airport hiccup leaves 100s of passengers pantless . The Local (de) . March 2016 . 5 February 2020.
  23. Web site: Johnson-Pint . Matt . List of 2020 Leap Day Bugs . Code of Matt . 29 February 2020 . 9 March 2020.
  24. Web site: 2024-02-29 . List of 2024 Leap Day Bugs . 2024-02-29 . Code of Matt . en.
  25. Web site: 2024-03-01 . Petrol pumps back online after day-long outage blamed on leap year glitch . 2024-02-29 . NZ Herald . en-NZ.