Skip to content

Use SQL Server datetime2 functions when translating DateTime.Now and DateTime.UtcNow #26780

@stevendarby

Description

@stevendarby

In the below code, DateTime.Now is translated to GETDATE() and DateTime.UtcNow is translated to GETUTCDATE() - these functions return SQL Server's datetime.

Should they use equivalent functions that return datetime2 instead? These are SYSDATETIME() and SYSUTCDATETIME() respectively. The precision and range of datetime2 is a closer (exact?) match to .NET DateTime so it feels like the more faithful translation, but I'm not sure if there are legacy reasons to use the older functions.

I can submit a PR to fix this if it's considered a valid proposal.

case nameof(DateTime.Now):
return _sqlExpressionFactory.Function(
declaringType == typeof(DateTime) ? "GETDATE" : "SYSDATETIMEOFFSET",
Array.Empty<SqlExpression>(),
nullable: false,
argumentsPropagateNullability: Array.Empty<bool>(),
returnType);
case nameof(DateTime.UtcNow):
var serverTranslation = _sqlExpressionFactory.Function(
declaringType == typeof(DateTime) ? "GETUTCDATE" : "SYSUTCDATETIME",
Array.Empty<SqlExpression>(),
nullable: false,
argumentsPropagateNullability: Array.Empty<bool>(),
returnType);

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions