Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18465 -- Set date formats correctly on Oracle

Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime
on Oracle backend.

Thanks to Josh Smeaton for report & patch.
  • Loading branch information...
commit fa182e8ae82f33764d5e1f70bcd45899e1bf17e6 1 parent 05d333b
Josh Smeaton authored June 12, 2012 akaariai committed June 26, 2012
16  django/db/backends/oracle/base.py
@@ -479,13 +479,19 @@ def _cursor(self):
479 479
                 del conn_params['use_returning_into']
480 480
             self.connection = Database.connect(conn_string, **conn_params)
481 481
             cursor = FormatStylePlaceholderCursor(self.connection)
  482
+            # Set the territory first. The territory overrides NLS_DATE_FORMAT
  483
+            # and NLS_TIMESTAMP_FORMAT to the territory default. When all of
  484
+            # these are set in single statement it isn't clear what is supposed
  485
+            # to happen.
  486
+            cursor.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
482 487
             # Set oracle date to ansi date format.  This only needs to execute
483 488
             # once when we create a new connection. We also set the Territory
484  
-            # to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR().
485  
-            cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
486  
-                           " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
487  
-                           " NLS_TERRITORY = 'AMERICA'"
488  
-                           + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
  489
+            # to 'AMERICA' which forces Sunday to evaluate to a '1' in
  490
+            # TO_CHAR().
  491
+            cursor.execute(
  492
+                "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
  493
+                " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
  494
+                + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
489 495
 
490 496
             if 'operators' not in self.__dict__:
491 497
                 # Ticket #14149: Check whether our LIKE implementation will
12  tests/regressiontests/backends/tests.py
@@ -66,6 +66,18 @@ def test_client_encoding(self):
66 66
         self.assertEqual(connection.connection.encoding, "UTF-8")
67 67
         self.assertEqual(connection.connection.nencoding, "UTF-8")
68 68
 
  69
+    @unittest.skipUnless(connection.vendor == 'oracle',
  70
+                         "No need to check Oracle connection semantics")
  71
+    def test_order_of_nls_parameters(self):
  72
+        # an 'almost right' datetime should work with configured
  73
+        # NLS parameters as per #18465.
  74
+        c = connection.cursor()
  75
+        query = "select 1 from dual where '1936-12-29 00:00' < sysdate"
  76
+        # Test that the query succeeds without errors - pre #18465 this
  77
+        # wasn't the case.
  78
+        c.execute(query)
  79
+        self.assertEqual(c.fetchone()[0], 1)
  80
+
69 81
 class MySQLTests(TestCase):
70 82
     @unittest.skipUnless(connection.vendor == 'mysql',
71 83
                         "Test valid only for MySQL")

0 notes on commit fa182e8

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