Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 017b8753a63d141e1ba5cbed5e7a609a7e1a4bab 1 parent 5bfab1e
Adrian Holovaty authored July 29, 2005

Showing 1 changed file with 11 additions and 8 deletions. Show diff stats Hide diff stats

  1. 19  django/core/db/typecasts.py
19  django/core/db/typecasts.py
@@ -9,16 +9,20 @@ def typecast_date(s):
9 9
 
10 10
 def typecast_time(s): # does NOT store time zone information
11 11
     if not s: return None
12  
-    bits = s.split(':')
13  
-    if len(bits[2].split('.')) > 1: # if there is a decimal (e.g. '11:16:36.181305')
14  
-        return datetime.time(int(bits[0]), int(bits[1]), int(bits[2].split('.')[0]),
15  
-            int(bits[2].split('.')[1].split('-')[0]))
16  
-    else: # no decimal was found (e.g. '12:30:00')
17  
-        return datetime.time(int(bits[0]), int(bits[1]), int(bits[2].split('.')[0]), 0)
  12
+    hour, minutes, seconds = s.split(':')
  13
+    if '.' in seconds: # check whether seconds have a fractional part
  14
+        seconds, microseconds = seconds.split('.')
  15
+    else:
  16
+        microseconds = '0'
  17
+    return datetime.time(int(hour), int(minutes), int(seconds), int(microseconds))
18 18
 
19 19
 def typecast_timestamp(s): # does NOT store time zone information
  20
+    # "2005-07-29 15:48:00.590358-05"
  21
+    # "2005-07-29 09:56:00-05"
20 22
     if not s: return None
21 23
     d, t = s.split()
  24
+    if t[-3] in ('-', '+'):
  25
+        t = t[:-3] # Remove the time-zone information, if it exists.
22 26
     dates = d.split('-')
23 27
     times = t.split(':')
24 28
     seconds = times[2]
@@ -27,8 +31,7 @@ def typecast_timestamp(s): # does NOT store time zone information
27 31
     else:
28 32
         microseconds = '0'
29 33
     return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]),
30  
-        int(times[0]), int(times[1]), int(seconds.split('-')[0]),
31  
-        int(microseconds.split('-')[0]))
  34
+        int(times[0]), int(times[1]), int(seconds), int(microseconds))
32 35
 
33 36
 def typecast_boolean(s):
34 37
     if s is None: return None

0 notes on commit 017b875

Please sign in to comment.
Something went wrong with that request. Please try again.