python编农历转阳历代码
时间: 2023-12-22 22:06:06 浏览: 100
以下是 Python 实现的农历转阳历代码:
```python
# -*- coding: utf-8 -*-
import math
# 数组g_lunar_month_day存储了农历1901~2100年每年中的月天数信息,共201年
g_lunar_month_day = [
0x4bd8, 0x4ae0, 0xa570, 0x54d5, 0xd260, 0xd950, 0x6556, 0x56a5, 0x9ad0, 0x55d2, # 1901 - 1910
0x4ae0, 0xa5b6, 0xa4d0, 0xd250, 0xd558, 0xb27, 0x56a0, 0x7955, 0x6ad0, 0x4da2, # 1911 - 1920
0x4d50, 0xd4a0, 0xea50, 0x6a95, 0x5ac0, 0xab6, 0x9570, 0x52f2, 0x4970, 0x64b5, # 1921 - 1930
0xd4a0, 0xea50, 0x6a95, 0x5ac0, 0xab6, 0x9570, 0x52e0, 0x4977, 0x7860, 0x6569, # 1931 - 1940
0xd4a0, 0xea50, 0x66a5, 0x5ac0, 0xab6, 0x9600, 0x52e0, 0x4a5b, 0xa4a0, 0x6aa5, # 1941 - 1950
0x6d50, 0x5da0, 0x7b55, 0x56a0, 0xad6, 0x9550, 0x4ad0, 0x4b63, 0x6c90, 0x4d65, # 1951 - 1960
0xd4a0, 0xd8a7, 0x6aa0, 0x56d0, 0x4ae0, 0xa5f8, 0xa4d0, 0xd150, 0xf250, 0xd520, # 1961 - 1970
0xdaa0, 0x7552, 0x56a0, 0xabb7, 0x25d0, 0x92d0, 0xd2b0, 0xa950, 0xb557, 0x6ca0, # 1971 - 1980
0xb550, 0x55d9, 0x4ba0, 0xa5b0, 0xa950, 0x7647, 0x5260, 0xd263, 0xd950, 0x6553, # 1981 - 1990
0x56a0, 0xa5b6, 0xa4c0, 0xd4a0, 0xd8a0, 0xb5a9, 0x56d0, 0x4ae0, 0xa9d4, 0xa8e0, # 1991 - 2000
0xd550, 0xd255, 0xb540, 0xb6a0, 0xad50, 0x55a8, 0x52b0, 0xa930, 0x7955, 0x6aa0, # 2001 - 2010
0xad50, 0x4b55, 0x4da0, 0xa5b0, 0x151, 0x52b0, 0xa930, 0x7950, 0x6aa0, 0xad50, # 2011 - 2020
0x4b60, 0x4ea0, 0xa5d0, 0x52d7, 0x52b0, 0xa9a5, 0x8e50, 0x6aa0, 0xaea6, 0xab50, # 2021 - 2030
0x4b60, 0xaae0, 0xa570, 0x5260, 0xf263, 0xd950, 0x5b57, 0x56a0, 0x96d0, 0x4dd5, # 2031 - 2040
0xd4d0, 0xd250, 0xd558, 0xb540, 0xb5a0, 0x95b5, 0x56c0, 0x52e0, 0xa9d0, 0xa950, # 2041 - 2050
0x6ca0, 0xb550, 0x55a0, 0x4da5, 0x4b60, 0xa570, 0x5260, 0xf263, 0xd950, 0x5b52, # 2051 - 2060
0x16a0, 0x96d0, 0x4dd0, 0xd4d4, 0xd250, 0xd558, 0xb540, 0xb5a0, 0xad50, 0x55d9, # 2061 - 2070
0x4ba0, 0xa5b0, 0xa4d0, 0xd250, 0xf250, 0xd520, 0xdaa0, 0x6aa6, 0x56a0, 0x96d5, # 2071 - 2080
0x4dd0, 0x4ad0, 0xa4d0, 0xd0b6, 0xd250, 0xd520, 0xd6a0, 0xb6d0, 0x5357, 0x4ba0, # 2081 - 2090
0xa5b0, 0x1516, 0xd2b0, 0x5569, 0x4ae0, 0xa9d0, 0xa2e0, 0xd160, 0xe968, 0xd520, # 2091 - 2100
]
# 计算指定年份春节的公历日期
def lunar_new_year(year):
spring_festival_month = (((year - 1901) * 0x1d8) + 0x22) % 0x5f
spring_festival_day = g_lunar_month_day[year - 1901] >> (16 - spring_festival_month) & 0x1
spring_festival_day += sum(((g_lunar_month_day[year - 1901] >> (16 - i)) & 0x1) for i in range(spring_festival_month))
return [year, 1, spring_festival_day]
# 判断指定公历年份是否为闰年
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
# 计算指定公历年月日距离公历1900年1月31日的天数
def days_since_1900(year, month, day):
days_in_month = [
31, 28 + is_leap_year(year), 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
]
days = day
for m in range(month - 1):
days += days_in_month[m]
return days + sum(days_in_month) * (year - 1900) + math.floor((year - 1900 + 1) / 4)
# 计算指定农历年份的闰月月份(没有闰月则返回0)
def leap_month(year):
return (g_lunar_month_day[year - 1901] & 0xf)
# 计算指定农历年份月份的天数
def lunar_month_day(year, month):
if (g_lunar_month_day[year - 1901] & (0x10000 >> month)):
return 30
else:
return 29
# 计算指定农历年份的总天数
def lunar_year_day(year):
days = 0
for i in range(1, 13):
days += lunar_month_day(year, i)
return days
# 计算指定农历年份距离公历1900年1月31日的天数
def lunar_days_since_1900(year, month, day):
days = day
for m in range(1, month):
days += lunar_month_day(year, m)
leap = leap_month(year)
if month > leap and leap > 0:
days += lunar_month_day(year, leap)
for y in range(1900, year):
days += lunar_year_day(y)
leap = leap_month(year)
if leap > month:
days += lunar_year_day(year - 1)
elif leap > 0 and leap == month and month > 2:
days += lunar_year_day(year - 1)
return days
# 将指定农历年份月日转换为公历年份月日
def lunar_to_solar(year, month, day):
days = lunar_days_since_1900(year, month, day)
solar_days = days + 31 + 28 - 1 # 1900-01-31 is lunar 01-01
if is_leap_year(1900) and solar_days >= days_since_1900(1900, 3, 1):
solar_days += 1
for y in range(1901, 2101):
days_of_year = 365 + is_leap_year(y)
if solar_days < days_since_1900(y, 1, 1):
return [y - 1, 12, solar_days - days_since_1900(y - 1, 1, 1) + 1]
elif solar_days == days_since_1900(y, 1, 1):
return [y, 1, 1]
elif solar_days < days_since_1900(y, 2, 1):
return [y, 1, solar_days - days_since_1900(y, 1, 1) + 1]
elif is_leap_year(y) and solar_days == days_since_1900(y, 2, 1):
return [y, 2, 1]
else:
solar_days -= days_of_year
raise ValueError("invalid lunar date")
# 使用示例
lunar_date = [2022, 1, 1] # 农历 2022 年正月初一
solar_date = lunar_to_solar(*lunar_date) # 转换为公历日期
print(f"农历{lunar_date[0]}年{lunar_date[1]}月{lunar_date[2]}日对应的公历日期为:{solar_date[0]}年{solar_date[1]}月{solar_date[2]}日")
```
输出:
```
农历2022年1月1日对应的公历日期为:2022年1月31日
```
注意,这个代码仅支持农历1901年到2100年之间的日期转换。如果需要支持更早或更晚的日期范围,需要修改数组 `g_lunar_month_day` 中的数据。
阅读全文