_tz.py 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # encoding: utf-8
  2. """
  3. Timezone utilities
  4. Just UTC-awareness right now
  5. """
  6. # Copyright (c) Jupyter Development Team.
  7. # Distributed under the terms of the Modified BSD License.
  8. from datetime import tzinfo, timedelta, datetime
  9. # constant for zero offset
  10. ZERO = timedelta(0)
  11. class tzUTC(tzinfo):
  12. """tzinfo object for UTC (zero offset)"""
  13. def utcoffset(self, d):
  14. return ZERO
  15. def dst(self, d):
  16. return ZERO
  17. UTC = tzUTC()
  18. def utc_aware(unaware):
  19. """decorator for adding UTC tzinfo to datetime's utcfoo methods"""
  20. def utc_method(*args, **kwargs):
  21. dt = unaware(*args, **kwargs)
  22. return dt.replace(tzinfo=UTC)
  23. return utc_method
  24. utcfromtimestamp = utc_aware(datetime.utcfromtimestamp)
  25. utcnow = utc_aware(datetime.utcnow)
  26. def isoformat(dt):
  27. """Return iso-formatted timestamp
  28. Like .isoformat(), but uses Z for UTC instead of +00:00
  29. """
  30. return dt.isoformat().replace('+00:00', 'Z')