public static int CalculateYearsOfExperience(this CandidateSearchCv profile, DateTime utcNow)
{
IEnumerable<DateRange> MergeOverlaps(IEnumerable<DateRange> dateRanges)
{
// The idea is to find contiguous clusters of overlapping date ranges and merge them into a single range.
//
// 2009 2010 2011 2012 2013 2014 2015 2016
// |-----------| |-----------| |-----|
// |-----------------|
// |
// |
// V
// |-----------------------------| |-----|
//
// This implementation is probably far from optimal but I don't expect dateRanges to ever be large:
// even the most prolific Dev Story authors will only have a few dozen experience entries.
var result = new List<DateRange>();
foreach (var range in dateRanges)
{
var (overlapping, nonOverlapping) = result.Partition(range.Overlaps, (t, f) => (t.ToList(), f.ToList()));
overlapping.Add(range); // a range always overlaps itself
nonOverlapping.Add(new DateRange(overlapping.Min(r => r.Start), overlapping.Max(r => r.End)));
result = nonOverlapping;
}
return result;
}
var experienceDateRanges = profile
.Experience
.Where(e => e.ExperienceStartDate.HasValue) // if they didn't give a start date, we can't reasonably figure out when that was
.Select(e =>
{
var endDate = e.ExperienceEndDate ?? utcNow; // if they didn't give an end date, assume they still work there
var startDate = e.ExperienceStartDate.Value;
// sometimes people have swapped date ranges in their dev story for some reason
return new DateRange(DateExtensions.Min(startDate, endDate), DateExtensions.Max(endDate, startDate));
});
var actualExperienceTime = MergeOverlaps(experienceDateRanges).Select(r => r.Length).Sum();
// I'd like to write TimeSpan.TotalYears, but it doesn't exist so I have to do this instead https://stackoverflow.com/a/4127396/1523776
var zeroTime = new DateTime(1, 1, 1);
return (zeroTime + actualExperienceTime).RoundToYear().Year - 1;
}
public static int CalculateYearsOfExperience(this CandidateSearchCv profile, DateTime utcNow)
{
IEnumerable<DateRange> MergeOverlaps(IEnumerable<DateRange> dateRanges)
{
// The idea is to find contiguous clusters of overlapping date ranges and merge them into a single range.
//
// 2009 2010 2011 2012 2013 2014 2015 2016
// |-----------| |-----------| |-----|
// |-----------------|
// |
// |
// V
// |-----------------------------| |-----|
//
// This implementation is probably far from optimal but I don't expect dateRanges to ever be large:
// even the most prolific Dev Story authors will only have a few dozen experience entries.
var result = new List<DateRange>();
foreach (var range in dateRanges)
{
var (overlapping, nonOverlapping) = result.Partition(range.Overlaps, (t, f) => (t.ToList(), f.ToList()));
overlapping.Add(range); // a range always overlaps itself
nonOverlapping.Add(new DateRange(overlapping.Min(r => r.Start), overlapping.Max(r => r.End)));
result = nonOverlapping;
}
return result;
}
var experienceDateRanges = profile
.Experience
.Where(e => e.ExperienceStartDate.HasValue) // if they didn't give a start date, we can't reasonably figure out when that was
.Select(e =>
{
var endDate = e.ExperienceEndDate ?? utcNow; // if they didn't give an end date, assume they still work there
var startDate = e.ExperienceStartDate.Value;
// sometimes people have swapped date ranges in their dev story for some reason
return new DateRange(DateExtensions.Min(startDate, endDate), DateExtensions.Max(endDate, startDate));
});
var actualExperienceTime = MergeOverlaps(experienceDateRanges).Select(r => r.Length).Sum();
// I'd like to write TimeSpan.TotalYears, but it doesn't exist so I have to do this instead https://stackoverflow.com/a/4127396/1523776
var zeroTime = new DateTime(1, 1, 1);
return (zeroTime + actualExperienceTime).RoundToYear().Year - 1;
}