Skip to content

Commit

Permalink
Translate DateTime.Date when mapped to PG date
Browse files Browse the repository at this point in the history
Fixes #2499

(cherry picked from commit f1a87fa)
  • Loading branch information
roji committed Sep 7, 2022
1 parent cab9057 commit a116e57
Showing 1 changed file with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,34 @@ public NpgsqlDateTimeMemberTranslator(
// When given a timestamptz, date_trunc performs the truncation with respect to TimeZone; to avoid that, we use the overload
// accepting a time zone, and pass UTC. For regular timestamp (or in legacy timestamp mode), we use the simpler overload without
// a time zone.
if (NpgsqlTypeMappingSource.LegacyTimestampBehavior || instance?.TypeMapping is NpgsqlTimestampTypeMapping)
switch (instance?.TypeMapping)
{
return _sqlExpressionFactory.Function(
"date_trunc",
new[] { _sqlExpressionFactory.Constant("day"), instance! },
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
returnType,
instance!.TypeMapping);
}

if (instance?.TypeMapping is NpgsqlTimestampTzTypeMapping)
{
return _sqlExpressionFactory.Function(
"date_trunc",
new[] { _sqlExpressionFactory.Constant("day"), instance, _sqlExpressionFactory.Constant("UTC") },
nullable: true,
argumentsPropagateNullability: TrueArrays[3],
returnType,
instance.TypeMapping);
case NpgsqlTimestampTypeMapping:
case NpgsqlTimestampTzTypeMapping when NpgsqlTypeMappingSource.LegacyTimestampBehavior:
return _sqlExpressionFactory.Function(
"date_trunc",
new[] { _sqlExpressionFactory.Constant("day"), instance! },
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
returnType,
instance.TypeMapping);

case NpgsqlTimestampTzTypeMapping:
return _sqlExpressionFactory.Function(
"date_trunc",
new[] { _sqlExpressionFactory.Constant("day"), instance, _sqlExpressionFactory.Constant("UTC") },
nullable: true,
argumentsPropagateNullability: TrueArrays[3],
returnType,
instance.TypeMapping);

// If DateTime.Date is invoked on a PostgreSQL date, simply no-op.
case NpgsqlDateTypeMapping:
return instance;

default:
return null;
}

return null;
}

return member.Name switch
Expand Down

0 comments on commit a116e57

Please sign in to comment.