Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subtract dates not datetimes #3219

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
Loading