Example #1
0
        /// <summary>
        /// Creates the appropriate ScheduleBase implementation from the specified cron expression.
        /// </summary>
        /// <param name="cronExpression">Cron expression to convert into a class.</param>
        /// <returns>Correct implementation of ScheduleBase to honor the expression.</returns>
        public static ScheduleBase FromCronExpression(string cronExpression)
        {
            if (string.IsNullOrEmpty(cronExpression))
            {
                throw new ArgumentException("Cannot have a null or empty cron expression.", "cronExpression");
            }

            var trimmedCronExpression = cronExpression.Trim();
            var cronExpressionItems = trimmedCronExpression.Split(' ');
            if (cronExpressionItems.Length != 5)
            {
                throw new ArgumentException("Incorrect number of values in the cron expression.", "cronExpression");
            }

            // probably should do this with regex but my regex is so bad...
            if (cronExpressionItems.All(_ => _ == "*"))
            {
                var ret = new IntervalSchedule { Interval = TimeSpan.FromMinutes(1) };
                return ret;
            }
            else if (cronExpressionItems.First().Contains("/"))
            {
                var minuteString = cronExpressionItems.First();
                var minuteItems = minuteString.Split('/');
                if (minuteItems[0] != "*")
                {
                    throw new ArgumentException("Invalid interval format, should be '*/[interval minutes]', not '" + minuteString + "'");
                }

                var intervalMinutesString = minuteItems[1];
                var intervalMinutesInt = int.Parse(intervalMinutesString);
                var intervalMinutesTimeSpan = TimeSpan.FromMinutes(intervalMinutesInt);
                var ret = new IntervalSchedule { Interval = intervalMinutesTimeSpan };
                return ret;
            }
            else if (cronExpressionItems.First().IsNumeric() && cronExpressionItems.Skip(1).All(_ => _ == "*"))
            {
                var minuteString = cronExpressionItems.First();
                var minute = int.Parse(minuteString);

                var ret = new HourlySchedule { Minute = minute };
                return ret;
            }
            else if (cronExpressionItems.First().IsNumeric() && cronExpressionItems.Second().IsNumeric() && cronExpressionItems.Skip(2).All(_ => _ == "*"))
            {
                var minuteString = cronExpressionItems.First();
                var minute = int.Parse(minuteString);

                var hourString = cronExpressionItems.Second();
                var hour = int.Parse(hourString);

                var ret = new DailyScheduleInUtc { Hour = hour, Minute = minute };
                return ret;
            }
            else if (
                cronExpressionItems.First().IsNumeric() && 
                cronExpressionItems.Second().IsNumeric() && 
                cronExpressionItems.Third() == "*" && 
                cronExpressionItems.Fourth() == "*" && 
                cronExpressionItems.Fifth().IsNumeric())
            {
                var minuteString = cronExpressionItems.First();
                var minute = int.Parse(minuteString);

                var hourString = cronExpressionItems.Second();
                var hour = int.Parse(hourString);

                var daysOfWeekString = cronExpressionItems.Fifth();
                var daysOfWeek = daysOfWeekString.Split(',').Select(int.Parse).Cast<DayOfWeek>().ToArray();

                var ret = new WeeklyScheduleInUtc { Hour = hour, Minute = minute, DaysOfWeek = daysOfWeek };
                return ret;
            }
            else if (
                cronExpressionItems.First().IsNumeric() && 
                cronExpressionItems.Second().IsNumeric() && 
                cronExpressionItems.Third().IsNumeric() && 
                cronExpressionItems.Fourth() == "*" && 
                cronExpressionItems.Fifth() == "*")
            {
                var minuteString = cronExpressionItems.First();
                var minute = int.Parse(minuteString);

                var hourString = cronExpressionItems.Second();
                var hour = int.Parse(hourString);

                var daysOfMonthString = cronExpressionItems.Third();
                var daysOfMonthInt = daysOfMonthString.Split(',').Select(int.Parse).ToArray();

                var ret = new MonthlyScheduleInUtc { Hour = hour, Minute = minute, DaysOfMonth = daysOfMonthInt };
                return ret;
            }
            else if (
                cronExpressionItems.First().IsNumeric() && 
                cronExpressionItems.Second().IsNumeric() &&
                cronExpressionItems.Third().IsNumeric() &&
                cronExpressionItems.Fourth().IsNumeric() && 
                cronExpressionItems.Fifth() == "*")
            {
                var minuteString = cronExpressionItems.First();
                var minute = int.Parse(minuteString);

                var hourString = cronExpressionItems.Second();
                var hour = int.Parse(hourString);

                var daysOfMonthString = cronExpressionItems.Third();
                var daysOfMonthInt = daysOfMonthString.Split(',').Select(int.Parse).ToArray();

                var monthsOfYearString = cronExpressionItems.Fourth();
                var monthsOfYear = monthsOfYearString.Split(',').Select(int.Parse).Cast<MonthOfYear>().ToArray();

                var ret = new YearlyScheduleInUtc { Hour = hour, Minute = minute, DaysOfMonth = daysOfMonthInt, MonthsOfYear = monthsOfYear };
                return ret;
            }
            else
            {
                throw new NotSupportedException("Expression is not supported for translation: " + cronExpression);
            }
        }
        public static ScheduleBase FromCronExpression(string cronExpression)
        {
            if (string.IsNullOrEmpty(cronExpression))
            {
                throw new ArgumentException("Cannot have a null or empty cron expression.", "cronExpression");
            }

            var trimmedCronExpression = cronExpression.Trim();
            var cronExpressionItems   = trimmedCronExpression.Split(' ');

            if (cronExpressionItems.Length != 5)
            {
                throw new ArgumentException("Incorrect number of values in the cron expression.", "cronExpression");
            }

            // probably should do this with regex but my regex is so bad...
            if (cronExpressionItems.All(_ => _ == "*"))
            {
                var ret = new IntervalSchedule {
                    Interval = TimeSpan.FromMinutes(1)
                };
                return(ret);
            }
            else if (cronExpressionItems.First().Contains("/"))
            {
                var minuteString = cronExpressionItems.First();
                var minuteItems  = minuteString.Split('/');
                if (minuteItems[0] != "*")
                {
                    throw new ArgumentException("Invalid interval format, should be '*/[interval minutes]', not '" + minuteString + "'");
                }

                var intervalMinutesString   = minuteItems[1];
                var intervalMinutesInt      = int.Parse(intervalMinutesString, CultureInfo.InvariantCulture);
                var intervalMinutesTimeSpan = TimeSpan.FromMinutes(intervalMinutesInt);
                var ret = new IntervalSchedule {
                    Interval = intervalMinutesTimeSpan
                };
                return(ret);
            }
            else if (cronExpressionItems.First().IsNumeric() && cronExpressionItems.Skip(1).All(_ => _ == "*"))
            {
                var minuteString = cronExpressionItems.First();
                var minute       = int.Parse(minuteString, CultureInfo.InvariantCulture);

                var ret = new HourlySchedule {
                    Minute = minute
                };
                return(ret);
            }
            else if (cronExpressionItems.First().IsNumeric() && cronExpressionItems.Second().IsNumeric() && cronExpressionItems.Skip(2).All(_ => _ == "*"))
            {
                var minuteString = cronExpressionItems.First();
                var minute       = int.Parse(minuteString, CultureInfo.InvariantCulture);

                var hourString = cronExpressionItems.Second();
                var hour       = int.Parse(hourString, CultureInfo.InvariantCulture);

                var ret = new DailyScheduleInUtc {
                    Hour = hour, Minute = minute
                };
                return(ret);
            }
            else if (
                cronExpressionItems.First().IsNumeric() &&
                cronExpressionItems.Second().IsNumeric() &&
                cronExpressionItems.Third() == "*" &&
                cronExpressionItems.Fourth() == "*" &&
                cronExpressionItems.Fifth().IsNumeric())
            {
                var minuteString = cronExpressionItems.First();
                var minute       = int.Parse(minuteString, CultureInfo.InvariantCulture);

                var hourString = cronExpressionItems.Second();
                var hour       = int.Parse(hourString, CultureInfo.InvariantCulture);

                var daysOfWeekString = cronExpressionItems.Fifth();
                var daysOfWeek       = daysOfWeekString.Split(',').Select(int.Parse).Cast <DayOfWeek>().ToArray();

                var ret = new WeeklyScheduleInUtc {
                    Hour = hour, Minute = minute, DaysOfWeek = daysOfWeek
                };
                return(ret);
            }
            else if (
                cronExpressionItems.First().IsNumeric() &&
                cronExpressionItems.Second().IsNumeric() &&
                cronExpressionItems.Third().IsNumeric() &&
                cronExpressionItems.Fourth() == "*" &&
                cronExpressionItems.Fifth() == "*")
            {
                var minuteString = cronExpressionItems.First();
                var minute       = int.Parse(minuteString, CultureInfo.InvariantCulture);

                var hourString = cronExpressionItems.Second();
                var hour       = int.Parse(hourString, CultureInfo.InvariantCulture);

                var daysOfMonthString = cronExpressionItems.Third();
                var daysOfMonthInt    = daysOfMonthString.Split(',').Select(int.Parse).ToArray();

                var ret = new MonthlyScheduleInUtc {
                    Hour = hour, Minute = minute, DaysOfMonth = daysOfMonthInt
                };
                return(ret);
            }
            else if (
                cronExpressionItems.First().IsNumeric() &&
                cronExpressionItems.Second().IsNumeric() &&
                cronExpressionItems.Third().IsNumeric() &&
                cronExpressionItems.Fourth().IsNumeric() &&
                cronExpressionItems.Fifth() == "*")
            {
                var minuteString = cronExpressionItems.First();
                var minute       = int.Parse(minuteString, CultureInfo.InvariantCulture);

                var hourString = cronExpressionItems.Second();
                var hour       = int.Parse(hourString, CultureInfo.InvariantCulture);

                var daysOfMonthString = cronExpressionItems.Third();
                var daysOfMonthInt    = daysOfMonthString.Split(',').Select(int.Parse).ToArray();

                var monthsOfYearString = cronExpressionItems.Fourth();
                var monthsOfYear       = monthsOfYearString.Split(',').Select(int.Parse).Cast <MonthOfYear>().ToArray();

                var ret = new YearlyScheduleInUtc {
                    Hour = hour, Minute = minute, DaysOfMonth = daysOfMonthInt, MonthsOfYear = monthsOfYear
                };
                return(ret);
            }
            else
            {
                throw new NotSupportedException("Expression is not supported for translation: " + cronExpression);
            }
        }