Skip to content
Open
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
1 change: 1 addition & 0 deletions news/+vtimezone-unique-tzname.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the cubic run time of :meth:`Timezone.get_transitions <icalendar.cal.timezone.Timezone.get_transitions>` for a ``VTIMEZONE`` with many equal ``STANDARD`` or ``DAYLIGHT`` subcomponents without ``TZNAME``, which made parsing such a calendar with the ``pytz`` timezone provider slow. A repeated name now gets a numeric suffix such as ``_2`` instead of another ``_1``. I used AI to assist me with this change. @shamsManna
28 changes: 18 additions & 10 deletions src/icalendar/cal/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,17 @@ def _extract_offsets(component: TimezoneDaylight | TimezoneStandard, tzname: str
return is_dst, transitions

@staticmethod
def _make_unique_tzname(tzname: str, tznames: set[str]) -> str:
def _make_unique_tzname(tzname: str, tznames: dict[str, int]) -> str:
"""Make a unique tzname and add it to ``tznames``.

A name that is already in use gets the next free numeric suffix.
``tznames`` remembers the next suffix to try for each name,
so that many equal names take linear time.

Parameters:
tzname: The candidate timezone name.
tznames: The timezone names already in use.
tznames: The timezone names already in use,
mapped to the next suffix to try for them.

Returns:
The unique timezone name.
Expand All @@ -146,18 +151,21 @@ def _make_unique_tzname(tzname: str, tznames: set[str]) -> str:
.. code-block:: pycon

>>> from icalendar import Timezone
>>> names = {"EST"}
>>> names = {"EST": 1}
>>> Timezone._make_unique_tzname("EST", names)
'EST_1'
>>> Timezone._make_unique_tzname("EST", names)
'EST_1_1'
'EST_2'
>>> sorted(names)
['EST', 'EST_1', 'EST_1_1']
['EST', 'EST_1', 'EST_2']
"""
# TODO better way of making sure tznames are unique
while tzname in tznames:
tzname += "_1"
tznames.add(tzname)
if tzname in tznames:
suffix = tznames[tzname]
while f"{tzname}_{suffix}" in tznames:
suffix += 1
tznames[tzname] = suffix + 1
tzname = f"{tzname}_{suffix}"
tznames[tzname] = 1
return tzname

def to_tz(self, tzp: TZP = tzp, lookup_tzid: bool = True):
Expand Down Expand Up @@ -204,7 +212,7 @@ def get_transitions(
zone = self.tz_name
transitions = []
dst = {}
tznames = set()
tznames: dict[str, int] = {}
for component in self.walk():
if isinstance(component, Timezone):
continue
Expand Down
53 changes: 53 additions & 0 deletions src/icalendar/tests/test_vtimezone_unique_tzname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Subcomponents of a VTIMEZONE without TZNAME get unique names.

Timezone.get_transitions names each STANDARD and DAYLIGHT subcomponent
without a TZNAME after its TZID, DTSTART and offsets.
Equal subcomponents used to get one more ``_1`` appended for each earlier one,
so n of them took cubic time and built names of length 2n.
This made parsing an untrusted calendar with the pytz provider slow.
"""

from icalendar import Calendar, Timezone

SUBCOMPONENT = (
"BEGIN:STANDARD\r\n"
"DTSTART:19701101T020000\r\n"
"TZOFFSETFROM:-0400\r\n"
"TZOFFSETTO:-0500\r\n"
"END:STANDARD\r\n"
)


def vtimezone(count: int) -> Timezone:
"""Return a VTIMEZONE with count equal subcomponents without TZNAME."""
calendar = Calendar.from_ical(
"BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nTZID:X-Custom\r\n"
+ SUBCOMPONENT * count
+ "END:VTIMEZONE\r\nEND:VCALENDAR\r\n"
)
return calendar.timezones[0]


def test_equal_subcomponents_get_unique_names():
"""Each subcomponent gets its own name."""
_, transition_info = vtimezone(3).get_transitions()
names = sorted(name for _, _, name in transition_info)
base = "X-Custom_19701101T020000_-0400_-0500"
assert names == [base, f"{base}_1", f"{base}_2"]


def test_many_equal_subcomponents_keep_short_names():
"""The names do not grow with the number of subcomponents."""
count = 2000
_, transition_info = vtimezone(count).get_transitions()
names = {name for _, _, name in transition_info}
assert len(names) == count
assert max(len(name) for name in names) < 60


def test_suffix_skips_names_in_use():
"""A suffix that is already in use is skipped."""
names = {"EST": 1, "EST_1": 1}
assert Timezone._make_unique_tzname("EST", names) == "EST_2"
assert Timezone._make_unique_tzname("EST_1", names) == "EST_1_1"
assert Timezone._make_unique_tzname("EST", names) == "EST_3"