From 77b73e79a4750dcbfabc528bf00cad81ff5bb4d9 Mon Sep 17 00:00:00 2001 From: Josh Smeaton Date: Sun, 15 May 2016 21:53:45 +1000 Subject: [PATCH] Refs #25774 -- Made Oracle truncate microseconds if USE_TZ=False. The tests for this change are in the fix for #25774. --- django/db/backends/oracle/operations.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/django/db/backends/oracle/operations.py b/django/db/backends/oracle/operations.py index 8670e2f4d1..aa00f93f92 100644 --- a/django/db/backends/oracle/operations.py +++ b/django/db/backends/oracle/operations.py @@ -115,20 +115,19 @@ WHEN (new.%(col_name)s IS NULL) _tzname_re = re.compile(r'^[\w/:+-]+$') def _convert_field_to_tz(self, field_name, tzname): - if not settings.USE_TZ: - return field_name - if not self._tzname_re.match(tzname): - raise ValueError("Invalid time zone name: %s" % tzname) - # Convert from UTC to local time, returning TIMESTAMP WITH TIME ZONE. - result = "(FROM_TZ(%s, '0:00') AT TIME ZONE '%s')" % (field_name, tzname) + if settings.USE_TZ: + if not self._tzname_re.match(tzname): + raise ValueError("Invalid time zone name: %s" % tzname) + # Convert from UTC to local time, returning TIMESTAMP WITH TIME ZONE. + field_name = "(FROM_TZ(%s, '0:00') AT TIME ZONE '%s')" % (field_name, tzname) # Extracting from a TIMESTAMP WITH TIME ZONE ignore the time zone. # Convert to a DATETIME, which is called DATE by Oracle. There's no # built-in function to do that; the easiest is to go through a string. - result = "TO_CHAR(%s, 'YYYY-MM-DD HH24:MI:SS')" % result - result = "TO_DATE(%s, 'YYYY-MM-DD HH24:MI:SS')" % result + field_name = "TO_CHAR(%s, 'YYYY-MM-DD HH24:MI:SS')" % field_name + field_name = "TO_DATE(%s, 'YYYY-MM-DD HH24:MI:SS')" % field_name # Re-convert to a TIMESTAMP because EXTRACT only handles the date part # on DATE values, even though they actually store the time part. - return "CAST(%s AS TIMESTAMP)" % result + return "CAST(%s AS TIMESTAMP)" % field_name def datetime_cast_date_sql(self, field_name, tzname): field_name = self._convert_field_to_tz(field_name, tzname)