/// <summary> /// Returns a union of the current date range and the specified /// date range. The date ranges do not have to overlap. /// </summary> /// <exception cref="ArgumentNullException"> /// Thrown if dateRange is null. /// </exception> public DateRange Union(DateRange dateRange) { if (dateRange == null) { throw new ArgumentNullException("dateRange"); } DateTime startDate = DateUtility.ZeroDateTime; DateTime stopDate = DateUtility.ZeroDateTime; if (_startDate <= dateRange.StartDate) { startDate = _startDate; } else { startDate = dateRange.StartDate; } if (_stopDate >= dateRange.StopDate) { stopDate = _stopDate; } else { stopDate = dateRange.StopDate; } return new DateRange(startDate, stopDate); }
/// <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="DateRange.Empty"/> is returned. /// </summary> /// <exception cref="ArgumentNullException"> /// Thrown if dateRange is null. /// </exception> public DateRange Intersect(DateRange dateRange) { // *************************************************************************** // scenario 1 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] ... [04/21/2005] // DateRange2 [01/30/1905] ..................................... [05/20/2010] // --------------------------------------------------------------------------- // Intersects [02/20/1972] ... [04/21/2005] // *************************************************************************** // *************************************************************************** // scenario 2 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] ..................................... [04/21/2005] // DateRange2 [07/01/1975] ... [07/31/1978] // --------------------------------------------------------------------------- // Intersects [07/01/1975] ... [07/31/1978] // *************************************************************************** // *************************************************************************** // scenario 3 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] .................... [04/21/2005] // DateRange2 [01/30/1905] .................... [12/20/1989] // --------------------------------------------------------------------------- // Intersects [02/20/1972] ... [12/20/1989] // *************************************************************************** // *************************************************************************** // scenario 4 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] .................... [04/21/2005] // DateRange2 [05/18/1975] .................... [12/20/2007] // --------------------------------------------------------------------------- // Intersects [05/18/1975] ... [04/21/2005] // *************************************************************************** // *************************************************************************** // scenario 5 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] ... [04/21/2005] // DateRange2 [04/21/2005] ... [12/20/2007] // --------------------------------------------------------------------------- // Intersects [04/21/2005] // *************************************************************************** // *************************************************************************** // scenario 6 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] ... [12/20/2007] // DateRange2 [04/15/1968] ... [02/20/1972] // --------------------------------------------------------------------------- // Intersects [02/20/1972] // *************************************************************************** // *************************************************************************** // scenario 7 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] ... [04/21/2005] // DateRange2 [02/20/1972] .................... [12/20/2007] // --------------------------------------------------------------------------- // Intersects [02/20/1972] ... [04/21/2005] // *************************************************************************** // *************************************************************************** // scenario 8 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] .................... [04/21/2005] // DateRange2 [04/12/1975] ... [04/21/2005] // --------------------------------------------------------------------------- // Intersects [04/12/1975] ... [04/21/2005] // *************************************************************************** // *************************************************************************** // scenario 9 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] ... [04/21/2005] // DateRange2 [06/23/2005] ... [04/21/2007] // --------------------------------------------------------------------------- // Intersects // *************************************************************************** // *************************************************************************** // scenario 10 // --------------------------------------------------------------------------- // DateRange1 [02/20/1972] ... [04/21/2005] // DateRange2 [06/23/1908] ... [04/21/1965] // --------------------------------------------------------------------------- // Intersects // *************************************************************************** if (dateRange == null) { throw new ArgumentNullException("dateRange"); } DateRange intersection = DateRange.Empty; // make sure this is not scenario 9 or 10 if (_startDate <= dateRange.StopDate && _stopDate >= dateRange.StartDate) { // if we passed the first test, then there is some overlap. DateTime startDate = DateUtility.ZeroDateTime; DateTime stopDate = DateUtility.ZeroDateTime; if (_startDate >= dateRange.StartDate) { startDate = _startDate; } else { startDate = dateRange.StartDate; } if (_stopDate <= dateRange.StopDate) { stopDate = _stopDate; } else { stopDate = dateRange.StopDate; } intersection = new DateRange(startDate, stopDate); } return intersection; }
/// <summary> /// Parses the specified string and returns the parsed date range. /// </summary> public static bool TryParse(string dateRangeString, out DateRange dateRangeOutput) { bool success = false; dateRangeOutput = null; if (dateRangeString != null && dateRangeString.Length != 0) { string[] dateRangeParts = dateRangeString.Split('-'); dateRangeOutput = new DateRange(); DateTime startDate = DateUtility.ZeroDateTime; if (DateTime.TryParse(dateRangeParts[0].Trim(), out startDate)) { dateRangeOutput.StartDate = startDate; success = true; if (dateRangeParts.Length != 2) { dateRangeOutput.StopDate = DateTime.MaxValue; } else { DateTime stopDate = DateUtility.ZeroDateTime; if (DateTime.TryParse(dateRangeParts[1].Trim(), out stopDate)) { dateRangeOutput.StopDate = stopDate; success = true; } else { success = false; } } } } return success; }
/// <summary> /// Returns whether the specified object equals the current <see cref="DateRange"/>. /// </summary> public bool Equals(DateRange compare) { bool equals = false; if (compare != null) { equals = (compare.StartDate == _startDate && compare.StopDate == _stopDate); } return equals; }
/// <summary> /// Parses the specified string and returns the parsed date range. /// </summary> public static DateRange Parse(string dateRange) { if (dateRange == null) { throw new ArgumentNullException("dateRange"); } if (dateRange.Length == 0) { throw new ArgumentException("Parameter \"dateRange\" must be specified."); } string[] dateRangeParts = dateRange.Split('-'); DateRange value = new DateRange(); value.StartDate = DateTime.Parse(dateRangeParts[0].Trim()); if (dateRangeParts.Length != 2) { value.StopDate = DateTime.MaxValue; } else { value.StopDate = DateTime.Parse(dateRangeParts[1].Trim()); } return value; }