-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheilagdagar.py
executable file
·104 lines (89 loc) · 2.78 KB
/
heilagdagar.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
import time
import re
from datetime import timedelta, date, datetime
from dateutil.easter import easter
from ics import Calendar, Event
def generate_norwegian_holidays(year):
easter_sunday = easter(year)
return [
# Fixed holidays
(date(year, 1, 1), "Nyttårsdag", "https://nn.wikipedia.org/wiki/Nytt%C3%A5r"),
(date(year, 12, 25), "Førstedag jul", "https://nn.wikipedia.org/wiki/Juledag"),
(
date(year, 12, 26),
"Annandag jul",
"https://nn.wikipedia.org/wiki/Andre_juledag",
),
(
date(year, 5, 1),
"Internasjonal arbeidardag",
"https://nn.wikipedia.org/wiki/F%C3%B8rste_mai",
),
(
date(year, 5, 17),
"Grunnlovsdagen",
"https://nn.wikipedia.org/wiki/Den_norske_grunnlovsdagen",
),
# Easter, pentecost, etc.
(
easter_sunday - timedelta(days=3),
"Skjærtorsdag",
"https://nn.wikipedia.org/wiki/Skj%C3%A6rtorsdag",
),
(
easter_sunday - timedelta(days=2),
"Langfredag",
"https://nn.wikipedia.org/wiki/Langfredag",
),
(easter_sunday, "Påskedagen", "https://nn.wikipedia.org/wiki/P%C3%A5skedag"),
(
easter_sunday + timedelta(days=1),
"Annandag påske",
"https://nn.wikipedia.org/wiki/Andre_p%C3%A5skedag",
),
(
easter_sunday + timedelta(days=39),
"Kristi himmelfartsdag",
"https://nn.wikipedia.org/wiki/Kristi_himmelferdsdag",
),
(
easter_sunday + timedelta(days=49),
"Førstedag pinse",
"https://nn.wikipedia.org/wiki/Pinse",
),
(
easter_sunday + timedelta(days=50),
"Annandag pinse",
"https://nn.wikipedia.org/wiki/Andre_pinsedag",
),
]
def make_uid(name: str, year):
clean = re.sub(r"\s+", "-", name).lower()
return f"no-{clean}-{year}@example.com"
def make_holiday_event(created, holiday):
dt, name, url = holiday
e = Event()
e.uid = make_uid(name, dt.year)
e.name = name
e.begin = dt
e.make_all_day()
e.duration = timedelta(days=1)
e.created = created
e.url = url
return e
def make_ical_file(holidays):
created = datetime.now().astimezone()
return Calendar(
events=[make_holiday_event(created, holiday) for holiday in holidays]
)
def main():
current_year = time.localtime().tm_year
holidays = [
holiday
for year in range(current_year - 1, current_year + 6)
for holiday in generate_norwegian_holidays(year)
]
print(make_ical_file(holidays))
if __name__ == "__main__":
main()