Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion django/db/backends/oracle/base.py
Expand Up @@ -482,9 +482,13 @@ def _cursor(self):
# Set oracle date to ansi date format. This only needs to execute
# once when we create a new connection. We also set the Territory
# to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR().
cursor.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
# We set the territory first, which overrides NLS_DATE_FORMAT and
# NLS_TIMESTAMP_FORMAT to the territory default, and then set
# the formats for predictable behaviour.
# see http://docs.oracle.com/cd/E11882_01/server.112/e10729/ch3globenv.htm#autoId14
cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
" NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
" NLS_TERRITORY = 'AMERICA'"
+ (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))

if 'operators' not in self.__dict__:
Expand Down
21 changes: 21 additions & 0 deletions tests/regressiontests/backends/tests.py
Expand Up @@ -65,6 +65,27 @@ def test_client_encoding(self):
connection.cursor() # Ensure the connection is initialized.
self.assertEqual(connection.connection.encoding, "UTF-8")
self.assertEqual(connection.connection.nencoding, "UTF-8")

@unittest.skipUnless(connection.vendor == 'oracle',
"No need to check Oracle connection semantics")
def test_order_of_nls_parameters(self):
# an 'almost right' datetime should work with configured
# NLS parameters as per #18465.
c = connection.cursor()
query = "select 1 from dual where '1936-12-29 00:00' < sysdate"
c.execute(query)
try:
# should change the NLS_DATE_FORMAT and NLS_TIMESTAMP_FORMAT
c.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
with self.assertRaises(DatabaseError):
c.execute(query)
finally:
try:
c.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
" NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
+ (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
except:
pass

class MySQLTests(TestCase):
@unittest.skipUnless(connection.vendor == 'mysql',
Expand Down