Ejemplo n.º 1
0
        /// <summary>
        /// Returns the union between the given interval and this interval, as long as they're overlapping or contiguous.
        /// </summary>
        /// <param name="interval">The specified interval from which to generate the union interval.</param>
        /// <returns>
        /// A <see cref="DateInterval"/> corresponding to the union between the given interval and the current
        /// instance, in the case the intervals overlap or are contiguous; a null reference otherwise.
        /// </returns>
        /// <exception cref="ArgumentException"><paramref name="interval" /> uses a different calendar to this date interval.</exception>
        public DateInterval?Union(DateInterval interval)
        {
            ValidateInterval(interval);

            var start = LocalDate.Min(Start, interval.Start);
            var end   = LocalDate.Max(End, interval.End);

            // Check whether the length of the interval we *would* construct is greater
            // than the sum of the lengths - if it is, there's a day in that candidate union
            // that isn't in either interval. Note the absence of "+ 1" and the use of >=
            // - it's equivalent to Period.DaysBetween(...) + 1 > Length + interval.Length,
            // but with fewer operations.
            return(Period.DaysBetween(start, end) >= Length + interval.Length
                ? null
                : new DateInterval(start, end));
        }