| .NET |
SQL |
| dateTimeOffset.DateTime |
CONVERT(datetime2, dateTimeOffset) |
| dateTimeOffset.UtcDateTime |
CONVERT(datetime2, SWITCHOFFSET(dateTimeOffset, '+00:00')) |
| dateTimeOffset.LocalDateTime |
CONVERT(datetime2, dateTimeOffset AT TIME ZONE CURRENT_TIMEZONE_ID()) |
| dateTimeOffset.Offset.TotalMinutes |
CAST(DATEPART(tz, dateTimeOffset) AS float) |
| dateTimeOffset.ToOffset(offset) |
SWITCHOFFSET(dateTimeOffset, offset) |
Original issue description
Currently some DateTimeOffset members shared with DateTime are translated, e.g. https://github.com/dotnet/efcore/blob/c8c6c21cafb9d25b7d8f3be316fd806cb2132de5/src/EFCore.SqlServer/Query/Internal/SqlServerDateTimeMemberTranslator.cs
Could add some more translations for instance members specific to DateTimeOffset. Some thoughts/suggestions for SQL Server:
-
DateTimeOffset.DateTime -> CONVERT(datetime2, @dateTimeOffset)
-- Lop off the offset
-
DateTimeOffset.UtcDateTime -> CONVERT(datetime2, SWITCHOFFSET(@dateTimeOffset, '+00:00'))
-- Adjust to 0 offset then lop off the offset
-- @dateTimeOffset AT TIME ZONE 'UTC' is an alternative for the adjustment part, but is only supported in 2016+
-
DateTimeOffset.LocalDateTime
-- Don't think this can be translated without AT TIME ZONE and a simple way to get the current timezone ID from SQL Server.
CONVERT(datetime2, dateTimeOffset)CONVERT(datetime2, SWITCHOFFSET(dateTimeOffset, '+00:00'))CONVERT(datetime2, dateTimeOffset AT TIME ZONE CURRENT_TIMEZONE_ID())CAST(DATEPART(tz, dateTimeOffset) AS float)SWITCHOFFSET(dateTimeOffset, offset)Original issue description
Currently some DateTimeOffset members shared with DateTime are translated, e.g. https://github.com/dotnet/efcore/blob/c8c6c21cafb9d25b7d8f3be316fd806cb2132de5/src/EFCore.SqlServer/Query/Internal/SqlServerDateTimeMemberTranslator.cs
Could add some more translations for instance members specific to DateTimeOffset. Some thoughts/suggestions for SQL Server:
DateTimeOffset.DateTime ->
CONVERT(datetime2, @dateTimeOffset)-- Lop off the offset
DateTimeOffset.UtcDateTime ->
CONVERT(datetime2, SWITCHOFFSET(@dateTimeOffset, '+00:00'))-- Adjust to 0 offset then lop off the offset
--
@dateTimeOffset AT TIME ZONE 'UTC'is an alternative for the adjustment part, but is only supported in 2016+DateTimeOffset.LocalDateTime
-- Don't think this can be translated without
AT TIME ZONEand a simple way to get the current timezone ID from SQL Server.