Skip to content

Commit 3c4327f

Browse files
committed
fix detection of today and tomorrow
When checking if days are the current or the 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 c878c10 commit 3c4327f

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

khal/controllers.py

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

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

6767
colors = {"reset": style("", reset=True), "bold": style("", bold=True, reset=False)}
6868
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ def test_weekdaypstr_invalid():
110110

111111
def test_construct_daynames():
112112
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'
113+
assert construct_daynames(dt.date(2016, 9, 19), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Today'
114+
assert construct_daynames(dt.date(2016, 9, 20), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Tomorrow'
115+
assert construct_daynames(dt.date(2016, 9, 21), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Wednesday'
116116

117117

118118
class TestGuessDatetimefstr:

0 commit comments

Comments
 (0)