Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented QuerySet.datetimes on PostgreSQL.

  • Loading branch information...
commit f6800fd04c93722b45f9236976389e0b2fe436f5 1 parent 0c829c2
Aymeric Augustin aaugustin authored
Showing with 17 additions and 0 deletions.
  1. +17 −0 django/db/backends/postgresql_psycopg2/operations.py
17 django/db/backends/postgresql_psycopg2/operations.py
View
@@ -1,5 +1,6 @@
from __future__ import unicode_literals
+from django.conf import settings
from django.db.backends import BaseDatabaseOperations
@@ -36,6 +37,22 @@ def date_trunc_sql(self, lookup_type, field_name):
# http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
+ def datetime_extract_sql(self, lookup_type, field_name):
+ if settings.USE_TZ:
+ field_name = "%s AT TIME ZONE %%s" % field_name
+ # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
+ if lookup_type == 'week_day':
+ # For consistency across backends, we return Sunday=1, Saturday=7.
+ return "EXTRACT('dow' FROM %s) + 1" % field_name
+ else:
+ return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
+
+ def datetime_trunc_sql(self, lookup_type, field_name):
+ if settings.USE_TZ:
+ field_name = "%s AT TIME ZONE %%s" % field_name
+ # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
+ return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
+
def deferrable_sql(self):
return " DEFERRABLE INITIALLY DEFERRED"
Please sign in to comment.
Something went wrong with that request. Please try again.