Skip to content

Commit

Permalink
Merge pull request #755 from torotil/fix-754
Browse files Browse the repository at this point in the history
fix(pendulum): Make PendulumDateTime work with postgres
  • Loading branch information
kurtmckee authored Sep 6, 2024
2 parents a66ee64 + f3f33f7 commit 19a0a10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
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

0 comments on commit 19a0a10

Please sign in to comment.