public override DateTime GetNextOccurrence()

in src/WebJobs.Extensions/Extensions/Timers/Scheduling/WeeklySchedule.cs [46:88]


        public override DateTime GetNextOccurrence(DateTime now)
        {
            if (schedule.All(p => p == null))
            {
                throw new InvalidOperationException("The schedule is empty.");
            }

            // Determine where we are in the weekly schedule
            int day = (int)now.DayOfWeek;
            List<TimeSpan> daySchedule = schedule[day];
            TimeSpan nextTime = default(TimeSpan);
            int nextTimeIndex = -1;
            if (daySchedule != null)
            {
                // We have a schedule for today. Determine the next time
                // where the time is strictly greater than the current time
                nextTimeIndex = daySchedule.FindIndex(p => p.TotalMilliseconds > now.TimeOfDay.TotalMilliseconds);
                if (nextTimeIndex != -1)
                {
                    nextTime = daySchedule[nextTimeIndex];
                }
            }

            // if we don't have a schedule for the current day,
            // or if we've already executed all occurrences for
            // today, advance to the next day with a schedule
            if (daySchedule == null || nextTimeIndex == -1)
            {
                while ((daySchedule = schedule[++day % 7]) == null)
                {
                }

                // select the first time for the schedule
                nextTime = daySchedule[0];
            }

            // construct the next occurrence date
            int deltaDays = day - (int)now.DayOfWeek;
            DateTime nextOccurrence = new DateTime(now.Year, now.Month, now.Day, nextTime.Hours, nextTime.Minutes, nextTime.Seconds, now.Kind);
            nextOccurrence = nextOccurrence.AddDays(deltaDays);

            return nextOccurrence;
        }