Skip to content

Commit

Permalink
Stop serializing timezone-naive datetime to timezone-aware dateime wi…
Browse files Browse the repository at this point in the history
…th UTC tz (#36379)
  • Loading branch information
hussein-awala authored Dec 25, 2023
1 parent 89aa437 commit 69f556d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 6 additions & 5 deletions airflow/serialization/serializers/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
serialize as serialize_timezone,
)
from airflow.utils.module_loading import qualname
from airflow.utils.timezone import convert_to_utc, is_naive

if TYPE_CHECKING:
import datetime
Expand All @@ -45,10 +44,8 @@ def serialize(o: object) -> tuple[U, str, int, bool]:

if isinstance(o, datetime):
qn = qualname(o)
if is_naive(o):
o = convert_to_utc(o)

tz = serialize_timezone(o.tzinfo)
tz = serialize_timezone(o.tzinfo) if o.tzinfo else None

return {TIMESTAMP: o.timestamp(), TIMEZONE: tz}, qn, __version__, True

Expand Down Expand Up @@ -83,7 +80,11 @@ def deserialize(classname: str, version: int, data: dict | str) -> datetime.date
else:
tz = timezone(data[TIMEZONE])
else:
tz = deserialize_timezone(data[TIMEZONE][1], data[TIMEZONE][2], data[TIMEZONE][0])
tz = (
deserialize_timezone(data[TIMEZONE][1], data[TIMEZONE][2], data[TIMEZONE][0])
if data[TIMEZONE]
else None
)

if classname == qualname(datetime.datetime) and isinstance(data, dict):
return datetime.datetime.fromtimestamp(float(data[TIMESTAMP]), tz=tz)
Expand Down
5 changes: 5 additions & 0 deletions tests/serialization/serializers/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def test_datetime(self):
d = deserialize(s)
assert i.timestamp() == d.timestamp()

i = datetime.datetime.now()
s = serialize(i)
d = deserialize(s)
assert i.timestamp() == d.timestamp()

def test_deserialize_datetime_v1(self):
s = {
"__classname__": "pendulum.datetime.DateTime",
Expand Down

0 comments on commit 69f556d

Please sign in to comment.