Skip to content

Commit

Permalink
✨ New LunarDate for last day in a year
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Mar 29, 2024
1 parent 6abc535 commit 09d6fdb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
10 changes: 6 additions & 4 deletions borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,19 @@ class Period:
"""A shortcut methods for some specified date Period."""

@staticmethod
def solar_year(year: int) -> Tuple[date, date]:
return date(year, 1, 1), date(year, 12, 31)
def solar_year(year: int, end_year: int = 0) -> Tuple[date, date]:
end_year = end_year or year
return date(year, 1, 1), date(end_year, 12, 31)

@staticmethod
def solar_month(year: int, month: int) -> Tuple[date, date]:
ndays = calendar.monthrange(year, month)[1]
return date(year, month, 1), date(year, month, ndays)

@staticmethod
def lunar_year(year: int) -> Tuple[LunarDate, LunarDate]:
return LunarDate(year, 1, 1), LunarDate(year + 1, 1, 1) - timedelta(days=1)
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)

@staticmethod
def lunar_month(year: int, month: int, leap: int = _IGNORE_LEAP_MONTH) -> Tuple[LunarDate, LunarDate]:
Expand Down
8 changes: 7 additions & 1 deletion borax/calendars/lunardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,12 @@ def tomorrow(cls) -> 'LunarDate':
sd = datetime.date.today() + datetime.timedelta(days=1)
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]
return cls(year, month, day, leap)

@classmethod
def strptime(cls, date_str: str, date_fmt: str) -> 'LunarDate':
"""Parse a LunarDate object from a whole string.
Expand All @@ -678,7 +684,7 @@ def __sub__(self, other):
:param other: a instance of LunarDate / date / timedelta
:return:
"""
if hasattr(other, 'solar'):
if hasattr(other, 'solar'): # For WrappedDate in festivals2 module
return self.to_solar_date() - other.solar
elif isinstance(other, LunarDate):
return self.to_solar_date() - other.to_solar_date()
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## v4.1.1

- 新增创建农历年最后一天的方法 `LunarDate.last_day_of_year`
- `SolarFestival``LunarFestival` 初始化函数 `freq` 参数支持字符串设置( [ #56](https://github.com/kinegratii/borax/issues/56)
- `Period.solar_year``Period.lunar_year` 新增 `end_year` 参数,支持跨年份计算

## v4.1.0 (20240131)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_festival2_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def test_solar_period(self):
self.assertEqual(date(2020, 5, 1), sd2)
self.assertEqual(date(2020, 5, 31), ed2)

sd3, ed3 = Period.solar_year(2020, 2021)
self.assertEqual(date(2020, 1, 1), sd3)
self.assertEqual(date(2021, 12, 31), ed3)

def test_lunar_period(self):
sd1, ed1 = Period.lunar_year(2020)
self.assertEqual(LunarDate(2020, 1, 1), sd1)
Expand All @@ -188,6 +192,10 @@ def test_lunar_period(self):
self.assertEqual(LunarDate(2020, 5, 1, 0), sd5)
self.assertEqual(LunarDate(2020, 5, 30, 0), ed5)

sd6, ed6 = Period.lunar_year(2020, 2021)
self.assertEqual(LunarDate(2020, 1, 1), sd6)
self.assertEqual(LunarDate(2021, 12, 29), ed6)


class WrappedDateTestCase(unittest.TestCase):

Expand Down
2 changes: 2 additions & 0 deletions tests/test_lunardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def test_create_specific_dates(self):
self.assertEqual(5, LCalendars.delta(today.after(5), today))
self.assertEqual(-5, LCalendars.delta(today.before(5), today))

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

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 09d6fdb

Please sign in to comment.