<PackageReference Include="NUnit" Version="3.8.0" />

TimeSpanToleranceComparer

Comparator for two DateTimes or TimeSpans.
using System; namespace NUnit.Framework.Constraints.Comparers { internal class TimeSpanToleranceComparer : IChainComparer { public bool? Equal(object x, object y, ref Tolerance tolerance) { if (tolerance == null || !(tolerance.Amount is TimeSpan)) return null; TimeSpan t = (TimeSpan)tolerance.Amount; TimeSpan timeSpan; if (x is DateTime && y is DateTime) { timeSpan = (DateTime)x - (DateTime)y; return timeSpan.Duration() <= t; } if (x is TimeSpan && y is TimeSpan) { timeSpan = (TimeSpan)x - (TimeSpan)y; return timeSpan.Duration() <= t; } return null; } } }