From 366acb0eae428c7ac3ce8c0534d26cc03e9e1049 Mon Sep 17 00:00:00 2001 From: bardrop Date: Thu, 26 Nov 2026 12:41:13 +0530 Subject: [PATCH 1/2] Raise ValueError for solar dates outside the supported range from_solar_date() returned bad dates instead of failing when the solar date was before 1900-01-31 or on/after 2100-02-09. For example from_solar_date(2100, 2, 9) gave LunarDate(2099, 1, 1, 0), and from_solar_date(1900, 1, 1) gave LunarDate(1900, 1, -29, 0). Adding a timedelta past the last day had the same problem. --- lunardate.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lunardate.py b/lunardate.py index 04d348e..077cfbd 100644 --- a/lunardate.py +++ b/lunardate.py @@ -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 @@ -396,6 +410,10 @@ def _calc_month_day(year_info: int, offset: int) -> tuple[int, int, int]: offset = int(offset) + if offset < 0 or offset >= sum(YEAR_DAYS): + end_date = cls._start_date + datetime.timedelta(days=sum(YEAR_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 From c644b6de7c331d6f23cbe546dfc03133dc4fcc81 Mon Sep 17 00:00:00 2001 From: bardrop Date: Mon, 30 Nov 2026 00:18:33 +0530 Subject: [PATCH 2/2] Compute the total day count and end date once --- lunardate.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lunardate.py b/lunardate.py index 077cfbd..a8661f5 100644 --- a/lunardate.py +++ b/lunardate.py @@ -410,9 +410,8 @@ def _calc_month_day(year_info: int, offset: int) -> tuple[int, int, int]: offset = int(offset) - if offset < 0 or offset >= sum(YEAR_DAYS): - end_date = cls._start_date + datetime.timedelta(days=sum(YEAR_DAYS)) - raise ValueError('solar date out of range [{}, {})'.format(cls._start_date, end_date)) + 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: @@ -511,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)