Skip to content

Commit a0161c7

Browse files
demaledettiBen Finney
authored andcommitted
Support date-only EXDATEs on recurring events with a time
Expanding a recurring event with a DATE-TIME DTSTART and a date-only EXDATE (e.g. `EXDATE;VALUE=DATE:20140703`) crashed in `sanitize_datetime`, which tried to localize a date. Only localize datetimes, and if an EXDATE does not match any instance exactly and is a date, exclude all instances starting on that day. DATE-TIME EXDATEs still only exclude the exact instance. Fixes #1436 Co-authored-by: Ben Finney <ben+python@benfinney.id.au>
1 parent a466b7c commit a0161c7

4 files changed

Lines changed: 76 additions & 10 deletions

File tree

‎AUTHORS.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ John Wassilak - john [at] wassilak [dot] com
6363
Pierre Wilke - wilke.pierre [at] gmail [dot] com
6464
Muhammad Umar Nadeem
6565
Aleksei Stepanov - penguinolog [at] gmail [dot] com
66+
Ben Finney - ben+python [at] benfinney [dot] id [dot] au
67+
Stefano Debenedetti - 44903077+demaledetti [at] users [dot] noreply [dot] github [dot] com

‎CHANGELOG.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ unreleased
2525
* FIX ikhal compatibility with urwid 5.0, which removed ``Columns.focus_col``,
2626
``Columns.set_focus_column()``, ``Pile.set_focus()``, ``Frame.set_body()`` and
2727
``AttrWrap.w``
28+
* FIX crash when expanding recurring events with a time whose EXDATE is a
29+
date only; such an EXDATE now excludes all instances on that day
2830

2931
0.14.1
3032
======

‎khal/icalendar.py‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,11 @@ def expand(
260260
events_tz = getattr(dtstart_prop.dt, "tzinfo", None) if not allday else None
261261

262262
def sanitize_datetime(date: dt.date) -> dt.date:
263-
if allday and isinstance(date, dt.datetime):
264-
date = date.date()
265-
if events_tz is not None:
266-
date = events_tz.localize(date)
263+
if isinstance(date, dt.datetime):
264+
if allday:
265+
date = date.date()
266+
if events_tz is not None:
267+
date = events_tz.localize(date)
267268
return date
268269

269270
rrule_param = vevent.get("RRULE")
@@ -356,14 +357,31 @@ def get_dates(vevent, key):
356357

357358
# remove excluded dates
358359
if expand:
359-
for date in get_dates(vevent, "EXDATE") or ():
360+
for exdate in get_dates(vevent, "EXDATE") or ():
360361
try:
361-
dtstartl.remove(date)
362+
# try the happy path first:
363+
# * assume recurrences have the same type as exdates
364+
# * and trust that they are supposed to match
365+
dtstartl.remove(exdate)
362366
except KeyError:
363-
logger.warning(
364-
f"In event {href}, excluded instance starting at {date} "
365-
"not found, event might be invalid."
366-
)
367+
found = False
368+
# if there was no match, and if the exdate is a date
369+
# exclude any recurrence datetime that has that date
370+
if type(exdate) is dt.date:
371+
_dtstartl = {
372+
start_datetime
373+
for start_datetime in dtstartl
374+
if (type(start_datetime) is dt.date or start_datetime.date() != exdate)
375+
}
376+
if dtstartl != _dtstartl:
377+
found = True
378+
dtstartl = _dtstartl
379+
# both attempts at finding this exdate among the recurrences failed
380+
if not found:
381+
logger.warning(
382+
f"In event {href}, excluded instance starting at {exdate} "
383+
"not found, event might be invalid."
384+
)
367385

368386
dtstartend = [(start, start + duration) for start in dtstartl]
369387
# not necessary, but I prefer deterministic output

‎tests/khalendar_utils_test.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,34 @@ def test_expand_rrule_notz_until_z(self):
731731
END:VCALENDAR
732732
"""
733733

734+
event_exdate_d = """BEGIN:VCALENDAR
735+
VERSION:2.0
736+
BEGIN:VEVENT
737+
UID:event_exdate_d123
738+
DTSTAMP:20140627T162546Z
739+
DTSTART;TZID=Europe/Berlin:20140702T190000
740+
DTEND;TZID=Europe/Berlin:20140702T193000
741+
SUMMARY:Test event
742+
RRULE:FREQ=DAILY;COUNT=10
743+
EXDATE:20140703
744+
END:VEVENT
745+
END:VCALENDAR
746+
"""
747+
748+
event_exdate_ds = """BEGIN:VCALENDAR
749+
VERSION:2.0
750+
BEGIN:VEVENT
751+
UID:event_exdate_ds123
752+
DTSTAMP:20140627T162546Z
753+
DTSTART;VALUE=DATE:20140702
754+
DTEND;VALUE=Date:20140702
755+
SUMMARY:Test event
756+
RRULE:FREQ=DAILY;COUNT=10
757+
EXDATE:20140703
758+
END:VEVENT
759+
END:VCALENDAR
760+
"""
761+
734762
latest_bug = """BEGIN:VCALENDAR
735763
VERSION:2.0
736764
BEGIN:VEVENT
@@ -818,6 +846,22 @@ def test_event_exdatesl_dt(self):
818846
assert dtstart[0][0] == berlin.localize(dt.datetime(2014, 7, 2, 19, 0))
819847
assert dtstart[-1][0] == berlin.localize(dt.datetime(2014, 7, 11, 19, 0))
820848

849+
def test_event_exdate_d(self):
850+
"""recurring event, one date excluded via EXCLUDE with date"""
851+
vevent = _get_vevent(event_exdate_d)
852+
dtstart = icalendar_helpers.expand(vevent, berlin)
853+
assert len(dtstart) == 9
854+
assert dtstart[0][0] == berlin.localize(dt.datetime(2014, 7, 2, 19, 0))
855+
assert dtstart[-1][0] == berlin.localize(dt.datetime(2014, 7, 11, 19, 0))
856+
857+
def test_event_exdate_ds(self):
858+
"""recurring event with dates, one date excluded via EXCLUDE with date"""
859+
vevent = _get_vevent(event_exdate_ds)
860+
dtstart = icalendar_helpers.expand(vevent, berlin)
861+
assert len(dtstart) == 9
862+
assert dtstart[0][0] == dt.date(2014, 7, 2)
863+
assert dtstart[-1][0] == dt.date(2014, 7, 11)
864+
821865
def test_event_exdates_remove(self):
822866
"""check if we can remove one more instance"""
823867
vevent = _get_vevent(event_exdatesl_dt)

0 commit comments

Comments
 (0)