Skip to content

Commit caaa5b5

Browse files
authored
yaml2ics: Allow a "timezone" option within each event. (#22)
* yaml2ics: Allow a "timezone" option within each event. - I at first tried using the yaml datetime "+03:00" explicit offset. But, this created an output that worked in Google Calendar/Thunderbird, but not office 365 cloud. The problem seems to relate to the quoting of `"UTC +03:00"` in one place but not the other (and, as it turns out, it didn't work with either quotes there, or no quotes there!) - I had made the idea in this PR earlier, but didn't submit it because "less is more". But now this seems simpler (and more explicit somehow). - Changes are minimal and it just worked in one of my test casse. There is no effect if 'timezone' is not given in an event. - Review: decide if this is a good idea or not. Is there any simpler way to implement it? * .pre-commit-config: Bump black (click incompatibility) - As described at the top of psf/black#2964
1 parent 1464735 commit caaa5b5

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repos:
99
- id: end-of-file-fixer
1010
- id: trailing-whitespace
1111
- repo: https://github.com/psf/black
12-
rev: 22.1.0
12+
rev: 22.3.0
1313
hooks:
1414
- id: black
1515
- repo: https://gitlab.com/pycqa/flake8

example/test_calendar.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ events:
1515
location: |
1616
Office 224, Monolith Bldg, Office Block C
1717
18+
- summary: 15-minute meeting with timezone
19+
timezone: America/New_York # symbolic timezone offset
20+
begin: 2021-09-23 15:00:00
21+
end: 2021-09-23 15:30:00
22+
1823
- summary: Half-an-hour meeting
1924
begin: 2021-09-23 15:00:00 -07:00 # explicit timezone offset
2025
end: 2021-09-23 15:30:00 -07:00 # explicit timezone offset

tests/test_calendar.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,41 @@ def test_calendar_default_timezone():
7777
assert "DTSTART:20220131T220000Z" # 1 feb
7878

7979

80+
def test_calendar_event_different_timezone():
81+
# This test is just like the one above, but with the timezone definition
82+
# locations changed.
83+
cal = files_to_calendar(
84+
[
85+
iowrap(
86+
"""
87+
timezone: America/New_York
88+
89+
events:
90+
- summary: New year's day
91+
timezone: Europe/Helsinki
92+
begin: 2022-01-01 00:00:00
93+
duration: {hours: 1}
94+
"""
95+
)
96+
]
97+
)
98+
cal_str = cal.serialize()
99+
assert cal_str.startswith("BEGIN:VCALENDAR")
100+
assert "SUMMARY:New year" in cal_str
101+
# It is possible that the ics-py TZID string changes, but hopefully this
102+
# substring is fairly safe to test against.
103+
assert "Europe/Helsinki:20220101T000000" in cal_str
104+
assert "America/New_York" not in cal_str
105+
106+
# Test again by normalizing to UTC. Helsinki is two hours ahead, so the
107+
# times should be 22:00:00.
108+
cal.normalize(datetime.timezone.utc)
109+
cal_norm_str = cal.serialize() # noqa: F841
110+
# 1 Feb midnight
111+
assert "DTSTART:20211231T220000Z" # 1 jan
112+
assert "DTSTART:20220131T220000Z" # 1 feb
113+
114+
80115
def test_calendar_name():
81116
cal = files_to_calendar(
82117
[

yaml2ics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
def event_from_yaml(event_yaml: dict, tz: tzinfo = None) -> ics.Event:
2929
d = event_yaml
3030
repeat = d.pop("repeat", None)
31+
if "timezone" in d:
32+
tz = gettz(d.pop("timezone"))
3133

3234
# Strip all string values, since they often end on `\n`
3335
for key in d:

0 commit comments

Comments
 (0)