from datetime import UTC, datetime
# ISO 8601 UTC — lexicographically sortable, unambiguous, standard.
_TS_FMT = '%Y-%m-%dT%H:%M:%SZ'
_TS_FMT_LEGACY = '%d-%m-%Y_%H-%M-%S' # accepted on read, never written
[docs]
def now_utc() -> datetime:
return datetime.now(UTC)
[docs]
def now_utc_str()-> str:
return now_utc().strftime(_TS_FMT)
[docs]
def parse_ts(s: str | None) -> datetime | None:
"""Parse a stored mission timestamp as a UTC-aware datetime.
Accepts both current ISO 8601 format and the legacy dd-mm-yyyy_hh-mm-ss
format. Returns None for missing values or strings that match neither format.
"""
if not s:
return None
for fmt in (_TS_FMT, _TS_FMT_LEGACY):
try:
return datetime.strptime(s, fmt).replace(tzinfo=UTC)
except (TypeError, ValueError):
continue
return None