Skip to content

Commit

Permalink
Use EqualsExact in DefaultDateTimeOffsetValueComparer (#26678)
Browse files Browse the repository at this point in the history
Fixes #26672

Co-authored-by: steven.darby <[email protected]>
  • Loading branch information
stevendarby and steven.darby authored Nov 15, 2021
1 parent b0eaace commit 36776f6
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/EFCore/ChangeTracking/ValueComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,18 @@ public override Expression ExtractEqualsBody(Expression leftExpression, Expressi

internal sealed class DefaultDateTimeOffsetValueComparer : DefaultValueComparer<DateTimeOffset>
{
private static readonly PropertyInfo _offsetPropertyInfo = typeof(DateTimeOffset).GetProperty(nameof(DateTimeOffset.Offset))!;
private static readonly MethodInfo _equalsExactMethodInfo
= typeof(DateTimeOffset).GetRequiredRuntimeMethod(nameof(DateTimeOffset.EqualsExact), typeof(DateTimeOffset));

// In .NET, two DateTimeOffset instances are considered equal if they represent the same point in time but with different
// time zone offsets. This comparer considers such DateTimeOffset as non-equal.
// time zone offsets. This comparer uses EqualsExact, which considers such DateTimeOffset as non-equal.
public DefaultDateTimeOffsetValueComparer(bool favorStructuralComparisons)
: base((v1, v2) => v1 == v2 && v1.Offset == v2.Offset, favorStructuralComparisons)
: base((v1, v2) => v1.EqualsExact(v2), favorStructuralComparisons)
{
}

public override Expression ExtractEqualsBody(Expression leftExpression, Expression rightExpression)
=> Expression.And(
Expression.Equal(leftExpression, rightExpression),
Expression.Equal(
Expression.Property(leftExpression, _offsetPropertyInfo),
Expression.Property(rightExpression, _offsetPropertyInfo)));
=> Expression.Call(leftExpression, _equalsExactMethodInfo, rightExpression);
}
}
}

0 comments on commit 36776f6

Please sign in to comment.