Python
A Small Piece of Leap-Year Knowledge Learned by Chance
Today, while looking through a tutorial, Professor happened to learn a few small points about leap years, so he is recording them.
On this page
Today, while looking through a tutorial, Professor happened to learn a few small points about leap years, so he is recording them.
Cause
During the holiday, while reading Python - 100天从新手到大师, I saw an exercise with an example for judging leap years:
"""
输入年份 如果是闰年输出True 否则输出False
Version: 0.1
Author: 骆昊
"""
year = int(input('请输入年份: '))
# 如果代码太长写成一行不便于阅读 可以使用\对代码进行折行
is_leap = year % 4 == 0 and year % 100 != 0 or \
year % 400 == 0
print(is_leap)Although the code’s logic is very clear, I was nevertheless quite puzzled: if this way of writing is used for the judgment, the starting point of leap years seems as if it should be the year 0 CE. So I briefly looked up some related knowledge about leap years and, sure enough, found some details I had never noticed before.
Definition of a Leap Year
The definition of a leap year learned from Wikipedia is as follows:
A leap year means a year that has 366 days, that is, one day more than an ordinary year. Leap years are established to make up for the gap between the human calendar’s stipulated year length of 365 days and the average tropical year of about 365.24219 days. Different calendars have different intercalation methods. In the Julian calendar, 1 leap day is inserted every 4 years, and 1 year averages 365.25 days. In the Gregorian calendar, 3 leap days are omitted every 400 years, and the average is 365.2425 days. The extra day is day 29 of month 2 in February.
As the Julian calendar is the predecessor of the Gregorian calendar, we do not need to dwell on it too much; it is enough to understand it according to the Gregorian calendar (the common calendar). Simply put, whenever it is a leap year, month 2 has day 29 as the leap day.
A year with a leap day is called a leap year; the opposite is an ordinary year. An ordinary year has 365 days, and a leap year has 366 days.
It should be noted here that the lunar calendar (also called the “old calendar”) has the concepts of leap years and leap months, and it is still quite different from leap years in the Gregorian calendar. The number of days in each lunar-calendar month is determined precisely according to the waxing, waning, fullness, and absence cycle of the phases of the moon. 1 synodic month (朔望月), from one new moon to the next, is about 29.53 days long. When calendar months are formulated, they are distinguished as big and small months: a big month has 30 days, and a small month has 29 days. The year is based on 12 months, and an ordinary year is about 11 days shorter than a tropical year (solar year). Therefore, in order to match the tropical year, one month is added every 2 to 3 years; the added month is the leap month, and that year is then a leap year. The leap month is calculated according to the rules of the lunar calendar, and the logical rules are slightly complex, so I will not expand on them here.
This article still mainly introduces the leap-year rules of the Gregorian calendar.
Starting Year of Leap Years
In the Gregorian calendar (common calendar), the starting year for leap years is fixed. According to the Gregorian calendar, the rules for leap years (after the Common Era) are recalculated starting from 4 CE. This is because in 46 BCE, the Roman emperor Julius Caesar adopted the suggestion of the Egyptian astronomer Sosigenes, introduced the concept of leap years into the Julian calendar (the predecessor of the Gregorian calendar), and began implementing it on month 1 day 1, 45 BCE (the first day of the Julian calendar). In order to correct the time deviation in the calendar system, Julius Caesar decided to set up a leap year every 4 years, that is, one extra day every 4 years. Day 25 of month 2 in 4 CE was the first adjusted day.
Therefore, starting from 4 CE, there is a leap year every 4 years. Of course, in order to correct the error of the Julian calendar, the Gregorian calendar added more complete rules for leap years. But there are also some small bugs in this, and the Gregorian calendar’s handling can also be said to be simple and direct. You can flip through the calendar App on your phone and look for month 10 of 1582; there will be a surprise.
There is also an extra small piece of knowledge: Russia’s October Revolution actually happened on month 11 day 7, 1917, but why is it called the “October” Revolution? In fact, it is also because Russia was still using the Julian calendar at the time and had not corrected the corresponding error according to the Gregorian calendar.
Next, let us look together at the intercalation rules in the Gregorian calendar.
Intercalation Rules
The calculation logic for leap years usually follows the rules below:
A year that can be divided exactly by 4 is a leap year. This is the most basic rule. If a year can be divided exactly by 4 (that is, the year can be divided by 4 with no remainder), then it may be a leap year. For example, the years 2004 and 2008 are leap years because they can be divided exactly by 4 with no remainder.
However, a year that can be divided exactly by 100 and cannot be divided exactly by 400 is not a leap year. This rule is to correct rule 1 for being too loose. If a year can be divided exactly by 100 (that is, the year can be divided by 100 with no remainder), but cannot be divided exactly by 400, then it is not a leap year. For example, the year 1900 is not a leap year, because it can be divided exactly by 100 but cannot be divided exactly by 400 with no remainder.
A year that can be divided exactly by 400 is still a leap year. Even if a year can be divided exactly by 100, if it can be divided exactly by 400, then it is still a leap year. This rule ensures that years such as 2000 and 2400, which can be divided exactly by 400, are leap years.
The combination of these three rules ensures the calculation logic for leap years. Through these rules, we can judge whether any given year is a leap year.
Summary in two sentences:
Every year divisible by 4 is a leap year, but a year divisible by 100 must also be divisible by 400 to be a leap year. Therefore, 1700, 1800, and 1900 are not leap years, while 2000 is a leap year.
Summary in one sentence:
Leap every four years, no leap every hundred years, leap again every four hundred years
In fact, the scope to which the rules above apply only reaches 3200 CE. If we extrapolate into a broader river of time, 2 more rules can be added:
- Every 3200 years, do not leap;
- Every 172800 years, leap;
In theory, the rules can continue to extend further onward, but for those of us living in 2023, the significance is already not great. I am really rather curious what the world in 3200 will be like, and what sort of appearance the Earth in 172800 will have.
Summary
Previously, I only knew that leap years require a leap every four years. I did not expect that according to the calendar, there are also the rules of no leap every hundred years and leap again every four hundred years. Sure enough, for detailed knowledge points, it is still necessary to follow a pragmatic spirit and understand them deeply and clearly.
This can also show that programming actually requires a very rigorous working attitude, and also needs sufficient related domain knowledge as support.
References
- https://github.com/jackfrued/Python-100-Days
- https://zh.wikipedia.org/wiki/%E9%97%B0%E5%B9%B4
- https://zh.wikipedia.org/wiki/%E5%85%AC%E5%8E%86
- https://zh.wikipedia.org/wiki/%E5%84%92%E7%95%A5%E6%9B%86
- https://zh.wikipedia.org/wiki/%E8%BE%B2%E6%9B%86
- https://www.topys.cn/article/31374