timezone.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. import datetime
  20. import sys
  21. from typing import TYPE_CHECKING, Any, cast
  22. from airflow.utils.module_loading import qualname
  23. if TYPE_CHECKING:
  24. from airflow.serialization.serde import U
  25. serializers = [
  26. "pendulum.tz.timezone.FixedTimezone",
  27. "pendulum.tz.timezone.Timezone",
  28. ]
  29. PY39 = sys.version_info >= (3, 9)
  30. if PY39:
  31. serializers.append("zoneinfo.ZoneInfo")
  32. else:
  33. serializers.append("backports.zoneinfo.ZoneInfo")
  34. deserializers = serializers
  35. __version__ = 1
  36. def serialize(o: object) -> tuple[U, str, int, bool]:
  37. """
  38. Encode a Pendulum Timezone for serialization.
  39. Airflow only supports timezone objects that implements Pendulum's Timezone
  40. interface. We try to keep as much information as possible to make conversion
  41. round-tripping possible (see ``decode_timezone``). We need to special-case
  42. UTC; Pendulum implements it as a FixedTimezone (i.e. it gets encoded as
  43. 0 without the special case), but passing 0 into ``pendulum.timezone`` does
  44. not give us UTC (but ``+00:00``).
  45. """
  46. from pendulum.tz.timezone import FixedTimezone
  47. name = qualname(o)
  48. if isinstance(o, FixedTimezone):
  49. if o.offset == 0:
  50. return "UTC", name, __version__, True
  51. return o.offset, name, __version__, True
  52. tz_name = _get_tzinfo_name(cast(datetime.tzinfo, o))
  53. if tz_name is not None:
  54. return tz_name, name, __version__, True
  55. if cast(datetime.tzinfo, o).utcoffset(None) == datetime.timedelta(0):
  56. return "UTC", qualname(FixedTimezone), __version__, True
  57. return "", "", 0, False
  58. def deserialize(classname: str, version: int, data: object) -> Any:
  59. from airflow.utils.timezone import parse_timezone
  60. if not isinstance(data, (str, int)):
  61. raise TypeError(f"{data} is not of type int or str but of {type(data)}")
  62. if version > __version__:
  63. raise TypeError(f"serialized {version} of {classname} > {__version__}")
  64. if "zoneinfo.ZoneInfo" in classname:
  65. try:
  66. from zoneinfo import ZoneInfo
  67. except ImportError:
  68. from backports.zoneinfo import ZoneInfo # type: ignore[no-redef]
  69. return ZoneInfo(data) # type: ignore[arg-type]
  70. return parse_timezone(data)
  71. # ported from pendulum.tz.timezone._get_tzinfo_name
  72. def _get_tzinfo_name(tzinfo: datetime.tzinfo | None) -> str | None:
  73. if tzinfo is None:
  74. return None
  75. if hasattr(tzinfo, "key"):
  76. # zoneinfo timezone
  77. return tzinfo.key
  78. elif hasattr(tzinfo, "name"):
  79. # Pendulum timezone
  80. return tzinfo.name
  81. elif hasattr(tzinfo, "zone"):
  82. # pytz timezone
  83. return tzinfo.zone # type: ignore[no-any-return]
  84. return None