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

Fix date exclusion #57

Draft
wants to merge 2 commits into
base: main
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
24 changes: 24 additions & 0 deletions tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ def test_calendar_event_different_timezone():
assert "DTSTART:20220131T220000Z" # 1 feb


def test_calendar_no_timezone():
"""We default to UTC if no timezone is given."""
cal = files_to_calendar(
[
iowrap(
"""
events:
- summary: Some new year's days
begin: 2022-01-01
repeat:
interval:
years: 1
until: 2030-01-01
except_on:
- 2023-01-01
"""
)
]
)
cal_str = cal.serialize()
assert cal_str.startswith("BEGIN:VCALENDAR")
assert "UTC" in cal_str


def test_calendar_name():
cal = files_to_calendar(
[
Expand Down
5 changes: 4 additions & 1 deletion tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ def test_exception():
)
)
event_str = event.serialize()
# Handle line overflow
event_str = event_str.replace(" ", "").replace("\n", "").replace("\r", "")

# DTEND exists and is the next day, calendar tools import this
# correctly as being a one-day event
assert (
"EXDATE;TZID=/ics.py/2020.1/America/Los_Angeles:20220713,20220714T060000"
"EXDATE;TZID=/ics.py/2020.1/America/Los_Angeles:20220713T000000,20220714T060000"
in event_str
)
assert "RDATE;TZID=/ics.py/2020.1/America/Los_Angeles:20221224T060000" in event_str
Expand Down
8 changes: 6 additions & 2 deletions yaml2ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def datetime2utc(date):
if isinstance(date, datetime.datetime):
return datetime.datetime.strftime(date, "%Y%m%dT%H%M%S")
elif isinstance(date, datetime.date):
return datetime.datetime.strftime(date, "%Y%m%d")
return datetime.datetime.strftime(date, "%Y%m%dT000000")
else:
raise RuntimeError(f"Unsure how to convert {date} to UTC")


# See RFC2445, 4.8.5 REcurrence Component Properties
Expand Down Expand Up @@ -156,9 +158,11 @@ def files_to_events(files: list) -> (ics.Calendar, str):
calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
else:
calendar_yaml = yaml.load(open(f), Loader=yaml.FullLoader)
tz = calendar_yaml.get("timezone", None)
tz = calendar_yaml.get("timezone")
if tz is not None:
tz = gettz(tz)
else:
tz = dateutil.tz.UTC
if "include" in calendar_yaml:
included_events, _name = files_to_events(
os.path.join(os.path.dirname(f), newfile)
Expand Down