From 07587cb6818dd47dc8c6c5eba202db9b4f31526d Mon Sep 17 00:00:00 2001 From: Roman Zimmermann Date: Fri, 6 Sep 2024 11:27:32 +0200 Subject: [PATCH 1/2] fix(pendulum): Make PendulumDateTime work with postgres fixes #754 --- .../enriched_datetime/pendulum_datetime.py | 18 +++++------------- tests/types/test_enriched_datetime_pendulum.py | 9 +++++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py b/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py index d73086bf..59db1aeb 100644 --- a/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py +++ b/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py @@ -1,4 +1,4 @@ -from datetime import datetime +import datetime from ...exceptions import ImproperlyConfigured @@ -24,26 +24,18 @@ def _coerce(self, impl, value): value = pendulum.from_timestamp(value) elif isinstance(value, str) and value.isdigit(): value = pendulum.from_timestamp(int(value)) - elif isinstance(value, datetime): - value = pendulum.datetime( - value.year, - value.month, - value.day, - value.hour, - value.minute, - value.second, - value.microsecond - ) + elif isinstance(value, datetime.datetime): + value = pendulum.DateTime.instance(value) else: value = pendulum.parse(value) return value def process_bind_param(self, impl, value, dialect): if value: - return self._coerce(impl, value).in_tz('UTC') + return self._coerce(impl, value).in_tz('UTC').naive() return value def process_result_value(self, impl, value, dialect): if value: - return pendulum.parse(value.isoformat()) + return pendulum.DateTime.instance(value.replace(tzinfo=datetime.timezone.utc)) return value diff --git a/tests/types/test_enriched_datetime_pendulum.py b/tests/types/test_enriched_datetime_pendulum.py index dc39cd30..547449bf 100644 --- a/tests/types/test_enriched_datetime_pendulum.py +++ b/tests/types/test_enriched_datetime_pendulum.py @@ -66,6 +66,15 @@ def test_utc(self, session, User): session.commit() assert user.created_at == time + @pytest.mark.usefixtures('postgresql_dsn') + def test_utc_postgres(self, session, User): + time = pendulum_datetime.pendulum.now("UTC") + user = User(created_at=time) + session.add(user) + assert user.created_at == time + session.commit() + assert user.created_at == time + def test_other_tz(self, session, User): time = pendulum_datetime.pendulum.now("UTC") local = time.in_tz('Asia/Tokyo') From f3f33f78337ca7ef195ee4ff8767e0848037ef1c Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Fri, 6 Sep 2024 06:10:10 -0500 Subject: [PATCH 2/2] Adjust line length to pass lint tests --- sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py b/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py index 59db1aeb..77c3defa 100644 --- a/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py +++ b/sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py @@ -37,5 +37,7 @@ def process_bind_param(self, impl, value, dialect): def process_result_value(self, impl, value, dialect): if value: - return pendulum.DateTime.instance(value.replace(tzinfo=datetime.timezone.utc)) + return pendulum.DateTime.instance( + value.replace(tzinfo=datetime.timezone.utc) + ) return value