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 [ dead link ].

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 a design defect (bug) in computer software. A computer program with many or serious bugs may be described as buggy.

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> Unexpected program exit due to an error

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">Year 2038 problem</span> Computer software bug occurring in 2038

The year 2038 problem is a time computing problem that leaves some computer systems unable to represent times after 03:14:07 UTC on 19 January 2038.

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

A glitch is a short-lived technical fault, such as a transient one 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.

Buffer overflow protection is any of various techniques used during software development to enhance the security of executable programs by detecting buffer overflows on stack-allocated variables, and preventing them from causing program misbehavior or from becoming serious security vulnerabilities. A stack buffer overflow occurs when a program writes to a memory address on the program's call stack outside of the intended data structure, which is usually a fixed-length buffer. Stack buffer overflow bugs are caused when a program writes more data to a buffer located on the stack than what is actually allocated for that buffer. This almost always results in corruption of adjacent data on the stack, which could lead to program crashes, incorrect operation, or security issues.

<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.

<span class="mw-page-title-main">Glitch, Inc.</span> American software company

Glitch, Inc. is a software company specializing in project management tools. Its products included project management and content management, and code review tools. Fastly acquired the company in 2022.

<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.

In software, a stack buffer overflow or stack buffer overrun occurs when a program writes to a memory address on the program's call stack outside of the intended data structure, which is usually a fixed-length buffer. Stack buffer overflow bugs are caused when a program writes more data to a buffer located on the stack than what is actually allocated for that buffer. This almost always results in corruption of adjacent data on the stack, and in cases where the overflow was triggered by mistake, will often cause the program to crash or operate incorrectly. Stack buffer overflow is a type of the more general programming malfunction known as buffer overflow. Overfilling a buffer on the stack is more likely to derail program execution than overfilling a buffer on the heap because the stack contains the return addresses for all active function calls.

<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> Fatal system error screen

The blue screen of death is a critical error screen displayed by the Microsoft Windows operating systems. It indicates a system crash, in which the operating system reaches a critical condition where it can no longer operate safely.

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. The C# programming language and 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 00:00:00 UTC on 1 January in the years AD 1 and AD 1601, respectively, making those points in time the epochs 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 term year 2000 problem, 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).

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. Archived from the original on 2011-08-12. 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.