-
Notifications
You must be signed in to change notification settings - Fork 490
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
kawazap
wants to merge
3
commits into
vacanza:dev
Choose a base branch
from
kawazap:feature/ical_export
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
"""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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can get actual language from |
||
|
||
for date, name in self.holidays.items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember:
|
||
yield from self.generate_event(date, name) | ||
|
||
yield "END:VCALENDAR" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.