my.calendar.holidays: unhardcode calendar, detect it from the location data
This commit is contained in:
parent
bdb5dcd221
commit
96113ad5ae
5 changed files with 49 additions and 7 deletions
|
@ -6,14 +6,24 @@ REQUIRES = [
|
|||
]
|
||||
|
||||
from datetime import date, datetime, timedelta
|
||||
from functools import lru_cache
|
||||
from typing import Union
|
||||
|
||||
from ..core.time import zone_to_countrycode
|
||||
|
||||
# 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?
|
||||
|
||||
@lru_cache(1)
|
||||
def _calendar():
|
||||
from workalendar.registry import registry # type: ignore
|
||||
# todo switch to using time.tz.main once _get_tz stabilizes?
|
||||
from ..time.tz import via_location as LTZ
|
||||
# TODO would be nice to do it dynamically depending on the past timezones...
|
||||
tz = LTZ._get_tz(datetime.now())
|
||||
assert tz is not None
|
||||
|
||||
code = zone_to_countrycode(tz.zone)
|
||||
Cal = registry.get_calendars()[code]
|
||||
return Cal()
|
||||
|
||||
# todo move to common?
|
||||
DateIsh = Union[datetime, date, str]
|
||||
|
@ -29,7 +39,7 @@ def as_date(dd: DateIsh) -> date:
|
|||
|
||||
def is_holiday(d: DateIsh) -> bool:
|
||||
day = as_date(d)
|
||||
return not cal.is_working_day(day)
|
||||
return not _calendar().is_working_day(day)
|
||||
|
||||
|
||||
def is_workday(d: DateIsh) -> bool:
|
||||
|
|
|
@ -15,3 +15,18 @@ tz_lookup['UTC'] = pytz.utc # ugh. otherwise it'z Zulu...
|
|||
@lru_cache(None)
|
||||
def abbr_to_timezone(abbr: str) -> tzinfo:
|
||||
return tz_lookup[abbr]
|
||||
|
||||
|
||||
def zone_to_countrycode(zone: str) -> str:
|
||||
# todo make optional?
|
||||
return _zones_to_countrycode()[zone]
|
||||
|
||||
|
||||
@lru_cache(1)
|
||||
def _zones_to_countrycode():
|
||||
# https://stackoverflow.com/a/13020785/706389
|
||||
res = {}
|
||||
for countrycode, timezones in pytz.country_timezones.items():
|
||||
for timezone in timezones:
|
||||
res[timezone] = countrycode
|
||||
return res
|
||||
|
|
|
@ -119,6 +119,7 @@ def _get_home_tz(loc) -> Optional[pytz.BaseTzInfo]:
|
|||
return pytz.timezone(zone)
|
||||
|
||||
|
||||
# TODO expose? to main as well?
|
||||
def _get_tz(dt: datetime) -> Optional[pytz.BaseTzInfo]:
|
||||
res = _get_day_tz(d=dt.date())
|
||||
if res is not None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue