HPI/my/calendar/holidays.py

49 lines
1.2 KiB
Python

"""
Holidays and days off work
"""
REQUIRES = [
'workalendar', # library to determine public holidays
]
from datetime import date, datetime, timedelta
from typing import Union
# TODO would be nice to do it dynamically depending on the past timezones...
from workalendar.europe import UnitedKingdom # type: ignore
cal = UnitedKingdom()
# TODO that should depend on country/'location' of residence I suppose?
# todo move to common?
DateIsh = Union[datetime, date, str]
def as_date(dd: DateIsh) -> date:
if isinstance(dd, datetime):
return dd.date()
elif isinstance(dd, date):
return dd
else:
# todo parse isoformat??
return as_date(datetime.strptime(dd, '%Y%m%d'))
def is_holiday(d: DateIsh) -> bool:
day = as_date(d)
return not cal.is_working_day(day)
def is_workday(d: DateIsh) -> bool:
return not is_holiday(d)
from ..core.common import Stats
def stats() -> Stats:
# meh, but not sure what would be a better test?
res = {}
year = datetime.now().year
jan1 = date(year=year, month=1, day=1)
for x in range(-7, 20):
d = jan1 + timedelta(days=x)
h = is_holiday(d)
res[d.isoformat()] = 'holiday' if h else 'workday'
return res