Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ignore = [
"ARG001", # Unused function argument
"PT012", # `pytest.raises()` block
"PLW0129", # Asserting on a non-empty string literal will always pass
"SIM108", # Use ternary operator instead of `if`-`else`-block
]

[tool.pytest.ini_options]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,19 @@ def test_events_with_multiple_timezones():
assert events[1].begin.tzname() in ("UTC", "Coordinated Universal Time")
assert events[2].begin.tzname() == "PDT"
assert events[3].begin.tzname() == "PST"


def test_events_without_timezone():
f = io.BytesIO(b"""
name: Default Timezone
events:
- summary: Meeting A
begin: 2025-09-02 17:00:00
duration: { minutes: 60 }
- summary: Meeting B
begin: 2025-12-01 09:00:00
end: 2025-12-01 10:00:00
""")
events, _ = files_to_events([f])
assert events[0].begin.tzname() in ("UTC", "Coordinated Universal Time")
assert events[1].begin.tzname() in ("UTC", "Coordinated Universal Time")
10 changes: 7 additions & 3 deletions yaml2ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,20 @@ def gettz(tzname: str) -> datetime.tzinfo:
# This function can be used to add a list of e.g. exception dates (EXDATE) or
# recurrence dates (RDATE) to a reoccurring event
def add_recurrence_property(
Copy link
Member

Choose a reason for hiding this comment

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

I think we make TZ required for now; this is for internal use anyway.

event: ics.Event, property_name, dates: map, tz: datetime.tzinfo = None
event: ics.Event, property_name, dates: map, tz: datetime.tzinfo = dateutil.tz.UTC
):
event.extra.append(
ics.ContentLine(
name=property_name,
params={"TZID": [str(ics.Timezone.from_tzinfo(tz))]} if tz else None,
params={"TZID": [str(ics.Timezone.from_tzinfo(tz))]},
value=",".join(dates),
)
)


def event_from_yaml(event_yaml: dict, tz: datetime.tzinfo = None) -> ics.Event:
def event_from_yaml(
event_yaml: dict, tz: datetime.tzinfo = dateutil.tz.UTC
Copy link
Member

Choose a reason for hiding this comment

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

I think either change the default, or change the handling below, but not both.

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel it makes most sense to change the default here, and then let the event timezone overwrite. Or it maybe cleaner to drop the tz kwarg altogether and rely only on the yaml and have a conditional?

    if "timezone" in d:
        tz = gettz(d.pop("timezone"))
    else:
        tz = dateutil.tz.UTC

Copy link
Member

Choose a reason for hiding this comment

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

Or, just break if the event_yaml does not have a timezone.

Copy link
Member

Choose a reason for hiding this comment

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

OK, after #125 this becomes the crux of the current PR. We should document this in the README.

) -> ics.Event:
d = event_yaml
repeat = d.pop("repeat", None)
ics_custom = d.pop("ics", None)
Expand Down Expand Up @@ -184,6 +186,8 @@ def files_to_events(files: list) -> (ics.Calendar, str):
tz = calendar_yaml.get("timezone", None)
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