Skip to content

Instantly share code, notes, and snippets.

@mhafalir
Created December 14, 2014 02:28
Show Gist options
  • Select an option

  • Save mhafalir/0ee637fedec5df269728 to your computer and use it in GitHub Desktop.

Select an option

Save mhafalir/0ee637fedec5df269728 to your computer and use it in GitHub Desktop.
Find DateTime Gaps
using System;
using System.Collections.Generic;
using System.Linq;
class Toolkit
{
public class DateTimeInterval
{
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
}
public static List<DateTimeInterval> FindGaps<T>(DateTimeInterval baseInterval, IEnumerable<T> list,
Func<T, DateTime> beginDateKeySelector, Func<T, DateTime> endDateKeySelector)
{
List<DateTimeInterval> result = new List<DateTimeInterval>();
List<T> tempList = list.ToList();
T currentItem, nextItem;
DateTime dateMin = baseInterval.BeginDate;
DateTime dateMax = baseInterval.EndDate;
if (dateMin >= dateMax)
return result;
if (tempList == null || tempList.Count() == 0)
{
result.Add(new DateTimeInterval() { BeginDate = dateMin, EndDate = dateMax });
return result;
}
currentItem = tempList.OrderBy(o => beginDateKeySelector(o)).FirstOrDefault();
tempList.RemoveAll(o => endDateKeySelector(o) <= endDateKeySelector(currentItem));
if (beginDateKeySelector(currentItem) > dateMin)
{
result.Add(new DateTimeInterval()
{
BeginDate = dateMin,
EndDate = beginDateKeySelector(currentItem)
}
);
}
while (true)
{
nextItem = tempList.
Where(o => beginDateKeySelector(o) <= endDateKeySelector(currentItem)).
OrderByDescending(o => endDateKeySelector(o)).FirstOrDefault();
if (nextItem == null)
{
nextItem = tempList.OrderBy(o => beginDateKeySelector(o)).FirstOrDefault();
if (nextItem == null)
{
if (endDateKeySelector(currentItem) < dateMax)
{
result.Add(new DateTimeInterval()
{
BeginDate = endDateKeySelector(currentItem),
EndDate = dateMax
}
);
}
break;
}
else
{
tempList.RemoveAll(o => endDateKeySelector(o) <= endDateKeySelector(nextItem));
result.Add(new DateTimeInterval()
{
BeginDate = endDateKeySelector(currentItem),
EndDate = beginDateKeySelector(nextItem)
}
);
currentItem = nextItem;
}
}
else
{
tempList.RemoveAll(o => endDateKeySelector(o) <= endDateKeySelector(nextItem));
currentItem = nextItem;
}
}
return result;
}
}
using System;
using System.Collections.Generic;
class MyIntervalClass
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime Finish { get; set; }
}
class IntervalRepo
{
public static List<MyIntervalClass> GetIntervals()
{
List<MyIntervalClass> result = new List<MyIntervalClass>();
// Add some MyIntervalClass instances to the list
return result;
}
}
class Program
{
static void Main(string[] args)
{
var list = IntervalRepo.GetIntervals();
var baseInterval = new Toolkit.DateTimeInterval() { BeginDate = DateTime.Today, EndDate = DateTime.Now };
var gapList = Toolkit.FindGaps<MyIntervalClass>(baseInterval, list, o => o.Start, o => o.Finish);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment