Skip to content

Commit a7b09e7

Browse files
committed
Obey the connection's TIME_ZONE setting when USE_TZ enabled
Given that SQL server has no built-in support for timezones, we have set DatabaseFeatures.supports_timezones = False. However, we incorrectly assumed that every datetime stored in the database is in UTC, and have hardcoded timezone.utc. Django allows developers to set the TIME_ZONE option in a database's connection settings to specify the timezone in which the datetimes are stored, and this library should obey that setting. See: https://docs.djangoproject.com/en/1.11/ref/settings/#time-zone To obey the TIME_ZONE setting, we replace timezone.utc with self.connection.timezone, which is a cached_property we inherit from BaseDatabaseWrapper. The calculation of the property includes various checks, and will default to timezone.utc if no TIME_ZONE setting is provided for a database connection - meaning this change will be backward compatible. See: https://github.com/django/django/blob/1.11.4/django/db/backends/base/base.py#L121-L128
1 parent 4df37f3 commit a7b09e7

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

sql_server/pyodbc/operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def convert_datetimefield_value(self, value, expression, connection, context):
134134
self._warn_legacy_driver('datetime2')
135135
value = datetime.datetime.strptime(value[:26], '%Y-%m-%d %H:%M:%S.%f')
136136
if settings.USE_TZ:
137-
value = timezone.make_aware(value, timezone.utc)
137+
value = timezone.make_aware(value, self.connection.timezone)
138138
return value
139139

140140
def convert_floatfield_value(self, value, expression, connection, context):
@@ -480,7 +480,7 @@ def adapt_datetimefield_value(self, value):
480480
return None
481481
if settings.USE_TZ and timezone.is_aware(value):
482482
# pyodbc donesn't support datetimeoffset
483-
value = value.astimezone(timezone.utc).replace(tzinfo=None)
483+
value = value.astimezone(self.connection.timezone).replace(tzinfo=None)
484484
if not self.connection.features.supports_microsecond_precision:
485485
value = value.replace(microsecond=0)
486486
return value

0 commit comments

Comments
 (0)