From 017b8753a63d141e1ba5cbed5e7a609a7e1a4bab Mon Sep 17 00:00:00 2001
From: Adrian Holovaty <adrian@holovaty.com>
Date: Fri, 29 Jul 2005 21:10:09 +0000
Subject: [PATCH] Fixed #219 and #188 -- Database timestamp typecast no longer
 assumes '-' delineates the time zone

git-svn-id: http://code.djangoproject.com/svn/django/trunk@346 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 django/core/db/typecasts.py | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/django/core/db/typecasts.py b/django/core/db/typecasts.py
index 1ede949aad..19d11d8444 100644
--- a/django/core/db/typecasts.py
+++ b/django/core/db/typecasts.py
@@ -9,16 +9,20 @@ def typecast_date(s):
 
 def typecast_time(s): # does NOT store time zone information
     if not s: return None
-    bits = s.split(':')
-    if len(bits[2].split('.')) > 1: # if there is a decimal (e.g. '11:16:36.181305')
-        return datetime.time(int(bits[0]), int(bits[1]), int(bits[2].split('.')[0]),
-            int(bits[2].split('.')[1].split('-')[0]))
-    else: # no decimal was found (e.g. '12:30:00')
-        return datetime.time(int(bits[0]), int(bits[1]), int(bits[2].split('.')[0]), 0)
+    hour, minutes, seconds = s.split(':')
+    if '.' in seconds: # check whether seconds have a fractional part
+        seconds, microseconds = seconds.split('.')
+    else:
+        microseconds = '0'
+    return datetime.time(int(hour), int(minutes), int(seconds), int(microseconds))
 
 def typecast_timestamp(s): # does NOT store time zone information
+    # "2005-07-29 15:48:00.590358-05"
+    # "2005-07-29 09:56:00-05"
     if not s: return None
     d, t = s.split()
+    if t[-3] in ('-', '+'):
+        t = t[:-3] # Remove the time-zone information, if it exists.
     dates = d.split('-')
     times = t.split(':')
     seconds = times[2]
@@ -27,8 +31,7 @@ def typecast_timestamp(s): # does NOT store time zone information
     else:
         microseconds = '0'
     return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]),
-        int(times[0]), int(times[1]), int(seconds.split('-')[0]),
-        int(microseconds.split('-')[0]))
+        int(times[0]), int(times[1]), int(seconds), int(microseconds))
 
 def typecast_boolean(s):
     if s is None: return None