Exemple #1
0
        /// <summary>
        /// Converts a schedule into a cron expression.
        /// </summary>
        /// <param name="schedule">Schedule to convert to a cron expression.</param>
        /// <returns>A cron expression of the schedule.</returns>
        public static string ToCronExpression(ScheduleBase schedule)
        {
            schedule.ThrowIfInvalid();

            var scheduleType = schedule.GetType();
            if (scheduleType == typeof(IntervalSchedule))
            {
                var intervalSchedule = (IntervalSchedule)schedule;
                if (intervalSchedule.Interval.TotalMinutes.Equals(1))
                {
                    return "* * * * *";
                }
                else
                {
                    return "*/" + intervalSchedule.Interval.TotalMinutes + " * * * *";
                }
            }
            else if (scheduleType == typeof(HourlySchedule))
            {
                var hourlySchedule = (HourlySchedule)schedule;
                return string.Format("{0} * * * *", hourlySchedule.Minute);
            }
            else if (scheduleType == typeof(DailyScheduleInUtc))
            {
                var dailySchedule = (DailyScheduleInUtc)schedule;
                return string.Format("{0} {1} * * *", dailySchedule.Minute, dailySchedule.Hour);
            }
            else if (scheduleType == typeof(WeeklyScheduleInUtc))
            {
                var weeklySchedule = (WeeklyScheduleInUtc)schedule;
                var daysOfWeekString = string.Join(",", weeklySchedule.DaysOfWeek.Select(_ => (int)_));
                return string.Format("{0} {1} * * {2}", weeklySchedule.Minute, weeklySchedule.Hour, daysOfWeekString);
            }
            else if (scheduleType == typeof(MonthlyScheduleInUtc))
            {
                var monthlySchedule = (MonthlyScheduleInUtc)schedule;
                var daysOfMonthString = string.Join(",", monthlySchedule.DaysOfMonth.Select(_ => (int)_));
                return string.Format("{0} {1} {2} * *", monthlySchedule.Minute, monthlySchedule.Hour, daysOfMonthString);
            }
            else if (scheduleType == typeof(YearlyScheduleInUtc))
            {
                var yearlySchedule = (YearlyScheduleInUtc)schedule;
                var daysOfMonthString = string.Join(",", yearlySchedule.DaysOfMonth.Select(_ => (int)_));
                var monthsOfYearString = string.Join(",", yearlySchedule.MonthsOfYear.Select(_ => (int)_));
                return string.Format(
                    "{0} {1} {2} {3} *",
                    yearlySchedule.Minute,
                    yearlySchedule.Hour,
                    daysOfMonthString,
                    monthsOfYearString);
            }
            else if (scheduleType == typeof(ExpressionSchedule))
            {
                var expressionSchedule = (ExpressionSchedule)schedule;
                return expressionSchedule.CronExpression;
            }

            throw new NotSupportedException("Unsupported Schedule: " + scheduleType.AssemblyQualifiedName);
        }
        public static string ToCronExpression(ScheduleBase schedule)
        {
            schedule.ThrowIfInvalid();

            var scheduleType = schedule.GetType();

            if (scheduleType == typeof(IntervalSchedule))
            {
                var intervalSchedule = (IntervalSchedule)schedule;
                if (intervalSchedule.Interval.TotalMinutes.Equals(1))
                {
                    return("* * * * *");
                }
                else
                {
                    return(FormattableString.Invariant($"*/{intervalSchedule.Interval.TotalMinutes} * * * *"));
                }
            }
            else if (scheduleType == typeof(HourlySchedule))
            {
                var hourlySchedule = (HourlySchedule)schedule;
                return(string.Format(CultureInfo.InvariantCulture, "{0} * * * *", hourlySchedule.Minute));
            }
            else if (scheduleType == typeof(DailyScheduleInUtc))
            {
                var dailySchedule = (DailyScheduleInUtc)schedule;
                return(string.Format(CultureInfo.InvariantCulture, "{0} {1} * * *", dailySchedule.Minute, dailySchedule.Hour));
            }
            else if (scheduleType == typeof(WeeklyScheduleInUtc))
            {
                var weeklySchedule   = (WeeklyScheduleInUtc)schedule;
                var daysOfWeekString = string.Join(",", weeklySchedule.DaysOfWeek.Select(_ => (int)_));
                return(string.Format(CultureInfo.InvariantCulture, "{0} {1} * * {2}", weeklySchedule.Minute, weeklySchedule.Hour, daysOfWeekString));
            }
            else if (scheduleType == typeof(MonthlyScheduleInUtc))
            {
                var monthlySchedule   = (MonthlyScheduleInUtc)schedule;
                var daysOfMonthString = string.Join(",", monthlySchedule.DaysOfMonth.Select(_ => (int)_));
                return(string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} * *", monthlySchedule.Minute, monthlySchedule.Hour, daysOfMonthString));
            }
            else if (scheduleType == typeof(YearlyScheduleInUtc))
            {
                var yearlySchedule     = (YearlyScheduleInUtc)schedule;
                var daysOfMonthString  = string.Join(",", yearlySchedule.DaysOfMonth.Select(_ => (int)_));
                var monthsOfYearString = string.Join(",", yearlySchedule.MonthsOfYear.Select(_ => (int)_));
                return(string.Format(
                           CultureInfo.InvariantCulture,
                           "{0} {1} {2} {3} *",
                           yearlySchedule.Minute,
                           yearlySchedule.Hour,
                           daysOfMonthString,
                           monthsOfYearString));
            }
            else if (scheduleType == typeof(ExpressionSchedule))
            {
                var expressionSchedule = (ExpressionSchedule)schedule;
                return(expressionSchedule.CronExpression);
            }

            throw new NotSupportedException("Unsupported Schedule: " + scheduleType.AssemblyQualifiedName);
        }