Skip to content

Commit a33769b

Browse files
KJhellicoarkid15r
andauthored
Add HolidayBase::is_weekend method (#2780)
Co-authored-by: Arkadii Yakovets <[email protected]>
1 parent d9b36d0 commit a33769b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

holidays/holiday_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,20 @@ def get_working_days_count(self, start: DateLike, end: DateLike) -> int:
11351135
days = (dt2 - dt1).days + 1
11361136
return sum(self.is_working_day(_timedelta(dt1, n)) for n in range(days))
11371137

1138+
def is_weekend(self, key: DateLike) -> bool:
1139+
"""Check if the given date's week day is a weekend day.
1140+
1141+
Args:
1142+
key:
1143+
The date to check.
1144+
1145+
Returns:
1146+
True if the date's week day is a weekend day, False otherwise.
1147+
"""
1148+
# To prioritize performance we avoid reusing the internal
1149+
# `HolidayBase._is_weekend` method and perform the check directly instead.
1150+
return self.__keytransform__(key).weekday() in self.weekend
1151+
11381152
def is_working_day(self, key: DateLike) -> bool:
11391153
"""Check if the given date is considered a working day.
11401154

tests/test_holiday_base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,29 @@ def _populate(self, year):
696696
self.assertIn("2020-07-13", hb)
697697

698698

699+
class TestIsWeekend(unittest.TestCase):
700+
def setUp(self):
701+
self.hb = CountryStub1()
702+
703+
def test_is_weekend(self):
704+
self.hb._populate(2022)
705+
706+
self.hb.weekend = {MON, TUE}
707+
for dt in (date(2022, 10, 3), date(2022, 10, 4), "2022-10-03", "2022-10-04"):
708+
self.assertTrue(self.hb.is_weekend(dt))
709+
710+
self.hb.weekend = set()
711+
for dt in (date(2022, 10, 3), date(2022, 10, 4), "2022-10-03", "2022-10-04"):
712+
self.assertFalse(self.hb.is_weekend(dt))
713+
714+
self.hb.weekend = {SAT, SUN}
715+
for dt in (date(2022, 10, 1), date(2022, 10, 2), "2022-10-01", "2022-10-02"):
716+
self.assertTrue(self.hb.is_weekend(dt))
717+
718+
for dt in (date(2022, 10, 3), date(2022, 10, 4), "2022-10-03", "2022-10-04"):
719+
self.assertFalse(self.hb.is_weekend(dt))
720+
721+
699722
class TestKeyTransforms(unittest.TestCase):
700723
def setUp(self):
701724
self.hb = CountryStub1()

0 commit comments

Comments
 (0)