Leap year problem

1

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:

Examples

Python

The following Python code is an example of a Category 1 leap year bug. It will work properly until becomes February 29. Then, it will attempt to create a February 29 of a common year, which does not exist. The constructor will raise a with the message "day is out of range for month".

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 to represent February 29 of a common year, a date which does not actually exist. Passing to any function that accepts a struct as a parameter will likely fail. For example, the call shown here will return an error code. Since that return value is unchecked (which is extremely common), this will result in being left uninitialized.

Microsoft C#

The following .NET C# code is an example of a Category 1 leap year bug. It will work properly until becomes February 29. Then, it will attempt to create a February 29 of a common year, which does not exist. The constructor will throw an.

JavaScript

The following JavaScript code is an example of a Category 2 leap year bug. It will work properly until 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 object will roll forward to the next valid date, which is 2021-03-01.

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. The correct leap year algorithm is explained at Leap Year Algorithm.

Occurrences

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article