Skip to content

Instantly share code, notes, and snippets.

@sandeeptalabathula
Last active October 14, 2022 15:06
Show Gist options
  • Save sandeeptalabathula/3b469065251eb1992dd92b168fea0b61 to your computer and use it in GitHub Desktop.
Save sandeeptalabathula/3b469065251eb1992dd92b168fea0b61 to your computer and use it in GitHub Desktop.
Determine Whether Two Date Ranges Overlap Tests
namespace ConsoleApplication
{
using System;
class Program
{
/// <param name="args"></param>
static void Main(string[] args)
{
bool isoverlap;
string testResult;
DateTime? A = new DateTime(2017, 10, 1);
DateTime? B = new DateTime(2017, 10, 10);
DateTime? C = null;
DateTime? D = null;
//#1
Console.WriteLine("SCENARIO#1:");
Console.WriteLine(" A |---------------------| B");
Console.WriteLine(" C |--------| D");
C = A.Value.AddDays(-2);
D = A.Value.AddDays(2);
Console.ForegroundColor = ConsoleColor.Yellow;
isoverlap = isoverlapping(A, B, C, D);
testResult = (isoverlap == true) ? "PASSED" : "FAILED";
Console.WriteLine("OVERLAP?: {0}", isoverlap.ToString());
Console.ForegroundColor = (testResult == "PASSED") ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("TEST RESULT: {0}", testResult);
Console.ResetColor();
//#2
Console.WriteLine("SCENARIO#2:");
Console.WriteLine(" A |---------------------| B");
Console.WriteLine(" C |--------| D");
C = A.Value.AddDays(2);
D = B.Value.AddDays(2);
Console.ForegroundColor = ConsoleColor.Yellow;
isoverlap = isoverlapping(A, B, C, D);
testResult = (isoverlap == true) ? "PASSED" : "FAILED";
Console.WriteLine("OVERLAP?: {0}", isoverlap.ToString());
Console.ForegroundColor = (testResult == "PASSED") ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("TEST RESULT: {0}", testResult);
Console.ResetColor();
//#3
Console.WriteLine("SCENARIO#3:");
Console.WriteLine(" A |---------------------| B");
Console.WriteLine(" C |---------------------| D");
C = A;
D = B;
Console.ForegroundColor = ConsoleColor.Yellow;
isoverlap = isoverlapping(A, B, C, D);
testResult = (isoverlap == true) ? "PASSED" : "FAILED";
Console.WriteLine("OVERLAP?: {0}", isoverlap.ToString());
Console.ForegroundColor = (testResult == "PASSED") ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("TEST RESULT: {0}", testResult);
Console.ResetColor();
//#4
Console.WriteLine("SCENARIO#4:");
Console.WriteLine(" A |---------------------| B");
Console.WriteLine(" C |--------| D");
C = A.Value.AddDays(2);
D = B.Value.AddDays(-2);
Console.ForegroundColor = ConsoleColor.Yellow;
isoverlap = isoverlapping(A, B, C, D);
testResult = (isoverlap == true) ? "PASSED" : "FAILED";
Console.WriteLine("OVERLAP?: {0}", isoverlap.ToString());
Console.ForegroundColor = (testResult == "PASSED") ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("TEST RESULT: {0}", testResult);
Console.ResetColor();
//#5
Console.WriteLine("SCENARIO#5:");
Console.WriteLine(" A |---------------------| B");
Console.WriteLine(" C |--------| D");
C = A.Value.AddDays(-2);
D = A.Value.AddDays(-1);
Console.ForegroundColor = ConsoleColor.Yellow;
isoverlap = isoverlapping(A, B, C, D);
testResult = (isoverlap == false) ? "PASSED" : "FAILED";
Console.WriteLine("OVERLAP?: {0}", isoverlap.ToString());
Console.ForegroundColor = (testResult == "PASSED") ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("TEST RESULT: {0}", testResult);
Console.ResetColor();
//#6
Console.WriteLine("SCENARIO#6:");
Console.WriteLine(" A |---------------------| B");
Console.WriteLine(" C |--------| D");
C = B.Value.AddDays(2);
D = B.Value.AddDays(3);
Console.ForegroundColor = ConsoleColor.Yellow;
isoverlap = isoverlapping(A, B, C, D);
testResult = (isoverlap == false) ? "PASSED" : "FAILED";
Console.WriteLine("OVERLAP?: {0}", isoverlap.ToString());
Console.ForegroundColor = (testResult == "PASSED") ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("TEST RESULT: {0}", testResult);
Console.ResetColor();
//#7
A = new DateTime(2017, 10, 1);
B = new DateTime(2017, 10, 1);
C = A;
D = B;
Console.WriteLine("SCENARIO#7: (Same Day for both sets)");
Console.WriteLine(" A || B");
Console.WriteLine(" C || D");
C = B;
D = B;
Console.ForegroundColor = ConsoleColor.Yellow;
isoverlap = isoverlapping(A, B, C, D);
testResult = (isoverlap == true) ? "PASSED" : "FAILED";
Console.WriteLine("OVERLAP?: {0}", isoverlap.ToString());
Console.ForegroundColor = (testResult == "PASSED") ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("TEST RESULT: {0}", testResult);
Console.ResetColor();
Console.ReadKey();
}
static bool isoverlapping(DateTime? A, DateTime? B, DateTime? C, DateTime? D)
{
//return ((A.HasValue ? (A < D) : true) && (B.HasValue ? (C < B) : true));
//return !((B.HasValue ? (B <= C) : true) || (A.HasValue ? (A >= D) : true));
return ((A == null || D == null || A <= D)
&& (C == null || B == null || C <= B)
&& (A == null || B == null || A <= B)
&& (C == null || D == null || C <= D)); //or: WORKS!!!
//(A <= D) && (A <= B) && (C <= B) && (C <= D) or,
//(A <= Min(B, D) && (C <= Min(B, D)) or:
//(Max(A, C) <= Min(B, D));
}
}
}
@sharad-saurav
Copy link

what about d is null and c is smaller than a, getting wrong result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment