Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented QuerySet.datetimes on PostgreSQL.

  • Loading branch information...
commit f6800fd04c93722b45f9236976389e0b2fe436f5 1 parent 0c829c2
Aymeric Augustin authored February 10, 2013
17  django/db/backends/postgresql_psycopg2/operations.py
... ...
@@ -1,5 +1,6 @@
1 1
 from __future__ import unicode_literals
2 2
 
  3
+from django.conf import settings
3 4
 from django.db.backends import BaseDatabaseOperations
4 5
 
5 6
 
@@ -36,6 +37,22 @@ def date_trunc_sql(self, lookup_type, field_name):
36 37
         # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
37 38
         return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
38 39
 
  40
+    def datetime_extract_sql(self, lookup_type, field_name):
  41
+        if settings.USE_TZ:
  42
+            field_name = "%s AT TIME ZONE %%s" % field_name
  43
+        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
  44
+        if lookup_type == 'week_day':
  45
+            # For consistency across backends, we return Sunday=1, Saturday=7.
  46
+            return "EXTRACT('dow' FROM %s) + 1" % field_name
  47
+        else:
  48
+            return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
  49
+
  50
+    def datetime_trunc_sql(self, lookup_type, field_name):
  51
+        if settings.USE_TZ:
  52
+            field_name = "%s AT TIME ZONE %%s" % field_name
  53
+        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
  54
+        return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
  55
+
39 56
     def deferrable_sql(self):
40 57
         return " DEFERRABLE INITIALLY DEFERRED"
41 58
 

0 notes on commit f6800fd

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