Skip to content

Commit 2dbfc3b

Browse files
bsipoczstefanv
andauthored
Default to UTC timezone (#116)
Co-authored-by: Stefan van der Walt <[email protected]>
1 parent b21e9de commit 2dbfc3b

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,28 @@ events:
6060
In this meeting we will ...
6161
```
6262
63+
## Timezones
64+
65+
The event timezone can be specified in three ways (in reverse order of precedence):
66+
67+
1. Using the `timezone: ...` field, as shown above under "Syntax".
68+
2. Adding it to an individual event:
69+
70+
```yaml
71+
- summary: My event
72+
timezone: US/Pacific
73+
```
74+
75+
3. By specifying a timezone in the event start time:
76+
77+
```
78+
2021-09-21 15:00:00 +07:00
79+
```
80+
6381
Valid timezones are listed at https://datetime.app/iana-timezones
6482

83+
If no timezone is set, we default to UTC.
84+
6585
## Contributing
6686

6787
Contributions are welcomed! This project is still in active development

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ ignore = [
8989
"ARG001", # Unused function argument
9090
"PT012", # `pytest.raises()` block
9191
"PLW0129", # Asserting on a non-empty string literal will always pass
92+
"SIM108", # Use ternary operator instead of `if`-`else`-block
9293
]
9394

9495
[tool.pytest.ini_options]

tests/test_events.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,19 @@ def test_events_with_multiple_timezones():
173173
assert events[1].begin.tzname() in ("UTC", "Coordinated Universal Time")
174174
assert events[2].begin.tzname() == "PDT"
175175
assert events[3].begin.tzname() == "PST"
176+
177+
178+
def test_events_without_timezone():
179+
f = io.BytesIO(b"""
180+
name: Default Timezone
181+
events:
182+
- summary: Meeting A
183+
begin: 2025-09-02 17:00:00
184+
duration: { minutes: 60 }
185+
- summary: Meeting B
186+
begin: 2025-12-01 09:00:00
187+
end: 2025-12-01 10:00:00
188+
""")
189+
events, _ = files_to_events([f])
190+
assert events[0].begin.tzname() in ("UTC", "Coordinated Universal Time")
191+
assert events[1].begin.tzname() in ("UTC", "Coordinated Universal Time")

yaml2ics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ def gettz(tzname: str) -> datetime.tzinfo:
5252
# This function can be used to add a list of e.g. exception dates (EXDATE) or
5353
# recurrence dates (RDATE) to a reoccurring event
5454
def add_recurrence_property(
55-
event: ics.Event, property_name, dates: map, tz: datetime.tzinfo = None
55+
event: ics.Event, property_name, dates: map, tz: datetime.tzinfo = dateutil.tz.UTC
5656
):
5757
event.extra.append(
5858
ics.ContentLine(
5959
name=property_name,
60-
params={"TZID": [str(ics.Timezone.from_tzinfo(tz))]} if tz else None,
60+
params={"TZID": [str(ics.Timezone.from_tzinfo(tz))]},
6161
value=",".join(dates),
6262
)
6363
)
@@ -68,6 +68,8 @@ def event_from_yaml(event_yaml: dict, tz: datetime.tzinfo = None) -> ics.Event:
6868
repeat = d.pop("repeat", None)
6969
ics_custom = d.pop("ics", None)
7070

71+
if tz is None:
72+
tz = dateutil.tz.UTC
7173
if "timezone" in d:
7274
tzname = d.pop("timezone")
7375
tz = gettz(tzname)

0 commit comments

Comments
 (0)