Skip to content

Commit

Permalink
Fix for when one DateTimeKind is unspecified. https://github.com/Greg…
Browse files Browse the repository at this point in the history
  • Loading branch information
GregFinzer committed Apr 7, 2022
1 parent 80bd7eb commit f2f32a3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Compare-NET-Objects-Tests/CompareDateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,27 @@ public void CompareDateTimeAFewMillisecondsOffIgnore()
[Test]
public void TestUtcvsLocalDates()
{
var dateTimeUtcNow = DateTime.UtcNow;
var dateTimeNow = dateTimeUtcNow.ToLocalTime();
DateTime dateTimeUtcNow = DateTime.UtcNow;
DateTime dateTimeNow = dateTimeUtcNow.ToLocalTime();
CompareLogic compareLogic = new CompareLogic();

ComparisonResult result = compareLogic.Compare(dateTimeNow, dateTimeUtcNow);

Assert.IsTrue(result.AreEqual);
}

[Test]
public void DateTimeKindUnspecifiedDifferent()
{
DateTime nowUtc = DateTime.UtcNow.Date;
DateTime nowUtcParsed;
DateTime.TryParse(nowUtc.ToString("yyyy-MM-dd"), out nowUtcParsed);

CompareLogic compareLogic = new CompareLogic();
var result = compareLogic.Compare(nowUtc, nowUtcParsed);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.AreEqual);
}
#endregion
}
}
18 changes: 18 additions & 0 deletions Compare-NET-Objects/ComparisonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,21 +544,38 @@ public Action<Difference> DifferenceCallback
#endif
public decimal DecimalPrecision { get; set; }

/// <summary>
/// When comparing Fields and Properties the types have to be the same. If IgnoreConcretTypes is true then they will be ignored. The default is false.
/// </summary>
#if !NETSTANDARD
[DataMember]
#endif
public bool IgnoreConcreteTypes { get; set; }


/// <summary>
/// If true, properties that are defined in the actual object but missing in the expected object will not be flagged as differences. Default is true.
/// </summary>
#if !NETSTANDARD
[DataMember]
#endif
public bool IgnoreMissingProperties { get; set; }

/// <summary>
/// If true, fields that are defined in the actual object but missing in the expected object will not be flagged as differences. Default is true.
/// </summary>
#if !NETSTANDARD
[DataMember]
#endif
public bool IgnoreMissingFields { get; set; }

/// <summary>
/// Specify a DateTimeKind to use when it is unspecified. The default is DateTimeKind.Utc. If null is specified it will not be defaulted.
/// </summary>
#if !NETSTANDARD
[DataMember]
#endif
public DateTimeKind? DateTimeKindToUseWhenUnspecified { get; set; }
#endregion

#region Methods
Expand Down Expand Up @@ -644,6 +661,7 @@ public void Reset()
IgnoreStringLeadingTrailingWhitespace = false;
IgnoreMissingProperties = true;
IgnoreMissingFields = true;
DateTimeKindToUseWhenUnspecified = DateTimeKind.Utc;
}
#endregion
}
Expand Down
6 changes: 6 additions & 0 deletions Compare-NET-Objects/TypeComparers/DateComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public override void CompareType(CompareParms parms)

if (date1.Kind != date2.Kind)
{
if (date1.Kind == DateTimeKind.Unspecified && parms.Config.DateTimeKindToUseWhenUnspecified != null)
date1 = DateTime.SpecifyKind(date1, parms.Config.DateTimeKindToUseWhenUnspecified.Value);

if (date2.Kind == DateTimeKind.Unspecified && parms.Config.DateTimeKindToUseWhenUnspecified != null)
date2 = DateTime.SpecifyKind(date2, parms.Config.DateTimeKindToUseWhenUnspecified.Value);

date1 = date1.ToUniversalTime();
date2 = date2.ToUniversalTime();
}
Expand Down

0 comments on commit f2f32a3

Please sign in to comment.