Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1953e16

Browse filesBrowse files
committed
Merge pull request PyMySQL#43 from tiwilliam/date-perf
Increase performance when casting dates
2 parents c528f50 + d0f96ff commit 1953e16
Copy full SHA for 1953e16

File tree

Expand file treeCollapse file tree

2 files changed

+34
-20
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-20
lines changed

‎MySQLdb/times.py

Copy file name to clipboardExpand all lines: MySQLdb/times.py
+31-20Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,32 @@ def format_TIMESTAMP(d):
4848

4949

5050
def DateTime_or_None(s):
51-
if ' ' in s:
52-
sep = ' '
53-
elif 'T' in s:
54-
sep = 'T'
55-
else:
56-
return Date_or_None(s)
57-
5851
try:
59-
d, t = s.split(sep, 1)
60-
if '.' in t:
61-
t, ms = t.split('.',1)
62-
ms = ms.ljust(6, '0')
52+
if len(s) < 11:
53+
return Date_or_None(s)
54+
55+
micros = s[20:]
56+
57+
if len(micros) == 0:
58+
# 12:00:00
59+
micros = 0
60+
elif len(micros) < 7:
61+
# 12:00:00.123456
62+
micros = int(micros) * 10 ** (6 - len(micros))
6363
else:
64-
ms = 0
65-
return datetime(*[ int(x) for x in d.split('-')+t.split(':')+[ms] ])
66-
except (SystemExit, KeyboardInterrupt):
67-
raise # pragma: no cover
68-
except:
69-
return Date_or_None(s)
64+
return None
65+
66+
return datetime(
67+
int(s[:4]), # year
68+
int(s[5:7]), # month
69+
int(s[8:10]), # day
70+
int(s[11:13] or 0), # hour
71+
int(s[14:16] or 0), # minute
72+
int(s[17:19] or 0), # second
73+
micros, # microsecond
74+
)
75+
except ValueError:
76+
return None
7077

7178
def TimeDelta_or_None(s):
7279
try:
@@ -107,14 +114,18 @@ def Time_or_None(s):
107114

108115
def Date_or_None(s):
109116
try:
110-
return date(*[ int(x) for x in s.split('-',2)])
111-
except (TypeError, ValueError):
117+
return date(
118+
int(s[:4]), # year
119+
int(s[5:7]), # month
120+
int(s[8:10]), # day
121+
)
122+
except ValueError:
112123
return None
113124

114125
def DateTime2literal(d, c):
115126
"""Format a DateTime object as an ISO timestamp."""
116127
return string_literal(format_TIMESTAMP(d), c)
117-
128+
118129
def DateTimeDelta2literal(d, c):
119130
"""Format a DateTimeDelta object as a time."""
120131
return string_literal(format_TIMEDELTA(d),c)

‎tests/test_MySQLdb_times.py

Copy file name to clipboardExpand all lines: tests/test_MySQLdb_times.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def test_date_or_none(self):
1919
assert times.Date_or_None('fail') is None
2020
assert times.Date_or_None('2015-12') is None
2121
assert times.Date_or_None('2015-12-40') is None
22+
assert times.Date_or_None('0000-00-00') is None
2223

2324
def test_time_or_none(self):
2425
assert times.Time_or_None('00:00:00') == time(0, 0)
@@ -44,6 +45,8 @@ def test_datetime_or_none(self):
4445

4546
assert times.DateTime_or_None('') is None
4647
assert times.DateTime_or_None('fail') is None
48+
assert times.DateTime_or_None('0000-00-00 00:00:00') is None
49+
assert times.DateTime_or_None('0000-00-00 00:00:00.000000') is None
4750
assert times.DateTime_or_None('2015-12-13T01:02:03.123456789') is None
4851

4952
def test_timedelta_or_none(self):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.