-
Notifications
You must be signed in to change notification settings - Fork 0
/
holidays.py
64 lines (51 loc) · 1.79 KB
/
holidays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import my.orig.my.calendar.holidays as M
from my.orig.my.calendar.holidays import *
is_holiday_orig = M.is_holiday
def is_holiday(d: DateIsh) -> bool:
# if it's a public holiday, definitely a holiday?
if is_holiday_orig(d):
return True
# then check private data of days off work
if is_day_off_work(d):
return True
return False
M.is_holiday = is_holiday
# NOTE: without overriding the original, the functions from M itself are capturing the old function?
# need to test it...
###
from datetime import datetime, date, datetime
from functools import lru_cache
from typing import Iterable, Tuple, List
import re
def is_day_off_work(d: DateIsh) -> bool:
day = as_date(d)
return day in _days_off_work()
@lru_cache(1)
def _days_off_work() -> List[date]:
return list(_iter_days_off_work())
def _iter_work_data() -> Iterable[Tuple[date, int]]:
from my.config.holidays_data import HOLIDAYS_DATA
emitted = 0
for x in HOLIDAYS_DATA.splitlines():
m = re.search(r'(\d\d/\d\d/\d\d\d\d)(.*)-(\d+.\d+) days \d+.\d+ days', x)
if m is None:
continue
(ds, cmnt, dayss) = m.groups()
if 'carry over' in cmnt:
continue
d = datetime.strptime(ds, '%d/%m/%Y').date()
dd, u = dayss.split('.')
assert u == '00' # TODO meh
yield d, int(dd)
emitted += 1
assert emitted > 5 # arbitrary, just a sanity check.. (todo move to tests?)
def _iter_days_off_work() -> Iterable[date]:
# TODO wtf is it doing?...
for d, span in _iter_work_data():
dd = d
while span > 0:
# only count it if it wasnt' a public holiday/weekend already
if M._calendar().is_working_day(dd):
yield dd
span -= 1
dd += timedelta(days=1)