forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime_utils.py
More file actions
26 lines (22 loc) · 803 Bytes
/
Copy pathdatetime_utils.py
File metadata and controls
26 lines (22 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from datetime import datetime, timezone
from typing import Any
def coerce_utc_datetime(value: Any) -> datetime | None:
"""Coerce a datetime/ISO timestamp to a timezone-aware UTC datetime.
Returns None for missing or malformed values so callers can retain records
while emitting domain-specific metrics instead of crashing on mixed timestamp
shapes.
"""
if value is None:
return None
if isinstance(value, datetime):
parsed = value
elif isinstance(value, str):
try:
parsed = datetime.fromisoformat(value.replace('Z', '+00:00'))
except ValueError:
return None
else:
return None
if parsed.tzinfo is None:
return parsed.replace(tzinfo=timezone.utc)
return parsed.astimezone(timezone.utc)