/// <summary> /// Returns a union of the current date range and the specified /// date range. The date ranges do not have to overlap. /// </summary> public TimeRange Union(TimeRange timeRange) { TimeSpan startTime = TimeSpan.Zero; TimeSpan stopTime = TimeSpan.Zero; if (Compare(_startTime, timeRange.StartTime) != 1) { startTime = _startTime; } else { startTime = timeRange.StartTime; } if (Compare(_stopTime, timeRange.StopTime) != -1) { stopTime = _stopTime; } else { stopTime = timeRange.StopTime; } return new TimeRange(startTime, stopTime); }
/// <summary> /// Returns whether the specified object equals the current <see cref="TimeRange"/>. /// </summary> public bool Equals(TimeRange compare) { return (Compare(_startTime, compare.StartTime) == 0 && Compare(_stopTime, compare.StopTime) == 0); }
/// <summary> /// Returns an intersection of the current data range and the specified /// date range. If there is no intersection between the date ranges, then /// <see cref="TimeRange.Empty"/> is returned. /// </summary> public TimeRange Intersect(TimeRange timeRange) { TimeRange intersection = TimeRange.Empty; // test for scenario 5 & 6 if (Compare(_startTime, timeRange.StopTime) != 1 && Compare(_stopTime, timeRange.StartTime) != -1) { // if we passed the first test, then there is some overlap. TimeSpan startTime = TimeSpan.Zero; TimeSpan stopTime = TimeSpan.Zero; if (Compare(_startTime, timeRange.StartTime) != -1) { startTime = _startTime; } else { startTime = timeRange.StartTime; } if (Compare(_stopTime, timeRange.StopTime) != 1) { stopTime = _stopTime; } else { stopTime = timeRange.StopTime; } intersection = new TimeRange(startTime, stopTime); } return intersection; }
/// <summary> /// Parses the specified string and returns the parsed date range. /// </summary> public static bool TryParse(string timeRangeString, out TimeRange timeRangeOutput) { timeRangeOutput = null; bool success = false; if (timeRangeString != null && timeRangeString.Length != 0) { string[] timeRangeParts = timeRangeString.Split('-'); timeRangeOutput = new TimeRange(); DateTime startDate = DateUtility.ZeroDateTime; string firstPart = timeRangeParts[0].Trim(); if (DateTime.TryParse(firstPart, out startDate)) { timeRangeOutput.StartTime = startDate.TimeOfDay; success = true; if (timeRangeParts.Length != 2) { timeRangeOutput.StopTime = TimeSpan.Zero; } else { DateTime stopDate = DateUtility.ZeroDateTime; if (DateTime.TryParse(timeRangeParts[1].Trim(), out stopDate)) { timeRangeOutput.StopTime = stopDate.TimeOfDay; success = true; } else { success = false; } } } else { TimeSpan startTime = TimeSpan.Zero; if (TimeSpan.TryParse(firstPart, out startTime)) { timeRangeOutput.StartTime = startTime; success = true; if (timeRangeParts.Length != 2) { timeRangeOutput.StopTime = TimeSpan.Zero; } else { TimeSpan stopTime = TimeSpan.Zero; if (TimeSpan.TryParse(timeRangeParts[1].Trim(), out stopTime)) { timeRangeOutput.StopTime = stopTime; success = true; } else { success = false; } } } } } return success; }