Commits

Michael Manfre  committed f6b5859

Allow backend to work in a timezone aware install while maintaining supports_timezones = False

  • Participants
  • Parent commits 83bda2e

Comments (0)

Files changed (3)

File sqlserver_ado/base.py

     has_bulk_insert = False
     
     supports_timezones = False
+    supports_sequence_reset = False
     
+    query_needed_to_fetch_return_id = True
 
 # IP Address recognizer taken from:
 # http://mail.python.org/pipermail/python-list/2006-March/375505.html

File sqlserver_ado/dbapi.py

 except ImportError:
     from django.utils import _decimal as decimal
 
+from django.conf import settings
 from django.db.utils import IntegrityError as DjangoIntegrityError, \
     DatabaseError as DjangoDatabaseError
+from django.utils import timezone
+
 import pythoncom
 import win32com.client
 
     day_count = int(date_as_float)
     fraction_of_day = abs(date_as_float - day_count)
 
-    return (datetime.datetime.fromordinal(day_count + _ordinal_1899_12_31) +
+    dt = (datetime.datetime.fromordinal(day_count + _ordinal_1899_12_31) +
         datetime.timedelta(milliseconds=fraction_of_day * _milliseconds_per_day))
+    
+    if settings.USE_TZ:
+        dt = dt.replace(tzinfo=timezone.utc)
+    return dt
 
 _variantConversions = MultiMap(
     {

File sqlserver_ado/operations.py

-from django.db.backends import BaseDatabaseOperations
 import datetime
 import time
+from django.conf import settings
+from django.db.backends import BaseDatabaseOperations
+from django.utils import timezone
 
 
 class DatabaseOperations(BaseDatabaseOperations):
         if value is None:
             return None
             
-        if value.tzinfo is not None:
-            raise ValueError("SQL Server 2005 does not support timezone-aware datetimes.")
+        if timezone.is_aware(value):
+            if settings.USE_TZ:
+                value = value.astimezone(timezone.utc).replace(tzinfo=None)
+            else:
+                raise ValueError("SQL Server backend does not support timezone-aware datetimes.")
 
         # SQL Server 2005 doesn't support microseconds
         if self.is_sql2005():
         if not self.is_sql2005():
             return value
 
+        if timezone.is_aware(value):
+            raise ValueError("SQL Server backend does not support timezone-aware times.")
+
         # MS SQL 2005 doesn't support microseconds
         #...but it also doesn't really suport bare times
         if value is None: