Skip to content

Commit

Permalink
cli: Display datetimes in local timezone
Browse files Browse the repository at this point in the history
Previously the start and end datetimes were always printed out in the
timezone that they appear in the calendar entry.

In 7a8d584 duration support was added and an attempt was already made
to display the datetime in the local timezone. Unfortunately in that
specific case the `start.astimezone(start.tzinfo)` is a no-op and does
absolutely nothing. Unlike the name suggests, `astimezone()` adjusts the
date and time data, such that they match the passed tzinfo, but since
that is the same timezone data as before, nothing changes. [0]

In order to properly convert to the user's local timezone, we need to
call the method with no arguments.

With this the timezone is always properly displayed, which makes up for
a much nicer UX for users of the cli.

The test has to be adapted to expect the datetime in the local timezone,
hence we cannot hardcode the entire expected string anymore.

[0] https://docs.python.org/3/library/datetime.html#datetime.datetime.astimezone
  • Loading branch information
vimpostor committed Sep 28, 2023
1 parent ed2c8cc commit 1e0c78f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Changelog

Minor changes:

- ...
- The cli utility now displays start and end datetimes in the user's local timezone.
Ref: #561
[vimpostor]

Breaking changes:

Expand Down
4 changes: 2 additions & 2 deletions src/icalendar/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def view(event):
end = event.decoded('dtend', default=start)
duration = event.decoded('duration', default=end - start)
if isinstance(start, datetime):
start = start.astimezone(start.tzinfo)
start = start.astimezone()
start = start.strftime('%c')
if isinstance(end, datetime):
end = end.astimezone(end.tzinfo)
end = end.astimezone()
end = end.strftime('%c')

return f""" Organizer: {organizer}
Expand Down
30 changes: 22 additions & 8 deletions src/icalendar/tests/test_cli_tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import unittest

from datetime import tzinfo, datetime
from icalendar import Calendar, cli
try:
import zoneinfo
except ModuleNotFoundError:
from backports import zoneinfo

INPUT = '''
BEGIN:VCALENDAR
Expand All @@ -21,7 +26,7 @@
ORGANIZER:organizer@test.test
ATTENDEE:attendee1@example.com
ATTENDEE:attendee2@test.test
SUMMARY:Test summury
SUMMARY:Test summary
DTSTART;TZID=Europe/Warsaw:20220820T200000
DTEND;TZID=Europe/Warsaw:20220820T203000
LOCATION:New Amsterdam, 1010 Test Street
Expand All @@ -36,13 +41,22 @@
END:VCALENDAR
'''

PROPER_OUTPUT = ''' Organizer: organizer <organizer@test.test>
def local_datetime(dt):
return datetime.strptime(dt, "%Y%m%dT%H%M%S").replace(tzinfo=zoneinfo.ZoneInfo("Europe/Warsaw")).astimezone().strftime('%c')

# datetimes are displayed in the local timezone, so we cannot just hardcode them
firststart = local_datetime('20220820T103400')
firstend = local_datetime('20220820T113400')
secondstart = local_datetime('20220820T200000')
secondend = local_datetime('20220820T203000')

PROPER_OUTPUT = f""" Organizer: organizer <organizer@test.test>
Attendees:
attendee1 <attendee1@example.com>
attendee2 <attendee2@test.test>
Summary : Test Summary
Starts : Sat Aug 20 10:34:00 2022
End : Sat Aug 20 11:34:00 2022
Starts : {firststart}
End : {firstend}
Duration : 1:00:00
Location : New Amsterdam, 1000 Sunrise Test Street
Comment : Comment
Expand All @@ -53,9 +67,9 @@
Attendees:
attendee1 <attendee1@example.com>
attendee2 <attendee2@test.test>
Summary : Test summury
Starts : Sat Aug 20 20:00:00 2022
End : Sat Aug 20 20:30:00 2022
Summary : Test summary
Starts : {secondstart}
End : {secondend}
Duration : 0:30:00
Location : New Amsterdam, 1010 Test Street
Comment :
Expand All @@ -75,7 +89,7 @@
Description:
'''
"""

class CLIToolTest(unittest.TestCase):
def test_output_is_proper(self):
Expand Down

0 comments on commit 1e0c78f

Please sign in to comment.