Some calendar creators such as Google Calendar
introduce the non-standard X-WR-TIMEZONE parameter
to ICS calendar files to change the timezone.
Strict interpretations according to RFC 5545 ignore the X-WR-TIMEZONE
parameter.
This causes the times of the events to differ from those
which make use of X-WR-TIMEZONE.
This module aims to bridge the gap by converting calendars
using X-WR-TIMEZONE to a strict RFC 5545 calendars.
So, let's put our heads together and solve this problem for everyone!
Some features of the module are:
- Easy install with Python's
pip. - Command line conversion of calendars.
- Piping of calendar files with
wgetorcurl.
Some of the requirements are:
- Calendars without
X-WR-TIMEZONEare kept unchanged. - Passing calendars twice to this module does not change them.
Install using pip:
python3 -m pip install x-wr-timezoneInstall with apt:
sudo apt-get install python3-x-wr-timezoneInstall with conda:
conda install -c conda-forge x-wr-timezoneYou can standardize the calendars using your command line interface.
The examples assume that in.ics is a calendar which may use
X-WR-TIMEZONE, whereas out.ics does not require X-WR-TIMEZONE
for proper display.
cat in.is | x-wr-timezone > out.ics
x-wr-timezone in.ics out.ics
curl https://example.org/in.ics | x-wr-timezone > out.ics
wget -O- https://example.org/in.ics | x-wr-timezone > out.icsYou can get usage help on the command line:
x-wr-timezone --helpAfter you have installed the library, you can import it.
import x_wr_timezoneThe function to_standard() converts an icalendar.Calendar.
x_wr_timezone.to_standard(an_icalendar)Here is a full example which does about as much as this module is supposed to do:
import icalendar # installed with x_wr_timezone
import x_wr_timezone
with open("in.ics", 'rb') as file:
calendar = icalendar.from_ical(file.read())
new_calendar = x_wr_timezone.to_standard(calendar)
# you could use the new_calendar variable now
with open('out.ics', 'wb') as file:
file.write(new_calendar.to_ical())to_standard(calendar, timezone=None) has these parameters:
calendaris theicalendar.Calendarobject.timezoneis an optional time zone. By default, the time zone incalendar['X-WR-TIMEZONE']is used to check if the calendar needs changing. Whentimezoneis notNonehowever,calendar['X-WR-TIMEZONE']will not be tested and it is assumed that thecalendarshould be changed as ifcalendar['X-WR-TIMEZONE']had the value oftimezone. This does not add or change the value ofcalendar['X-WR-TIMEZONE']. You would need to do that yourself.timezonecan be a string like"UTC"or"Europe/Berlin"or apytz.timezoneor something thatdatetimeaccepts as a time zone..- Return value: The
calendarargument is not modified at all. The calendar returned has the attributes and subcomponents of thecalendaronly changed and copied where needed to return the proper value. As such, the returned calendar might be identical to the one passed to the function as thecalendarargument. Keep that in mind if you modify the return value. add_timezone_component : bool = False. If set to True, it adds the VTIMEZONE component to the calendar object. This is required to have a valid RFC5545 calendar for exporting and sharing but not to process the events and other components.
Clone the repository or its fork and
cd x-wr-timezone.Optional: Install virtualenv and Python3 and create a virtual environment:
pip install virtualenv virtualenv -p python3 ENV source ENV/bin/activate # you need to do this for each shell
Install the packages and this module so it can be edited:
pip install -r test-requirements.txt -e .Run the tests:
pytest
To test all functions:
pytest --x-wr-timezone allYou can use tox to test the package in different Python versions.
toxThis tests all the different functionalities:
tox -- --x-wr-timezone allTo release new versions,
edit the Changelog Section
edit setup.py, the
__version__variablecreate a commit and push it
Wait for CI tests to finish the build.
run
python3 setup.py tag_and_deploy
notify the issues about their release
If you need to add or remove a Python version, you need to modify these files:
- ... README.rst in the changelog section
- ... setup.py
- ... tox.ini
- ... tests.yml
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.
- v3.0.1
- Reintroduce
CalendarWalker.is_UTC()as a compatibility hook backed byicalendar.is_utc().
- Reintroduce
- v3.0.0
- Handle calendars that provide multiple
X-WR-TIMEZONEvalues. If all values match, use the repeated timezone; if values conflict, ignore them because the target timezone is ambiguous. - Use
icalendar.is_utc()instead ofis_UTC()to identify UTC, catching aliases likeEtc/GMT. - Remove
is_UTC()fromCalendarWalker. - Test
pytz.timezone("UTC")anddatetime.timezone.utcas arguments toto_standard(). - Add tests to ensure
DTSTAMP,CREATED,LAST-MODIFIED,TRIGGERandACKNOWLEDGEDare not converted.
- Handle calendars that provide multiple
- v2.0.1
- Reuse the generated timezone component because that takes a long time.
- Fix bug where the timezone was not changed in the result if you add the component.
- v2.0.0
- Add VTIMEZONE component by default to
x-wr-timezonecommand. - Add
--no-timezoneparameter to skip adding the VTIMEZONE component. - Add
add_timezone_component=Falseparameter toto_standard()to optionally add a timezone component in the resultingicalendar.Calendar. - Add
clickdependency.
- Add VTIMEZONE component by default to
- v1.0.2
- Add support for Python 3.13
- v1.0.1
- Use zoneinfo instead of pytz
- Test compatibility with pytz and zoneinfo as argument to to_standard
- Remove pytz as a dependency
- Add tzdata as a dependency
- Add typing
- Update Python versions
- v0.0.7
- Rename master branch to main
- Use proper SPDX license ID
- Test Python 3.12
- v0.0.6
- Obsolete Python 3.7
- Support Python 3.11
- Fix localization issue for pytz when datetime has no timezone
- Run tests on GitHub Actions
- Require icalendar 5.0.11 for tests
- Fix pytz localization issue when dateime is not in UTC and has no time zone.
- v0.0.5
- Revisit README and CLI and fix spelling mistakes.
- Modified behavior to treat events without time zone found in a calendar using the X-WR-TIMEZONE property, see Pull Request 7
- v0.0.4
- Test automatic deployment with Gitlab CI.
- v0.0.3
- Use
tzname()function ofdatetimeto test for UTC. This helps support zoneinfo time zones. - Split up visitor class and rename it to walker.
- Use
- v0.0.2
- Implement the
timezoneargument. - Do not modify the value of the
calendarargument and only copy it where needed.
- Implement the
- v0.0.1
- Initial release supports DTSTART, DTEND, EXDATE, RDATE, RECURRENCE-ID attributes of events.
- Command line interface as
x-wr-timezone.
We have a Security Policy in place.
This module was created because of these issues:
This module uses the icalendar library for parsing calendars.
This library is used by python-recurring-ical-events
to get events at specific dates.
This software is licensed under LGPLv3, see the LICENSE file.