/// <summary> /// Extends the input at both ends with the given parameter. /// </summary> /// <param name="input">The input to extend.</param> /// <param name="expand">A copy of the extended input.</param> /// <returns></returns> public static IEnumerable <TimeWindow> Extend(this IEnumerable <TimeWindow> input, TimeSpan expand) { TimeWindow previous = null; foreach (var tw in input) { var extended = tw.Extend(expand); if (previous == null) { previous = extended; } else if (extended != null && extended.From <= previous.To) { previous.To = extended.To; } else { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (previous != null) { yield return(previous); } previous = extended; } } if (previous != null) { yield return(previous); } }
/// <summary> /// Creates an intersect of this instance and the time window parameter. /// </summary> /// <param name="other">The time window parameter to insersect with.</param> /// <returns>The intersection of the time windows.</returns> public TimeWindow Intersect(TimeWindow other) { return(Validate(new TimeWindow { From = this.From > other.From ? this.From : other.From, To = this.To < other.To ? this.To : other.To })); }
/// <summary> /// Returns the input time window validated. /// </summary> /// <param name="input">The input tiem window.</param> /// <returns>The validated input or null if the input was invalid.</returns> private static TimeWindow Validate(TimeWindow input) { if (input.From < input.To) { return(input); } else { return(null); } }