internal bool NextBusinessDay(DateTime startDate, out DateTime nextDate, out BusinessDay businessDay)
        {
            nextDate = startDate;
            businessDay = null;

            var tree = GetDayTree();

            // loop no more then 7 times
            for (int x = 0; x < 7; x++)
            {
                DayOfWeek dayOfWeek = nextDate.DayOfWeek;

                if (!tree.ContainsKey(dayOfWeek))
                {
                    // no business days on this day of the week
                    nextDate = nextDate.AddDays(1).Date;
                    continue;
                }

                IList<BusinessDay> businessDays = tree[dayOfWeek];
                if (businessDays == null)
                    continue;

                foreach (BusinessDay day in businessDays)
                {
                    if (day == null)
                        continue;

                    TimeSpan timeOfDay = nextDate.TimeOfDay;

                    if (timeOfDay >= day.StartTime && timeOfDay < day.EndTime)
                    {
                        // working date in range
                        businessDay = day;
                        return true;
                    }

                    // past this business day, try other for this day
                    if (timeOfDay >= day.StartTime)
                        continue;

                    // move to start time.
                    businessDay = day;
                    nextDate = nextDate.Date.Add(day.StartTime);

                    return true;
                }

                // next day
                nextDate = nextDate.AddDays(1).Date;
            }

            // should never reach here
            return false;
        }
Beispiel #2
0
        internal bool NextBusinessDay(DateTime startDate, out DateTime nextDate, out BusinessDay businessDay)
        {
            nextDate    = startDate;
            businessDay = null;

            var tree = GetDayTree();

            // loop no more then 7 times
            for (int x = 0; x < 7; x++)
            {
                DayOfWeek dayOfWeek = nextDate.DayOfWeek;

                if (!tree.ContainsKey(dayOfWeek))
                {
                    // no business days on this day of the week
                    nextDate = nextDate.AddDays(1).Date;
                    continue;
                }

                IList <BusinessDay> businessDays = tree[dayOfWeek];
                if (businessDays == null)
                {
                    continue;
                }

                foreach (BusinessDay day in businessDays)
                {
                    if (day == null)
                    {
                        continue;
                    }

                    TimeSpan timeOfDay = nextDate.TimeOfDay;

                    if (timeOfDay >= day.StartTime && timeOfDay < day.EndTime)
                    {
                        // working date in range
                        businessDay = day;
                        return(true);
                    }

                    // past this business day, try other for this day
                    if (timeOfDay >= day.StartTime)
                    {
                        continue;
                    }

                    // move to start time.
                    businessDay = day;
                    nextDate    = nextDate.Date.Add(day.StartTime);

                    return(true);
                }

                // next day
                nextDate = nextDate.AddDays(1).Date;
            }

            // should never reach here
            return(false);
        }