Skip to content

Commit

Permalink
Subtract dates not datetimes (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgboss authored Nov 7, 2023
1 parent 41334be commit d1d8460
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions api/app/tests/utils/test_time.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion api/app/utils/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

0 comments on commit d1d8460

Please sign in to comment.