Skip to content

Commit 0226f1a

Browse files
committed
Fix problem with return None if Datetime field contained microsecond (Issue #24)
1 parent 63ab179 commit 0226f1a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

MySQLdb/times.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ def DateTime_or_None(s):
6060
def TimeDelta_or_None(s):
6161
try:
6262
h, m, s = s.split(':')
63-
h, m, s = int(h), int(m), float(s)
64-
td = timedelta(hours=abs(h), minutes=m, seconds=int(s),
65-
microseconds=int(math.modf(s)[0] * 1000000))
63+
if '.' in s:
64+
s, ms = s.split('.')
65+
else:
66+
ms = 0
67+
h, m, s, ms = int(h), int(m), int(s), int(ms)
68+
td = timedelta(hours=abs(h), minutes=m, seconds=s,
69+
microseconds=ms)
6670
if h < 0:
6771
return -td
6872
else:
@@ -74,9 +78,13 @@ def TimeDelta_or_None(s):
7478
def Time_or_None(s):
7579
try:
7680
h, m, s = s.split(':')
77-
h, m, s = int(h), int(m), float(s)
78-
return time(hour=h, minute=m, second=int(s),
79-
microsecond=int(math.modf(s)[0] * 1000000))
81+
if '.' in s:
82+
s, ms = s.split('.')
83+
else:
84+
ms = 0
85+
h, m, s, ms = int(h), int(m), int(s), int(ms)
86+
return time(hour=h, minute=m, second=s,
87+
microsecond=ms)
8088
except ValueError:
8189
return None
8290

0 commit comments

Comments
 (0)