Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions caldav/calendarobjectresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2972,7 +2972,11 @@ def _prepare_recurring_thisandfuture(self, completion_timestamp) -> None:
algorithm description. The caller does the single
``save(increase_seqno=False)`` that follows.
"""
recurrences = self.icalendar_instance.subcomponents
recurrences = [
x
for x in self.icalendar_instance.subcomponents
if not isinstance(x, icalendar.Timezone)
]
orig = recurrences[0]
if "STATUS" not in orig:
orig["STATUS"] = "NEEDS-ACTION"
Expand All @@ -2984,6 +2988,7 @@ def _prepare_recurring_thisandfuture(self, completion_timestamp) -> None:
just_completed.add("RECURRENCE-ID", orig.get("DTSTART", completion_timestamp))
seqno = just_completed.pop("SEQUENCE", 0)
just_completed.add("SEQUENCE", seqno + 1)
self.icalendar_instance.add_component(just_completed)
recurrences.append(just_completed)

prev = recurrences[-1]
Expand Down Expand Up @@ -3040,8 +3045,7 @@ def _complete_recurring_thisandfuture(self, completion_timestamp) -> None:
recurring tasks is ... but this is my shot at it.

1) The original, with rrule, will be kept as it is. The rrule
string is fetched from the first subcomponent of the
icalendar.
string is fetched from the first VTODO of the icalendar.

2) If there are multiple recurrence instances in subcomponents
and the last one is marked with RANGE=THISANDFUTURE, then
Expand Down
14 changes: 13 additions & 1 deletion caldav/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,14 +1328,20 @@ async def _async_save_with_invites(
return obj

def _use_or_create_ics(self, ical, objtype, **ical_data):
add_vtimezones = ical_data.pop("add_vtimezones", None)
if ical_data or (
(isinstance(ical, str) or isinstance(ical, bytes))
and b"BEGIN:VCALENDAR" not in to_wire(ical)
):
## TODO: the ical_fragment code is not much tested
if ical and "ical_fragment" not in ical_data:
ical_data["ical_fragment"] = ical
return vcal.create_ical(objtype=objtype, **ical_data)
if add_vtimezones is None:
add_vtimezones = not (
self.client
and self.client.features.is_supported("save-load.icalendar.olson-tzid")
)
return vcal.create_ical(objtype=objtype, add_vtimezones=add_vtimezones, **ical_data)
return ical

def add_object(
Expand Down Expand Up @@ -1363,6 +1369,12 @@ def add_object(
increase_seqno: bump SEQUENCE if the ical data carries one. Defaults to
True; pass False when storing data verbatim (sync, copy, import).
See :meth:`CalendarObjectResource.save`.
add_vtimezones: add a VTIMEZONE for every TZID lacking one that
icalendar knows, when the object is built or amended from
properties. Defaults to True unless the server hint
``save-load.icalendar.olson-tzid`` says the server handles a
bare TZID. Ignored when ``ical`` is a complete VCALENDAR (text
or object) and no properties are given.
dtstart: properties to be inserted into the icalendar object
dtend: properties to be inserted into the icalendar object
summary: properties to be inserted into the icalendar object
Expand Down
51 changes: 47 additions & 4 deletions caldav/lib/vcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import re
import uuid
from collections.abc import Iterator

import icalendar

Expand Down Expand Up @@ -235,13 +236,14 @@ def __call__(self, line):


## sorry for being english-language-euro-centric ... fits rather perfectly as default language for me :-)
def create_ical(ical_fragment=None, objtype=None, language="en_DK", **props):
def create_ical(
ical_fragment=None, objtype=None, language="en_DK", *, add_vtimezones=True, **props
):
"""Creates some icalendar based on properties given as parameters.
It basically creates an icalendar object with all the boilerplate,
some sensible defaults, the properties given and returns it as a
string.

TODO: timezones not supported so far
string. Unless add_vtimezones is False, a VTIMEZONE is added for
every known TZID that has none.
"""
ical_fragment = to_normal_str(ical_fragment)
if "class_" in props:
Expand Down Expand Up @@ -307,6 +309,8 @@ def create_ical(ical_fragment=None, objtype=None, language="en_DK", **props):
component.add(prop, props[prop])
if alarm:
add_alarm(my_instance, alarm)
if add_vtimezones:
_add_vtimezones(my_instance)
ret = to_normal_str(my_instance.to_ical())
if ical_fragment and ical_fragment.strip():
ret = re.sub(
Expand All @@ -319,6 +323,45 @@ def create_ical(ical_fragment=None, objtype=None, language="en_DK", **props):
return ret


def _add_vtimezones(ical: icalendar.Calendar) -> None:
missing = ical.get_used_tzids() - {"UTC"} - {tz.get("TZID") for tz in ical.timezones}
if not missing:
return
## vobject keeps the last offset after the last RDATE, icalendar stops at 2038
## and starts at 1970, so stretch the range to every date in the object.
## Stop after 2200: one RDATE per transition, and 9999 is often used for "no end"
years = list(_years(ical))
first_date = datetime.date(min([1970, *years]), 1, 1)
last_date = datetime.date(min(max([2099, *years]), 2200) + 1, 1, 1)
for tzid in sorted(missing):
try:
vtimezone = icalendar.Timezone.from_tzid(
tzid, first_date=first_date, last_date=last_date
)
except (ValueError, OSError):
continue
for observance in vtimezone.subcomponents:
rdates = observance.pop("RDATE", [])
## vobject 0.9.9 only reads the first value of an RDATE list in a VTIMEZONE
for rdate in rdates if isinstance(rdates, list) else [rdates]:
for value in rdate.dts:
observance.add("RDATE", value.dt)
## at the end, so subcomponents[0] stays the event as the docs use it
ical.add_component(vtimezone)


def _years(ical: icalendar.Calendar) -> Iterator[int]:
"""The year of every date and date-time value in the calendar."""
for component in ical.walk():
for value in component.values():
for item in value if isinstance(value, list) else [value]:
## RDATE and EXDATE hold a list of values in .dts
for prop in item.dts if hasattr(item, "dts") else [item]:
dt = getattr(prop, "dt", None)
if isinstance(dt, datetime.date):
yield dt.year


def add_alarm(ical, alarm):
ia = icalendar.Alarm()
for prop in alarm:
Expand Down
1 change: 1 addition & 0 deletions changes/849.fixed.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
An object caldav builds from keyword arguments, e.g. `add_event(dtstart=...)` or `add_todo(due=...)` with a time zone other than UTC, had a `TZID` without the `VTIMEZONE` that RFC 5545 section 3.6.5 requires. A Home Assistant user reports such events shifted by the UTC offset in Synology Calendar. The missing `VTIMEZONE` for a known time zone is now added, covering 1970 to 2100, or the earlier or later dates in the object up to the end of 2200, with one `RDATE` per line, as vobject reads only the first value of an `RDATE` list in a `VTIMEZONE`. A client that reads the `VTIMEZONE` may show a summer-time date after 2200 off by the daylight saving offset. It is left out where the server hint `save-load.icalendar.olson-tzid` says it is supported, or with `add_vtimezones=False`.
1 change: 1 addition & 0 deletions changes/849.fixed.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`complete(handle_rrule=True, rrule_mode="thisandfuture")` treated every component of the object as an instance of the task, the first as the series and the last as the one to complete. So it raised `KeyError: 'RRULE'` for a task with its `VTIMEZONE` before the `VTODO`, the usual order in tasks from other clients and after reading `vobject_instance`, and completed a `VTIMEZONE` that came after it. It now skips `VTIMEZONE` components.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ dependencies = [
"niquests", ## see docs/source/http-libraries.rst
"recurring-ical-events>=2.0.0",
"typing_extensions;python_version<'3.11'",
"icalendar>6.0.0",
"icalendar>=6.1.0", ## Timezone.from_tzid(), Calendar.get_used_tzids()
"icalendar-searcher>=1.1.0,<2",
"dnspython",
"python-dateutil",
Expand Down
147 changes: 147 additions & 0 deletions tests/test_caldav_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3457,6 +3457,73 @@ def test_reply_to_invite_does_not_bump_auto_schedule_async(self):
existing.save.assert_awaited_once_with(increase_seqno=False)


class TestOlsonTzidHint:
@staticmethod
def _add_event(client: DAVClient, *largs: Any, **kwargs: Any) -> CalendarObjectResource:
put = []
with mock.patch.object(
CalendarObjectResource, "_create", lambda self_, *largs, **kwargs: put.append(self_)
):
Calendar(client, url="/calendar/").add_event(*largs, **kwargs)
return put[0]

@staticmethod
def _berlin() -> datetime:
from zoneinfo import ZoneInfo

return datetime(2026, 6, 6, 20, tzinfo=ZoneInfo("Europe/Berlin"))

@pytest.mark.parametrize(
"support, vtimezone",
[
(None, True),
("unsupported", True),
("fragile", True),
("quirk", False),
("full", False),
],
)
def test_add_event_sends_vtimezone_unless_olson_tzid_is_full(
self, support: str | None, vtimezone: bool
) -> None:
from caldav.compatibility_hints import FeatureSet

client = MockedDAVClient("")
if support:
client.features = FeatureSet({"save-load.icalendar.olson-tzid": {"support": support}})
put = self._add_event(client, summary="tz", dtstart=self._berlin())
assert "DTSTART;TZID=Europe/Berlin:20260606T200000" in put.data
assert ("BEGIN:VTIMEZONE" in put.data) is vtimezone

def test_add_event_passes_add_vtimezones_on(self) -> None:
put = self._add_event(
MockedDAVClient(""), summary="tz", dtstart=self._berlin(), add_vtimezones=False
)
assert "VTIMEZONE" not in put.data

def test_add_vtimezones_true_overrides_the_hint(self) -> None:
from caldav.compatibility_hints import FeatureSet

client = MockedDAVClient("")
client.features = FeatureSet({"save-load.icalendar.olson-tzid": {"support": "full"}})
put = self._add_event(client, summary="tz", dtstart=self._berlin(), add_vtimezones=True)
assert "BEGIN:VTIMEZONE" in put.data

def test_add_vtimezones_does_not_become_a_property(self) -> None:
ical = icalendar.Calendar.from_ical(
"BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//test//EN\n"
"BEGIN:VTIMEZONE\nTZID:Europe/Berlin\n"
"BEGIN:STANDARD\nDTSTART:19701025T030000\n"
"TZOFFSETFROM:+0200\nTZOFFSETTO:+0100\n"
"END:STANDARD\nEND:VTIMEZONE\n"
"BEGIN:VEVENT\nUID:complete@example.com\nDTSTAMP:20261101T000000Z\n"
"DTSTART;TZID=Europe/Berlin:20261207T130000\nSUMMARY:complete\n"
"END:VEVENT\nEND:VCALENDAR\n"
)
put = self._add_event(MockedDAVClient(""), ical, add_vtimezones=False)
assert put.icalendar_instance.to_ical() == ical.to_ical()


class TestFreeBusyScheduleResponse:
"""Unit tests for parsing RFC6638 schedule-response to a freebusy request.

Expand Down Expand Up @@ -6356,6 +6423,86 @@ def test_thisandfuture_completed_early_advances(self) -> None:
assert thisandfuture["RECURRENCE-ID"].dt == nxt
assert thisandfuture["DTSTART"].dt == nxt

_prague_vtimezone = (
"BEGIN:VTIMEZONE\nTZID:Europe/Prague\n"
"BEGIN:STANDARD\nDTSTART:19701025T030000\n"
"RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\n"
"TZOFFSETFROM:+0200\nTZOFFSETTO:+0100\n"
"END:STANDARD\n"
"BEGIN:DAYLIGHT\nDTSTART:19700329T020000\n"
"RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\n"
"TZOFFSETFROM:+0100\nTZOFFSETTO:+0200\n"
"END:DAYLIGHT\nEND:VTIMEZONE\n"
)

@staticmethod
def _prague_vtodo(day: int, extra: str) -> str:
return (
"BEGIN:VTODO\nUID:vtimezone-thisandfuture@example.com\n"
"DTSTAMP:20261101T000000Z\n"
f"DTSTART;TZID=Europe/Prague:202611{day:02}T090000\n"
f"DUE;TZID=Europe/Prague:202611{day:02}T100000\n"
f"{extra}SUMMARY:Weekly chore\nEND:VTODO\n"
)

def _prague_todo(self, vtodos: str, vtimezone_first: bool = True) -> Todo:
body = (
self._prague_vtimezone + vtodos if vtimezone_first else vtodos + self._prague_vtimezone
)
return self._make_todo(
"BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//test//EN\n" + body + "END:VCALENDAR\n"
)

@pytest.mark.parametrize("vtimezone_first", [True, False])
def test_prepare_thisandfuture_leaves_vtimezone_alone(self, vtimezone_first: bool) -> None:
todo = self._prague_todo(self._prague_vtodo(2, "RRULE:FREQ=WEEKLY\n"), vtimezone_first)
before = todo.icalendar_instance.walk("VTIMEZONE")[0].to_ical()

todo._prepare_recurring_thisandfuture(datetime(2026, 11, 3, 12, tzinfo=timezone.utc))

assert [tz.to_ical() for tz in todo.icalendar_instance.walk("VTIMEZONE")] == [before]
master, completed, thisandfuture = todo.icalendar_instance.walk("VTODO")
assert "RRULE" in master
assert str(master["STATUS"]) == "NEEDS-ACTION"
assert str(completed["STATUS"]) == "COMPLETED"
assert completed["RECURRENCE-ID"].dt == master["DTSTART"].dt
assert thisandfuture["RECURRENCE-ID"].params["RANGE"] == "THISANDFUTURE"

def test_prepare_thisandfuture_completes_the_counted_series_not_the_vtimezone(self) -> None:
todo = self._prague_todo(self._prague_vtodo(2, "RRULE:FREQ=WEEKLY;COUNT=2\n"))
before = todo.icalendar_instance.walk("VTIMEZONE")[0].to_ical()

todo._prepare_recurring_thisandfuture(datetime(2026, 11, 3, 12, tzinfo=timezone.utc))
todo._prepare_recurring_thisandfuture(datetime(2026, 11, 10, 12, tzinfo=timezone.utc))

assert [tz.to_ical() for tz in todo.icalendar_instance.walk("VTIMEZONE")] == [before]
assert [str(t["STATUS"]) for t in todo.icalendar_instance.walk("VTODO")] == [
"COMPLETED"
] * 3

def test_prepare_thisandfuture_continues_a_series_with_the_vtimezone_last(self) -> None:
todo = self._prague_todo(
self._prague_vtodo(2, "RRULE:FREQ=WEEKLY\nSTATUS:NEEDS-ACTION\n")
+ self._prague_vtodo(
2,
"RECURRENCE-ID;TZID=Europe/Prague:20261102T090000\n"
"STATUS:COMPLETED\nCOMPLETED:20261103T120000Z\n",
)
+ self._prague_vtodo(
9, "RECURRENCE-ID;RANGE=THISANDFUTURE;TZID=Europe/Prague:20261109T090000\n"
),
vtimezone_first=False,
)
before = todo.icalendar_instance.walk("VTIMEZONE")[0].to_ical()

todo._prepare_recurring_thisandfuture(datetime(2026, 11, 10, 12, tzinfo=timezone.utc))

assert [tz.to_ical() for tz in todo.icalendar_instance.walk("VTIMEZONE")] == [before]
*_, completed, thisandfuture = todo.icalendar_instance.walk("VTODO")
assert str(completed["STATUS"]) == "COMPLETED"
assert "RANGE" not in completed["RECURRENCE-ID"].params
assert thisandfuture["RECURRENCE-ID"].params["RANGE"] == "THISANDFUTURE"

def test_safe_completion_issues_two_puts(self, monkeypatch: Any) -> None:
"""The standalone completed copy must not be PUT twice.

Expand Down
Loading
Loading