Fix for bug #2479317 (patch #2496558) by kylev. Make some time handling

stuff more consistent.
This commit is contained in:
adustman
2009-03-11 00:48:08 +00:00
parent ae8a1a8a75
commit d57be3fb50

View File

@ -4,6 +4,7 @@ This module provides some Date and Time classes for dealing with MySQL data.
Use Python datetime module to handle date and time columns.""" Use Python datetime module to handle date and time columns."""
import math
from time import localtime from time import localtime
from datetime import date, datetime, time, timedelta from datetime import date, datetime, time, timedelta
from _mysql import string_literal from _mysql import string_literal
@ -55,25 +56,26 @@ def DateTime_or_None(s):
return Date_or_None(s) return Date_or_None(s)
def TimeDelta_or_None(s): def TimeDelta_or_None(s):
from math import modf
try: try:
h, m, s = s.split(':') h, m, s = s.split(':')
td = timedelta(hours=int(h), minutes=int(m), seconds=int(float(s)), h, m, s = int(h), int(m), float(s)
microseconds=int(modf(float(s))[0]*1000000)) td = timedelta(hours=abs(h), minutes=m, seconds=int(s),
microseconds=int(math.modf(s)[0] * 1000000))
if h < 0: if h < 0:
return -td return -td
else: else:
return td return td
except: except ValueError:
# unpacking or int/float conversion failed
return None return None
def Time_or_None(s): def Time_or_None(s):
from math import modf
try: try:
h, m, s = s.split(':') h, m, s = s.split(':')
return time(hour=int(h), minute=int(m), second=int(float(s)), h, m, s = int(h), int(m), float(s)
microsecond=int(modf(float(s))[0]*1000000)) return time(hour=h, minute=m, second=int(s),
except: microsecond=int(math.modf(s)[0] * 1000000))
except ValueError:
return None return None
def Date_or_None(s): def Date_or_None(s):