Skip to main content

Find Overlapping Date Ranges

I recently had to implement a "DateRange" validator to check for any overlaps in the date ranges.

Here is my approach to it :

I started with a custom DateRange class.
public class DateRange
public int SortOrder { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }

public DateRange() {}

public DateRange(DateTime sTime, DateTime eTime)
Start = sTime;
End = eTime;

Then I call the "HasOverlap" method on my validator to check for overlaps.
* 1. Take a List of date ranges. Example:
[Tuple A] -------- [Sort Order 1]
[Tuple B]-------- [Sort Order 2]
[Tuple C] -------- [Sort Order 3]
[Tuple D] -------- [Sort Order 4]
[Tuple E] -------- [Sort Order 5]
[Tuple F] ---------- [Sort Order 6]

* 2. Now sort the range by start + End Dates. This results in:
[Tuple B]-------- [Sort Order 1]
[Tuple C] -------- [Sort Order 2]
[Tuple D] -------- [Sort Order 3]
[Tuple F] ---------- [Sort Order 4]
[Tuple A] -------- [Sort Order 5]
[Tuple E] -------- [Sort Order 6]

* 3. The Logic is that there will be an overlap IF EVEN ONE of
* the 2 base conditions are met:
* (a) After sorting the list, a given tuple (TUPLE X)
* will be deemed overlapping
* of ANY tuple WHERE the sort order is GREATER THAN that of (TUPLE X)
* --- OR ---
* (b) After sorting the list, a given tuple (TUPLE X)
* will be deemed overlapping
* the START-DATE and END-DATE of ANY tuple
* WHERE the sort order is GREATER THAN that of (TUPLE X)
private bool HasOverlap(IList<DateRange> ranges)
// 1. Sort Dates based on Start & End Dates
var sortedRange = ranges
.OrderBy(p => p.Start)
.ThenBy(p => p.End)

var sortCounter = 0;
sortedRange.ForEach(e =>
e.SortOrder = sortCounter;

// 2. Check if the end dates are > any start dates except the same one
return sortedRange
.Any(tuple => (from innerLoop in sortedRange
where (
tuple.End > innerLoop.Start &&
innerLoop.SortOrder > tuple.SortOrder
) ||
tuple.End == innerLoop.End &&
tuple.Start == innerLoop.Start &&
innerLoop.SortOrder > tuple.SortOrder
select innerLoop).Any());

Additionally I also pass my DateRanges through a sanity check to ensure that the start < end dates.


Popular posts from this blog

Internet Information Services(IIS) reveals its real or internal IP Address

In the ever changing world of global data communications, inexpensive Internet connections, and fast-paced software development, security is becoming more and more of an issue. Security is now a basic requirement because global computing is inherently insecure.

Keeping that in mind, we recently ran our flagship product through a security audit. It was such a helpful exercise in tying-off any remaining lose ends in our application in terms of application security. 
Based on the security audit report, there was a relatively minor issue that appeared when accessing the /images directory of our application. Turns out that the Location response header of the 301 request returns an Internal IP address. The issue is detailed below.

Issue reportedInternet Information Services (IIS) may reveal its real or internal IP address in the Location header via a request to the /images directory. The value returned whilst pen testing is

The riskInformation regarding internal IP add…

C# Console app that displays twitter feed using Linq To Twitter (using Single User Authorization)

I recently had to add a twitter feed to my existing ASP.NET MVC 4 application. All I had to do was pull the last 10 tweets for a given user. It took me a while (shamefully, 3 hours) to get it working so I thought of writing a simple tutorial that explains how to pull a twitter feed for a console app using LINQ to Twitter.

LINQ to Twitter is an open source 3rd party LINQ Provider for the Twitter micro-blogging service. It uses standard LINQ syntax for queries and includes method calls for changes via the Twitter API
What took me long to figure out was the way twitter has implemented authentication using OAuth. Before you do anything, make sure you read the Learning to use OAuth document.
In my example, I used Single User Authorization. Single User Authorization is designed for scenarios where you'll only ever have one account accessing Twitter. i.e. if your Web site does periodic Twitter updates, regardless of user or you have a server that monitors general information. 
Before we be…

Unit Testing HttpContext.Current.Session in MVC3 .NET

We recently changed some functionality where during the "CREATE" process, we go through a wizard to save application data. This data is saved only to the session in the final step when the user clicks the final submit.

This was easy enough to implement but when I started writing unit tests for my static methods that Add, Update, Delete or Modify the contents of our application data in the session, I got the following error:
System.NullReferenceException: Object reference not set to an instance of an object.

Turns out I had forgotten to setup the HttpContext.
The following "TestInitialise" method fixed my problem :)

public void TestSetup()
// We need to setup the Current HTTP Context as follows:

// Step 1: Setup the HTTP Request
var httpRequest = new HttpRequest("", "http://localhost/", "");

// Step 2: Setup the HTTP Response
var httpResponce = new HttpResponse(new StringWriter());

// Step 3: Se…