From f6800fd04c93722b45f9236976389e0b2fe436f5 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 10 Feb 2013 21:43:03 +0100 Subject: [PATCH] Implemented QuerySet.datetimes on PostgreSQL. --- .../backends/postgresql_psycopg2/operations.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py index 40fe629110..64efba0dbe 100644 --- a/django/db/backends/postgresql_psycopg2/operations.py +++ b/django/db/backends/postgresql_psycopg2/operations.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +from django.conf import settings from django.db.backends import BaseDatabaseOperations @@ -36,6 +37,22 @@ class DatabaseOperations(BaseDatabaseOperations): # 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"