Skip to content

Commit

Permalink
Merge c7a4016 into df7ad69
Browse files Browse the repository at this point in the history
  • Loading branch information
jhudsonbbm committed Jun 4, 2021
2 parents df7ad69 + c7a4016 commit e605e06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 14 additions & 0 deletions sqlobject/converters.py
Expand Up @@ -22,6 +22,13 @@
mxDateTimeType = None
mxDateTimeDeltaType = None

try:
import pendulum
except ImportError:
pendulumDateTimeType = None
else:
pendulumDateTimeType = pendulum.DateTime

try:
from DateTime import DateTime as zopeDateTime
except ImportError:
Expand Down Expand Up @@ -215,6 +222,13 @@ def mxTimeConverter(value, db):
registerConverter(mxDateTimeDeltaType, mxTimeConverter)


if pendulumDateTimeType:
def pendulumConverter(value, db):
return "'%s'" % value.to_datetime_string()

registerConverter(pendulum.DateTime, pendulumConverter)


if zopeDateTimeType:
def zopeDateTimeConverter(value, db):
return "'%s'" % value.strftime("%Y-%m-%d %H:%M:%S")
Expand Down
22 changes: 21 additions & 1 deletion sqlobject/tests/test_datetime.py
Expand Up @@ -7,7 +7,7 @@
DATETIME_IMPLEMENTATION, MXDATETIME_IMPLEMENTATION, mxdatetime_available, \
ZOPE_DATETIME_IMPLEMENTATION, zope_datetime_available
from sqlobject.tests.dbtest import getConnection, setupClass

from sqlobject.converters import pendulumDateTimeType

########################################
# Date/time columns
Expand Down Expand Up @@ -130,6 +130,26 @@ def test_mxDateTime():
assert dt2.col3.minute == _now.minute
assert dt2.col3.second == int(_now.second)

if pendulumDateTimeType:
import pendulum

class DateTimePendulum(SQLObject):
col1 = DateTimeCol()

def test_PendulumDateTime():
setupClass(DateTimePendulum)
_now = pendulum.now()
dtp = DateTimePendulum(col1=_now)

assert isinstance(dtp.col1, datetime)
assert dtp.col1.year == _now.year
assert dtp.col1.month == _now.month
assert dtp.col1.day == _now.day
assert dtp.col1.hour == _now.hour
assert dtp.col1.minute == _now.minute
assert int(dtp.col1.second) == int(_now.second)


if zope_datetime_available:
col.default_datetime_implementation = ZOPE_DATETIME_IMPLEMENTATION
from DateTime import DateTime as zopeDateTime
Expand Down

0 comments on commit e605e06

Please sign in to comment.