예제 #1
0
        /// <summary>
        /// Caculates the number of business days between two dates given a list of public holidays
        /// </summary>
        public static double BusinessDaysBetweenTwoDates(DateTime firstDate, DateTime secondDate, IList <DateTime> publicHolidays)
        {
            //Compare supplied dates
            if (ValidateInputDates(firstDate, secondDate))
            {
                return(0);
            }

            //Get the date range between the two dates excluding firstDate and secondDate
            var dateRange = DayCounterHelper.GetDateRangeBetweenTwoDates(firstDate, secondDate);

            //Count the number of dates that are a business day
            //A business day is a week day that is either a full day or a half day
            //A public holiday is either defined as a full public holiday or a half public holiday

            //When comparing Hour, it uses the 24 format therefore it is safe to assume that 12 means 12pm otherwise it would be 0
            //A half public holidays is defined as a date with the time set to 12PM
            var halfDayPublicHolidays = publicHolidays.Where(d => d.Hour == 12);
            var fullDayPublicHolidays = publicHolidays.Where(d => d.Hour != 12);


            //First get the true full business days (a half day is not a full business day, so we need to pass the half day list)
            var fullBusinessDays = DayCounterHelper.GetFullBusinessDays(dateRange, fullDayPublicHolidays, halfDayPublicHolidays);

            //Then get the true half business days
            var halfBusinessDays = DayCounterHelper.GetHalfBusinessDays(dateRange, fullDayPublicHolidays, halfDayPublicHolidays);

            //Return the count
            return((double)fullBusinessDays.Count() + ((double)halfBusinessDays.Count() / 2));
        }