--- conversion.py 2014-02-11 22:37:15.000000000 +0100 +++ conversion.py.patched 2014-04-28 19:53:51.609247000 +0200 @@ -260,14 +260,28 @@ Returns a bytes. """ - (hours, remainder) = divmod(value.seconds, 3600) + return_format = '{:02d}:{:02d}:{:02d}' + msc = value.microseconds + total_sec = abs(value.days * 86400 + value.seconds) # abs() is important! + + if msc: + return_format = return_format + '.{:06d}' + if value.days < 0: + msc = 1000000 - msc + total_sec -= 1 + + (hours, remainder) = divmod(total_sec, 3600) # divmod with total_sec < 0 is terrible! (mins, secs) = divmod(remainder, 60) - hours = hours + (value.days * 24) + + if value.days < 0: + return_format = '-' + return_format + if value.microseconds: - return '{:02d}:{:02d}:{:02d}.{:06d}'.format( - hours, mins, secs, value.microseconds).encode('ascii') - return '{:02d}:{:02d}:{:02d}'.format( - hours, mins, secs).encode('ascii') + return return_format.format( + hours, mins, secs, msc).encode('ascii') + else: + return return_format.format( + hours, mins, secs).encode('ascii') def _decimal_to_mysql(self, value): """ @@ -369,7 +383,7 @@ def _TIME_to_python(self, value, dsc=None): # pylint: disable=C0103 """ - Returns TIME column type as datetime.time type. + Returns TIME column type as datetime.timedelta type. """ time_val = None try: @@ -380,6 +394,8 @@ mcs = 0 try: (hrs, mins, sec) = [int(i) for i in hms.split(b':')] + if value[0] == b'-'[0]: # important + mins, sec, mcs = -mins, -sec, -mcs time_val = datetime.timedelta(hours=hrs, minutes=mins, seconds=sec, microseconds=mcs) except ValueError: