Skip to content

Commit

Permalink
✨ New LunarDate for last day in a year or a month
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Apr 2, 2024
1 parent 960ddbf commit 85aec53
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def solar_month(year: int, month: int) -> Tuple[date, date]:
@staticmethod
def lunar_year(year: int, end_year: int = 0) -> Tuple[LunarDate, LunarDate]:
end_year = end_year or year
return LunarDate(year, 1, 1), LunarDate.last_day_of_year(end_year)
return LunarDate(year, 1, 1), LunarDate.last_day(end_year)

@staticmethod
def lunar_month(year: int, month: int, leap: int = _IGNORE_LEAP_MONTH) -> Tuple[LunarDate, LunarDate]:
Expand Down
18 changes: 15 additions & 3 deletions borax/calendars/lunardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,21 @@ def tomorrow(cls) -> 'LunarDate':
return cls.from_solar_date(sd.year, sd.month, sd.day)

@classmethod
def last_day_of_year(cls, year: int) -> 'LunarDate':
"""return the last day in a lunar year."""
month, day, leap = list(LCalendars.iter_year_month(year))[-1]
def last_day(cls, year: int, month: int = 0, leap: int = 0) -> 'LunarDate':
"""return the last day in a lunar year or a lunar month."""
mdls = list(LCalendars.iter_year_month(year))
if month == 0:
index = -1
else:
leap_month_of_year = LCalendars.leap_month(year)
if leap:
if month == leap_month_of_year:
index = month
else:
raise InvalidLunarDateError(f'{year=}, {month=},leap=1 does not exist.')
else:
index = month - 1 + int(leap_month_of_year and month > leap_month_of_year)
month, day, leap = mdls[index]
return cls(year, month, day, leap)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## v4.1.1

- 新增创建农历年最后一天的方法 `LunarDate.last_day_of_year`
- 新增创建农历年或农历月最后一天的方法 `LunarDate.last_day`
- `SolarFestival``LunarFestival` 初始化函数 `freq` 参数支持字符串设置( [ #56](https://github.com/kinegratii/borax/issues/56)
- `Period.solar_year``Period.lunar_year` 新增 `end_year` 参数,支持跨年份计算
- 废弃模块:`borax.choices`
Expand Down
11 changes: 11 additions & 0 deletions tests/test_lunardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def test_create_specific_dates(self):

self.assertEqual(LunarDate(2023, 12, 30), LunarDate.last_day_of_year(2023))

def test_last_day(self):
self.assertEqual(LunarDate(2023, 12, 30), LunarDate.last_day(2023))
self.assertEqual(LunarDate(2023, 1, 29), LunarDate.last_day(2023, 1))
self.assertEqual(LunarDate(2023, 2, 30), LunarDate.last_day(2023, 2))
self.assertEqual(LunarDate(2023, 2, 29, 1), LunarDate.last_day(2023, 2, leap=1))
self.assertEqual(LunarDate(2023, 3, 29), LunarDate.last_day(2023, 3))

self.assertEqual(LunarDate(2024, 12, 29), LunarDate.last_day(2024))
with self.assertRaises(Exception):
LunarDate.last_day(2024, 2, 1)

def test_convert_datetime(self):
dt = LunarDate(1976, 8, 8, 1).to_solar_date()
self.assertEqual(date(1976, 10, 1), dt)
Expand Down

0 comments on commit 85aec53

Please sign in to comment.