credential_pool_timestamps.py 1009 B

12345678910111213141516171819202122232425262728293031
  1. """Timestamp parsing shared by credential-pool status, refresh, and cooldown bookkeeping."""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from typing import Any, Optional
  5. def _parse_absolute_timestamp(value: Any) -> Optional[float]:
  6. """Best-effort parse of epoch seconds / epoch ms / ISO-8601 into epoch seconds."""
  7. if value is None or value == "":
  8. return None
  9. if isinstance(value, (int, float)):
  10. numeric = float(value)
  11. if numeric <= 0:
  12. return None
  13. return numeric / 1000.0 if numeric > 1_000_000_000_000 else numeric
  14. if isinstance(value, str):
  15. raw = value.strip()
  16. if not raw:
  17. return None
  18. try:
  19. numeric = float(raw)
  20. return numeric / 1000.0 if numeric > 1_000_000_000_000 else numeric
  21. except ValueError:
  22. pass
  23. try:
  24. return datetime.fromisoformat(raw).timestamp()
  25. except ValueError:
  26. return None
  27. return None