Skip to main content

A Python package for converting between Gregorian and Lunar calendars.

Project description

chinese_lunar_calendar_converter

chinese_lunar_calendar_converter 是一个用于公历和农历之间相互转换的 Python 包,并提供万年历打印功能。

安装

您可以通过 pip 安装 chinese_lunar_calendar_converter

pip install chinese_lunar_calendar_converter

使用示例

公历转农历

from chinese_lunar_calendar_converter import solar_to_lunar

gregorian_date = "2025-01-29"
lunar_info = solar_to_lunar(gregorian_date)
print(f"{gregorian_date} 为农历:{lunar_info[0]}{lunar_info[1]}{lunar_info[2]}{lunar_info[3]}{lunar_info[4]}")
# 输出示例: 2025-01-29 为农历:甲辰年 丁丑月 戊戌日 正月初一

农历转公历

from chinese_lunar_calendar_converter import lunar_to_solar

lunar_year = 2025 # 农历年份
lunar_date_str = "正月初一"
gregorian_info = lunar_to_solar(lunar_year, lunar_date_str)
print(f"农历{lunar_year}{lunar_date_str} 为公历:{gregorian_info[0]}-{gregorian_info[1]:02d}-{gregorian_info[2]:02d}")
# 输出示例: 农历2025年正月初一 为公历:2025-01-29

打印万年历

from chinese_lunar_calendar_converter import print_perpetual_calendar

# 打印2024年全年日历
print_perpetual_calendar(2024)

# 打印2024年3月日历
print_perpetual_calendar(2024, 3)

API 接口

solar_to_lunar(gregorian_date_str: str) -> Tuple[str, str, str, str, str]

将公历日期字符串转换为农历日期元组。

  • 参数:
    • gregorian_date_str (str): 公历日期字符串,格式为 "YYYY-MM-DD"。
  • 返回:
    • Tuple[str, str, str, str, str]: 包含农历年干支、月干支、日干支、农历月名和农历日名的元组。 例如: ("甲辰", "丁丑", "戊戌", "正月", "初一")
  • 可能抛出的异常:
    • ValueError: 如果输入日期格式不正确或日期无效。

lunar_to_solar(lunar_year: int, lunar_date_str: str) -> Tuple[int, int, int]

将农历日期转换为公历日期元组。

  • 参数:
    • lunar_year (int): 农历年份(例如:1999)。
    • lunar_date_str (str): 农历日期字符串,格式为 "[闰]月名日名" (例如 "五月十七" 或 "闰五月十七")。也可以是干支日名 (例如 "癸丑日")。
  • 返回:
    • Tuple[int, int, int]: 包含公历年、月、日的元组。 例如: (2025, 1, 29)
  • 可能抛出的异常:
    • ValueError: 如果农历日期格式不正确或日期无效。

print_perpetual_calendar(year: int, month: int = 0) -> None

打印指定年份(或月份)的万年历(农历及公历对照)。

  • 参数:
    • year (int): 公历年份。
    • month (int, optional): 公历月份 (1-12)。如果为 0,则输出全年日历。默认为 0。
  • 可能抛出的异常:
    • ValueError: 如果年份为 0。

贡献

欢迎贡献!如果您有任何建议或发现 bug,请随时提交 issue 或 pull request。

许可证

本项目采用 MIT 许可证。详见 LICENSE 文件。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chinese_lunar_calendar_converter-0.1.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file chinese_lunar_calendar_converter-0.1.0.tar.gz.

File metadata

File hashes

Hashes for chinese_lunar_calendar_converter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 806f26b29c4b8768cc084db9c382276b48cd061ff97cce2c05d6fea6e3da16c7
MD5 9b121687603464a18065cdddcbbdc176
BLAKE2b-256 fa7b522821df085ff3d5ea8286ae1f2a5349e87652eba20bfb1b6405c1fd40d9

See more details on using hashes here.

File details

Details for the file chinese_lunar_calendar_converter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for chinese_lunar_calendar_converter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 554385f7ca351acc24d49de664ee82db836e3e477c20df49cdfa9b3e07204232
MD5 553b90b5025f893879907beeb93b4171
BLAKE2b-256 26f4a170b52814b40dd277874da92313e45aef3a9e0e717ace13a02e9865938d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page