public void Overlaps_Finishes_True() { // Arrange var(dateTime, first) = Fixture.Create <LocalDateTime, DateTimeInterval>((d, i) => d < i.Start); var second = new DateTimeInterval(dateTime, first.End); // Act var overlaps = first.Overlaps(second); var overlapsInverted = second.Overlaps(first); // Assert overlaps.Should().BeTrue(); overlapsInverted.Should().BeTrue(); }
public void Overlaps_Starts_True() { // Arrange var(first, dateTime) = Fixture.Create <DateTimeInterval, LocalDateTime>((x, y) => x.End < y); var second = new DateTimeInterval(first.Start, dateTime); // Act var overlaps = first.Overlaps(second); var overlapsInverted = second.Overlaps(first); // Assert overlaps.Should().BeTrue(); overlapsInverted.Should().BeTrue(); }
/// <summary> /// Returns the overlap between the two intervals. /// </summary> /// <param name="first">An interval.</param> /// <param name="second">The other interval to find an overlap with.</param> /// <returns>A date time interval equal to the overlap of the two intervals, if they overlap; <c>null</c> otherwise.</returns> /// <exception cref="System.ArgumentException"><paramref name="first"/> is not in the same calendar as <paramref name="second"/>.</exception> /// <exception cref="System.ArgumentNullException">If <paramref name="first"/> or <paramref name="second"/> is <c>null</c>.</exception> public static DateTimeInterval GetOverlapWith(this DateTimeInterval first, DateTimeInterval second) { if (first == null) { throw new System.ArgumentNullException(nameof(first)); } if (second == null) { throw new System.ArgumentNullException(nameof(second)); } if (!first.Start.Calendar.Equals(second.Start.Calendar)) { throw new System.ArgumentException("The given interval must be in the same calendar as this interval.", nameof(second)); } var start = first.Start >= second.Start ? first.Start : second.Start; var end = first.End <= second.End ? first.End : second.End; return(first.Overlaps(second) ? new DateTimeInterval(start, end) : null); }