Skip to content

Commit

Permalink
Merge pull request #59 from kinegratii/develop
Browse files Browse the repository at this point in the history
Release v4.1.1
  • Loading branch information
kinegratii authored Jun 30, 2024
2 parents 2735fe7 + 80dec66 commit e7a75d4
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 136 deletions.
2 changes: 1 addition & 1 deletion borax/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '4.1.0'
__version__ = '4.1.1'
__author__ = 'kinegratii'
27 changes: 20 additions & 7 deletions borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class FreqConst:
YEARLY = 0
MONTHLY = 1

LABEL2VAL = {'monthly': 1, 'yearly': 0, 'm': 1, 'y': 0}


class FestivalCatalog:
basic = 'basic'
Expand Down Expand Up @@ -167,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(end_year)

@staticmethod
def lunar_month(year: int, month: int, leap: int = _IGNORE_LEAP_MONTH) -> Tuple[LunarDate, LunarDate]:
Expand Down Expand Up @@ -460,12 +464,17 @@ class SolarFestival(Festival):
"""
date_class = date

def __init__(self, *, day: int, freq: int = FreqConst.YEARLY, month: int = 0, name: str = None):
def __init__(self, *, day: int, freq: Union[int, Literal['yearly', 'monthly', 'y', 'm']] = FreqConst.YEARLY,
month: int = 0, name: str = None):
if day < 0:
day = -day
reverse = 1
else:
reverse = 0
if isinstance(freq, str):
freq = FreqConst.LABEL2VAL.get(freq, -1)
if freq == -1:
raise ValueError('Invalid freq string.')
super().__init__(name=name, freq=freq, month=month, day=day, reverse=reverse, schema=FestivalSchema.SOLAR)

def _get_description(self) -> str:
Expand Down Expand Up @@ -680,13 +689,17 @@ class LunarFestival(Festival):
"""
date_class = LunarDate

def __init__(self, *, day: int, freq: int = FreqConst.YEARLY, month: int = 0, leap: int = _IGNORE_LEAP_MONTH,
name: str = None):
def __init__(self, *, day: int, freq: Union[int, Literal['yearly', 'monthly', 'y', 'm']] = FreqConst.YEARLY,
month: int = 0, leap: int = _IGNORE_LEAP_MONTH, name: str = None):
if day < 0:
day = -day
reverse = 1
else:
reverse = 0
if isinstance(freq, str):
freq = FreqConst.LABEL2VAL.get(freq, -1)
if freq == -1:
raise ValueError('Invalid freq string.')
super().__init__(freq=freq, name=name, month=month, day=day, leap=leap, reverse=reverse,
schema=FestivalSchema.LUNAR)

Expand Down
20 changes: 19 additions & 1 deletion borax/calendars/lunardate.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,24 @@ 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(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
def strptime(cls, date_str: str, date_fmt: str) -> 'LunarDate':
"""Parse a LunarDate object from a whole string.
Expand All @@ -678,7 +696,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
13 changes: 7 additions & 6 deletions borax/capp/borax_calendar_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from borax.calendars.ui import CalendarFrame, FestivalTableFrame
from borax.calendars.utils import ThreeNineUtils
from borax.capp.festival_creator import FestivalCreatePanel
from borax.ui.widgets import MessageLabel

library = FestivalLibrary.load_builtin().sort_by_countdown()

Expand Down Expand Up @@ -82,7 +83,7 @@ def __init__(self, master=None):

ttk.Button(self._tool_form_frame, text='计算', command=self.run_date_delta).grid(
row=2, column=0, columnspan=4, pady=8)
self.result1_label = ttk.Label(self._tool_form_frame, text='')
self.result1_label = MessageLabel(self._tool_form_frame, text='')
self.result1_label.grid(row=3, column=0, columnspan=4)
notebook.add(self._tool_form_frame, text='日期间隔', padding=4)

Expand All @@ -104,7 +105,7 @@ def __init__(self, master=None):
delta_days_com.grid(row=2, column=2, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S)
ttk.Button(deduction_frame, text='计算', command=self.run_date_deduction).grid(
row=3, column=0, columnspan=4, pady=8)
self.result2_label = ttk.Label(deduction_frame, text='')
self.result2_label = MessageLabel(deduction_frame, text='')
self.result2_label.grid(row=4, column=0, columnspan=4)
# init
self.day_delta_s.set(1)
Expand All @@ -131,17 +132,17 @@ def run_date_delta(self):
d1, d2 = self._data_stores['d1'].get_date(), self._data_stores['d2'].get_date()
if d1 and d2:
ndays = (d2.solar - d1.solar).days
self.result1_label.config(text=f'相差 {ndays} 天')
self.result1_label.show_text(text=f'相差 {ndays} 天')
else:
self.result1_label.config(text='未选择日期,无法计算')
self.result1_label.show_error_splash(text='未选择日期,无法计算')

def run_date_deduction(self):
d3 = self._data_stores['d3'].get_date()
if d3:
result2 = d3 + timedelta(self.day_delta_s.get() * self.delta_days.get())
self.result2_label.config(text=str(result2))
self.result2_label.show_text(str(result2))
else:
self.result2_label.config(text='未选择日期,无法计算')
self.result2_label.show_error_splash(text='未选择日期,无法计算')


class DateDetailFrame(ttk.LabelFrame):
Expand Down
Loading

0 comments on commit e7a75d4

Please sign in to comment.