Leap year problem

Last updated

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.

Contents

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]

fromdatetimeimportdatetoday=date.today()later=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]

SYSTEMTIMEst;FILETIMEft;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]

DateTimedt=DateTime.Now;DateTimeresult=newDateTime(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]

vardt=newDate();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]

boolisLeapYear=year%4==0;

The correct leap year algorithm is explained at Leap Year Algorithm.

Occurrences

See also

Related Research Articles

<span class="mw-page-title-main">Buffer overflow</span> Anomaly in computer security and programming

In programming and information security, a buffer overflow or buffer overrun is an anomaly whereby a program writes data to a buffer beyond the buffer's allocated memory, overwriting adjacent memory locations.

<span class="mw-page-title-main">Microsoft Excel</span> Spreadsheet editor, part of Microsoft 365

Microsoft Excel is a spreadsheet editor developed by Microsoft for Windows, macOS, Android, iOS and iPadOS. It features calculation or computation capabilities, graphing tools, pivot tables, and a macro programming language called Visual Basic for Applications (VBA). Excel forms part of the Microsoft 365 suite of software.

In computing, a segmentation fault or access violation is a fault, or failure condition, raised by hardware with memory protection, notifying an operating system (OS) the software has attempted to access a restricted area of memory. On standard x86 computers, this is a form of general protection fault. The operating system kernel will, in response, usually perform some corrective action, generally passing the fault on to the offending process by sending the process a signal. Processes can in some cases install a custom signal handler, allowing them to recover on their own, but otherwise the OS default signal handler is used, generally causing abnormal termination of the process, and sometimes a core dump.

A software bug is an error, flaw or fault in the design, development, or operation of computer software that causes it to produce an incorrect or unexpected result, or to behave in unintended ways. The process of finding and correcting bugs is termed "debugging" and often uses formal techniques or tools to pinpoint bugs. Since the 1950s, some computer systems have been designed to detect or auto-correct various software errors during operations.

Defensive programming is a form of defensive design intended to develop programs that are capable of detecting potential security abnormalities and make predetermined responses. It ensures the continuing function of a piece of software under unforeseen circumstances. Defensive programming practices are often used where high availability, safety, or security is needed.

<span class="mw-page-title-main">Crash (computing)</span> When a computer program stops functioning properly and self-terminates

In computing, a crash, or system crash, occurs when a computer program such as a software application or an operating system stops functioning properly and exits. On some operating systems or individual applications, a crash reporting service will report the crash and any details relating to it, usually to the developer(s) of the application. If the program is a critical part of the operating system, the entire system may crash or hang, often resulting in a kernel panic or fatal system error.

<span class="mw-page-title-main">Glitch</span> Short-lived fault in a computer system

A glitch is a short-lived fault in a system, such as a transient fault that corrects itself, making it difficult to troubleshoot. The term is particularly common in the computing and electronics industries, in circuit bending, as well as among players of video games. More generally, all types of systems including human organizations and nature experience glitches.

<span class="mw-page-title-main">2,147,483,647</span> Natural number

The number 2,147,483,647 is the eighth Mersenne prime, equal to 231 − 1. It is one of only four known double Mersenne primes.

In computer science and computer programming, system time represents a computer system's notion of the passage of time. In this sense, time also includes the passing of days on the calendar.

<span class="mw-page-title-main">Year 1900 problem</span> Time formatting bug

The year 1900 problem concerns the misinterpretation of years recorded by only their last two digits, and whether they occurred before or after the year 1900. Unlike the year 2000 problem, it is not tied to computer software alone, since the problem existed before electronic computers did and has also cropped up in manual systems.

Memory safety is the state of being protected from various software bugs and security vulnerabilities when dealing with memory access, such as buffer overflows and dangling pointers. For example, Java is said to be memory-safe because its runtime error detection checks array bounds and pointer dereferences. In contrast, C and C++ allow arbitrary pointer arithmetic with pointers implemented as direct memory addresses with no provision for bounds checking, and thus are potentially memory-unsafe.

<span class="mw-page-title-main">Zune 30</span> Portable media player developed by Microsoft

The Zune 30 is a portable media player developed by Microsoft, and the first hardware device in Microsoft's Zune brand. It was released on November 14, 2006, simply named the Zune. After subsequent versions with different hard drive capacities, the original Zune was renamed Zune 30.

In computer science, data type limitations and software bugs can cause errors in time and date calculation or display. These are most commonly manifestations of arithmetic overflow, but can also be the result of other issues. The most well-known consequence of this type is the Y2K problem, but many other milestone dates or times exist that have caused or will cause problems depending on various programming deficiencies.

<span class="mw-page-title-main">Blue screen of death</span> Error screen displayed after a fatal system error on a computer running Microsoft Windows or ReactOS

The blue screen of death (BSoD), blue screen error, blue screen, fatal error, or bugcheck, and officially known as a stop error, is a critical error screen displayed by the Microsoft Windows and ReactOS operating systems in the event of a fatal system error. It indicates a system crash, in which the operating system has reached a critical condition where it can no longer operate safely.

<span class="mw-page-title-main">Stack Overflow</span> Question-and-answer website for computer programmers

Stack Overflow is a question-and-answer website for computer programmers. It is the flagship site of the Stack Exchange Network. It was created in 2008 by Jeff Atwood and Joel Spolsky. It features questions and answers on certain computer programming topics. It was created to be a more open alternative to earlier question and answer websites such as Experts-Exchange. Stack Overflow was sold to Prosus, a Netherlands-based consumer internet conglomerate, on 2 June 2021 for $1.8 billion.

In computing, an epoch is a fixed date and time used as a reference from which a computer measures system time. Most computer systems determine time as a number representing the seconds removed from a particular arbitrary date and time. For instance, Unix and POSIX measure time as the number of seconds that have passed since Thursday 1 January 1970 00:00:00 UT, a point in time known as the Unix epoch. Windows NT systems, up to and including Windows 11 and Windows Server 2022, measure time as the number of 100-nanosecond intervals that have passed since 1 January 1601 00:00:00 UTC, making that point in time the epoch for those systems. Computing epochs are almost always specified as midnight Universal Time on some particular date.

<span class="mw-page-title-main">Year 2000 problem</span> Computer bugs related to the year 2000

The year 2000 problem, also commonly known as the Y2K problem, Y2K scare, millennium bug, Y2K bug, Y2K glitch, Y2K error, or simply Y2K, refers to potential computer errors related to the formatting and storage of calendar data for dates in and after the year 2000. Many programs represented four-digit years with only the final two digits, making the year 2000 indistinguishable from 1900. Computer systems' inability to distinguish dates correctly had the potential to bring down worldwide infrastructures for computer reliant industries.

A code sanitizer is a programming tool that detects bugs in the form of undefined or suspicious behavior by a compiler inserting instrumentation code at runtime. The class of tools was first introduced by Google's AddressSanitizer of 2012, which uses directly mapped shadow memory to detect memory corruption such as buffer overflows or accesses to a dangling pointer (use-after-free).

<span class="mw-page-title-main">Visual Studio Code</span> Source code editor developed by Microsoft

Visual Studio Code, also commonly referred to as VS Code, is a source-code editor developed by Microsoft for Windows, Linux and macOS. Features include support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git. Users can change the theme, keyboard shortcuts, preferences, and install extensions that add functionality.

References

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