From 7d0bf5f2dcf34c7a28925e28fce67f44183dd77d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 9 Jan 2006 00:54:35 +0000 Subject: [PATCH] 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 --- AUTHORS | 1 + django/core/formfields.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index 10b41c2e7c..5a9012dffd 100644 --- a/AUTHORS +++ b/AUTHORS @@ -95,6 +95,7 @@ answer newbie questions, and generally made Django that much better: Rachel Willmer wojtek ye7cakf02@sneakemail.com + Cheng Zhang A big THANK YOU goes to: diff --git a/django/core/formfields.py b/django/core/formfields.py index c761d340a7..530c8b554b 100644 --- a/django/core/formfields.py +++ b/django/core/formfields.py @@ -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)