Skip to content

Commit a436a43

Browse files
committed
test(export): add additional unit tests for iso_timestamp_to_date function
1 parent b9d4d39 commit a436a43

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

tests/test_export_day_filter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
def test_iso_timestamp_to_date():
1717
assert iso_timestamp_to_date("2026-04-06T12:00:00Z") == date(2026, 4, 6)
18+
# 23:00 -05:00 is 04:00 UTC the next calendar day (not the Y-M-D prefix).
19+
assert iso_timestamp_to_date("2026-04-06T23:00:00-05:00") == date(2026, 4, 7)
20+
assert iso_timestamp_to_date("2026-04-06") == date(2026, 4, 6)
1821
assert iso_timestamp_to_date(None) is None
1922

2023

utils/export_day_filter.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,29 @@
99

1010

1111
def iso_timestamp_to_date(ts: str | None) -> date | None:
12-
"""First 10 chars of an ISO timestamp as a UTC calendar date."""
12+
"""Calendar date in UTC for an ISO-8601 *ts* (offset-aware → convert; naive → that instant's date)."""
1313
if not ts or not isinstance(ts, str):
1414
return None
1515
s = ts.strip()
1616
if len(s) < 10:
1717
return None
18+
# Date-only: no instant to shift by offset; keep the given calendar day.
19+
if len(s) == 10 and "T" not in s:
20+
try:
21+
return date.fromisoformat(s[:10])
22+
except ValueError:
23+
return None
24+
normalized = s.replace("Z", "+00:00")
1825
try:
19-
return date.fromisoformat(s[:10])
26+
dt = datetime.fromisoformat(normalized)
2027
except ValueError:
21-
return None
28+
try:
29+
return date.fromisoformat(s[:10])
30+
except ValueError:
31+
return None
32+
if dt.tzinfo is not None and dt.utcoffset() is not None:
33+
return dt.astimezone(timezone.utc).date()
34+
return dt.date()
2235

2336

2437
def session_calendar_bounds(

0 commit comments

Comments
 (0)