处理中国历法的图书馆
lunisolar的Python项目详细描述
lunisolar是一个处理中国日历的python包。lunisolar包含一组帮助函数,用于方便在公历(西方)和中国历法之间进行转换。
基于Helmer Aslaksen的工作。构建在日历3.0的python实现PyCalCal之上,这是一组与日历相关的算法,如Dershowitz and Reingold’s book “Calendrical Calculations”所述。
用法
初始化中文日期:
>>> from lunisolar import ChineseDate >>> mid_autumn = ChineseDate.from_chinese(chinese_year=2013, chinese_month=8, chinese_day=15, is_leap_month=False) >>> mid_autumn chinese_date(year=2013, month=8, day=15, is_leap_month=False) >>> mid_autumn.gregorian_date datetime.date(2013, 9, 19)
中文日期对象可以从公历(西方)日期初始化:
>>> from lunisolar import ChineseDate >>> moon_landing = ChineseDate.from_gregorian(1969, 7, 20) >>> moon_landing chinese_date(year=1969, month=6, day=7, is_leap_month=False)
chinestade类与datetime共享相同的构造函数。日期:
>>> ChineseDate.today() chinese_date(year=2012, month=12, day=29, is_leap_month=False) >>> timestamp = 1360414893.724195 >>> ChineseDate.fromtimestamp(timestamp) chinese_date(year=2012, month=12, day=29, is_leap_month=False) >>> ordinal = 734908 >>> ChineseDate.fromordinal(ordinal) chinese_date(year=2012, month=12, day=29, is_leap_month=False)
正在检索中文日历的属性:
>>> moon_landing = ChineseDate.from_gregorian(1969, 7, 20) >>> moon_landing.year 1969 >>> moon_landing.month 6 >>> moon_landing.day 7 >>> moon_landing.is_leap_month False >>> moon_landing.zodiac rooster >>> moon_landing.element earth >>> moon_landing.heavenly_stem ji >>> moon_landing.earthly_branch you
chinestade的加法、减法和比较运算符与datetime.date对象的运算符类似。为了进行减法和比较,chinestade和datetime.date可以互换使用。
>>> from datetime import timedelta >>> cdate = ChineseDate.from_gregorian(1969, 7, 20) >>> gdate = datetime.date(2013, 2, 10) >>> cdate > gdate False >>> gdate - cdate datetime.timedelta(15911) >>> diff = timedelta(200) >>> cdate + diff chinese_date(year=1969, month=12, day=29, is_leap_month=False)