Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iCalendar export support #2261

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
58 changes: 58 additions & 0 deletions holidays/ical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# holidays
# --------
# A fast, efficient Python library for generating country, province and state
# specific sets of holidays on the fly. It aims to make determining whether a
# specific date is a holiday as fast and flexible as possible.
#
# Authors: Vacanza Team and individual contributors (see AUTHORS file)
# dr-prodigy <[email protected]> (c) 2017-2023
# ryanss <[email protected]> (c) 2014-2017
# Website: https://github.com/vacanza/holidays
# License: MIT (see LICENSE file)


class ICalExporter:
def __init__(self, holidays_object, language="en", return_bytes=False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now language parameter is not needed.

"""Initialize iCalendar exporter

Args:
- holidays_object: Holidays object containing holiday data
- language (str) Output language code (default: 'en')
- return_bytes(bool): If True, return bytes instead of string
"""
self.holidays = holidays_object
self.return_bytes = return_bytes

def generate_event(self, date, holiday_name):
"""Generate a single holiday event
Args:
- date: Holiday date
- holiday_name: Holiday name

Returns:
- list[str]: List of iCalender format event lines
"""
formatted_date = date.strftime("%Y%m%d")

return [
"BEGIN:VEVENT",
f"SUMMARY:{holiday_name}",
f"DTSTART;VALUE=DATE:{formatted_date}",
"DURATION:P1D",
"END:VEVENT",
]

def generate(self):
"""Generate iCalendar data

Yields:
- str: Each line of iCalendar format
"""
yield "BEGIN:VCALENDAR"
yield "VERSION:2.0"
yield "PRODID:-//holidays Framework//NONSGML v1.9//EN"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get actual language from self.holidays.language.


for date, name in self.holidays.items():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember:

  1. one date can have multiple holidays
  2. iCal has a limited line length

yield from self.generate_event(date, name)

yield "END:VCALENDAR"
46 changes: 46 additions & 0 deletions tests/test_ical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# holidays
# --------
# A fast, efficient Python library for generating country, province and state
# specific sets of holidays on the fly. It aims to make determining whether a
# specific date is a holiday as fast and flexible as possible.
#
# Authors: Vacanza Team and individual contributors (see AUTHORS file)
# dr-prodigy <[email protected]> (c) 2017-2023
# ryanss <[email protected]> (c) 2014-2017
# Website: https://github.com/vacanza/holidays
# License: MIT (see LICENSE file)


from unittest import TestCase

from holidays import country_holidays
from holidays.ical import ICalExporter


class TestIcalExporter(TestCase):
def setUp(self):
self.us_holidays = country_holidays("US", years=2024)
self.exporter = ICalExporter(self.us_holidays)

def test_basic_calendar_structure(self):
output = "\n".join(self.exporter.generate())

self.assertIn("BEGIN:VCALENDAR", output)
self.assertIn("VERSION:2.0", output)
self.assertIn("END:VCALENDAR", output)

def test_single_holiday_event(self):
output = "\n".join(self.exporter.generate())

self.assertIn("BEGIN:VEVENT", output)
self.assertIn("SUMMARY:New Year's Day", output)
self.assertIn("DTSTART;VALUE=DATE:20240101", output)
self.assertIn("DURATION:P1D", output)
self.assertIn("END:VEVENT", output)

def test_localized_holiday_names(self):
jp_holidays = country_holidays("JP", years=2024, language="ja")
jp_exporter = ICalExporter(jp_holidays)
output = "\n".join(jp_exporter.generate())

self.assertIn("SUMMARY:元日", output)