_tz.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. Timezone utilities
  3. Just UTC-awareness right now
  4. """
  5. # Copyright (c) Jupyter Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. from __future__ import annotations
  8. from datetime import datetime, timedelta, timezone, tzinfo
  9. # constant for zero offset
  10. ZERO = timedelta(0)
  11. class tzUTC(tzinfo): # noqa: N801
  12. """tzinfo object for UTC (zero offset)"""
  13. def utcoffset(self, d: datetime | None) -> timedelta:
  14. """Compute utcoffset."""
  15. return ZERO
  16. def dst(self, d: datetime | None) -> timedelta:
  17. """Compute dst."""
  18. return ZERO
  19. def utcnow() -> datetime:
  20. """Return timezone-aware UTC timestamp"""
  21. return datetime.now(timezone.utc)
  22. def utcfromtimestamp(timestamp: float) -> datetime:
  23. return datetime.fromtimestamp(timestamp, timezone.utc)
  24. UTC = tzUTC() # type:ignore[abstract]
  25. def isoformat(dt: datetime) -> str:
  26. """Return iso-formatted timestamp
  27. Like .isoformat(), but uses Z for UTC instead of +00:00
  28. """
  29. return dt.isoformat().replace("+00:00", "Z")