diff --git a/mssql/base.py b/mssql/base.py index c69046ac..9c4d5299 100644 --- a/mssql/base.py +++ b/mssql/base.py @@ -88,7 +88,8 @@ def handle_datetimeoffset(dto_value): # Decode bytes returned from SQL Server # source: https://github.com/mkleehammer/pyodbc/wiki/Using-an-Output-Converter-function tup = struct.unpack("<6hI2h", dto_value) # e.g., (2017, 3, 16, 10, 35, 18, 500000000) - return datetime.datetime(tup[0], tup[1], tup[2], tup[3], tup[4], tup[5], tup[6] // 1000) + return datetime.datetime(tup[0], tup[1], tup[2], tup[3], tup[4], tup[5], tup[6] // 1000, + datetime.timezone(datetime.timedelta(hours=tup[7], minutes=tup[8]))) class DatabaseWrapper(BaseDatabaseWrapper): diff --git a/mssql/functions.py b/mssql/functions.py index 861c6ae4..0cc75c87 100644 --- a/mssql/functions.py +++ b/mssql/functions.py @@ -153,9 +153,11 @@ def sqlserver_exists(self, compiler, connection, template=None, **extra_context) return sql, params def sqlserver_now(self, compiler, connection, **extra_context): - return self.as_sql( - compiler, connection, template="SYSDATETIME()", **extra_context - ) + if settings.USE_TZ: + return self.as_sql(compiler, connection, template="SYSDATETIMEOFFSET()", **extra_context) + else: + # continue using SYSDATETIME to get the DB local time when django is not TZ aware + return self.as_sql(compiler, connection, template="SYSDATETIME()", **extra_context) def sqlserver_lookup(self, compiler, connection): # MSSQL doesn't allow EXISTS() to be compared to another expression