Click here to Skip to main content
Licence CPOL
First Posted 28 Jul 2011
Views 6,282
Downloads 362
Bookmarked 15 times

Date Range Overlap Testing

By | 2 Aug 2011 | Article
A class to determine if there are any overlaps in a set of date ranges
Sample Image

Introduction

Before I wrote this class, I used to test for date range overlaps the hard way having to check each endpoint with lots of date comparisons. This way is much simpler and also more efficient.

Background

With testing for overlaps on any range of numbers/dates, there are three possibilities. These are shown below.

  1. Where the second interval (CD) overlaps on the beginning of the first (AB)
  2. Where the second interval (EF) overlaps on the end of the first (AB)
  3. Where the first interval (AB) exists inside the second (GH)

In the first case, you have to test if D lies between A and B. In the second, you have to test if E lies between A and B and in the third case, you have to test if both A and B lie between G and H. This leads to a lot of date comparisons to ensure you don't miss any case of an overlap.

There is a better way to test for overlaps using timespans which also enables you to test for more than two date ranges at a time. If you add up the total timespans of all the intervals AB, CD, EF and GH and compare this with the timespan of the minimum date (C) and maximum date (F), if the sum of the intervals is greater than the total timespan then there is an overlap, otherwise there is not.

In the image below, you can see that if you add up all the timespans of the intervals, this will be less than the total timespan and hence there is no overlap.

Using the Code

I have included two functions in the DateRange class that perform the testing. The first only tests if there is an overlap at all and return true if there is. The second returns a list of which date range pairs have a conflict.

Here are the prototypes:

/// <summary>
/// Tests for an overlap of given date ranges
/// </summary>
/// <param name="TestEndPoints">Set to true if you want to count the date range 
/// end points as an overlap and false if you don't care if the end points overlap.
/// </param>
/// <param name="Ranges">The set of date ranges that you want to test.</param>
/// <returns>True if there is an overlap of the ranges. False if there is no overlap.
/// </returns>
public static bool HasOverlap(bool TestEndPoints, params DateRange[] Ranges); 
/// <summary>
/// Tests for an overlap of given date ranges
/// </summary>
/// <param name="TestEndPoints">Set to true if you want to count the date range 
/// end points as an overlap and false if you don't care if the end points overlap.
/// </param>
/// <param name="Ranges">The set of date ranges that you want to test.</param>
/// <returns>A list of pairs of date ranges that overlap each other. 
/// The list will be empty if there is no overlaps.</returns>
public static IEnumerable<OverlapPairs> OverlapingRanges
	(bool TestEndPoints, params DateRange[] Ranges);

The code snippet below will return true.

DateRange.DateRange.HasOverlap(false, 
	new DateRange.DateRange(DateTime.Now,DateTime.Now.AddDays(1)), 
	new DateRange.DateRange(DateTime.Now.AddDays(-1),DateTime.Now.AddDays(2)));

Whereas this code snippet will return a list containing one Overlap Pairs of the first two date ranges. The third date range does not conflict at all.

DateRange.DateRange.OverlapingRanges(false, 
	new DateRange.DateRange(DateTime.Now,DateTime.Now.AddDays(1)), 
	new DateRange.DateRange(DateTime.Now.AddDays(-1),DateTime.Now.AddDays(2)),
	new DateRange.DateRange(DateTime.Now.AddDays(-5),DateTime.Now.AddDays(-3)));

History

  • 28th July, 2011 - Original version posted
  • 2nd August, 2011 - Updated code to account for whitespace in more than 2 date ranges 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mathew Crothers

Software Developer (Senior)
Purtill Bros Nominees Pty Ltd
Australia Australia

Member

Several years experience with a number of different languages including C/C++, Java, PHP, MS SQL, MySQL, VB.NET, C#.NET.
 
I have worked with Windows from 3.1 to 7, and NT 4 to Server 2008. I also have professional experience with Linux, mainly as Samba servers and minor experience with Asterisk phone system.
 
Most recently developing winforms programs for the current company I work for which operates in the transport industry.
 
In the process of becoming a CPA as well.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberArlen Navasartian15:03 26 Apr '12  
SuggestionA naive suggestion [modified] Pinmemberdproteau7:27 2 Aug '11  
GeneralMy vote of 1 PinmemberPaulLinton13:49 1 Aug '11  
QuestionDoesn't work PinmemberPaulLinton13:32 1 Aug '11  
AnswerRe: Doesn't work PinmemberMathew Crothers18:00 1 Aug '11  
SuggestionSimple approach PinmemberMember 38608368:03 1 Aug '11  
GeneralRe: Simple approach PinmemberMathew Crothers12:31 1 Aug '11  
GeneralRe: Simple approach Pinmemberpépé5:04 2 Aug '11  
SuggestionAlternate Approach: Timeline PinmemberJani Giannoudis0:26 29 Jul '11  
SuggestionSuggestion Pinmemberpépé6:01 28 Jul '11  
GeneralRe: Suggestion PinmemberMathew Crothers12:37 28 Jul '11  
GeneralRe: Suggestion Pinmemberpépé21:40 28 Jul '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Aug 2011
Article Copyright 2011 by Mathew Crothers
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid