union method

DateInterval? union(
  1. DateInterval interval
)

Returns the union between the given interval and this interval, as long as they're overlapping or contiguous.

  • interval: The specified interval from which to generate the union interval.

A 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.

  • ArgumentException: interval uses a different calendar to this date interval.

Implementation

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 IPeriod.daysBetween(_start, _end) >= length + interval.length
      ? null
      : DateInterval(_start, _end);
}