From b4ea9faae4124b82eff06f5ec70a27c3c1e00a93 Mon Sep 17 00:00:00 2001 From: Hassane SALHI Date: Thu, 27 Jan 2022 10:43:39 +0100 Subject: [PATCH] Take in account DateTime Kind while comparing --- Compare-NET-Objects-Tests/CompareDateTimeTests.cs | 12 ++++++++++++ Compare-NET-Objects/Compare-NET-Objects.csproj | 6 +++--- Compare-NET-Objects/TypeComparers/DateComparer.cs | 6 ++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Compare-NET-Objects-Tests/CompareDateTimeTests.cs b/Compare-NET-Objects-Tests/CompareDateTimeTests.cs index c55592d..91a77bd 100644 --- a/Compare-NET-Objects-Tests/CompareDateTimeTests.cs +++ b/Compare-NET-Objects-Tests/CompareDateTimeTests.cs @@ -60,6 +60,18 @@ public void CompareDateTimeAFewMillisecondsOffIgnore() if (!result.AreEqual) throw new Exception(result.DifferencesString); } + + [Test] + public void TestUtcvsLocalDates() + { + var dateTimeUtcNow = DateTime.UtcNow; + var dateTimeNow = dateTimeUtcNow.ToLocalTime(); + CompareLogic compareLogic = new CompareLogic(); + + ComparisonResult result = compareLogic.Compare(dateTimeNow, dateTimeUtcNow); + + Assert.IsTrue(result.AreEqual); + } #endregion } } diff --git a/Compare-NET-Objects/Compare-NET-Objects.csproj b/Compare-NET-Objects/Compare-NET-Objects.csproj index 50d84f0..6c92a30 100644 --- a/Compare-NET-Objects/Compare-NET-Objects.csproj +++ b/Compare-NET-Objects/Compare-NET-Objects.csproj @@ -20,8 +20,8 @@ compare comparison equality equal deep objects difference compareobjects deepequal deepequals What you have been waiting for. Perform a deep compare of any two .NET objects using reflection. Shows the differences between the two objects. gfinzer - 4.74.0 - 4.74.0.0 + 4.75.0 + 4.75.0.0 en-US Kellerman Software Support for Nullable DateTimeOffset by goyzhang https://github.com/GregFinzer/Compare-Net-Objects/issues/230 @@ -30,7 +30,7 @@ New config option to IgnoreConcreteTypes by goyzhang https://github.com/GregFin Fix for circular references in EnumerableComparer by idealist1508 https://github.com/GregFinzer/Compare-Net-Objects/issues/237 Copyright © 2022 - 4.74.0.0 + 4.75.0.0 License.txt NuGetIcon.png diff --git a/Compare-NET-Objects/TypeComparers/DateComparer.cs b/Compare-NET-Objects/TypeComparers/DateComparer.cs index 1c748d5..f39f95e 100644 --- a/Compare-NET-Objects/TypeComparers/DateComparer.cs +++ b/Compare-NET-Objects/TypeComparers/DateComparer.cs @@ -39,6 +39,12 @@ public override void CompareType(CompareParms parms) DateTime date1 = (DateTime) parms.Object1; DateTime date2 = (DateTime) parms.Object2; + if (date1.Kind != date2.Kind) + { + date1 = date1.ToUniversalTime(); + date2 = date2.ToUniversalTime(); + } + if (Math.Abs(date1.Subtract(date2).TotalMilliseconds) > parms.Config.MaxMillisecondsDateDifference) AddDifference(parms);