Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

fixes https://code.djangoproject.com/ticket/18465 #149

Closed
wants to merge 2 commits into from

2 participants

Josh Smeaton Anssi Kääriäinen
Josh Smeaton

Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime with
respect to the Oracle backend.

added some commits June 12, 2012
Josh Smeaton fixes https://code.djangoproject.com/ticket/18465
Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime with
respect to the Oracle backend.
e729c57
Josh Smeaton regression test added to Oracle Backend for issue #18465 ae73f89
Anssi Kääriäinen
Owner

I have forgotten to close this pull requests. Merged in manually in fa182e8

Anssi Kääriäinen akaariai closed this July 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jun 12, 2012
Josh Smeaton fixes https://code.djangoproject.com/ticket/18465
Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime with
respect to the Oracle backend.
e729c57
Josh Smeaton regression test added to Oracle Backend for issue #18465 ae73f89
This page is out of date. Refresh to see the latest.
6  django/db/backends/oracle/base.py
@@ -482,9 +482,13 @@ def _cursor(self):
482 482
             # Set oracle date to ansi date format.  This only needs to execute
483 483
             # once when we create a new connection. We also set the Territory
484 484
             # to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR().
  485
+            cursor.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
  486
+            # We set the territory first, which overrides NLS_DATE_FORMAT and
  487
+            # NLS_TIMESTAMP_FORMAT to the territory default, and then set
  488
+            # the formats for predictable behaviour.
  489
+            # see http://docs.oracle.com/cd/E11882_01/server.112/e10729/ch3globenv.htm#autoId14
485 490
             cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
486 491
                            " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
487  
-                           " NLS_TERRITORY = 'AMERICA'"
488 492
                            + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
489 493
 
490 494
             if 'operators' not in self.__dict__:
21  tests/regressiontests/backends/tests.py
@@ -65,6 +65,27 @@ def test_client_encoding(self):
65 65
         connection.cursor()  # Ensure the connection is initialized.
66 66
         self.assertEqual(connection.connection.encoding, "UTF-8")
67 67
         self.assertEqual(connection.connection.nencoding, "UTF-8")
  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
+        c.execute(query)
  77
+        try:
  78
+            # should change the NLS_DATE_FORMAT and NLS_TIMESTAMP_FORMAT
  79
+            c.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
  80
+            with self.assertRaises(DatabaseError):
  81
+                c.execute(query)
  82
+        finally:
  83
+            try:
  84
+                c.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
  85
+                          " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
  86
+                          + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
  87
+            except:
  88
+                pass
68 89
 
69 90
 class MySQLTests(TestCase):
70 91
     @unittest.skipUnless(connection.vendor == 'mysql',
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.