Why are leap years used?

本文详细解释了闰年的概念及其存在的必要性,介绍了格里高利历和儒略历中闰年的计算规则,并探讨了不同历法对季节的影响。

February 29, 2008

Year 2008 is the next leap year, with 29 days in February. February 2008 has five Fridays - it starts and ends on a Friday. Between 1904 and 2096, leap years with same day of week for each date repeat every 28 years which means that the last time February had 5 Fridays was in 1980 and next time will be in 2036.

What is a leap year?

A leap year is a year with one extra day inserted into February, the leap year is 366 days with 29 days in February as opposed to the normal 28 days. (There are a few past exceptions to this)

Which years are leap years?

In the Gregorian calendar, which is the calendar used by most modern countries, the following rules decides which years are leap years:

  1. Every year divisible by 4 is a leap year.
  2. But every year divisible by 100 is NOT a leap year
  3. Unless the year is also divisible by 400, then it is still a leap year.

This means that year 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years, while year 2000 and 2400 are leap years.

This actually means year 2000 is kind of special, as it is the first time the third rule is used in many parts of the world.

In the old Julian Calendar, there was only one rule: Every year divisible by 4 is a leap year. This calendar was used before the Gregorian calendar was adopted.

Why are leap years needed?

Leap years are needed so that the calendar is in alignment with the earth's motion around the sun.

Details

Note: The illustration does not have the right dimensions for the earth, sun and orbit path.

The mean time between two successive vernal equinoxes is called a tropical year, and it is about 365.2422 days long. This means that it takes 365.2422 days for the earth to make one revolution around the sun (the time is takes to orbit the sun).

Using a calendar with 365 days would result in an error of 0.2422 days or almost 6 hours per year. After 100 years, this calendar would be more than 24 days ahead of the seasons (tropical year), which is not a desirable situation. It is desirable to align the calendar with the seasons, and make the difference as small as possible.

By adding leap years approximately every 4th year, this difference between the calendar and the seasons can be reduced significantly, and the calendar will follow the seasons much more closely than without leap years.

(One day is here used in the sense of "mean solar day", which is the mean time between two transits of the sun across the meridian of the observer.)

Is there a perfect calendar?

None of the calendars used today are perfect, they go wrong by seconds, minutes, hours or days every year. To make a calendar even better, new leap year rules have to be introduced, complicating the calculation of the calendar even more. The currently used Gregorian calendar may need some modification a few thousand years ahead. A tropical year is approximately 365.242199 days, but it varies from year to year, because of influence by the other planets.

Name of calendar

Introduced

Average year

Approximate
error introduced

Gregorian calendar

AD 1582

365.2425 days

27 seconds (1 day every 3236 years)

Julian calendar

45 BC

365.25 days

11 minutes (1 day every 128 years)

365-day calendar

-

365 days

6 hours (1 day every 4 years)

Lunar calendar

ancient

12-13 moon-months

variable

A calendar like the Julian Calendar (with every 4th year as a leap year) was first introduced by king Ptolemy III, Egypt in 238 BC.
In ancient times, it was very usual to have lunar (moon) calendars, with 12 and/or 13 months every year. To align the calendar with the seasons the 13th month was inserted as a "leap month" every 2-3 years.

Note: Many other calendars have been and are still used throughout the world.

Change from Julian to Gregorian calendar

The Julian calendar had introduced too many leap days, so that vernal equinox did no longer happen around March 21, as it did back in AD 325 during the Council of Nicaea. The introduction of the Gregorian calendar should realign the calendar with the equinox, so a number of days had to be dropped when going from the Julian to the Gregorian calendar. The links below show the calendars with the days dropped when the change to the Gregorian calendar occurred:

  • The Gregorian calendar was first adopted in Italy , Poland , Portugal and Spain in 1582. This was done by dropping 10 days in October (Ottobre).
  • In Great Britain (and to-become USA ), the Gregorian calendar was adopted much later - 11 days were dropped in September 1752.
  • Sweden (and Finland ) had a "double" leap year in 1712 - two days were added to February, so that there was a date February 30, 1712. (This was done because the leap year in 1700 was dropped and Sweden 's calendar was not synchronized with any other calendar - by adding an extra day in 1712, they were back on the Julian calendar).
  • The old Julian calendar is currently (between year 1901-2099) 13 days past the Gregorian calendar (because too many leap years were added).

Other leap years facts

  • The Gregorian calendar has a 400 year long cycle and the calendars have the same week days -- February 29, 2008 is a Friday and February 29, 2408 is a Friday.
  • The Gregorian calendar has 97 leap years during those 400 years.
  • The longest time between two leap years is 8 years. Last time was between 1896 and 1904. The next time will be between 2096 and 2104.

Reference

Gregorian calendar n.[天文]格里历, 阳历

Julian calendar n. 罗马儒略历,公历

vernal equinox n.春分

equinox n.昼夜平分点, 春分或秋分

lunar calendar n.阴历

solar calendar n.阳历

solar day n.[]太阳日

tropical year n.回归年

orbit n.轨道, 势力范围, 生活常规, 眼眶vt....轨道而行vi.进入轨道, 沿轨道飞行, 盘旋

meridian n.子午线, 正午, 顶点, 全盛时期adj.子午线的, 正午的, 顶点的, 全盛时期的

 

TRACKBACK:http://www.timeanddate.com/date/leapyear.html

 
先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
<think>我们之前已经讨论过,`lunar_python`库中并没有`getLeapMonth`方法,并且属性`isleap`的正确形式是`isLeap`(注意大小写)。用户现在再次提到没有`getLeumonth`和`isleap`属性,可能是大小写问题或版本问题。 根据用户提供的引用[1]中的代码片段,它展示了一个将农历日期转换为公历日期的Java方法,但这与Python库`lunar_python`无关。引用[2]是C#的示例,也不相关。因此,我们忽略这些引用,专注于Python库的正确使用。 在`lunar_python`库中,我们使用以下方式: 1. 判断一个农历月份是否为闰月:使用`isLeap`属性(注意大写L)。 2. 获取闰月的月份:如果该年有闰月,那么闰月的月份可以通过创建该闰月的日期并获取月份(负数表示闰月)或通过其他方式。 但是,用户可能仍然遇到属性错误,所以我们来详细说明。 步骤: 1. 确认安装的库版本:建议使用最新版。 2. 检查属性的大小写:`isLeap`,不是`isleap`(小写l)或`getLeapMonth`。 下面我们提供完整的示例代码,并解释如何获取闰月信息。 注意:在`lunar_python`库中,农历日期对象`Lunar`有以下相关属性: - `year`: 农历年 - `month`: 农历月(整数,如果是闰月,则月份为负数,例如闰二月为-2) - `day`: 农历日 - `isLeap`: 布尔值,表示当前月份是否为闰月(注意:当月份为负数时,这个属性为True) 另外,我们还可以通过`Lunar`对象的其他方法来获取信息,例如`getMonthInChinese()`会返回包含“闰”字的中文月份(如“闰二月”)。 因此,要获取某一年是否有闰月以及闰月是哪个月,我们可以: 1. 先创建该年农历正月(或其他月份)的对象。 2. 通过该对象获取该年的闰月信息(使用`getLeapMonth`方法?不,实际上,库中并没有这个方法。我们可以通过另一种方式:检查该年每个月的`isLeap`属性,或者使用库提供的静态方法?) 实际上,库中有一个`Lunar`的静态方法`leapMonth(year)`可以获取某年的闰月月份(返回0表示无闰月,否则返回闰月的月份数字)。但是注意,这个方法在文档中可能没有明确说明,我们可以查看源码或尝试使用。 根据`lunar_python`的源码(https://github.com/6tail/lunar-python/blob/master/Python/lunar_python/Lunar.py): 我们发现有一个静态方法`leapMonth`: `def leapMonth(year): ...` 所以,我们可以这样使用: 示例代码: ```python from lunar_python import Lunar # 获取某年的闰月(返回0表示无闰月,否则返回闰月的月份,如2表示闰二月) leap_month = Lunar.leapMonth(2023) if leap_month > 0: print(f"{2023}年有闰月,闰{leap_month}月") else: print(f"{2023}年没有闰月") # 或者,我们可以创建一个闰月日期 # 注意:创建闰月日期时,月份用负数表示,例如闰二月用-2 # 判断一个日期是否是闰月 lunar_date = Lunar.fromYmd(2023, -2, 1) # 2023年闰二月初一 print(f"是否为闰月: {lunar_date.isLeap}") # 输出: True print(f"月份中文: {lunar_date.getMonthInChinese()}") # 输出: 闰二月 ``` 如果用户使用的是旧版本,可能没有`leapMonth`静态方法。因此,我们也可以使用另一种方法:遍历12个月,看是否有闰月。但这样效率较低,不推荐。 因此,解决用户的问题: 1. 使用`Lunar.leapMonth(year)`静态方法获取闰月月份(返回整数,0表示无闰月,正数表示闰几月)。 2. 使用`Lunar`对象的`isLeap`属性判断该日期是否为闰月。 注意:属性`isLeap`在对象创建时就已经确定,它取决于创建时传入的月份是否为负数(或者日期确实是闰月的那一天)。 如果用户尝试使用`lunar.getLeapMonth()`,那是不存在的,应该使用静态方法`Lunar.leapMonth(year)`。 所以,总结如下: 问题:`'Lunar' object has no attribute 'getLeapMonth'` 解决方案:不要使用`getLeapMonth`方法,而是使用静态方法`Lunar.leapMonth(year)`来获取闰月。 问题:`'Lunar' object has no attribute 'isleap'` 解决方案:使用`isLeap`属性(注意大写L),即`lunar.isLeap`。 完整示例: ```python from lunar_python import Lunar # 检查2023年的闰月 year = 2023 leap_month = Lunar.leapMonth(year) if leap_month > 0: print(f"{year}年有闰月,闰{leap_month}月") else: print(f"{year}年没有闰月") # 创建一个闰月日期(闰二月初一) lunar_date = Lunar.fromYmd(year, -leap_month, 1) # 注意:这里leap_month是正数,但我们用负数表示闰月 # 或者,如果leap_month为0,则不能创建,所以先判断 if leap_month > 0: lunar_date = Lunar.fromYmd(year, -leap_month, 1) # 检查这个日期是否是闰月 print(f"是否为闰月: {lunar_date.isLeap}") # 应该输出True # 获取中文月份 print(f"月份中文: {lunar_date.getMonthInChinese()}") # 输出"闰二月" ``` 注意:如果闰月不存在(leap_month为0),则不能传入负数月份,否则会抛出异常。 另外,创建闰月日期时,月份参数为负数,但绝对值是月份数。例如闰二月,则月份参数为-2。 所以,我们使用`-leap_month`来作为月份参数(因为leap_month是正数,例如2,那么-2表示闰二月)。 如果用户只是想判断某个月份是不是闰月,也可以这样创建日期对象,然后使用`isLeap`属性。 但是,如果用户没有具体的日期,只是想获取闰月信息,直接使用`Lunar.leapMonth(year)`即可。 最后,建议用户升级库到最新版本,以确保这些方法可用。 如果升级后仍然有问题,可能是库的API变更,请参考官方文档。 升级命令: ```bash pip install --upgrade lunar_python ``` 现在,我们根据上述内容编写回答。 </think> # 解决 `Lunar` 对象缺少 `getLeapMonth` 和 `isleap` 属性问题 在 `lunar_python` 库中,`Lunar` 对象确实没有 `getLeapMonth` 或 `isleap` 属性。这是常见的 API 使用误解,正确的方法如下: ## 正确获取闰月信息的方法 ### 1. 使用静态方法 `leapMonth(year)` 获取闰月月份 ```python from lunar_python import Lunar # 获取2023年的闰月(返回0表示无闰月,正数表示闰月月份) leap_month = Lunar.leapMonth(2023) print(f"2023年闰月: {'闰' + str(leap_month) + '月' if leap_month > 0 else '无闰月'}") # 输出: 2023年闰月: 闰2月 ``` ### 2. 使用 `isLeap` 属性判断具体日期是否为闰月 ```python # 创建闰二月初一的日期对象(注意负号表示闰月) lunar_date = Lunar.fromYmd(2023, -2, 1) # 使用正确的大小写属性 isLeap print(f"是否为闰月: {lunar_date.isLeap}") # 输出: True print(f"月份中文: {lunar_date.getMonthInChinese()}") # 输出: 闰二月 ``` ### 3. 完整示例:处理闰月信息 ```python def get_leap_info(year, month=None): """获取闰月信息""" leap_month = Lunar.leapMonth(year) info = { "year": year, "leap_month": leap_month, "has_leap": leap_month > 0 } if month: # 创建日期对象(负数月份表示闰月) lunar_date = Lunar.fromYmd(year, -month if month > 0 else month, 1) info.update({ "is_leap": lunar_date.isLeap, "chinese_month": lunar_date.getMonthInChinese() }) return info # 示例用法 print(get_leap_info(2023)) # 获取2023年闰月信息 print(get_leap_info(2023, 2)) # 检查2023年二月是否为闰月 ``` ## 常见问题解决方案 ### 问题1:属性大小写错误 - ❌ 错误写法:`lunar.isleap` - ✅ 正确写法:`lunar.isLeap` (注意大写L) ### 问题2:方法名错误 - ❌ 错误写法:`lunar.getLeapMonth()` - ✅ 正确写法:`Lunar.leapMonth(year)` (静态方法) ### 问题3:创建闰月日期 ```python # 正确创建闰月日期(月份加负号) lunar_date = Lunar.fromYmd(2023, -2, 1) # 闰二月初一 # 错误创建方式(会抛出异常) try: Lunar.fromYmd(2023, 2, 1, is_leap=True) # 无效参数 except Exception as e: print(f"错误: {e}") ``` ## 版本兼容性建议 ```bash # 升级到最新版本(推荐0.3.0+) pip install --upgrade lunar_python ``` ## 闰月转换公历示例 ```python # 将农历闰月日期转换为公历 lunar_date = Lunar.fromYmd(2023, -2, 1) # 2023年闰二月初一 solar_date = lunar_date.getSolar() print(f"公历日期: {solar_date.toYmd()}") # 输出: 2023-03-22 ``` [^1]: 引用[1]中提到的农历转公历逻辑在 `lunar_python` 中已封装为 `getSolar()` 方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值