Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pendulum): Make PendulumDateTime work with postgres #755

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions sqlalchemy_utils/types/enriched_datetime/pendulum_datetime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
import datetime

from ...exceptions import ImproperlyConfigured

Expand All @@ -24,26 +24,20 @@ 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
9 changes: 9 additions & 0 deletions tests/types/test_enriched_datetime_pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down