A leap year bug is a code defect that creates a problematic, unintended outcome when executed within the context of a leap year, typically within the proleptic Gregorian calendar system.
The years 2016, 2020, 2024, 2028, etc. are leap years.
There are two attributes that are unique to leap years:
- Leap years have a February 29th, while common years do not.
- Leap years have 366 days in total, while common years have only 365.
This post is intended to help others understand the nature of leap year bugs, what they look like in various languages, and how to correct them.
Leap year bugs typically fall into two impact categories:
- Category 1: Those that lead to error conditions, such as exceptions, error return codes, uninitialized variables, or endless loops
- Category 2: Those that lead to incorrect data, such as off-by-one problems in range queries or aggregation
With each answer, please indicate the programming language and/or platform, as well as the impact category as defined above. (Follow the template used by existing answers please.)
Please create one separate answer per language and type of defect, and vote for your favorite, especially those you have personally encountered (leaving comments with anecdotes where possible).
I will seed a few answers to get started, and update with additional examples over time.
Z
. SincetoISOString
also outputs in UTC, it's masked. If you either left theZ
off, or usedformat
for the output (or both), then the behavior would be clearer. (It's not really leaping forward.) – Periphrasis