ICal has some complexity to it: Events, TODOs, Journal entries and Alarms can be repeated, removed from the feed and edited later on. This tool takes care of these circumstances.
Let's put our expertise together and build a tool that can solve this!
- day light saving time (DONE)
- recurring events (DONE)
- recurring events with edits (DONE)
- recurring events where events are omitted (DONE)
- recurring events events where the edit took place later (DONE)
- normal events (DONE)
- recurrence of dates but not hours, minutes, and smaller (DONE)
- endless recurrence (DONE)
- ending recurrence (DONE)
- events with start date and no end date (DONE)
- events with start as date and start as datetime (DONE)
- RRULE (DONE)
- events with multiple RRULE (DONE)
- RDATE (DONE)
- DURATION (DONE)
- EXDATE (DONE)
- X-WR-TIMEZONE compatibilty (DONE)
- non-gregorian event repetitions (TODO)
- RECURRENCE-ID with THISANDFUTURE - modify all future events (DONE)
Not included:
- EXRULE (deprecated), see 8.3.2. Properties Registry
You can install this package using pip
.
pip install 'recurring-ical-events==3.*'
On Debian/Ubuntu, you use the package manager to install python-recurring-ical-events.
sudo apt-get install python-recurring-ical-events
If you would like to use this functionality on the command line or in the shell, you can use ics-query.
- Support using GitHub Sponsors
- Fund specific issues using Polar
- Support using Open Collective
- Support using thanks.dev
We accept donations to sustain our work, once or regular. Consider donating money to open-source as everyone benefits.
The icalendar module is responsible for parsing and converting calendars. The recurring_ical_events module uses such a calendar and creates all repetitions of its events within a time span.
To import this module, write
>>> import recurring_ical_events
There are several methods you can use to unfold repeating events, such as at(a_time)
and between(a_start, an_end)
.
>>> import icalendar
>>> import recurring_ical_events
>>> from pathlib import Path
# read the calendar file and parse it
# CALENDARS = Path("to/your/calendar/directory")
>>> calendar_file : Path = CALENDARS / "fablab_cottbus.ics"
>>> ical_string = calendar_file.read_bytes()
>>> print(ical_string[:28])
BEGIN:VCALENDAR
VERSION:2.0
>>> a_calendar = icalendar.Calendar.from_ical(ical_string)
# request the events in a specific interval
# start on the 1st of January 2017 0:00
>>> start_date = (2017, 1, 1)
# the event on the 1st of January 2018 is not included
>>> end_date = (2018, 1, 1)
>>> events = recurring_ical_events.of(a_calendar).between(start_date, end_date)
>>> for event in events:
... start = event["DTSTART"].dt
... summary = event["SUMMARY"]
... print(f"start {start} summary {summary}")
start 2017-03-11 17:00:00+01:00 summary Vereinssitzung
start 2017-06-10 10:00:00+02:00 summary Repair und Recycling Café
start 2017-06-11 16:30:00+02:00 summary Brandenburger Maker-Treffen
start 2017-07-05 17:45:00+02:00 summary Der Computer-Treff fällt aus
start 2017-07-29 14:00:00+02:00 summary Sommerfest
start 2017-10-19 16:00:00+02:00 summary 3D-Modelle programmieren mit OpenSCAD
start 2017-10-20 16:00:00+02:00 summary Programmier dir deine eigene Crypto-Währung
start 2017-10-21 13:00:00+02:00 summary Programmiere deine eigene Wetterstation
start 2017-10-22 13:00:00+02:00 summary Luftqualität: Ein Workshop zum selber messen (Einsteiger)
start 2017-10-22 13:00:00+02:00 summary Websites selbst programmieren
You can get all events which take place at a_date
.
A date can be a year, e.g. 2023
, a month of a year e.g. January in 2023 (2023, 1)
, a day of a certain month e.g. (2023, 1, 1)
, an hour e.g. (2023, 1, 1, 0)
, a minute e.g. (2023, 1, 1, 0, 0)
, or second as well as a datetime.date object and datetime.datetime.
The start and end are inclusive. As an example: if an event is longer than one day it is still included if it takes place at a_date
.
>>> import datetime
# save the query object for the calendar
>>> query = recurring_ical_events.of(a_calendar)
>>> len(query.at(2023)) # a year - 2023 has 12 events happening
12
>>> len(query.at((2023,))) # a year
12
>>> len(query.at((2023, 1))) # January in 2023 - only one event is in January
1
>>> len(query.at((2023, 1, 1))) # the 1st of January in 2023
0
>>> len(query.at("20230101")) # the 1st of January in 2023
0
>>> len(query.at((2023, 1, 1, 0))) # the first hour of the year 2023
0
>>> len(query.at((2023, 1, 1, 0, 0))) # the first minute in 2023
0
>>> len(query.at(datetime.date(2023, 1, 1))) # the first day in 2023
0
The resulting events
are a list of icalendar events, see below.
between(start, end)
returns all events happening between a start and an end time. Both arguments can be datetime.datetime, datetime.date, tuples of numbers passed as arguments to datetime.datetime or strings in the form of
%Y%m%d
(yyyymmdd
) and %Y%m%dT%H%M%SZ
(yyyymmddThhmmssZ
).
Additionally, the end
argument can be a datetime.timedelta
to express that the end is relative to the start
.
For examples of arguments, see at(a_date)
above.
>>> query = recurring_ical_events.of(a_calendar)
# What happens in 2016, 2017 and 2018?
>>> events = recurring_ical_events.of(a_calendar).between(2016, 2019)
>>> len(events) # quite a lot is happening!
39
The resulting events
are in a list of icalendar events, see below.
You can retrieve events that happen after a time or date using after(earliest_end)
.
Events that are happening during the earliest_end
are included in the iteration.
>>> earlierst_end = 2023
>>> for i, event in enumerate(query.after(earlierst_end)):
... print(f"{event['SUMMARY']} ends {event['DTEND'].dt}") # all dates printed are after January 1st 2023
... if i > 10: break # we might get endless events and a lot of them!
Repair Café ends 2023-01-07 17:00:00+01:00
Repair Café ends 2023-02-04 17:00:00+01:00
Repair Café ends 2023-03-04 17:00:00+01:00
Repair Café ends 2023-04-01 17:00:00+02:00
Repair Café ends 2023-05-06 17:00:00+02:00
Repair Café ends 2023-06-03 17:00:00+02:00
Repair Café ends 2023-07-01 17:00:00+02:00
Repair Café ends 2023-08-05 17:00:00+02:00
Repair Café ends 2023-09-02 17:00:00+02:00
Repair Café ends 2023-10-07 17:00:00+02:00
Repair Café ends 2023-11-04 17:00:00+01:00
Repair Café ends 2023-12-02 17:00:00+01:00
If you wish to iterate over all occurrences of the components, then you can use all()
.
Since a calendar can define a huge amount of recurring entries, this method generates them
and forgets them, reducing memory overhead.
This example shows the first event that takes place in the calendar:
>>> first_event = next(query.all()) # not all events are generated
>>> print(f"The first event is {first_event['SUMMARY']}")
The first event is Weihnachts Repair-Café
You can count occurrences of events and other components using count()
.
>>> number_of_TODOs = recurring_ical_events.of(a_calendar, components=["VTODO"]).count()
>>> print(f"You have {number_of_TODOs} things to do!")
You have 0 things to do!
>>> number_of_journal_entries = recurring_ical_events.of(a_calendar, components=["VJOURNAL"]).count()
>>> print(f"There are {number_of_journal_entries} journal entries in the calendar.")
There are 0 journal entries in the calendar.
However, this can be very costly!
The result of both between(start, end)
and at(a_date)
is a list of icalendar events.
By default, all attributes of the event with repetitions are copied, like UID
and SUMMARY
.
However, these attributes may differ from the source event:
DTSTART
which is the start of the event instance. (always present)DTEND
which is the end of the event instance. (always present)RDATE
,EXDATE
,RRULE
are the rules to create event repetitions. They are not included in repeated events, see Issue 23. To change this, useof(calendar, keep_recurrence_attributes=True)
.
If the resulting components are ordered when after(earliest_end)
or all()
is used.
The result is an iterator that returns the events in order.
for event in recurring_ical_events.of(an_icalendar_object).after(datetime.datetime.now()):
print(event["DTSTART"]) # The start is ordered
By default the recurring_ical_events
only selects events as the name already implies.
However, there are different components available in a calendar.
You can select which components you like to have returned by passing components
to the of
function:
of(a_calendar, components=["VEVENT"])
Here is a template code for choosing the supported types of components:
>>> query_events = recurring_ical_events.of(a_calendar)
>>> query_journals = recurring_ical_events.of(a_calendar, components=["VJOURNAL"])
>>> query_todos = recurring_ical_events.of(a_calendar, components=["VTODO"])
>>> query_all = recurring_ical_events.of(a_calendar, components=["VTODO", "VEVENT", "VJOURNAL"])
If a type of component is not listed here, it can be added. Please create an issue for this in the source code repository.
For further customization, please refer to the section on how to extend the default functionality.
Alarms are subcomponents of events and todos. They only make sense with an event or todo. Thus the interface is slightly different.
Add VALARM
as the component you would like:
>>> query_alarms = recurring_ical_events.of(a_calendar, components=["VALARM"])
In the following example, an event has an alarm one week before it starts.
The component returned is not a VALARM
component but instead the VEVENT
with only
one VALARM
in it.
# read an .ics file with an event with an alarm
>>> calendar_with_alarm = recurring_ical_events.example_calendar("alarm_1_week_before_event")
>>> alarm_day = datetime.date(2024, 12, 2)
# we get the event that has an alarm on that day
>>> event = recurring_ical_events.of(calendar_with_alarm, components=["VALARM"]).at(alarm_day)[0]
>>> len(event.alarms.times)
1
>>> alarm = event.alarms.times[0]
# the alarm happens one week before the event
>>> event.start - alarm.trigger
datetime.timedelta(days=7)
In the following code, we query the same day for a VEVENT
component and we find nothing.
The event happens a week later.
# No events on that day. There is only an alarm.
>>> recurring_ical_events.of(calendar_with_alarm, components=["VEVENT"]).at(alarm_day)
[]
When querying events and todos, they keep their alarms and other subcompnents. These alarm times can be outside of the dates requested.
In this example, we get an event and find that it has several alarms in it.
>>> event_day = alarm_day + datetime.timedelta(days=7)
>>> event = recurring_ical_events.of(calendar_with_alarm, components=["VEVENT"]).at(event_day)[0]
# The event a week later has more than one alarm.
>>> len(event.walk("VALARM"))
2
If you use between()
or at()
several times, it is faster to re-use the object coming from of()
.
>>> query = recurring_ical_events.of(a_calendar)
>>> events_of_day_1 = query.at((2019, 2, 1))
>>> events_of_day_2 = query.at((2019, 2, 2))
>>> events_of_day_3 = query.at((2019, 2, 3))
# ... and so on
Some events may be badly formatted and therefore cannot be handled by recurring-ical-events
.
Passing skip_bad_series=True
as of()
argument will totally skip theses events.
# Create a calendar that contains broken events.
>>> calendar_file = CALENDARS / "bad_rrule_missing_until_event.ics"
>>> calendar_with_bad_event = icalendar.Calendar.from_ical(calendar_file.read_bytes())
# By default, broken events result in errors.
>>> recurring_ical_events.of(calendar_with_bad_event, skip_bad_series=False).count()
Traceback (most recent call last):
...
recurring_ical_events.BadRuleStringFormat: UNTIL parameter is missing: FREQ=WEEKLY;BYDAY=TH;WKST=SU;UNTL=20191023
# With skip_bad_series=True we skip the series that we cannot handle.
>>> recurring_ical_events.of(calendar_with_bad_event, skip_bad_series=True).count()
0
Each icalendar Calendar can contain Events, Journal entries,
TODOs and others, called Components.
Those entries are grouped by their UID
.
Such a UID
defines a Series of Occurrences that take place at
a given time.
Since each Component is different, the ComponentAdapter offers a unified
interface to interact with them.
The Calendar gets filtered and for each UID
,
a Series can use one or more ComponentAdapters to create
Occurrences of what happens in a time span.
These Occurrences are used internally and convert to Components for further use.
All the functionality of recurring-ical-events
can be extended and modified.
To understand where to extend, have a look at the Architecture.
The first place for extending is the collection of components.
Components are collected into a Series
.
A series belongs together because all components have the same UID
.
In this example, we collect one VEVENT which matches a certain UID:
>>> from recurring_ical_events import SelectComponents, EventAdapter, Series
>>> from icalendar.cal import Component
>>> from typing import Sequence
# create the calendar
>>> calendar_file = CALENDARS / "machbar_16_feb_2019.ics"
>>> machbar_calendar = icalendar.Calendar.from_ical(calendar_file.read_bytes())
# Create a collector of components that searches for an event with a specific UID
>>> class CollectOneUIDEvent(SelectComponents):
... def __init__(self, uid:str) -> None:
... self.uid = uid
... def collect_series_from(self, source: Component, suppress_errors: tuple) -> Sequence[Series]:
... components : list[Component] = []
... for component in source.walk("VEVENT"):
... if component.get("UID") == self.uid:
... components.append(EventAdapter(component))
... return [Series(components)] if components else []
# collect only one UID: 4mm2ak3in2j3pllqdk1ubtbp9p@google.com
>>> one_uid = CollectOneUIDEvent("4mm2ak3in2j3pllqdk1ubtbp9p@google.com")
>>> uid_query = recurring_ical_events.of(machbar_calendar, components=[one_uid])
>>> uid_query.count() # the event has no recurrence and thus there is only one
1
Several ways of extending the functionality have been created to override internals. These can be subclassed or composed.
Below, you can choose to collect all components. Subclasses can be created for the
Series
and the Occurrence
.
>>> from recurring_ical_events import AllKnownComponents, Series, Occurrence
# we create a calendar with one event
>>> calendar_file = CALENDARS / "one_event.ics"
>>> one_event = icalendar.Calendar.from_ical(calendar_file.read_bytes())
# You can override the Occurrence and Series classes for all computable components
>>> select_all_known = AllKnownComponents(series=Series, occurrence=Occurrence)
>>> select_all_known.names # these are the supported types of components
['VALARM', 'VEVENT', 'VJOURNAL', 'VTODO']
>>> query_all_known = recurring_ical_events.of(one_event, components=[select_all_known])
# There should be exactly one event.
>>> query_all_known.count()
1
This example shows that the behavior for specific types of components can be extended.
Additional to the series, you can change the ComponentAdapter
that provides
a unified interface for all the components with the same name (VEVENT
for example).
>>> from recurring_ical_events import ComponentsWithName, EventAdapter, JournalAdapter, TodoAdapter
# You can also choose to select only specific subcomponents by their name.
# The default arguments are added to show the extensibility.
>>> select_events = ComponentsWithName("VEVENT", adapter=EventAdapter, series=Series, occurrence=Occurrence)
>>> select_todos = ComponentsWithName("VTODO", adapter=TodoAdapter, series=Series, occurrence=Occurrence)
>>> select_journals = ComponentsWithName("VJOURNAL", adapter=JournalAdapter, series=Series, occurrence=Occurrence)
# There should be one event happening and nothing else
>>> recurring_ical_events.of(one_event, components=[select_events]).count()
1
>>> recurring_ical_events.of(one_event, components=[select_todos]).count()
0
>>> recurring_ical_events.of(one_event, components=[select_journals]).count()
0
So, if you would like to modify all events that are returned by the query,
you can do that subclassing the Occurrence
class.
# This occurence changes adds a new attribute to the resulting events
>>> class MyOccurrence(Occurrence):
... """An occurrence that modifies the component."""
... def as_component(self, keep_recurrence_attributes: bool) -> Component:
... """Return a shallow copy of the source component and modify some attributes."""
... component = super().as_component(keep_recurrence_attributes)
... component["X-MY-ATTRIBUTE"] = "my occurrence"
... return component
>>> query = recurring_ical_events.of(one_event, components=[ComponentsWithName("VEVENT", occurrence=MyOccurrence)])
>>> event = next(query.all())
>>> event["X-MY-ATTRIBUTE"]
'my occurrence'
This library allows extension of functionality during the selection of components to calculate using these classes:
ComponentsWithName
- for components of a certain nameAllKnownComponents
- for all components knownSelectComponents
- the interface to provide
You can further customize behaviour by subclassing these:
ComponentAdapter
such asEventAdapter
,JournalAdapter
orTodoAdapter
.Series
Occurrence
CalendarQuery
If you use this library in your code, you may want to make sure that
updates can be received but they do not break your code.
The version numbers are handeled this way: a.b.c
example: 0.1.12
c
is changed for each minor bug fix.b
is changed whenever new features are added.a
is changed when the interface or major assumptions change that may break your code.
So, I recommend to version-fix this library to stay with the same a
while b
and c
can change.
Please install pre-commit before git commit. It will ensure that the code is formatted and linted as expected using ruff.
pre-commit install
This project's development is driven by tests. Tests assure a consistent interface and less knowledge lost over time. If you like to change the code, tests help that nothing breaks in the future. They are required in that sense. Example code and ics files can be transferred into tests and speed up fixing bugs.
You can view the tests in the test folder.
If you have a calendar ICS file for which this library does not
generate the desired output, you can add it to the test/calendars
folder and write tests for what you expect.
If you like, open an issue first, e.g. to discuss the changes and
how to go about it.
To run the tests, we use tox
.
tox
tests all different Python versions which we want to be compatible to.
pip3 install tox
To run all the tests:
tox
To run the tests in a specific Python version:
tox -e py39
To release new versions,
edit the Changelog Section
edit setup.py, the
__version__
variablecreate a commit and push it
wait for GitHub Actions to finish the build
run
python3 setup.py tag_and_deploy
notify the issues about their release
- v3.4.0
- Add
VALARM
support: Calculate alarm times. See Issue 186
- Add
- v3.3.4
- Allow
x-wr-timezone
1.* and 2.* for this lib to remove dependency update problems.
- Allow
- v3.3.3
- Fix: Events with DTSTART of type date have a duration of one day, see Issue 179
- v3.3.2
- Update x-wr-timezone
- v3.3.1
- v3.3.0
- v3.2.0
- Allow
datetime.timedelta
as second argument tobetween(absolute_time, datetime.timedelta())
- Allow
- v3.1.1
- v3.1.0
- Add
count() -> int
to count all occurrences within a calendar - Add
all() -> Generator[icalendar.Component]
to iterate over the whole calendar
- Add
- v3.0.0
- v2.2.3
- Fix: Edits of whole event are now considering RDATE and EXDATE, see Issue 148
- v2.2.2
- Test support for
icalendar==6.*
- Remove Python 3.7 from tests and compatibility list
- Remove pytz from requirements
- Test support for
- v2.2.1
- Add support for multiple RRULE in events.
- v2.2.0
- Add
after()
method to iterate over upcoming events.
- Add
- v2.1.3
- Test and support Python 3.12.
- Change SPDX license header.
- Fix RRULE with negative COUNT, see Issue 128
- v2.1.2
- v2.1.1
- Claim and test support for Python 3.11.
- Support deleting events by setting RRULE UNTIL < DTSTART, see Issue 117.
- v2.1.0
- Added support for PERIOD values in RDATE. See Issue 113.
- Fixed
icalendar>=5.0.9
to supportRDATE
of typePERIOD
with a time zone. - Fixed
pytz>=2023.3
to assure compatibility.
- v2.0.2
- Fixed omitting last event of
RRULE
withUNTIL
when usingpytz
, the event starting in winter time and ending in summer time. See Issue 107.
- Fixed omitting last event of
- v2.0.1
- Fixed crasher with duplicate RRULE. See Pull Request 104
- v2.0.0b
- Only return
VEVENT
by default. Addof(... ,components=...)
parameter to select which kinds of components should be returned. See Issue 101. - Remove
beta
indicator. This library works okay: Feature requests come in, not so much bug reports.
- Only return
- v1.1.0b
- Add repeated TODOs and Journals. See Pull Request 100 and Issue 97.
- v1.0.3b
- Remove syntax anomalies in README.
- Switch to GitHub actions because GitLab decided to remove support.
- v1.0.2b
- Add support for
X-WR-TIMEZONE
calendars which contain events without an explicit time zone, see Issue 86.
- Add support for
- v1.0.1b
- Add support for
zoneinfo.ZoneInfo
time zones, see Issue 57. - Migrate from Travis CI to Gitlab CI.
- Add code coverage on Gitlab.
- Add support for
- v1.0.0b
- v0.2.4b
- Events with a duration of 0 seconds are correctly returned.
between()
andat()
take the same kind of arguments. These arguments are documented.
- v0.2.3b
- v0.2.2b
- Check that
at()
does not return an event starting at the next day, see Issue 44.
- Check that
- v0.2.1b
- Check that recurring events are removed if they are modified to leave the requested time span, see Issue 62.
- v0.2.0b
- Add ability to keep the recurrence attributes (RRULE, RDATE, EXDATE) on the event copies instead of stripping them. See Pull Request 54.
- v0.1.21b
- Fix issue with repetitions over DST boundary. See Issue 48.
- v0.1.20b
- Fix handling of modified recurrences with lower sequence number than their base event Pull Request 45
- v0.1.19b
- v0.1.18b
- v0.1.17b
- Handle Issue 28 where passed arguments lead to errors where it is expected to work.
- v0.1.16b
- Events with an empty RRULE are handled like events without an RRULE.
- Remove fixed dependency versions, see Issue 14
- v0.1.15b
- Repeated events also include subcomponents. Issue 6
- v0.1.14b
- Fix compatibility issue 20: EXDATEs of different time zones are now supported.
- v0.1.13b
- Remove attributes RDATE, EXDATE, RRULE from repeated events Issue 23
- Use vDDDTypes instead of explicit date/datetime type Pull Request 19
- Start Changelog
- python-dateutil - to compute the recurrences of events using
rrule
- icalendar - the library used to parse ICS files
- pytz - for timezones
- x-wr-timezone for handling the non-standard
X-WR-TIMEZONE
property.
- icalevents - another library for roughly the same use-case
- Open Web Calendar - a web calendar to embed into websites which uses this library
- icspy - to create your own calendar events
- pyICSParser - parse icalendar files and return event times (GitHub)
- ics-query - a command line impementation of
recurring-ical-events
Nicco Kunzmann talked about this library at the FOSSASIA 2022 Summit:
- RFC 5545
- RFC 7986 -- an update to RFC 5545. It does not change any properties useful for scheduling events.
- Stackoverflow question this is created for
- https://github.com/oberron/annum
- https://stackoverflow.com/questions/20268204/ical-get-date-from-recurring-event-by-rrule-and-dtstart
- collective/icalendar#162
- https://stackoverflow.com/questions/46471852/ical-parsing-reoccuring-events-in-python
- RDATE https://stackoverflow.com/a/46709850/1320237