tz_util.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2010-2015 MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Timezone related utilities for BSON."""
  15. from datetime import (timedelta,
  16. tzinfo)
  17. ZERO = timedelta(0)
  18. class FixedOffset(tzinfo):
  19. """Fixed offset timezone, in minutes east from UTC.
  20. Implementation based from the Python `standard library documentation
  21. <http://docs.python.org/library/datetime.html#tzinfo-objects>`_.
  22. Defining __getinitargs__ enables pickling / copying.
  23. """
  24. def __init__(self, offset, name):
  25. if isinstance(offset, timedelta):
  26. self.__offset = offset
  27. else:
  28. self.__offset = timedelta(minutes=offset)
  29. self.__name = name
  30. def __getinitargs__(self):
  31. return self.__offset, self.__name
  32. def utcoffset(self, dt):
  33. return self.__offset
  34. def tzname(self, dt):
  35. return self.__name
  36. def dst(self, dt):
  37. return ZERO
  38. utc = FixedOffset(0, "UTC")
  39. """Fixed offset timezone representing UTC."""