Skip to content

Commit b5c22da

Browse files
kislyukUbuntu
authored andcommitted
Fall back to strptime if fromisoformat is not available
Also add tests for datetimes on MySQL
1 parent 38e4b9d commit b5c22da

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

sqlalchemy_aurora_data_api/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ def process(value):
5555
# and datetime.datetime.fromisoformat can't parse the result (example: '2019-10-31 09:37:17.31869'). Pad it.
5656
if isinstance(value, str) and self.iso_ts_re.match(value):
5757
value = self.iso_ts_re.sub(lambda match: match.group(0).ljust(26, "0"), value)
58-
return self.py_type.fromisoformat(value) if isinstance(value, str) else value
58+
if isinstance(value, str):
59+
try:
60+
return self.py_type.fromisoformat(value)
61+
except AttributeError: # fromisoformat not supported on Python < 3.7
62+
if self.py_type == datetime.date:
63+
return datetime.datetime.strptime(value, "%Y-%m-%d").date()
64+
if self.py_type == datetime.time:
65+
return datetime.datetime.strptime(value, "%H:%M:%S").time()
66+
return datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
67+
return value
5968
return process
6069

6170

test/test.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os, sys, json, unittest, logging, datetime, getpass, enum
22
from uuid import uuid4
33

4-
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Float, LargeBinary, Numeric, Date, Text, Enum
4+
from sqlalchemy import (create_engine, Column, Integer, String, Boolean, Float, LargeBinary, Numeric, Date, Time,
5+
DateTime, Text, Enum)
56
from sqlalchemy.dialects.postgresql import UUID, JSONB, DATE, TIME, TIMESTAMP, ARRAY
67
from sqlalchemy.ext.declarative import declarative_base
78
from sqlalchemy.orm import sessionmaker
@@ -102,12 +103,15 @@ class Socks(enum.Enum):
102103

103104

104105
class BasicUser(BasicBase):
105-
__tablename__ = "sqlalchemy_aurora_data_api_test"
106+
__tablename__ = "sqlalchemy_aurora_data_api_testC"
106107

107108
id = Column(Integer, primary_key=True)
108109
name = Column(String(64))
109110
fullname = Column(String(64))
110111
nickname = Column(String(64))
112+
birthday = Column(Date)
113+
eats_breakfast_at = Column(Time)
114+
married_at = Column(DateTime)
111115

112116

113117
class User(Base):
@@ -228,7 +232,11 @@ def test_execute(self):
228232

229233
def test_orm(self):
230234
BasicBase.metadata.create_all(self.engine)
231-
ed_user = BasicUser(name='ed', fullname='Ed Jones', nickname='edsnickname')
235+
birthday = datetime.datetime.fromtimestamp(0).date()
236+
eats_breakfast_at = datetime.time(9, 0, 0, 123)
237+
married_at = datetime.datetime(2020, 2, 20, 2, 20, 2, 200200)
238+
ed_user = BasicUser(name='ed', fullname='Ed Jones', nickname='edsnickname',
239+
birthday=birthday, eats_breakfast_at=eats_breakfast_at, married_at=married_at)
232240
Session = sessionmaker(bind=self.engine)
233241
session = Session()
234242

@@ -241,6 +249,9 @@ def test_orm(self):
241249
self.assertGreater(session.query(BasicUser).filter(BasicUser.name.like('%ed')).count(), 0)
242250
u = session.query(BasicUser).filter(BasicUser.name.like('%ed')).first()
243251
self.assertEqual(u.nickname, "edsnickname")
252+
self.assertEqual(u.birthday, birthday)
253+
self.assertEqual(u.eats_breakfast_at, eats_breakfast_at.replace(microsecond=0))
254+
self.assertEqual(u.married_at, married_at.replace(microsecond=0))
244255

245256

246257
if __name__ == "__main__":

0 commit comments

Comments
 (0)