Skip to content

Commit 25a877f

Browse files
committed
fix detection of today and tomorrow
When checking if days are the current or next day, we did not take the local timezone into account. Therefore, if the local timezone was not UTC, the previous or next day could erroneously be detected as `today` (same error for `tomorrow`).
1 parent ee285e8 commit 25a877f

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

khal/controllers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def format_day(day: dt.date, format_string: str, locale, attributes=None):
6161
attributes["date"] = day.strftime(locale['dateformat'])
6262
attributes["date-long"] = day.strftime(locale['longdateformat'])
6363

64-
attributes["name"] = parse_datetime.construct_daynames(day)
64+
attributes["name"] = parse_datetime.construct_daynames(day, local_timezone=locale['local_timezone'])
6565

6666
colors = {"reset": style("", reset=True), "bold": style("", bold=True, reset=False)}
6767
for c in ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]:

khal/parse_datetime.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,15 @@ def weekdaypstr(dayname: str) -> int:
125125
raise ValueError('invalid weekday name `%s`' % dayname)
126126

127127

128-
def construct_daynames(date_: dt.date) -> str:
128+
def construct_daynames(date_: dt.date, local_timezone) -> str:
129129
"""converts datetime.date into a string description
130130
131131
either `Today`, `Tomorrow` or name of weekday.
132132
"""
133-
if date_ == dt.date.today():
133+
today = dt.datetime.now(local_timezone).date()
134+
if date_ == today:
134135
return 'Today'
135-
elif date_ == dt.date.today() + dt.timedelta(days=1):
136+
elif date_ == today + dt.timedelta(days=1):
136137
return 'Tomorrow'
137138
else:
138139
return date_.strftime('%A')

tests/parse_datetime_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import OrderedDict
33

44
import pytest
5+
import pytz
56
from freezegun import freeze_time
67

78
from khal.exceptions import DateTimeParseError, FatalError
@@ -110,9 +111,9 @@ def test_weekdaypstr_invalid():
110111

111112
def test_construct_daynames():
112113
with freeze_time('2016-9-19'):
113-
assert construct_daynames(dt.date(2016, 9, 19)) == 'Today'
114-
assert construct_daynames(dt.date(2016, 9, 20)) == 'Tomorrow'
115-
assert construct_daynames(dt.date(2016, 9, 21)) == 'Wednesday'
114+
assert construct_daynames(dt.date(2016, 9, 19), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Today'
115+
assert construct_daynames(dt.date(2016, 9, 20), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Tomorrow'
116+
assert construct_daynames(dt.date(2016, 9, 21), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Wednesday'
116117

117118

118119
class TestGuessDatetimefstr:

0 commit comments

Comments
 (0)