/// <summary> /// Returns the intersection between the given interval and this interval. /// </summary> /// <param name="interval"> /// The specified interval to intersect with this one. /// </param> /// <returns> /// A <see cref="DateInterval"/> corresponding to the intersection between the given interval and the current /// instance. If there is no intersection, a null reference is returned. /// </returns> /// <exception cref="ArgumentException"><paramref name="interval" /> uses a different /// calendar to this date interval.</exception> public DateInterval?Intersection(DateInterval interval) => Contains(interval) ? interval : interval.Contains(this) ? this : interval.Contains(Start) ? new DateInterval(Start, interval.End) : interval.Contains(End) ? new DateInterval(interval.Start, End) : null;
private void ValidateInterval(DateInterval interval) { Preconditions.CheckNotNull(interval, nameof(interval)); Preconditions.CheckArgument(interval.Calendar.Equals(Start.Calendar), nameof(interval), "The specified interval uses a different calendar system to this one"); }
/// <summary> /// Checks whether the given interval is within this interval. This requires that the start date of the specified /// interval is not earlier than the start date of this interval, and the end date of the specified interval is not /// later than the end date of this interval. /// </summary> /// <remarks> /// An interval contains another interval with same start and end dates, or itself. /// </remarks> /// <param name="interval">The interval to check for containment within this interval.</param> /// <exception cref="ArgumentException"><paramref name="interval" /> uses a different /// calendar to this date interval.</exception> /// <returns><c>true</c> if <paramref name="interval"/> is within this interval; <c>false</c> otherwise.</returns> public bool Contains(DateInterval interval) { ValidateInterval(interval); return(Start <= interval.Start && interval.End <= End); }