Skip to content

Commit

Permalink
Improve docs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niccokunzmann committed Nov 11, 2024
1 parent ee7e4fe commit b7bba44
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/icalendar/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/icalendar/tests/test_issue_722_generate_vtimezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit b7bba44

Please sign in to comment.