Exemplo n.º 1
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++)
            {
                var dayOfWeek = nextDate.DayOfWeek;

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

                var businessDays = tree[dayOfWeek];
                if (businessDays == null)
                {
                    continue;
                }

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

                    var 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.SafeAdd(day.StartTime);

                    return(true);
                }

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

            // should never reach here
            return(false);
        }
        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.SafeAdd(day.StartTime);

                    return true;
                }

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

            // should never reach here
            return false;
        }