Skip to content

Commit 67af28b

Browse files
committed
BUG24938411: Fix datetime microsecond conversion
This patch fixes the microsecond conversion from MySQL datetime to Python datetime when using fractional values with the C extension enabled. A test was added for regression.
1 parent c8116ad commit 67af28b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ v8.0.23
1717
- BUG#31882419: Fix error when getting the connection ID from a CMySQLConnection
1818
- BUG#29195610: Fix cursor.callproc() using namedtuple and dictionary cursors
1919
- BUG#26834307: Make cursor.fetchone() and cursor.fetchmany() PEP 249 compliant
20+
- BUG#24938411: Fix datetime microsecond conversion
2021

2122
v8.0.22
2223
=======

src/mysql_capi_conversion.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2020, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -453,7 +453,7 @@ mytopy_datetime(const char *data, const unsigned long length)
453453
if (data != end && end - data >= 2 && *data == '.')
454454
{
455455
// Fractional part
456-
int field_length= 6; // max fractional - 1
456+
int field_length= 5;
457457
data++;
458458
value= (unsigned int)(*data - '0');
459459
while (data++ != end && isdigit(*data))
@@ -463,6 +463,13 @@ mytopy_datetime(const char *data, const unsigned long length)
463463
value= (value * 10) + (unsigned int)(*data - '0');
464464
}
465465
}
466+
if (field_length >= 0)
467+
{
468+
while (field_length-- > 0)
469+
{
470+
value*= 10;
471+
}
472+
}
466473
parts[6]= value;
467474
}
468475

tests/test_bugs.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5781,3 +5781,30 @@ def test_callproc_cursor_types(self):
57815781
with self.cnx.cursor(prepared=True) as cur:
57825782
self.assertRaises(errors.NotSupportedError,
57835783
cur.callproc, 'sp_bug29195610', (2020,))
5784+
5785+
5786+
class BugOra24938411(tests.MySQLConnectorTests):
5787+
"""BUG#24938411: FIX MICROSECOND CONVERSION FROM MYSQL DATETIME TO PYTHON
5788+
DATETIME.
5789+
"""
5790+
5791+
@tests.foreach_cnx()
5792+
def test_datetime_fractional(self):
5793+
with self.cnx.cursor() as cur:
5794+
cur.execute("DROP TABLE IF EXISTS bug24938411")
5795+
cur.execute(
5796+
"CREATE TABLE bug24938411 "
5797+
"(mydate datetime(3) DEFAULT NULL) ENGINE=InnoDB"
5798+
)
5799+
cur.execute(
5800+
'INSERT INTO bug24938411 (mydate) '
5801+
'VALUES ("2020-01-01 01:01:01.543")'
5802+
)
5803+
cur.execute(
5804+
"SELECT mydate, CAST(mydate AS CHAR) AS mydate_char "
5805+
"FROM bug24938411"
5806+
)
5807+
row = cur.fetchone()
5808+
self.assertEqual(row[0], datetime(2020, 1, 1, 1, 1, 1, 543000))
5809+
self.assertEqual(row[1], "2020-01-01 01:01:01.543")
5810+
cur.execute("DROP TABLE IF EXISTS bug24938411")

0 commit comments

Comments
 (0)