在公历日期和其他历法系统之间转换。历法包括:巴哈伊历法、法国共和历法、希伯来历法、印度文明历法、伊斯兰教历法、伊斯兰教历法、朱利安历法、玛雅历法和波斯语历法。

convertdate的Python项目详细描述


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, 1, 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]
# ]

特殊选项

法国共和党人

法国共和党人日历上的闰年计算是一个有争议的问题。默认情况下,convertdate使用秋分计算闰年。您还可以使用三种多年来提出的更系统的方法之一。

  • 罗姆姆是日历的共同创造者,他建议闰年的年份可以被4整除,但年份可以被100整除。
  • 有些协和式是在19世纪制定的,每4年给出闰年,在3除以4(19、23、27等)的年份中给出剩余的3。
  • von m_dler提出了闰年的概念,即闰年可以被4整除,但年份可以被128整除。

您可以在french_republican转换函数中使用method关键字参数指定这三个方法中的任何一个。

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 Mädler's method
french_republican.to_gregorian(20, 1, 1), method='madler')
# (1811, 9, 23)

所有的转换方法都正确地分配了使用日历时实现的闰年(3、7、11)。

在共同时代之前

对于公共纪元(第一年)之前的日期,convertdate使用 天文符号:公元前1年记为0,公元前2年记为-1,等等。 以忽略自定义为代价,使算术变得更容易。

注意,对于4ce之前的日期,convertdate使用proleptic Julian calendar。朱利安历法从公元前45年开始使用,但在公元前4年之前闰年的模式是不规则的。

proleptic Gregorian calendar用于1582年前的日期, 公历改革的一年。

假期

北美假日是当前的焦点。$ 模块,但欢迎拉取请求。

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)

实用程序

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)

其他实用功能:

  • 最近的工作日
  • 下一个或当前工作日
  • 上一个工作日
  • 上一个工作日或当前工作日

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
IE中的java跨域cookie问题   重复java中已经满足的循环   编译java RMI服务器时出错   JavaServlet POST中作为参数传递的javascript大型JSON数组数据为空   java片段未每次刷新/调用   java无法编译。错误消息   java如何构造大型类?   java Hibernate:TableThingsDB。事情并不存在   java如何操作从匹配项创建的数组。发现   循环以搜索和显示数组Java的某些部分   加载或注册SQLite JDBC驱动程序时出现java问题   活动和服务之间的java连接   JavaGWTG2D:ie8中的drawImage   java在安卓中设置hessian阈值   在Tomcat中使用Logback时发生java错误