Skip to content

Commit

Permalink
Add subdivisions support
Browse files Browse the repository at this point in the history
  • Loading branch information
KJhellico committed Nov 29, 2023
1 parent e1e0963 commit 3077dcd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
25 changes: 11 additions & 14 deletions holidays/holiday_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,10 @@ def _add_substituted_holidays(self):
substituted_label = self.tr(self.substituted_label)
substituted_date_format = self.tr(self.substituted_date_format)

substituted_holidays_mapping_names = ["substituted_holidays"]
# Check category specific special observed holidays.
for category in sorted(self.categories):
substituted_holidays_mapping_names.append(f"substituted_{category}_holidays")

for mapping_name in substituted_holidays_mapping_names:
for hol in _normalize_tuple(getattr(self, mapping_name, {}).get(self._year, ())):
for mapping_name in self._get_static_holiday_mapping_names():
for hol in _normalize_tuple(
getattr(self, f"substituted_{mapping_name}", {}).get(self._year, ())
):
from_year = hol[0] if len(hol) == 5 else self._year
from_month, from_day, to_month, to_day = hol[-4:]
from_date = date(from_year, from_month, from_day).strftime(substituted_date_format)
Expand Down Expand Up @@ -758,30 +755,30 @@ def _populate(self, year: int) -> None:
# Populate substituted holidays.
self._add_substituted_holidays()

def _get_special_holiday_mapping_names(self):
def _get_static_holiday_mapping_names(self):
# Check for general special holidays.
mapping_names = ["special_holidays"]
mapping_names = ["holidays"]

# Check subdivision specific special holidays.
if self.subdiv is not None:
subdiv = self.subdiv.replace("-", "_").replace(" ", "_").lower()
mapping_names.append(f"special_{subdiv}_holidays")
mapping_names.append(f"{subdiv}_holidays")

# Check category specific special holidays (both general and per subdivision).
for category in sorted(self.categories):
mapping_names.append(f"special_{category}_holidays")
mapping_names.append(f"{category}_holidays")
if self.subdiv is not None:
mapping_names.append(f"special_{subdiv}_{category}_holidays")
mapping_names.append(f"{subdiv}_{category}_holidays")

return mapping_names

def _add_special_holidays(self):
if not hasattr(self, "_has_special"):
return None

for mapping_name in self._get_special_holiday_mapping_names():
for mapping_name in self._get_static_holiday_mapping_names():
for month, day, name in _normalize_tuple(
getattr(self, mapping_name, {}).get(self._year, ())
getattr(self, f"special_{mapping_name}", {}).get(self._year, ())
):
self._add_holiday(name, date(self._year, month, day))

Expand Down
4 changes: 2 additions & 2 deletions holidays/observed_holiday_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ def _add_special_holidays(self):
if not self.observed:
return None

for mapping_name in self._get_special_holiday_mapping_names():
for mapping_name in self._get_static_holiday_mapping_names():
for month, day, name in _normalize_tuple(
getattr(self, f"{mapping_name}_observed", {}).get(self._year, ())
getattr(self, f"special_{mapping_name}_observed", {}).get(self._year, ())
):
self._add_holiday(
self.tr(self.observed_label) % self.tr(name), date(self._year, month, day)
Expand Down

0 comments on commit 3077dcd

Please sign in to comment.