Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

magic-removal: Fixed #1423 -- Improved implementation of get_date_tru…

…nc_sql() for MySQL backend. Thanks, Geert Vanderkelen

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2735 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 08c28d989736b0659185e0f793ffc659ffa222e4 1 parent dee1af5
@adrianholovaty adrianholovaty authored
Showing with 12 additions and 12 deletions.
  1. +1 −0  AUTHORS
  2. +11 −12 django/db/backends/mysql/base.py
View
1  AUTHORS
@@ -106,6 +106,7 @@ answer newbie questions, and generally made Django that much better:
Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
Malcolm Tredinnick
Amit Upadhyay
+ Geert Vanderkelen
Milton Waddams
Rachel Willmer <http://www.willmer.com/kb/>
wojtek
View
23 django/db/backends/mysql/base.py
@@ -124,18 +124,17 @@ def get_date_extract_sql(lookup_type, table_name):
def get_date_trunc_sql(lookup_type, field_name):
# lookup_type is 'year', 'month', 'day'
- # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
- # MySQL doesn't support DATE_TRUNC, so we fake it by subtracting intervals.
- # If you know of a better way to do this, please file a Django ticket.
- # Note that we can't use DATE_FORMAT directly because that causes the output
- # to be a string rather than a datetime object, and we need MySQL to return
- # a date so that it's typecasted properly into a Python datetime object.
- subtractions = ["interval (DATE_FORMAT(%s, '%%%%s')) second - interval (DATE_FORMAT(%s, '%%%%i')) minute - interval (DATE_FORMAT(%s, '%%%%H')) hour" % (field_name, field_name, field_name)]
- if lookup_type in ('year', 'month'):
- subtractions.append(" - interval (DATE_FORMAT(%s, '%%%%e')-1) day" % field_name)
- if lookup_type == 'year':
- subtractions.append(" - interval (DATE_FORMAT(%s, '%%%%m')-1) month" % field_name)
- return "(%s - %s)" % (field_name, ''.join(subtractions))
+ fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
+ format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape.
+ format_def = ('0000-', '01', '-01', ' 00:', '00', ':00')
+ try:
+ i = fields.index(lookup_type) + 1
+ except ValueError:
+ sql = field_name
+ else:
+ format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]])
+ sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str)
+ return sql
def get_limit_offset_sql(limit, offset=None):
sql = "LIMIT "
Please sign in to comment.
Something went wrong with that request. Please try again.