Commits

Michael Manfre committed 2fe6e77

Implement date_interval_sql() with calls to DATEADD. Django expressions_regress.FTimeDeltaTests unit tests fail due to SQL server datetime microsecond precision. Fixes #9

Comments (0)

Files changed (1)

sqlserver_ado/operations.py

     def date_extract_sql(self, lookup_type, field_name):
         return "DATEPART(%s, %s)" % (lookup_type, self.quote_name(field_name))
 
+    def date_interval_sql(self, sql, connector, timedelta):
+        """
+        implements the interval functionality for expressions
+        format for SQL Server.
+        """
+        sign = 1 if connector == '+' else -1
+        seconds = ((timedelta.days * 86400) + timedelta.seconds) * sign
+        out = sql
+        if seconds:
+            out = u'DATEADD(SECOND, {0}, {1})'.format(seconds, sql)
+        if timedelta.microseconds:
+            # DATEADD with datetime doesn't support ms, must cast up
+            out = u'DATEADD(MICROSECOND, {ms}, CAST({sql} as datetime2))'.format(
+                ms=timedelta.microseconds * sign,
+                sql=out,
+            )
+        return out
+
     def date_trunc_sql(self, lookup_type, field_name):
         return "DATEADD(%s, DATEDIFF(%s, 0, %s), 0)" % (lookup_type, lookup_type, field_name)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.