Example #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));
        }
Example #2
0
        public void GetDateRange_Between_TwoInvalidDates(string startDate, string endDate)
        {
            //Given two dates with the startDate greater than the endDate
            var testStartDate = DateTime.ParseExact(startDate, "d/M/yyyy", CultureInfo.InvariantCulture);
            var testEndtDate  = DateTime.ParseExact(endDate, "d/M/yyyy", CultureInfo.InvariantCulture);

            //When I generate a range of dates
            Action act = () => DayCounterHelper.GetDateRangeBetweenTwoDates(testStartDate, testEndtDate);

            //Then I should not be able to generate a date range
            Assert.Throws <ArgumentException>(act);
        }
Example #3
0
        public void GetDateRange_Between_TwoValidDates(string startDate, string endDate, int expectedCount)
        {
            //Given two dates
            var testStartDate = DateTime.ParseExact(startDate, "d/M/yyyy", CultureInfo.InvariantCulture);
            var testEndDate   = DateTime.ParseExact(endDate, "d/M/yyyy", CultureInfo.InvariantCulture);

            //When I generate a range of dates between the two dates
            var actualCount = DayCounterHelper.GetDateRangeBetweenTwoDates(testStartDate, testEndDate).Count();

            //Then the count should exclude the two dates
            Assert.Equal(expectedCount, actualCount);
        }
Example #4
0
        /// <summary>
        /// Caculates the number of weekdays between two dates
        /// </summary>
        public static int WeekdaysBetweenTwoDates(DateTime firstDate, DateTime secondDate)
        {
            //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 weekday "Monday, Tuesday, Wednesday, Thursday, Friday"
            var weekdayCount = DayCounterHelper.GetWeekdays(dateRange).Count();

            //Return the count
            return(weekdayCount);
        }