tz.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # encoding: utf-8
  2. """
  3. Timezone utilities
  4. Just UTC-awareness right now
  5. Deprecated since IPython 8.19.0.
  6. """
  7. # -----------------------------------------------------------------------------
  8. # Copyright (C) 2013 The IPython Development Team
  9. #
  10. # Distributed under the terms of the BSD License. The full license is in
  11. # the file COPYING, distributed as part of this software.
  12. # -----------------------------------------------------------------------------
  13. # -----------------------------------------------------------------------------
  14. # Imports
  15. # -----------------------------------------------------------------------------
  16. import warnings
  17. from datetime import tzinfo, timedelta, datetime
  18. # -----------------------------------------------------------------------------
  19. # Code
  20. # -----------------------------------------------------------------------------
  21. __all__ = ["tzUTC", "utc_aware", "utcfromtimestamp", "utcnow"]
  22. # constant for zero offset
  23. ZERO = timedelta(0)
  24. def __getattr__(name):
  25. if name not in __all__:
  26. err = f"IPython.utils.tz is deprecated and has no attribute {name}"
  27. raise AttributeError(err)
  28. _warn_deprecated()
  29. return getattr(name)
  30. def _warn_deprecated():
  31. msg = "The module `IPython.utils.tz` is deprecated and will be completely removed."
  32. warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
  33. class tzUTC(tzinfo):
  34. """tzinfo object for UTC (zero offset)
  35. Deprecated since IPython 8.19.0.
  36. """
  37. _warn_deprecated()
  38. def utcoffset(self, d):
  39. return ZERO
  40. def dst(self, d):
  41. return ZERO
  42. UTC = tzUTC() # type: ignore[abstract]
  43. def utc_aware(unaware):
  44. """decorator for adding UTC tzinfo to datetime's utcfoo methods
  45. Deprecated since IPython 8.19.0.
  46. """
  47. def utc_method(*args, **kwargs):
  48. _warn_deprecated()
  49. dt = unaware(*args, **kwargs)
  50. return dt.replace(tzinfo=UTC)
  51. return utc_method
  52. utcfromtimestamp = utc_aware(datetime.utcfromtimestamp)
  53. utcnow = utc_aware(datetime.utcnow)