From d1d846091e6ce3926f65c242931e0ea0347e3cb6 Mon Sep 17 00:00:00 2001 From: dgboss Date: Tue, 7 Nov 2023 09:24:58 -0800 Subject: [PATCH] Subtract dates not datetimes (#3219) --- api/app/tests/utils/test_time.py | 17 +++++++++++++++++ api/app/utils/time.py | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/api/app/tests/utils/test_time.py b/api/app/tests/utils/test_time.py index f8881e9d9..077eca2d0 100644 --- a/api/app/tests/utils/test_time.py +++ b/api/app/tests/utils/test_time.py @@ -1,5 +1,6 @@ from datetime import datetime from app.utils.time import get_utc_datetime, get_days_from_range +import pytz def test_get_utc_datetime(): @@ -42,3 +43,19 @@ def test_time_range_end_before_start(): oct_10_2022 = datetime(year=2022, month=10, day=10, hour=8, minute=30, second=30) days = get_days_from_range(oct_10_2022, oct_1_2022) assert (len(days) == 0) + +def test_time_range_across_time_change_november(): + "Time change should not impact range." + vancouver_tz = pytz.timezone("America/Vancouver") + nov_1_2023 = vancouver_tz.localize(datetime(year=2023, month=11, day=1, hour=0, minute=0, second=0)) + nov_6_2023 = vancouver_tz.localize(datetime(year=2023, month=11, day=6, hour=23, minute=59, second=59)) + days = get_days_from_range(nov_1_2023, nov_6_2023) + assert (len(days) == 6) + +def test_time_range_across_time_change_march(): + "Time change should not impact range." + vancouver_tz = pytz.timezone("America/Vancouver") + march_6_2024 = vancouver_tz.localize(datetime(year=2024, month=3, day=6, hour=0, minute=0, second=0)) + march_12_2024 = vancouver_tz.localize(datetime(year=2024, month=3, day=12, hour=23, minute=59, second=59)) + days = get_days_from_range(march_6_2024, march_12_2024) + assert (len(days) == 7) \ No newline at end of file diff --git a/api/app/utils/time.py b/api/app/utils/time.py index 065187b54..a6389f069 100644 --- a/api/app/utils/time.py +++ b/api/app/utils/time.py @@ -120,4 +120,4 @@ def get_utc_datetime(input_datetime: datetime): def get_days_from_range(start_time: datetime, end_time: datetime) -> List[datetime]: - return [start_time + timedelta(days=x) for x in range((end_time - start_time).days + 1)] + return [start_time + timedelta(days=x) for x in range((end_time.date() - start_time.date()).days + 1)]