Fixed #1188 -- Changed TimeField.html2python to handle microseconds. Thanks, Cheng

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1872 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-01-09 00:54:35 +00:00
parent 6af37477bd
commit 7d0bf5f2dc
2 changed files with 9 additions and 4 deletions

View File

@ -95,6 +95,7 @@ answer newbie questions, and generally made Django that much better:
Rachel Willmer <http://www.willmer.com/kb/>
wojtek
ye7cakf02@sneakemail.com
Cheng Zhang
A big THANK YOU goes to:

View File

@ -746,7 +746,7 @@ class DateField(TextField):
class TimeField(TextField):
"""A FormField that automatically converts its data to a datetime.time object.
The data should be in the format HH:MM:SS."""
The data should be in the format HH:MM:SS or HH:MM:SS.mmmmmm."""
def __init__(self, field_name, is_required=False, validator_list=[]):
validator_list = [self.isValidTime] + validator_list
TextField.__init__(self, field_name, length=8, maxlength=8,
@ -762,11 +762,15 @@ class TimeField(TextField):
"Converts the field into a datetime.time object"
import time, datetime
try:
part_list = data.split('.')
try:
time_tuple = time.strptime(data, '%H:%M:%S')
time_tuple = time.strptime(part_list[0], '%H:%M:%S')
except ValueError: # seconds weren't provided
time_tuple = time.strptime(data, '%H:%M')
return datetime.time(*time_tuple[3:6])
time_tuple = time.strptime(part_list[0], '%H:%M')
t = datetime.time(*time_tuple[3:6])
if (len(part_list) == 2):
t = t.replace(microsecond=int(part_list[1]))
return t
except (ValueError, TypeError):
return None
html2python = staticmethod(html2python)