public static IEnumerable <DateTime> GetNextRunTimes(TaskTimeTrigger trigger, DateTime baseRunTime)
        {
            var startRunTime = trigger.Start.GetValueOrDefault(DateTime.MinValue);
            var endRunTime   = trigger.End.GetValueOrDefault(DateTime.MaxValue);

            if (startRunTime > endRunTime)
            {
                throw new InvalidOperationException("Trigger start date-time must be earlier than the end date-time when both are specified");
            }

            var nextRunTime = baseRunTime;

            while (nextRunTime < endRunTime)
            {
                if (baseRunTime > startRunTime)
                {
                    nextRunTime = startRunTime;
                }
                else
                {
                    var repeatInterval = trigger.RepeatInterval.GetValueOrDefault(TimeSpan.MaxValue);
                }

                yield return(nextRunTime);
            }
        }
 public static IEnumerable <DateTime> GetNextRunTimes(TaskTimeTrigger trigger)
 {
     return(GetNextRunTime(trigger, DateTime.Now));
 }
        private static TaskBaseTrigger CreateTaskTrigger(Trigger trigger)
        {
            var taskTrigger = default(TaskBaseTrigger);

            switch (trigger.TriggerType)
            {
            case TaskTriggerType.Daily:
                var dailyTrigger = trigger as DailyTrigger;

                taskTrigger = new TaskDailyTrigger
                {
                    Interval = TimeSpan.FromDays(dailyTrigger.DaysInterval)
                };
                break;

            case TaskTriggerType.Monthly:
                var monthlyTrigger = trigger as MonthlyTrigger;

                taskTrigger = new TaskMonthlyTrigger
                {
                    DaysOfTheMonth = monthlyTrigger.RunOnLastDayOfMonth
              ? monthlyTrigger.DaysOfMonth.Append(LastDayOfTheMonth).ToArray()
              : monthlyTrigger.DaysOfMonth,
                    Months = GetMonthNumbers(monthlyTrigger.MonthsOfYear)
                };
                break;

            case TaskTriggerType.MonthlyDOW:
                var monthlyDOWTrigger = trigger as MonthlyDOWTrigger;

                taskTrigger = new TaskMonthlyTrigger
                {
                    DaysOfTheWeek   = GetDaysOfWeek(monthlyDOWTrigger.DaysOfWeek),
                    Months          = GetMonthNumbers(monthlyDOWTrigger.MonthsOfYear),
                    WeeksOfTheMonth = GetWeekNumbers(monthlyDOWTrigger.WeeksOfMonth)
                };
                break;

            case TaskTriggerType.Weekly:
                var weeklyTrigger = trigger as WeeklyTrigger;

                taskTrigger = new TaskWeeklyTrigger
                {
                    DaysOfTheWeek = GetDaysOfWeek(weeklyTrigger.DaysOfWeek),
                    Interval      = TimeSpan.FromDays(weeklyTrigger.WeeksInterval * DaysPerWeek)
                };
                break;

            case TaskTriggerType.Time:
                taskTrigger = new TaskTimeTrigger();
                break;

            default:
                return(null);
            }

            taskTrigger.IsActive = trigger.Enabled;

            if (trigger.EndBoundary != DateTime.MaxValue)
            {
                taskTrigger.End = trigger.EndBoundary;
            }

            if (trigger.StartBoundary != DateTime.MinValue)
            {
                taskTrigger.Start = trigger.StartBoundary;
            }

            if (trigger.Repetition != null && trigger.Repetition.IsSet())
            {
                if (trigger.Repetition.Interval != TimeSpan.Zero)
                {
                    taskTrigger.RepeatInterval = trigger.Repetition.Interval;
                }

                if (trigger.Repetition.Duration != TimeSpan.MaxValue)
                {
                    taskTrigger.RepeatInterval = trigger.Repetition.Duration;
                }
            }

            return(taskTrigger);
        }