Skip to content

Commit acf9844

Browse files
committed
Add per-calendar default timezone (in meta header)
- A 'meta' dict at the top level can be used to specific metadata: in this case 'tz', in the future it could also be things like 'name' and so on. - Each event gets this timezone added to it: localized if it is naive. If it has a current timezone (non-'floating'), there is no effect. - Unknown interaction with rrules, but current tests imply that this does work. If not, it can be fixed later. - Review: anyone with ics knowledge could take a look. Also make sure that this doesn't break any other current semantics (though not specifying it should change nothing).
1 parent 464e18e commit acf9844

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

yaml2ics.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import yaml
22
import sys
33
import os
4-
from datetime import datetime
4+
from datetime import datetime, tzinfo
55

66
import ics
77
import dateutil
88
import dateutil.rrule
9+
from dateutil.tz import gettz
910

1011

1112
interval_type = {
@@ -19,7 +20,7 @@
1920
}
2021

2122

22-
def event_from_yaml(event_yaml: dict) -> ics.Event:
23+
def event_from_yaml(event_yaml: dict, tz: tzinfo=None) -> ics.Event:
2324
d = event_yaml
2425
repeat = d.pop('repeat', None)
2526

@@ -73,6 +74,8 @@ def event_from_yaml(event_yaml: dict) -> ics.Event:
7374
))
7475

7576
event.dtstamp = datetime.utcnow().replace(tzinfo=dateutil.tz.UTC)
77+
if tz and event.floating:
78+
event.replace_timezone(tz)
7679
return event
7780

7881

@@ -85,13 +88,18 @@ def events_to_calendar(events: list) -> str:
8588
def files_to_calendar(files: list) -> ics.Calendar:
8689
"""'main' function: list of files to our result"""
8790
all_events = [ ]
91+
name = None
8892
for f in files:
8993
if hasattr(f, 'read'):
9094
calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
9195
else:
9296
calendar_yaml = yaml.load(open(f, 'r'), Loader=yaml.FullLoader)
97+
tz = None
98+
if 'meta' in calendar_yaml:
99+
if 'tz' in calendar_yaml['meta']:
100+
tz = gettz(calendar_yaml['meta']['tz'])
93101
for event in calendar_yaml['events']:
94-
all_events.append(event_from_yaml(event))
102+
all_events.append(event_from_yaml(event, tz=tz))
95103
calendar = events_to_calendar(all_events)
96104
return calendar
97105

0 commit comments

Comments
 (0)