Created
December 6, 2012 21:02
-
-
Save bsommardahl/4228366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BusinessDayCalculator : IBusinessDayCalculator | |
{ | |
private readonly IHolidayProvider _holidayProvider; | |
public BusinessDayCalculator(IHolidayProvider holidayProvider) | |
{ | |
_holidayProvider = holidayProvider; | |
} | |
public DateTime AddBusinessDays(DateTime startDate, int businessDays) | |
{ | |
var holidays = _holidayProvider.GetHolidays(startDate) ?? new List<Holiday>(); | |
var listOfBusinessDays = new List<DateTime>(); | |
var currentBusinessDayNumber = 1; | |
var currentDayNumber = 1; | |
while (currentBusinessDayNumber <= Math.Abs(businessDays)) | |
{ | |
var dateCandidate = startDate.AddDays(currentDayNumber * Math.Sign(businessDays)); | |
var notWeekend = dateCandidate.DayOfWeek != DayOfWeek.Saturday | |
&& dateCandidate.DayOfWeek != DayOfWeek.Sunday; | |
var notHoliday = !holidays.Any(x => x.Date == dateCandidate); | |
if (notWeekend && notHoliday) | |
{ | |
currentBusinessDayNumber++; | |
listOfBusinessDays.Add(dateCandidate); | |
} | |
currentDayNumber++; | |
} | |
return listOfBusinessDays.Last(); | |
} | |
} | |
public interface IHolidayProvider | |
{ | |
List<Holiday> GetHolidays(DateTime startDate); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment