test_local_date_bounds.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """A date filter means a local calendar day (DB-C12).
  2. The Stats page's Today / This Week / This Month sent UTC dates, and the
  3. backend turned each into a UTC day: in Berlin, "Today" at 01:30 showed the
  4. prints from 02:00 the day before until 01:59. A date is now a day in the
  5. zone the caller names (the Stats page sends the browser's), else in the
  6. server's TZ.
  7. """
  8. from datetime import date, datetime, timezone
  9. import pytest
  10. from backend.app.utils.local_time import local_date_end, local_date_start
  11. @pytest.fixture
  12. def berlin(monkeypatch):
  13. monkeypatch.setenv("TZ", "Europe/Berlin")
  14. def test_a_berlin_day_starts_at_its_midnight(berlin):
  15. assert local_date_start(date(2026, 10, 16)) == datetime(2026, 10, 15, 22, 0, tzinfo=timezone.utc)
  16. def test_a_berlin_day_ends_just_before_the_next_midnight(berlin):
  17. end = local_date_end(date(2026, 10, 16))
  18. assert end == datetime(2026, 10, 16, 21, 59, 59, 999999, tzinfo=timezone.utc)
  19. def test_the_dst_change_day_is_25_hours(berlin):
  20. start = local_date_start(date(2026, 10, 25))
  21. end = local_date_end(date(2026, 10, 25))
  22. assert (end - start).total_seconds() == pytest.approx(25 * 3600, abs=1)
  23. def test_without_a_tz_a_day_is_a_utc_day(monkeypatch):
  24. monkeypatch.delenv("TZ", raising=False)
  25. assert local_date_start(date(2026, 10, 16)) == datetime(2026, 10, 16, tzinfo=timezone.utc)
  26. def test_the_far_ends_of_the_calendar_do_not_overflow(berlin):
  27. """An open-ended range sent as 0001-01-01 .. 9999-12-31 must not 500."""
  28. assert local_date_start(date(1, 1, 1)) == datetime.min.replace(tzinfo=timezone.utc)
  29. assert local_date_end(date(9999, 12, 31)) == datetime.max.replace(tzinfo=timezone.utc)
  30. def test_a_zone_name_that_is_a_folder_falls_back(monkeypatch):
  31. from backend.app.utils.local_time import day_zone
  32. monkeypatch.setenv("TZ", "UTC")
  33. assert str(day_zone("America")) in ("UTC", "UTC+00:00")
  34. assert str(day_zone("../etc/passwd")) in ("UTC", "UTC+00:00")