From c8324ad49466d340a14e1ca995c794996354a6d5 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Thu, 8 Jun 2023 13:09:42 -0700 Subject: [PATCH 1/2] Default to UTC if no timezone given --- tests/test_calendar.py | 24 ++++++++++++++++++++++++ yaml2ics.py | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 3d6784b..3b5433d 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -112,6 +112,30 @@ def test_calendar_event_different_timezone(): assert "DTSTART:20220131T220000Z" # 1 feb +def test_calendar_no_timezone(): + """We default to UTC if no timezone is given.""" + cal = files_to_calendar( + [ + iowrap( + """ + events: + - summary: Some new year's days + begin: 2022-01-01 + repeat: + interval: + years: 1 + until: 2030-01-01 + except_on: + - 2023-01-01 + """ + ) + ] + ) + cal_str = cal.serialize() + assert cal_str.startswith("BEGIN:VCALENDAR") + assert "UTC" in cal_str + + def test_calendar_name(): cal = files_to_calendar( [ diff --git a/yaml2ics.py b/yaml2ics.py index df0cdef..520ee9f 100644 --- a/yaml2ics.py +++ b/yaml2ics.py @@ -156,9 +156,11 @@ def files_to_events(files: list) -> (ics.Calendar, str): calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader) else: calendar_yaml = yaml.load(open(f), Loader=yaml.FullLoader) - tz = calendar_yaml.get("timezone", None) + tz = calendar_yaml.get("timezone") if tz is not None: tz = gettz(tz) + else: + tz = dateutil.tz.UTC if "include" in calendar_yaml: included_events, _name = files_to_events( os.path.join(os.path.dirname(f), newfile) From b4090e5ad51df1c1f14c081331a90c6fcc750044 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Thu, 8 Jun 2023 13:10:49 -0700 Subject: [PATCH 2/2] Use UTC 00:00:00 when no exdate time specified --- tests/test_events.py | 5 ++++- yaml2ics.py | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_events.py b/tests/test_events.py index 2f89f4d..cb633c7 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -86,10 +86,13 @@ def test_exception(): ) ) event_str = event.serialize() + # Handle line overflow + event_str = event_str.replace(" ", "").replace("\n", "").replace("\r", "") + # DTEND exists and is the next day, calendar tools import this # correctly as being a one-day event assert ( - "EXDATE;TZID=/ics.py/2020.1/America/Los_Angeles:20220713,20220714T060000" + "EXDATE;TZID=/ics.py/2020.1/America/Los_Angeles:20220713T000000,20220714T060000" in event_str ) assert "RDATE;TZID=/ics.py/2020.1/America/Los_Angeles:20221224T060000" in event_str diff --git a/yaml2ics.py b/yaml2ics.py index 520ee9f..98d20b7 100644 --- a/yaml2ics.py +++ b/yaml2ics.py @@ -29,7 +29,9 @@ def datetime2utc(date): if isinstance(date, datetime.datetime): return datetime.datetime.strftime(date, "%Y%m%dT%H%M%S") elif isinstance(date, datetime.date): - return datetime.datetime.strftime(date, "%Y%m%d") + return datetime.datetime.strftime(date, "%Y%m%dT000000") + else: + raise RuntimeError(f"Unsure how to convert {date} to UTC") # See RFC2445, 4.8.5 REcurrence Component Properties