From 25a877fa738edc507d7c7ac7a372f12034d0f071 Mon Sep 17 00:00:00 2001 From: Christian Geier Date: Fri, 3 Nov 2023 23:22:16 +0100 Subject: [PATCH] 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`). --- khal/controllers.py | 2 +- khal/parse_datetime.py | 7 ++++--- tests/parse_datetime_test.py | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/khal/controllers.py b/khal/controllers.py index a850b33c8..6af65592d 100644 --- a/khal/controllers.py +++ b/khal/controllers.py @@ -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"]: diff --git a/khal/parse_datetime.py b/khal/parse_datetime.py index c06057969..b13b8dd51 100644 --- a/khal/parse_datetime.py +++ b/khal/parse_datetime.py @@ -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') diff --git a/tests/parse_datetime_test.py b/tests/parse_datetime_test.py index 1dd9cd8a6..448a7a6a5 100644 --- a/tests/parse_datetime_test.py +++ b/tests/parse_datetime_test.py @@ -2,6 +2,7 @@ from collections import OrderedDict import pytest +import pytz from freezegun import freeze_time from khal.exceptions import DateTimeParseError, FatalError @@ -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: