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 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: