Django

Code

Ticket #1188: formfields.TimeField.patch

File formfields.TimeField.patch, 1.4 kB (added by Cheng <czhang.cmu+web@gmail.com>, 3 years ago)
  • core/formfields.py

    old new  
    746746 
    747747class TimeField(TextField): 
    748748    """A FormField that automatically converts its data to a datetime.time object. 
    749     The data should be in the format HH:MM:SS.""" 
     749    The data should be in the format HH:MM:SS or HH:MM:SS.mmmmmm.""" 
    750750    def __init__(self, field_name, is_required=False, validator_list=[]): 
    751751        validator_list = [self.isValidTime] + validator_list 
    752752        TextField.__init__(self, field_name, length=8, maxlength=8, 
     
    762762        "Converts the field into a datetime.time object" 
    763763        import time, datetime 
    764764        try: 
     765            part_list = data.split('.') 
    765766            try: 
    766                 time_tuple = time.strptime(data, '%H:%M:%S') 
     767                time_tuple = time.strptime(part_list[0], '%H:%M:%S') 
    767768            except ValueError: # seconds weren't provided 
    768                 time_tuple = time.strptime(data, '%H:%M') 
    769             return datetime.time(*time_tuple[3:6]) 
     769                time_tuple = time.strptime(part_list[0], '%H:%M') 
     770            t = datetime.time(*time_tuple[3:6]) 
     771            if (len(part_list) == 2): 
     772                t = t.replace(microsecond=int(part_list[1])) 
     773            return t 
    770774        except (ValueError, TypeError): 
    771775            return None 
    772776    html2python = staticmethod(html2python)