diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index a2a1757..c80dddc 100644
a
|
b
|
class BaseDatabaseOperations(object):
|
1166 | 1166 | """ |
1167 | 1167 | first = datetime.datetime(value, 1, 1) |
1168 | 1168 | second = datetime.datetime(value, 12, 31, 23, 59, 59, 999999) |
1169 | | if settings.USE_TZ: |
| 1169 | if self.connection.settings_dict['USE_TZ']: |
1170 | 1170 | tz = timezone.get_current_timezone() |
1171 | 1171 | first = timezone.make_aware(first, tz) |
1172 | 1172 | second = timezone.make_aware(second, tz) |
diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
index b38d00c..1dc0b66 100644
a
|
b
|
parse_datetime = conversions[FIELD_TYPE.DATETIME]
|
62 | 62 | def parse_datetime_with_timezone_support(value): |
63 | 63 | dt = parse_datetime(value) |
64 | 64 | # Confirm that dt is naive before overwriting its tzinfo. |
| 65 | # XXX must inject use_tz instead of using the global setting |
65 | 66 | if dt is not None and settings.USE_TZ and timezone.is_naive(dt): |
66 | 67 | dt = dt.replace(tzinfo=timezone.utc) |
67 | 68 | return dt |
… |
… |
def parse_datetime_with_timezone_support(value):
|
69 | 70 | |
70 | 71 | def adapt_datetime_with_timezone_support(value, conv): |
71 | 72 | # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL. |
| 73 | # XXX must inject use_tz instead of using the global setting |
72 | 74 | if settings.USE_TZ: |
73 | 75 | if timezone.is_naive(value): |
74 | 76 | warnings.warn("MySQL received a naive datetime (%s)" |
… |
… |
class DatabaseOperations(BaseDatabaseOperations):
|
238 | 240 | return sql |
239 | 241 | |
240 | 242 | def datetime_extract_sql(self, lookup_type, field_name, tzname): |
241 | | if settings.USE_TZ: |
| 243 | if self.connection.settings_dict['USE_TZ']: |
242 | 244 | field_name = "CONVERT_TZ(%s, 'UTC', %%s)" % field_name |
243 | 245 | params = [tzname] |
244 | 246 | else: |
… |
… |
class DatabaseOperations(BaseDatabaseOperations):
|
253 | 255 | return sql, params |
254 | 256 | |
255 | 257 | def datetime_trunc_sql(self, lookup_type, field_name, tzname): |
256 | | if settings.USE_TZ: |
| 258 | if self.connection.settings_dict['USE_TZ']: |
257 | 259 | field_name = "CONVERT_TZ(%s, 'UTC', %%s)" % field_name |
258 | 260 | params = [tzname] |
259 | 261 | else: |
… |
… |
class DatabaseOperations(BaseDatabaseOperations):
|
355 | 357 | |
356 | 358 | # MySQL doesn't support tz-aware datetimes |
357 | 359 | if timezone.is_aware(value): |
358 | | if settings.USE_TZ: |
| 360 | if self.connection.settings_dict['USE_TZ']: |
359 | 361 | value = value.astimezone(timezone.utc).replace(tzinfo=None) |
360 | 362 | else: |
361 | 363 | raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.") |
diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py
index 6a6a877..bc40175 100644
a
|
b
|
WHEN (new.%(col_name)s IS NULL)
|
197 | 197 | return "CAST(%s AS TIMESTAMP)" % result |
198 | 198 | |
199 | 199 | def datetime_extract_sql(self, lookup_type, field_name, tzname): |
200 | | if settings.USE_TZ: |
| 200 | if self.connection.settings_dict['USE_TZ']: |
201 | 201 | field_name = self._convert_field_to_tz(field_name, tzname) |
202 | 202 | if lookup_type == 'week_day': |
203 | 203 | # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday. |
… |
… |
WHEN (new.%(col_name)s IS NULL)
|
208 | 208 | return sql, [] |
209 | 209 | |
210 | 210 | def datetime_trunc_sql(self, lookup_type, field_name, tzname): |
211 | | if settings.USE_TZ: |
| 211 | if self.connection.settings_dict['USE_TZ']: |
212 | 212 | field_name = self._convert_field_to_tz(field_name, tzname) |
213 | 213 | # http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084 |
214 | 214 | if lookup_type in ('year', 'month'): |
… |
… |
WHEN (new.%(col_name)s IS NULL)
|
437 | 437 | |
438 | 438 | # Oracle doesn't support tz-aware datetimes |
439 | 439 | if timezone.is_aware(value): |
440 | | if settings.USE_TZ: |
| 440 | if self.connection.settings_dict['USE_TZ']: |
441 | 441 | value = value.astimezone(timezone.utc).replace(tzinfo=None) |
442 | 442 | else: |
443 | 443 | raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.") |
… |
… |
WHEN (new.%(col_name)s IS NULL)
|
470 | 470 | # off of datetime objects, leaving almost an entire second out of |
471 | 471 | # the year under the default implementation. |
472 | 472 | bounds = super(DatabaseOperations, self).year_lookup_bounds_for_datetime_field(value) |
473 | | if settings.USE_TZ: |
| 473 | if self.connection.settings_dict['USE_TZ']: |
474 | 474 | bounds = [b.astimezone(timezone.utc).replace(tzinfo=None) for b in bounds] |
475 | 475 | return [b.isoformat(b' ') for b in bounds] |
476 | 476 | |
… |
… |
class DatabaseWrapper(BaseDatabaseWrapper):
|
589 | 589 | # once when we create a new connection. We also set the Territory |
590 | 590 | # to 'AMERICA' which forces Sunday to evaluate to a '1' in |
591 | 591 | # TO_CHAR(). |
| 592 | tz = " TIME_ZONE = 'UTC'" if self.settings_dict['USE_TZ'] else "" |
592 | 593 | cursor.execute( |
593 | 594 | "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'" |
594 | | " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'" |
595 | | + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else '')) |
| 595 | " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'" + tz) |
596 | 596 | cursor.close() |
597 | 597 | if 'operators' not in self.__dict__: |
598 | 598 | # Ticket #14149: Check whether our LIKE implementation will |
… |
… |
class OracleParam(object):
|
702 | 702 | def __init__(self, param, cursor, strings_only=False): |
703 | 703 | # With raw SQL queries, datetimes can reach this function |
704 | 704 | # without being converted by DateTimeField.get_db_prep_value. |
| 705 | # XXX must inject use_tz instead of using the global setting |
705 | 706 | if settings.USE_TZ and isinstance(param, datetime.datetime): |
706 | 707 | if timezone.is_naive(param): |
707 | 708 | warnings.warn("Oracle received a naive datetime (%s)" |
… |
… |
def _rowfactory(row, cursor):
|
956 | 957 | # of "dates" queries, which are returned as DATETIME. |
957 | 958 | elif desc[1] in (Database.TIMESTAMP, Database.DATETIME): |
958 | 959 | # Confirm that dt is naive before overwriting its tzinfo. |
| 960 | # XXX must inject use_tz instead of using the global setting |
959 | 961 | if settings.USE_TZ and value is not None and timezone.is_naive(value): |
960 | 962 | value = value.replace(tzinfo=timezone.utc) |
961 | 963 | elif desc[1] in (Database.STRING, Database.FIXED_CHAR, |
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
index cb23c33..b142b89 100644
a
|
b
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
131 | 131 | def init_connection_state(self): |
132 | 132 | settings_dict = self.settings_dict |
133 | 133 | self.connection.set_client_encoding('UTF8') |
134 | | tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE') |
| 134 | tz = 'UTC' if settings_dict['USE_TZ'] else settings_dict['TIME_ZONE'] |
135 | 135 | if tz: |
136 | 136 | try: |
137 | 137 | get_parameter_status = self.connection.get_parameter_status |
… |
… |
class DatabaseWrapper(BaseDatabaseWrapper):
|
151 | 151 | |
152 | 152 | def create_cursor(self): |
153 | 153 | cursor = self.connection.cursor() |
154 | | cursor.tzinfo_factory = utc_tzinfo_factory if settings.USE_TZ else None |
| 154 | cursor.tzinfo_factory = utc_tzinfo_factory if self.settings_dict['USE_TZ'] else None |
155 | 155 | return cursor |
156 | 156 | |
157 | 157 | def close(self): |
diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py
index b949686..ae08177 100644
a
|
b
|
class DatabaseOperations(BaseDatabaseOperations):
|
38 | 38 | return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) |
39 | 39 | |
40 | 40 | def datetime_extract_sql(self, lookup_type, field_name, tzname): |
41 | | if settings.USE_TZ: |
| 41 | if self.connection.settings_dict['USE_TZ']: |
42 | 42 | field_name = "%s AT TIME ZONE %%s" % field_name |
43 | 43 | params = [tzname] |
44 | 44 | else: |
… |
… |
class DatabaseOperations(BaseDatabaseOperations):
|
52 | 52 | return sql, params |
53 | 53 | |
54 | 54 | def datetime_trunc_sql(self, lookup_type, field_name, tzname): |
55 | | if settings.USE_TZ: |
| 55 | if self.connection.settings_dict['USE_TZ']: |
56 | 56 | field_name = "%s AT TIME ZONE %%s" % field_name |
57 | 57 | params = [tzname] |
58 | 58 | else: |
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index 6c97288..78cdefe 100644
a
|
b
|
IntegrityError = Database.IntegrityError
|
49 | 49 | def parse_datetime_with_timezone_support(value): |
50 | 50 | dt = parse_datetime(value) |
51 | 51 | # Confirm that dt is naive before overwriting its tzinfo. |
| 52 | # XXX must inject use_tz instead of using the global setting |
52 | 53 | if dt is not None and settings.USE_TZ and timezone.is_naive(dt): |
53 | 54 | dt = dt.replace(tzinfo=timezone.utc) |
54 | 55 | return dt |
… |
… |
def parse_datetime_with_timezone_support(value):
|
56 | 57 | |
57 | 58 | def adapt_datetime_with_timezone_support(value): |
58 | 59 | # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL. |
| 60 | # XXX must inject use_tz instead of using the global setting |
59 | 61 | if settings.USE_TZ: |
60 | 62 | if timezone.is_naive(value): |
61 | 63 | warnings.warn("SQLite received a naive datetime (%s)" |
… |
… |
class DatabaseOperations(BaseDatabaseOperations):
|
186 | 188 | |
187 | 189 | def datetime_extract_sql(self, lookup_type, field_name, tzname): |
188 | 190 | # Same comment as in date_extract_sql. |
189 | | if settings.USE_TZ: |
| 191 | if self.connection.settings_dict['USE_TZ']: |
190 | 192 | if pytz is None: |
191 | 193 | from django.core.exceptions import ImproperlyConfigured |
192 | 194 | raise ImproperlyConfigured("This query requires pytz, " |
… |
… |
class DatabaseOperations(BaseDatabaseOperations):
|
196 | 198 | |
197 | 199 | def datetime_trunc_sql(self, lookup_type, field_name, tzname): |
198 | 200 | # Same comment as in date_trunc_sql. |
199 | | if settings.USE_TZ: |
| 201 | if self.connection.settings_dict['USE_TZ']: |
200 | 202 | if pytz is None: |
201 | 203 | from django.core.exceptions import ImproperlyConfigured |
202 | 204 | raise ImproperlyConfigured("This query requires pytz, " |
… |
… |
class DatabaseOperations(BaseDatabaseOperations):
|
256 | 258 | |
257 | 259 | # SQLite doesn't support tz-aware datetimes |
258 | 260 | if timezone.is_aware(value): |
259 | | if settings.USE_TZ: |
| 261 | if self.connection.settings_dict['USE_TZ']: |
260 | 262 | value = value.astimezone(timezone.utc).replace(tzinfo=None) |
261 | 263 | else: |
262 | 264 | raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.") |
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
index 8bed641..b4b2f28 100644
a
|
b
|
def typecast_timestamp(s): # does NOT store time zone information
|
151 | 151 | seconds, microseconds = seconds.split('.') |
152 | 152 | else: |
153 | 153 | microseconds = '0' |
| 154 | # XXX must inject use_tz instead of using the global setting |
154 | 155 | tzinfo = utc if settings.USE_TZ else None |
155 | 156 | return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]), |
156 | 157 | int(times[0]), int(times[1]), int(seconds), |
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 58180e1..f2759b2 100644
a
|
b
|
class QuerySet(object):
|
650 | 650 | "'kind' must be one of 'year', 'month', 'day', 'hour', 'minute' or 'second'." |
651 | 651 | assert order in ('ASC', 'DESC'), \ |
652 | 652 | "'order' must be either 'ASC' or 'DESC'." |
| 653 | # XXX must inject use_tz instead of using the global setting |
653 | 654 | if settings.USE_TZ: |
654 | 655 | if tzinfo is None: |
655 | 656 | tzinfo = timezone.get_current_timezone() |
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 98b9539..062eea3 100644
a
|
b
|
class SQLDateTimeCompiler(SQLCompiler):
|
1080 | 1080 | datetime = typecast_timestamp(str(datetime)) |
1081 | 1081 | # Datetimes are artifically returned in UTC on databases that |
1082 | 1082 | # don't support time zone. Restore the zone used in the query. |
| 1083 | # XXX must inject use_tz instead of using the global setting |
1083 | 1084 | if settings.USE_TZ: |
1084 | 1085 | if datetime is None: |
1085 | 1086 | raise ValueError("Database returned an invalid value " |
diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py
index e9e292e..13a0ccc 100644
a
|
b
|
class DateQuery(Query):
|
243 | 243 | def _check_field(self, field): |
244 | 244 | assert isinstance(field, DateField), \ |
245 | 245 | "%r isn't a DateField." % field.name |
| 246 | # XXX must inject use_tz instead of using the global setting |
246 | 247 | if settings.USE_TZ: |
247 | 248 | assert not isinstance(field, DateTimeField), \ |
248 | 249 | "%r is a DateTimeField, not a DateField." % field.name |
diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
index 44a4ce9..0d86acb 100644
a
|
b
|
class WhereNode(tree.Node):
|
244 | 244 | return ('%s BETWEEN %%s and %%s' % field_sql, params) |
245 | 245 | elif is_datetime_field and lookup_type in ('month', 'day', 'week_day', |
246 | 246 | 'hour', 'minute', 'second'): |
| 247 | # XXX must inject use_tz instead of using the global setting |
247 | 248 | tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None |
248 | 249 | sql, tz_params = connection.ops.datetime_extract_sql(lookup_type, field_sql, tzname) |
249 | 250 | return ('%s = %%s' % sql, tz_params + params) |
diff --git a/django/db/utils.py b/django/db/utils.py
index 43abaf9..91f08d4 100644
a
|
b
|
class ConnectionHandler(object):
|
177 | 177 | conn['ENGINE'] = 'django.db.backends.dummy' |
178 | 178 | conn.setdefault('CONN_MAX_AGE', 0) |
179 | 179 | conn.setdefault('OPTIONS', {}) |
180 | | conn.setdefault('TIME_ZONE', 'UTC' if settings.USE_TZ else settings.TIME_ZONE) |
| 180 | conn.setdefault('USE_TZ', settings.USE_TZ) |
| 181 | conn.setdefault('TIME_ZONE', settings.TIME_ZONE) |
181 | 182 | for setting in ['NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']: |
182 | 183 | conn.setdefault(setting, '') |
183 | 184 | for setting in ['TEST_CHARSET', 'TEST_COLLATION', 'TEST_NAME', 'TEST_MIRROR']: |
diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
index d9cb07e..e55e165 100644
a
|
b
|
Default: ``''`` (Empty string)
|
570 | 570 | The port to use when connecting to the database. An empty string means the |
571 | 571 | default port. Not used with SQLite. |
572 | 572 | |
| 573 | .. setting:: DATABASES-TIMEZONE |
| 574 | |
| 575 | TIME_ZONE |
| 576 | ~~~~~~~~~ |
| 577 | |
| 578 | Default: global :setting:`TIME_ZONE` setting |
| 579 | |
| 580 | Time zone for this database connection. This is for advanced use only. |
| 581 | |
| 582 | This parameter isn't used when :setting:`DATABASES-USE_TZ` is ``False`` and |
| 583 | has no effect on databases that do not support setting the connection's time |
| 584 | zone (SQLite and MySQL). |
| 585 | |
573 | 586 | .. setting:: USER |
574 | 587 | |
575 | 588 | USER |
… |
… |
Default: ``''`` (Empty string)
|
579 | 592 | |
580 | 593 | The username to use when connecting to the database. Not used with SQLite. |
581 | 594 | |
| 595 | .. setting:: DATABASES-USE_TZ |
| 596 | |
| 597 | USE_TZ |
| 598 | ~~~~~~ |
| 599 | |
| 600 | Default: global :setting:`USE_TZ` setting |
| 601 | |
| 602 | Controls if this database connection returns aware datetimes. This is for |
| 603 | advanced use only. |
| 604 | |
| 605 | If you override this value, you must ensure that your application code |
| 606 | performs appropriate conversions between naive and aware datetimes when |
| 607 | getting them from or passing them to the ORM. |
| 608 | |
582 | 609 | .. setting:: TEST_CHARSET |
583 | 610 | |
584 | 611 | TEST_CHARSET |