Beispiel #1
0
    def test_sub(self):
        test_cases = [(ZhDate(2019, 1, 1), ZhDate(2018, 1, 1), 354),
                      (ZhDate(2019, 1, 1), 354, ZhDate(2018, 1, 1)),
                      (ZhDate(2019, 1, 1), datetime(2018, 2, 16), 354)]

        for case in test_cases:
            self.assertEqual(case[0] - case[1], case[2])
Beispiel #2
0
 def zhongqiu(self):
     lunar = ZhDate(self.year, 8, 15)  # 新建农历 2010年正月初一 的日期对象
     solar = lunar.to_datetime()
     # solar = pd.date_range(str(solar).split(' ')[0], periods=1)
     str_solar = str(solar).split(' ')[0]
     year, month, day = str_solar.split('-')[0], str_solar.split(
         '-')[1], str_solar.split('-')[-1]
     return self.some_holiday(year, month, day)
Beispiel #3
0
 def chunjie(self):
     lunar = ZhDate(self.year, 1, 1)
     solar = lunar.to_datetime()
     #solar = pd.date_range(str(solar).split(' ')[0], periods=1)
     str_solar = str(solar).split(' ')[0]
     year, month, day = str_solar.split('-')[0], str_solar.split(
         '-')[1], str_solar.split('-')[-1]
     chuxi_solar = datetime(int(year), int(month),
                            int(day)) + timedelta(days=-1)
     # chuxi_time, make_chuxi_time = self.Spring_Festival(chuxi_solar)
     return self.Spring_Festival(chuxi_solar)
 def lunar_to_solar(cls, year: int, month: int, day: int):
     """
     convert lunar date to solar date
     :param year:
     :param month:
     :param day:
     :return: solar date
     """
     lunar = ZhDate(year, month, day)
     solar_datetime = lunar.to_datetime()
     return solar_datetime.year, solar_datetime.month, solar_datetime.day
Beispiel #5
0
def getYearData(year, one, yinli):
    date_2_price = {}

    for (k, v) in date2price.items():
        if k.year == int(year):
            if yinli:
                day = ZhDate.from_datetime(k)
                if day.lunar_month == 2 and day.lunar_day >= 29:
                    continue
                k = datetime.date(day.lunar_year, day.lunar_month,
                                  day.lunar_day)
            if not one:
                k = datetime.date(2020, k.month, k.day)
            date_2_price[k] = v
    dates = sorted(date_2_price)
    prices = []
    for k in dates:
        prices.append(date_2_price[k])
    return [dates, prices]
Beispiel #6
0
from zhdate import ZhDate
from datetime import datetime, timedelta

if __name__ == '__main__':
    date1 = ZhDate(2010, 1, 1)  # 新建农历 2010年正月初一 的日期对象
    # print(date1)  # 直接返回农历日期字符串
    dt_date1 = date1.to_datetime()  # 农历转换成阳历日期 datetime 类型

    dt_date2 = datetime(2010, 2, 6)
    date2 = ZhDate.from_datetime(dt_date2)  # 从阳历日期转换成农历日期对象

    date3 = ZhDate(2020, 4, 29, leap_month=True)  # 新建农历 2020年闰4月30日
    # print(date3.to_datetime())

    # 支持比较
    if ZhDate(2019, 1, 1) == ZhDate.from_datetime(datetime(2019, 2, 5)):
        pass

    # 减法支持
    new_zhdate = ZhDate(2019, 1, 1) - 30  #减整数,得到差额天数的新农历对象
    new_zhdate2 = ZhDate(2019, 1, 1) - ZhDate(2018, 1,
                                              1)  #两个zhdate对象相减得到两个农历日期的差额
    new_zhdate3 = ZhDate(2019, 1, 1) - datetime(2019, 1,
                                                1)  # 减去阳历日期,得到农历日期和阳历日期之间的天数差额

    # 加法支持
    new_zhdate4 = ZhDate(2019, 1, 1) + 30  # 加整数返回相隔天数以后的新农历对象

    print(ZhDate(1900, 9, 1, False).chinese())
    print(ZhDate.today().chinese())
Beispiel #7
0
def get_lunar_date():
    return str(ZhDate.from_datetime(date_now))
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
try:
    from lunar import Lunar
except:
    from .lunar import Lunar

import pandas as pd

for y in range(1901, 2100):
    print(f"检查{y}年")
    date_range = pd.date_range(f"{y}0101", f"{y}1231", closed=None)
    for i in date_range:
        dtm = i.to_pydatetime()
        if y in [
                2034,
        ] and dtm in [datetime.datetime(2034, 1, 1)]:
            continue
        l = Lunar(dtm)
        l2 = ZhDate.from_datetime(dtm)
        try:
            assert (l2.lunar_year, l2.lunar_month,
                    l2.lunar_day) == l.get_lunarDateNum()
            assert l2.leap_month == l.isLunarLeapMonth
        except Exception as e:
            print(dtm)
            print((l2.lunar_year, l2.lunar_month, l2.lunar_day), l2.leap_month)
            print(l.get_lunarDateNum(), l.isLunarLeapMonth)
            print(e)
            raise e