Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EqualsExact in DefaultDateTimeOffsetValueComparer #26678

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}