diff --git a/test_files/vobject_0050.ics b/test_files/vobject_0050.ics new file mode 100644 index 0000000..cdb7a8c --- /dev/null +++ b/test_files/vobject_0050.ics @@ -0,0 +1,23 @@ + +BEGIN:VCALENDAR +PRODID:-//Force.com Labs//iCalendar Export//EN +VERSION:2.0 +METHOD: REQUEST +CALSCALE:GREGORIAN +BEGIN:VEVENT +STATUS:CONFIRMED +ORGANIZER;CN=Wells Fargo and Company:mailto:appointments@wellsfargo.com +UID:appointments@wellsfargo.com +LOCATION:POJOAQUE +CREATED:20240812T192015Z +DTSTART:20240812T213000Z +DTEND: 20240812T223000Z +TRANSP:OPAQUE +DURATION:PT60M +SUMMARY:Personal: Open a new account +DTSTAMP:20240812T192015Z +LAST-MODIFIED:20240812T192015Z +SEQUENCE:0 +DESCRIPTION:Personal: Open a new account +END:VEVENT +END:VCALENDAR \ No newline at end of file diff --git a/tests.py b/tests.py index 7db519e..6d8a0e0 100644 --- a/tests.py +++ b/tests.py @@ -843,6 +843,17 @@ def test_recurrence_offset_naive(self): self.assertEqual(dates[1], datetime.datetime(2013, 1, 24, 0, 0)) self.assertEqual(dates[-1], datetime.datetime(2013, 3, 28, 0, 0)) + def test_issue50(self): + """ + Ensure leading spaces in a DATE-TIME value are ignored when not in + strict mode. + + See https://github.com/py-vobject/vobject/issues/50 + """ + test_file = get_test_file("vobject_0050.ics") + cal = base.readOne(test_file) + self.assertEqual(datetime.datetime(2024, 8, 12, 22, 30, tzinfo=tzutc()), cal.vevent.dtend.value) + class TestChangeTZ(unittest.TestCase): """ diff --git a/vobject/icalendar.py b/vobject/icalendar.py index 294ec18..fa10bc1 100644 --- a/vobject/icalendar.py +++ b/vobject/icalendar.py @@ -1726,10 +1726,13 @@ def stringToDate(s): return datetime.date(year, month, day) -def stringToDateTime(s, tzinfo=None): +def stringToDateTime(s, tzinfo=None, strict=False): """ Returns datetime.datetime object. """ + if not strict: + s = s.strip() + try: year = int(s[0:4]) month = int(s[4:6])