From b7bba442178f5251740b7f7601925bced6d570c5 Mon Sep 17 00:00:00 2001 From: Nicco Kunzmann Date: Mon, 11 Nov 2024 15:04:00 +0000 Subject: [PATCH] Improve docs and add tests --- src/icalendar/cal.py | 21 +++++++++++++++++-- .../test_issue_722_generate_vtimezone.py | 8 +++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index 78ce38d5..27bab73f 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -1422,11 +1422,22 @@ def timezones(self) -> list[Timezone]: """ return self.walk("VTIMEZONE") - def add_missing_timezones(self): + def add_missing_timezones( + self, + first_date:date=Timezone._DEFAULT_FIRST_DATE, + last_date:date=Timezone._DEFAULT_LAST_DATE, + ): """Add all missing VTIMEZONE components. This adds all the timezone components that are required. + .. note:: + + Timezones that are not known will not be added. + + :param first_date: earlier than anything that happens in the calendar + :param last_date: later than anything happening in the calendar + >>> from icalendar import Calendar, Event >>> from datetime import datetime >>> from zoneinfo import ZoneInfo @@ -1439,10 +1450,16 @@ def add_missing_timezones(self): >>> calendar.add_missing_timezones() >>> calendar.timezones[0].tz_name 'Europe/Berlin' + >>> calendar.get_missing_tzids() # check that all are added + set() """ for tzid in self.get_missing_tzids(): try: - timezone = Timezone.from_tzid(tzid) + timezone = Timezone.from_tzid( + tzid, + first_date=first_date, + last_date=last_date + ) except ValueError: continue self.add_component(timezone) diff --git a/src/icalendar/tests/test_issue_722_generate_vtimezone.py b/src/icalendar/tests/test_issue_722_generate_vtimezone.py index f2999e42..f95bb03a 100644 --- a/src/icalendar/tests/test_issue_722_generate_vtimezone.py +++ b/src/icalendar/tests/test_issue_722_generate_vtimezone.py @@ -370,3 +370,11 @@ def test_cannot_add_unknown_timezone(calendars): def test_cannot_create_a_timezone_from_an_invalid_tzid(): with pytest.raises(ValueError): Timezone.from_tzid("invalid/tzid") + +def test_dates_before_and_after_are_considered(): + """When we add the timezones, we should check the calendar to see + if all dates really occur in the span we use. + + We should also consider a huge default range. + """ + pytest.skip("todo")