/// <summary> /// Get intersection of two TimeRanges. /// </summary> /// <param name="second">Second TimeRange.</param> /// <returns>'True' if they intersects, otherwise no.</returns> public bool Intersects(TimeRange second) { return this.Intersection(second) != null; }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns></returns> public object Clone() { TimeRange obj = new TimeRange(); obj.From = From; obj.To = To; return obj; }
/// <summary> /// Get intersection of two TimeRanges. /// </summary> /// <param name="second">Second timerange.</param> /// <returns>If they intersects - return intersection /// time range, otherwise - return 'null'.</returns> public TimeRange Intersection(TimeRange second) { // If second TimeRange is null - return null. if (second == null) return null; // Detect intersection time range. var start = this.From > second.From ? this.From : second.From; var finish = this.To < second.To ? this.To : second.To; if (start <= finish) { return new TimeRange(start, finish); } else return null; }
/// <summary> /// Get intersection of two TimeRanges. /// </summary> /// <param name="firstStart">First time range start.</param> /// <param name="firstFinish">First time range finish.</param> /// <param name="secondStart">Second time range start.</param> /// <param name="secondFinish">Second time range finish.</param> /// <returns>If they intersects - return intersection /// time range, otherwise - return 'null'.</returns> public static TimeRange Intersection(TimeSpan firstStart, TimeSpan firstFinish, TimeSpan secondStart, TimeSpan secondFinish) { // Check that both time ranges are valid. if (firstStart == null || firstFinish == null || secondStart == null || secondFinish == null) return null; var firstRange = new TimeRange(firstStart, firstFinish); return firstRange.Intersection(new TimeRange(firstStart, firstFinish)); }