Skip to content

DateTimeOffset checks

Cyrille DUPUYDAUBY edited this page Jan 31, 2021 · 7 revisions

DateTimeOffset specific checks were added in V 2.6.0

Equality

IsEqualToIgnoringMilliseconds(expected)

Checks that the sut is equal to the expected DateTimeOffset, disregarding milliseconds. Note that the TimeSpan needs to be equal to the expected one. If you need to check the aggregated DateTime, consider using MatchTheSameUtcInstant.

var sut = new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 152), TimeSpan.Zero);
// This check succeeds
Check.That(sut).IsEqualToIgnoringMilliseconds(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 870), TimeSpan.Zero);
// This check fails
Check.That(sut).IsEqualToIgnoringMilliseconds(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 13, 152), TimeSpan.Zero);

IsEqualToIgnoringSeconds(expected)

Checks that the sut is equal to the expected DateTime, disregarding seconds.

var sut = new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 152), TimeSpan.Zero);
// This check succeeds
Check.That(sut).IsEqualToIgnoringSeconds(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 870), TimeSpan.Zero);
// This check fails
Check.That(sut).IsEqualToIgnoringSeconds(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 13, 152), TimeSpan.Zero);

IsEqualToIgnoringMinutes(expected)

Checks that the sut is equal to the expected DateTimeOffset, disregarding minutes.

var sut = new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 152), TimeSpan.Zero);
// This check succeeds
Check.That(sut).IsEqualToIgnoringMinutes(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 870), TimeSpan.Zero);
// This check fails
Check.That(sut).IsEqualToIgnoringMinutes(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 13, 152), TimeSpan.Zero);

IsEqualToIgnoringHours(expected)

Checks that the sut is equal to the expected DateTimeOffset, disregarding minutes.

var sut = new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 152), TimeSpan.Zero);
// This check succeeds
Check.That(sut).IsEqualToIgnoringHours(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12, 870), TimeSpan.Zero);
// This check fails
Check.That(sut).IsEqualToIgnoringHours(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 13, 152), TimeSpan.Zero);

MatchTheSameUtcInstant(expected)

Checks that the sut represents the expected Utc date time.

// successful checks
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 0, 1, 0), TimeSpan.FromHours(1));
Check.That(dateTimeOffset1).MatchTheSameUtcInstant(dateTimeOffset2);
// failing checks (even if time difference is only 1ms)
DateTimeOffset dateTimeOffsetA = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 0, 1, 999), TimeSpan.FromHours(1));
Check.That(dateTimeOffsetA).MatchTheSameUtcInstant(dateTimeOffsetB);

MatchTheSameUtcInstantIgnoringMillis(expected)

Checks that the sut represents the expected Utc date time disregarding the milliseconds.

// successful checks
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 0, 1, 100), TimeSpan.FromHours(1));
Check.That(dateTimeOffset1).MatchTheSameUtcInstantIgnoringMillis(dateTimeOffset2);
// failing checks (even if time difference is only 1ms)
DateTimeOffset dateTimeOffsetA = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 0, 0, 999), TimeSpan.FromHours(1));
Check.That(dateTimeOffsetA).MatchTheSameUtcInstantIgnoringMillis(dateTimeOffsetB);

MatchTheSameUtcInstantIgnoringSeconds(expected)

Checks that the sut represents the expected Utc date time disregarding the seconds.

// successful checks
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 0, 9, 0), TimeSpan.FromHours(1));
Check.That(dateTimeOffset1).MatchTheSameUtcInstantIgnoringSeconds(dateTimeOffset2);
// failing checks (even if time difference is only 1ms)
DateTimeOffset dateTimeOffsetA = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 1, 0, 999), TimeSpan.FromHours(1));
Check.That(dateTimeOffsetA).MatchTheSameUtcInstantIgnoringSeconds(dateTimeOffsetB);

MatchTheSameUtcInstantIgnoringMinutes(expected)

Checks that the sut represents the expected Utc date time disregarding the minutes.

// successful checks
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 5, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 1, 9, 0), TimeSpan.FromHours(1));
Check.That(dateTimeOffset1).MatchTheSameUtcInstantIgnoringMinutes(dateTimeOffset2);
// failing checks (even if time difference is only 1ms)
DateTimeOffset dateTimeOffsetA = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 2, 1, 0, 999), TimeSpan.FromHours(1));
Check.That(dateTimeOffsetA).MatchTheSameUtcInstantIgnoringMinutes(dateTimeOffsetB);

MatchTheSameUtcInstantIgnoringHours(expected)

Checks that the sut represents the expected Utc date time disregarding the hours.

// successful checks
DateTimeOffset dateTimeOffset1 = new DateTimeOffset(new DateTime(2000, 1, 1, 2, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 1, 1, 1, 9, 0), TimeSpan.FromHours(1));
Check.That(dateTimeOffset1).MatchTheSameUtcInstantIgnoringHours(dateTimeOffset2);
// failing checks (even if time difference is only 1ms)
DateTimeOffset dateTimeOffsetA = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 1, 0), TimeSpan.Zero);
DateTimeOffset dateTimeOffset2 = new DateTimeOffset(new DateTime(2000, 1, 2, 2, 1, 0, 999), TimeSpan.FromHours(1));
Check.That(dateTimeOffsetA).MatchTheSameUtcInstantIgnoringHours(dateTimeOffsetB);

IsCloseTo(expected, tolerance)

Checks that sut is close to the expected date time, within +/- tolerance. The tolerance can be a Timespan or a Duration.

var sut = new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 12), TimeSpan.Zero);
// This check succeeds
Check.That(sut).IsCloseTo(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 15), TimeSpan.Zero), new Duration(5, TimeUnit.Minutes));
// This check succeeds
Check.That(sut).IsCloseTo(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 15), TimeSpan.Zero), TimeSpan.FromMinutes(5));
// This check fails
Check.That(sut).IsCloseTo(new DateTimeOffset(new DateTime(2018, 2, 6, 9, 18, 02), TimeSpan.Zero), new Duration(5, TimeUnit.Minutes));
Clone this wiki locally