Skip to content

Commit 71dcca2

Browse files
committed
Correct timedelta value for negative MySQL TIME datatype
1. minimum value for MySQL TIME datatype: '-838:59:59.000000' = ADDTIME('-838:00:00', ADDTIME('-00:59:00', '-00:00:59')) 2. appropriate timedelta representation of the same value in Python: datetime.timedelta(hours=-838, minutes=-59, seconds=-59) = -1 * datetime.timedelta(hours=838, minutes=59, seconds=59) 'sign' must be multiplied to the entire timedelta value, not just to the 'hours' value as in the existing code. refer to convert_timedelta() in pymysql: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py#L189 Datatype tests were modified to test the correct minimum value for TIME datatype.
1 parent 3de6ff4 commit 71dcca2

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

pymysqlreplication/row_event.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ def __read_time2(self, column):
285285
data = ~data + 1
286286

287287
t = datetime.timedelta(
288-
hours=sign*self.__read_binary_slice(data, 2, 10, 24),
288+
hours=self.__read_binary_slice(data, 2, 10, 24),
289289
minutes=self.__read_binary_slice(data, 12, 6, 24),
290290
seconds=self.__read_binary_slice(data, 18, 6, 24),
291291
microseconds=self.__read_fsp(column)
292-
)
292+
) * sign
293293
return t
294294

295295
def __read_date(self):

pymysqlreplication/tests/test_data_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def test_time(self):
291291
microseconds=(((838*60) + 59)*60 + 59)*1000000
292292
))
293293
self.assertEqual(event.rows[0]["values"]["test2"], datetime.timedelta(
294-
microseconds=(((-838*60) + 59)*60 + 59)*1000000
294+
microseconds=-(((838*60) + 59)*60 + 59)*1000000
295295
))
296296

297297
def test_time2(self):
@@ -306,7 +306,7 @@ def test_time2(self):
306306
microseconds=(((838*60) + 59)*60 + 59)*1000000 + 0
307307
))
308308
self.assertEqual(event.rows[0]["values"]["test2"], datetime.timedelta(
309-
microseconds=(((-838*60) + 59)*60 + 59)*1000000 + 0
309+
microseconds=-(((838*60) + 59)*60 + 59)*1000000 + 0
310310
))
311311

312312
def test_zero_time(self):

0 commit comments

Comments
 (0)