Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@adrianholovaty adrianholovaty authored
Showing with 11 additions and 8 deletions.
  1. +11 −8 django/core/db/typecasts.py
View
19 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
Please sign in to comment.
Something went wrong with that request. Please try again.