Skip to content

Commit

Permalink
fix detection of today and tomorrow
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
geier committed Nov 3, 2023
1 parent ee285e8 commit 25a877f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion khal/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def format_day(day: dt.date, format_string: str, locale, attributes=None):
attributes["date"] = day.strftime(locale['dateformat'])
attributes["date-long"] = day.strftime(locale['longdateformat'])

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

colors = {"reset": style("", reset=True), "bold": style("", bold=True, reset=False)}
for c in ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]:
Expand Down
7 changes: 4 additions & 3 deletions khal/parse_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ def weekdaypstr(dayname: str) -> int:
raise ValueError('invalid weekday name `%s`' % dayname)


def construct_daynames(date_: dt.date) -> str:
def construct_daynames(date_: dt.date, local_timezone) -> str:
"""converts datetime.date into a string description
either `Today`, `Tomorrow` or name of weekday.
"""
if date_ == dt.date.today():
today = dt.datetime.now(local_timezone).date()
if date_ == today:
return 'Today'
elif date_ == dt.date.today() + dt.timedelta(days=1):
elif date_ == today + dt.timedelta(days=1):
return 'Tomorrow'
else:
return date_.strftime('%A')
Expand Down
7 changes: 4 additions & 3 deletions tests/parse_datetime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import OrderedDict

import pytest
import pytz
from freezegun import freeze_time

from khal.exceptions import DateTimeParseError, FatalError
Expand Down Expand Up @@ -110,9 +111,9 @@ def test_weekdaypstr_invalid():

def test_construct_daynames():
with freeze_time('2016-9-19'):
assert construct_daynames(dt.date(2016, 9, 19)) == 'Today'
assert construct_daynames(dt.date(2016, 9, 20)) == 'Tomorrow'
assert construct_daynames(dt.date(2016, 9, 21)) == 'Wednesday'
assert construct_daynames(dt.date(2016, 9, 19), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Today'
assert construct_daynames(dt.date(2016, 9, 20), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Tomorrow'
assert construct_daynames(dt.date(2016, 9, 21), local_timezone=LOCALE_BERLIN['local_timezone']) == 'Wednesday'


class TestGuessDatetimefstr:
Expand Down

0 comments on commit 25a877f

Please sign in to comment.