Beispiel #1
0
        // This is to test rule if same start date and end date are provided code doesn't raise any exceptions
        // and returns valid business days which should be 0.
        public void Test_Same_Start_End_Date()
        {
            var r1 = _calculator.WeekdaysBetweenTwoDates(new DateTime(2013, 10, 7), new DateTime(2013, 10, 7));

            Assert.AreEqual(r1.Item1, 0);

            var publicHolidats = new List <DateTime>();

            publicHolidats.Add(new DateTime(2013, 12, 25));
            publicHolidats.Add(new DateTime(2013, 12, 26));
            publicHolidats.Add(new DateTime(2014, 1, 1));
            var r2 = _calculator.BusinessDaysBetweenTwoDates(new DateTime(2013, 10, 7), new DateTime(2013, 10, 7), publicHolidats);

            Assert.AreEqual(r2.Item1, 0);


            HolidayRule holidayRule = new HolidayRule();

            holidayRule.Always_Same_Day.Add(new Always_Same_Day()
            {
                Name = "Anzac Day", Day = 25, Month = 4
            });
            var result = _calculator.BusinessDaysBetweenTwoDates(new DateTime(2019, 4, 1), new DateTime(2019, 4, 1), holidayRule);

            Assert.AreEqual(result.Item1, 0);
        }
Beispiel #2
0
        // This is to test business days calculation with rule given occurrence of given week day for a given month.
        public void Test_Calculate_Business_Days_Rules_Nth_Occurance_Given_Month_Given_Week()
        {
            HolidayRule holidayRule = new HolidayRule();

            holidayRule.Nth_Occurance_Given_Month_Given_Week.Add(new Nth_Occurrence_Given_Month_Given_Week()
            {
                Name = "Queens Birthday", DayOfWeek = Convert.ToInt32(DayOfWeek.Monday), Month = 6, WeekOfMonth = 2
            });

            var result = _calculator.BusinessDaysBetweenTwoDates(new DateTime(2020, 6, 1), new DateTime(2020, 6, 19), holidayRule);

            Assert.AreEqual(12, result.Item1);
        }
Beispiel #3
0
        // This is to test configuration is valid for rule given occurrence of given week day for a given month.
        public void Test_Calculate_Business_Days_Rules_Alway_On_Same_Day()
        {
            HolidayRule holidayRule = new HolidayRule();

            holidayRule.Always_Same_Day.Add(new Always_Same_Day()
            {
                Name = "Anzac Day", Day = 25, Month = 4
            });

            var result = _calculator.BusinessDaysBetweenTwoDates(new DateTime(2019, 4, 1), new DateTime(2019, 4, 30), holidayRule);

            Assert.AreEqual(19, result.Item1);
        }
Beispiel #4
0
        // This is to test business days calculation with rule always on same day except weekend, when next working day should be considered.
        public void Test_Calculate_Business_Days_Rules_Always_On_Same_Day_Except_Weekend()
        {
            HolidayRule holidayRule = new HolidayRule();

            holidayRule.Always_On_Same_Day_Except_Weekend.Add(new Always_On_Same_Day_Except_Weekend()
            {
                Name = "New Year Day", Day = 1, Month = 1
            });

            var result = _calculator.BusinessDaysBetweenTwoDates(new DateTime(2017, 12, 30), new DateTime(2018, 1, 5), holidayRule);

            Assert.AreEqual(3, result.Item1);
        }
        // This is to test configuration is valid for rule always on same day except weekend.
        public void Test_Validate_Business_Days_Rules_Nth_Occurance_Given_Month_Given_Week()
        {
            HolidayRule holidayRule = new HolidayRule();

            holidayRule.Nth_Occurance_Given_Month_Given_Week.Add(new Nth_Occurrence_Given_Month_Given_Week()
            {
                Name = "Queens Birthday", DayOfWeek = 12, WeekOfMonth = 60
            });

            var result = _calculator.BusinessDaysBetweenTwoDates(new DateTime(2019, 4, 1), new DateTime(2019, 4, 30), holidayRule);

            Assert.NotNull(result.Item2);
        }
        // This is to test configuration is valid for rule always on same day except weekend.
        public void Test_Validate_Business_Days_Rules_Always_On_Same_Day_Except_Weekend()
        {
            HolidayRule holidayRule = new HolidayRule();

            holidayRule.Always_On_Same_Day_Except_Weekend.Add(new Always_On_Same_Day_Except_Weekend()
            {
                Name = "New Year Day", Day = 40, Month = 40
            });

            var result = _calculator.BusinessDaysBetweenTwoDates(new DateTime(2019, 4, 1), new DateTime(2019, 4, 30), holidayRule);

            Assert.NotNull(result.Item2);
        }
Beispiel #7
0
        public bool IsHoliday(DateTime dateTime, HolidayRule rule)
        {
            bool isHoliday = false;

            DateTime holiday = new DateTime(dateTime.Year, rule.MonthOfYear, rule.DateOfMonth);

            if (rule.IsNextMonday && holiday.IsWeekend())
            {
                holiday = holiday.NextWorkingDate();
            }
            if (dateTime == holiday)
            {
                isHoliday = true;
            }
            return(isHoliday);
        }
        /// <summary>
        /// This method checks if validation rules have been set properly for all rules if not returns reason explaining what has not been set.
        /// </summary>
        /// <param name="holidayRule">List of rules to be validated.</param>
        /// <returns>Text explaining what has not been set properly for which rule. If all configuration valid returns null </returns>
        public string ValidateHolidayRule(HolidayRule holidayRule)
        {
            string validationMessage                  = null;
            var    ruleConfigurationValid             = true;
            HolidayRuleValidator holidayRuleValidator = new HolidayRuleValidator();

            // Check if configuration valid for Always_Same_Day
            if (holidayRule.Always_Same_Day != null)
            {
                validationMessage = holidayRuleValidator.Validate_Rule_Always_Same_Day <Always_Same_Day>(holidayRule.Always_Same_Day);

                if (validationMessage != null)
                {
                    ruleConfigurationValid = false;
                }
            }

            // Check if configuration is valid for Always_On_Same_Day_Except_Weekend
            if (ruleConfigurationValid && holidayRule.Always_On_Same_Day_Except_Weekend != null)
            {
                validationMessage = holidayRuleValidator.Validate_Rule_Always_Same_Day <Always_On_Same_Day_Except_Weekend>(holidayRule.Always_On_Same_Day_Except_Weekend);

                if (validationMessage != null)
                {
                    ruleConfigurationValid = false;
                }
            }

            // Check if configuration is valid for Always_On_Same_Day_Except_Weekend
            if (ruleConfigurationValid && holidayRule.Nth_Occurance_Given_Month_Given_Week != null)
            {
                validationMessage = holidayRuleValidator.Validate_Rule_Nth_Occurance_Given_Month_Given_Week(holidayRule.Nth_Occurance_Given_Month_Given_Week);
            }

            return(validationMessage);
        }
Beispiel #9
0
 public void AddHolidayRules(HolidayRule dayOfWeekHolidayRule)
 {
     _holidayRules.Add(dayOfWeekHolidayRule);
 }
        /// <summary>
        /// This takes two dates, applies rules specified for holidays and returns business days between two dates(exclusive)
        /// </summary>
        /// <param name="firstDate"> date to start counting business day from (exclusive)</param>
        /// <param name="secondDate">date to stop counting business day to (exclusive) </param>
        /// <param name="holidayRule">list of rules specifying holidays</param>
        /// <returns> tuple containing no of Business days between two dates and validation message if validation failed </returns>
        public Tuple <int, string> BusinessDaysBetweenTwoDates(DateTime firstDate, DateTime secondDate, HolidayRule holidayRule)
        {
            List <DateTime> resultDateList          = new List <DateTime>();
            List <DateTime> holidayOnWeekEndList    = new List <DateTime>();
            List <DateTime> dateRange               = new List <DateTime>();
            List <DateTime> selectedDates_Week_Days = new List <DateTime>();
            List <DateTime> selectedDates_WeekEnds  = new List <DateTime>();

            try
            {
                string validationMessage    = null;
                var    holidayRuleValidator = new HolidayRuleValidator();

                if (holidayRule != null)
                {
                    validationMessage = holidayRuleValidator.ValidateHolidayRule(holidayRule);

                    // If rules configuration is valid allow to proceed further.
                    if (validationMessage == null)
                    {
                        var DateHelper = new DateHelper();

                        dateRange = DateHelper.GetSelectedDateRange(firstDate, secondDate);
                        selectedDates_Week_Days = DateHelper.GetWeekDays(dateRange);
                        selectedDates_WeekEnds  = DateHelper.GetWeekEnds(dateRange);

                        // Find holidays which are always on same day and falls on week days.
                        foreach (Always_Same_Day always_Same_Day_Rule in holidayRule.Always_Same_Day)
                        {
                            resultDateList.AddRange(selectedDates_Week_Days.ToList().FindAll(d => d.Day == always_Same_Day_Rule.Day &&
                                                                                             d.Month == always_Same_Day_Rule.Month &&
                                                                                             !resultDateList.Any(rs => rs == d)));
                        }

                        // Find holidays which are always on same day except on weekend.
                        foreach (Always_On_Same_Day_Except_Weekend always_On_Same_Day_Except_Weekend in holidayRule.Always_On_Same_Day_Except_Weekend)
                        {
                            // Always on same day except on weekend, which are not on weekends
                            resultDateList.AddRange(selectedDates_Week_Days.ToList().FindAll(d => d.Day == always_On_Same_Day_Except_Weekend.Day &&
                                                                                             d.Month == always_On_Same_Day_Except_Weekend.Month &&
                                                                                             !resultDateList.Any(rs => rs == d)));

                            // So we are only interested in weekends which also are public holidays and are part of Always_On_Same_Day_Except_Weekend
                            holidayOnWeekEndList = selectedDates_WeekEnds.ToList().FindAll(d => d.Day == always_On_Same_Day_Except_Weekend.Day &&
                                                                                           d.Month == always_On_Same_Day_Except_Weekend.Month);
                        }

                        // Find holidays which falls on Nth occurrence of given week day in given month.
                        foreach (Nth_Occurrence_Given_Month_Given_Week objRule in holidayRule.Nth_Occurance_Given_Month_Given_Week)
                        {
                            // We only need to consider week day as week end has already been taken care of.
                            resultDateList.AddRange(selectedDates_Week_Days.ToList().FindAll(d => d.Month == objRule.Month &&
                                                                                             Convert.ToInt32(d.DayOfWeek) == objRule.DayOfWeek &&
                                                                                             d.Date == DateHelper.GetDayOfMonth(d.Date
                                                                                                                                , Convert.ToInt32(objRule.WeekOfMonth), Convert.ToInt32(objRule.DayOfWeek))));
                        }
                    }
                }

                return(Tuple.Create(selectedDates_Week_Days.Count - (resultDateList.Count + holidayOnWeekEndList.Count), validationMessage));
            }
            catch (Exception ex)
            {
                return(Tuple.Create(0, "A fatal error occurred while calculating business days"));
            }
            finally
            {
                resultDateList          = null;
                holidayOnWeekEndList    = null;
                dateRange               = null;
                selectedDates_Week_Days = null;
                selectedDates_WeekEnds  = null;
            }
        }
 public bool IsHoliday(DateTime dateTime, HolidayRule rule)
 {
     return(dateTime.DayOfWeek == rule.HolidayOfWeek && dateTime.Month == rule.MonthOfYear && dateTime.GetWeekNumberOfMonth() == rule.WeekOfMonth);
 }