From 94d9f278268bbfb4437837ff99e55d9aca3d5d9b Mon Sep 17 00:00:00 2001 From: Richard Darst Date: Thu, 28 Apr 2022 13:51:47 +0300 Subject: [PATCH 1/2] 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? --- example/test_calendar.yaml | 5 +++++ tests/test_calendar.py | 35 +++++++++++++++++++++++++++++++++++ yaml2ics.py | 2 ++ 3 files changed, 42 insertions(+) diff --git a/example/test_calendar.yaml b/example/test_calendar.yaml index 7ea50dc..ea8d23d 100644 --- a/example/test_calendar.yaml +++ b/example/test_calendar.yaml @@ -15,6 +15,11 @@ events: location: | Office 224, Monolith Bldg, Office Block C + - summary: 15-minute meeting with timezone + timezone: America/New_York # symbolic timezone offset + begin: 2021-09-23 15:00:00 + end: 2021-09-23 15:30:00 + - summary: Half-an-hour meeting begin: 2021-09-23 15:00:00 -07:00 # explicit timezone offset end: 2021-09-23 15:30:00 -07:00 # explicit timezone offset diff --git a/tests/test_calendar.py b/tests/test_calendar.py index bfceffb..3d6784b 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -77,6 +77,41 @@ def test_calendar_default_timezone(): assert "DTSTART:20220131T220000Z" # 1 feb +def test_calendar_event_different_timezone(): + # This test is just like the one above, but with the timezone definition + # locations changed. + cal = files_to_calendar( + [ + iowrap( + """ + timezone: America/New_York + + events: + - summary: New year's day + timezone: Europe/Helsinki + begin: 2022-01-01 00:00:00 + duration: {hours: 1} + """ + ) + ] + ) + cal_str = cal.serialize() + assert cal_str.startswith("BEGIN:VCALENDAR") + assert "SUMMARY:New year" in cal_str + # It is possible that the ics-py TZID string changes, but hopefully this + # substring is fairly safe to test against. + assert "Europe/Helsinki:20220101T000000" in cal_str + assert "America/New_York" not in cal_str + + # Test again by normalizing to UTC. Helsinki is two hours ahead, so the + # times should be 22:00:00. + cal.normalize(datetime.timezone.utc) + cal_norm_str = cal.serialize() # noqa: F841 + # 1 Feb midnight + assert "DTSTART:20211231T220000Z" # 1 jan + assert "DTSTART:20220131T220000Z" # 1 feb + + def test_calendar_name(): cal = files_to_calendar( [ diff --git a/yaml2ics.py b/yaml2ics.py index ad3423b..272f420 100644 --- a/yaml2ics.py +++ b/yaml2ics.py @@ -28,6 +28,8 @@ def event_from_yaml(event_yaml: dict, tz: tzinfo = None) -> ics.Event: d = event_yaml repeat = d.pop("repeat", None) + if "timezone" in d: + tz = gettz(d.pop("timezone")) # Strip all string values, since they often end on `\n` for key in d: From 8f620db3da644bb0fa596970dd6ba90f1db67ef7 Mon Sep 17 00:00:00 2001 From: Richard Darst Date: Thu, 28 Apr 2022 14:06:53 +0300 Subject: [PATCH 2/2] .pre-commit-config: Bump black (click incompatibility) - As described at the top of https://github.com/psf/black/issues/2964 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fbca482..29ef555 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/psf/black - rev: 22.1.0 + rev: 22.3.0 hooks: - id: black - repo: https://gitlab.com/pycqa/flake8