Last active
April 23, 2020 11:34
-
-
Save jokull/032322e6e85aaf2a3c93fe39ea88cf3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Look at the split of non-working-day windows for each year | |
Produces something like this: | |
2020 - 117 frídagar (119 í frígluggum sem hefjast á árinu) | |
- 1x fimm daga frí - 9.4 (Holy Thursday) | |
- 2x fjögurra daga frí - 24.12 (Christmas Eve), 31.12 (New Year's Eve) | |
- 3x þriggja daga frí - 1.5 (Labour Day), 3.5 (Whit Monday), 1.8 (Commerce Day) | |
- 47x tveggja daga frí | |
- 3x eins daga frí - 23.4 (First day of summer), 21.5 (Ascension Thursday), 17.6 (Icelandic National Day) | |
2021 - 115 frídagar (114 í frígluggum sem hefjast á árinu) | |
- 1x fimm daga frí - 1.4 (Holy Thursday) | |
- 4x þriggja daga frí - 22.5 (Whit Monday), 31.7 (Commerce Day), 24.12 (Christmas Eve), 31.12 (New Year's Eve) | |
- 47x tveggja daga frí | |
- 3x eins daga frí - 22.4 (First day of summer), 13.5 (Ascension Thursday), 17.6 (Icelandic National Day) | |
2022 - 114 frídagar (113 í frígluggum sem hefjast á árinu) | |
- 1x fimm daga frí - 14.4 (Holy Thursday) | |
- 4x þriggja daga frí - 4.6 (Whit Monday), 17.6 (Icelandic National Day), 3.7 (Commerce Day), 24.12 (Christmas Eve) | |
- 47x tveggja daga frí | |
- 2x eins daga frí - 21.4 (First day of summer), 26.5 (Ascension Thursday) | |
2023 - 115 frídagar (115 í frígluggum sem hefjast á árinu) | |
- 1x fimm daga frí - 6.4 (Holy Thursday) | |
- 1x fjögurra daga frí - 23.12 (Christmas Eve) | |
- 4x þriggja daga frí - 29.4 (Labour Day), 27.5 (Whit Monday), 5.8 (Commerce Day), 3.12 (New Year's Eve) | |
- 46x tveggja daga frí | |
- 2x eins daga frí - 2.4 (First day of summer), 18.5 (Ascension Thursday) | |
""" | |
import datetime as dt | |
from collections import defaultdict | |
from workalendar.europe import Iceland | |
cal = Iceland() | |
this_year = dt.date.today().year | |
wordize = { | |
1: "eins", | |
2: "tveggja", | |
3: "þriggja", | |
4: "fjögurra", | |
5: "fimm", | |
6: "sex", | |
} | |
for year in range(this_year, this_year + 21): | |
holidays = dict(cal.holidays(year)) | |
day = dt.date(year, 1, 1) | |
total = 0 | |
while day.year == year: | |
if not cal.is_working_day(day): | |
total += 1 | |
day += dt.timedelta(days=1) | |
windows = defaultdict(list) | |
day = dt.date(year, 1, 1) | |
# Always start with the first working day of the year | |
while not cal.is_working_day(day): | |
day += dt.timedelta(days=1) | |
while day.year == year: | |
# we want to find each non-working day window | |
if not cal.is_working_day(day): | |
length = 0 | |
start = day | |
holiday = None | |
while not cal.is_working_day(day): | |
length += 1 | |
if holiday is None: | |
holiday = holidays.get(day) | |
day = day + dt.timedelta(days=1) | |
windows[length].append((start, day, holiday)) | |
day = day + dt.timedelta(days=1) | |
window_total = sum(i * len(j) for i, j in windows.items()) | |
shifts = window_total - total | |
header = ( | |
f"{year} - {total} frídagar ({window_total} í frígluggum sem hefjast á árinu)" | |
) | |
print(header) | |
for length, instances in sorted( | |
windows.items(), key=lambda pair: pair[0], reverse=True | |
): | |
occurances = len(instances) | |
message = f"- {occurances}x {wordize[length]} daga frí" | |
# add a date preview for non weekend windows | |
preview = ", ".join( | |
"{}{}".format( | |
i[0].strftime("%d.%m").replace("0", ""), | |
" ({})".format(i[2]) if i[2] else "", | |
) | |
for i in instances[:4] | |
if i[0].weekday() < 5 or length != 2 | |
) | |
if len(instances) > 4 and preview: | |
preview += "..." | |
if preview: | |
message = f"{message.ljust(24)} - {preview}" | |
print(message) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment