Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lunardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ def from_solar_date(year: int, month: int, day: int) -> LunarDate:
LunarDate(2033, 10, 1, 0)
>>> LunarDate.from_solar_date(1956, 12, 2)
LunarDate(1956, 11, 1, 0)
>>> LunarDate.from_solar_date(2100, 2, 8)
LunarDate(2099, 12, 30, 0)
>>> LunarDate.from_solar_date(1900, 1, 30)
Traceback (most recent call last):
...
ValueError: solar date out of range [1900-01-31, 2100-02-09)
>>> LunarDate.from_solar_date(2100, 2, 9)
Traceback (most recent call last):
...
ValueError: solar date out of range [1900-01-31, 2100-02-09)
>>> LunarDate(2099, 12, 30) + datetime.timedelta(days=1)
Traceback (most recent call last):
...
ValueError: solar date out of range [1900-01-31, 2100-02-09)
'''
solar_date = datetime.date(year, month, day)
offset = (solar_date - LunarDate._start_date).days
Expand Down Expand Up @@ -396,6 +410,9 @@ def _calc_month_day(year_info: int, offset: int) -> tuple[int, int, int]:

offset = int(offset)

if offset < 0 or offset >= TOTAL_DAYS:
raise ValueError('solar date out of range [{}, {})'.format(cls._start_date, END_DATE))

for idx, year_day in enumerate(YEAR_DAYS):
if offset < year_day:
break
Expand Down Expand Up @@ -493,6 +510,8 @@ def year_info_to_year_day(year_info: int) -> int:
return res

YEAR_DAYS: list[int] = [year_info_to_year_day(x) for x in YEAR_INFOS]
TOTAL_DAYS: int = sum(YEAR_DAYS)
END_DATE: datetime.date = LunarDate._start_date + datetime.timedelta(days=TOTAL_DAYS)

def day_to_lunar_date(offset: int) -> LunarDate:
return LunarDate._from_offset(offset)
Expand Down
Loading