在公历日期和其他日历系统之间转换
项目描述
convertdate
convertdate包最初由Phil Schwartz开发为"Python Date Utils"。它已经得到了显著的更新和扩展。
可用的日历
- 亚美尼亚
- 巴哈伊
- 科普特(亚历山大里亚)
- 法兰西共和国
- 公历
- 希伯来
- 印度民法
- 伊斯兰
- 儒略
- 玛雅
- 波斯
- 实证主义者
- 玛雅
- ISO
- 序数(年日)
- 都柏林日计数
- 儒略日计数
holidays
模块还提供了一些有用的假日计算,重点关注北美和犹太假日。
安装
pip install convertdate
或者下载包并运行python setup.py install
。
示例
>>> from convertdate import french_republican
>>> from convertdate import hebrew
>>> french_republican.from_gregorian(2014, 10, 31)
(223, 2, 9)
>>> hebrew.from_gregorian(2014, 10, 31)
(5775, 8, 7)
请注意,在某些日历系统中,一天从日落开始。convertdate给出的是有关日的正午的转换。
每个模块都包含一个monthcalendar
函数,该函数将生成一个类似日历的嵌套列表,用于年和月(每个日期列表从星期日到星期六运行)
>>> hebrew.monthcalendar(5775, 8)
[
[None, None, None, None, None, None, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29]
]
>>> julian.monthcalendar(2015, 1)
[
[None, None, None, 1, 2, 3, 4],
[5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24, 25],
[26, 27, 28, 29, 30, 31, None]
]
特殊选项
亚美尼亚
亚美尼亚历始于公元552年7月11日(儒略历),有两种计算方式。第一种是不变长度的版本,由12个月份和5个附加日组成,每个月30天;第二种是1084年由约万尼斯·萨卡瓦格建立的版本,将一年的第一天与儒略历固定,并且每四年增加一个附加日。
默认情况下使用不变历,但可以从亚美尼亚533年(公元1084年8月11日)开始使用萨卡瓦格历,通过向相关函数传递参数method='sarkawag'
。
法兰西共和国
在法国共和历中,闰年的计算存在争议。默认情况下,convertdate
使用秋分来计算闰年。您还可以使用多年来提出的三个更系统的方法之一。
- 历法的共同创造者罗梅提出了闰年的计算方法,即除以4的年份为闰年,但除以100的年份除外。
- 19世纪制定了一些对照表,将每4年一次的闰年分配给除以4余3的年份(19、23、27等)。
- 冯·梅德勒提出,除以4的年份为闰年,但除以128的年份除外。
您可以使用french_republican
转换函数中的方法关键字参数来指定这三种方法中的任何一种。
from convertdate import french_republican
# Romme's method
french_republican.to_gregorian(20, 1, 1), method='romme')
# (1811, 9, 23)
# continuous method
french_republican.to_gregorian(20, 1, 1), method='continuous')
# (1811, 9, 24)
# von Madler's method
french_republican.to_gregorian(20, 1, 1), method='madler')
# (1811, 9, 23)
所有转换方法都正确地分配了历法使用期间实现的闰年(3、7、11)。
巴哈伊历法
巴哈伊(巴迪)历法有一个闰月,即阿亚姆-伊-哈,它出现在第18个月和第19个月之间。这个时期的日期以第19个月返回,而阿拉月则报告为第20个月。
from convertdate import bahai
# the first day of Ayyam-i-Ha:
bahai.to_gregorian(175, 19, 1)
# (2019, 2, 26)
# The first day of 'Ala:
bahai.to_gregorian(175, 20, 1)
# (2019, 3, 2)
公元以前
对于公元以前(第1年)的日期,convertdate
使用天文学表示法:公元前1年记录为0,公元前2年记录为-1等。这使得算术变得更容易,但牺牲了习俗。
请注意,对于公元前4年以前的日期,convertdate
使用拟儒略历。儒略历从公元前45年开始使用,但在公元前4年以前,闰年的模式是不规则的。
使用拟格历历法来表示1582年以前,即公历改革那年的日期。
假日
北美假日是holidays
模块当前的关注点,但也欢迎拉取请求。
from convertdate import holidays
# For simplicity, functions in the holidays module return a tuple
# In the format (year, month, day)
holidays.new_years(2014)
# (2014, 1, 1)
holidays.memorial_day(2014)
# (2014, 5, 26)
# USA is default
holidays.thanksgiving(2014)
# (2014, 11, 27)
# But there is a Canadian option for some holidays
holidays.thanksgiving(2014, 'canada')
# (2014, 10, 13)
# Mexican national holidays
holidays.natalicio_benito_juarez(2016)
# (2016, 3, 21)
holidays.dia_revolucion(2016)
# (2016, 11, 21)
# Some Jewish holidays are included
holidays.rosh_hashanah(2014)
# Easter can be calculated according to different churches
# ('western', 'orthodox', 'eastern')
# The eastern Christian computation differs from the Orthodox one
# 4 times in each 532-year cycle.
holidays.easter(2019)
# (2019, 4, 21)
holidays.easter(2019, church="orthodox")
# (2019, 4, 28)
holidays.easter(2019, church="orthodox")
# (2019, 4, 28)
实用工具
convertdate包括一些用于处理和计算日期的实用工具。
from convertdate import utils
# Calculate an arbitrary day of the week
THUR = 3
APRIL = 4
# 3rd Thursday in April
utils.nth_day_of_month(3, THUR, APRIL, 2014)
# (2014, 4, 17)
utils.nth_day_of_month(5, THUR, APRIL, 2014)
# IndexError: No 5th day of month 4
# Use 0 for the first argument to get the last weekday of a month
utils.nth_day_of_month(0, THUR, APRIL, 2014)
# (2014, 4, 24)
请注意,当计算工作日时,convertdate使用日历和时间的约定:星期一为0,星期日为6。
from convertdate import gregorian
SUN = 6
day = gregorian.to_jd(2014, 4, 17)
nextsunday = utils.next_weekday(SUN, day)
gregorian.from_jd(nextsunday)
# (2014, 4, 20)
其他实用函数
- nearest_weekday
- next_or_current_weekday
- previous_weekday
- previous_or_current_weekday
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。
源代码分发
构建分发版
convertdate-2.4.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 770c6b2195544d3e451e230b3f1c9b121ed02680b877f896306a04cf6f26b48f |
|
MD5 | 320965d9ae24060c385110606c61d040 |
|
BLAKE2b-256 | 043d04148ceb732dfb6f10e9b89fa5915080a91e27fe28fd982c259bc4d29ced |
convertdate-2.4.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fcffe3a67522172648cf03b0c3757cfd079726fe5ae04ce29989ad3958039e4e |
|
MD5 | 848e757732829631f368ba8b78b547c3 |
|
BLAKE2b-256 | 27653deecc820ce91716225ec72b584b48ba9512ed9583ad48619e3dbbbbd714 |