From 24e1bb0ef7a9502fa81b06d7ddb98f2b6d9ee049 Mon Sep 17 00:00:00 2001 From: dlichtistw Date: Sun, 30 Sep 2018 12:28:41 +0200 Subject: [PATCH] Fix: Support events without end According to RFC5545, an event may have no DTEND nor DURATION property. Set end = start in that case. --- icalevents/icalparser.py | 14 ++++++++------ test/test_data/duration.ics | 7 ++++++- test/test_icalevents.py | 4 ++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/icalevents/icalparser.py b/icalevents/icalparser.py index 66a6577..69beeba 100644 --- a/icalevents/icalparser.py +++ b/icalevents/icalparser.py @@ -97,8 +97,6 @@ class Event: :param uid: UID of new event :return: new event """ - duration = self.end - self.start - if not new_start: new_start = self.start @@ -109,7 +107,11 @@ class Event: ne.summary = self.summary ne.description = self.description ne.start = new_start - ne.end = (new_start + duration) + + if self.end: + duration = self.end - self.start + ne.end = (new_start + duration) + ne.all_day = (self.all_day and (new_start - self.start).seconds == 0) ne.uid = uid @@ -131,10 +133,10 @@ def create_event(component, tz=UTC): if component.get('dtend'): event.end = normalize(component.get('dtend').dt, tz=tz) - elif component.get('duration'): + elif component.get('duration'): # compute implicit end as start + duration event.end = event.start + component.get('duration').dt - else: - raise ValueError("Event has neither end, nor duration property.") + else: # compute implicit end as start + 0 + event.end = event.start event.summary = str(component.get('summary')) event.description = str(component.get('description')) diff --git a/test/test_data/duration.ics b/test/test_data/duration.ics index 5d0a8de..4d2bef3 100644 --- a/test/test_data/duration.ics +++ b/test/test_data/duration.ics @@ -6,9 +6,14 @@ DESCRIPTION:Event with duration (1 day), instead of explicit end. SUMMARY:Duration Event END:VEVENT BEGIN:VEVENT -DTSTART:20180120T100000 +DTSTART:20180115T100000 DURATION:PT1H DESCRIPTION:Event with duration (1 hour), instead of explicit end. SUMMARY:Duration Event END:VEVENT +BEGIN:VEVENT +DTSTART:20180120T120000 +DESCRIPTION:Event without explicit dtend, nor duration property. +SUMMARY:Short event +END:VEVENT END:VCALENDAR diff --git a/test/test_icalevents.py b/test/test_icalevents.py index 33200f0..500e1b3 100644 --- a/test/test_icalevents.py +++ b/test/test_icalevents.py @@ -45,6 +45,10 @@ class ICalEventsTests(unittest.TestCase): e2 = evs[1] self.assertEqual(e2.start.hour, 10, "explicit event start") self.assertEqual(e2.end.hour, 11, "implicit event end") + + e3 = evs[2] + self.assertEqual(e3.start.hour, 12, "explicit event start") + self.assertEqual(e3.end.hour, 12, "implicit event end") def test_events_recurring(self): ical = "test/test_data/recurring.ics"