tz.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # encoding: utf-8
  2. """
  3. Timezone utilities
  4. Just UTC-awareness right now
  5. """
  6. #-----------------------------------------------------------------------------
  7. # Copyright (C) 2013 The IPython Development Team
  8. #
  9. # Distributed under the terms of the BSD License. The full license is in
  10. # the file COPYING, distributed as part of this software.
  11. #-----------------------------------------------------------------------------
  12. #-----------------------------------------------------------------------------
  13. # Imports
  14. #-----------------------------------------------------------------------------
  15. from datetime import tzinfo, timedelta, datetime
  16. #-----------------------------------------------------------------------------
  17. # Code
  18. #-----------------------------------------------------------------------------
  19. # constant for zero offset
  20. ZERO = timedelta(0)
  21. class tzUTC(tzinfo):
  22. """tzinfo object for UTC (zero offset)"""
  23. def utcoffset(self, d):
  24. return ZERO
  25. def dst(self, d):
  26. return ZERO
  27. UTC = tzUTC() # type: ignore[abstract]
  28. def utc_aware(unaware):
  29. """decorator for adding UTC tzinfo to datetime's utcfoo methods"""
  30. def utc_method(*args, **kwargs):
  31. dt = unaware(*args, **kwargs)
  32. return dt.replace(tzinfo=UTC)
  33. return utc_method
  34. utcfromtimestamp = utc_aware(datetime.utcfromtimestamp)
  35. utcnow = utc_aware(datetime.utcnow)