tz_util.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 __future__ import annotations
  16. from datetime import datetime, timedelta, tzinfo
  17. from typing import Optional, Tuple, Union
  18. ZERO: timedelta = timedelta(0)
  19. class FixedOffset(tzinfo):
  20. """Fixed offset timezone, in minutes east from UTC.
  21. Implementation based from the Python `standard library documentation
  22. <http://docs.python.org/library/datetime.html#tzinfo-objects>`_.
  23. Defining __getinitargs__ enables pickling / copying.
  24. """
  25. def __init__(self, offset: Union[float, timedelta], name: str) -> None:
  26. if isinstance(offset, timedelta):
  27. self.__offset = offset
  28. else:
  29. self.__offset = timedelta(minutes=offset)
  30. self.__name = name
  31. def __getinitargs__(self) -> Tuple[timedelta, str]:
  32. return self.__offset, self.__name
  33. def utcoffset(self, dt: Optional[datetime]) -> timedelta:
  34. return self.__offset
  35. def tzname(self, dt: Optional[datetime]) -> str:
  36. return self.__name
  37. def dst(self, dt: Optional[datetime]) -> timedelta:
  38. return ZERO
  39. utc: FixedOffset = FixedOffset(0, "UTC")
  40. """Fixed offset timezone representing UTC."""