Пример #1
0
        /// <summary>
        /// Try to parse the value for the schedule
        /// </summary>
        /// <param name="value"></param>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public static bool TryParse(string value, out CrontabSchedule schedule)
        {
            schedule = null;
            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }
            string[] array = value.Split(Separator, StringSplitOptions.RemoveEmptyEntries);
            if (array.Length == 0)
            {
                return(false);
            }
            var v1 = array[0];

            if (CrontabValueMinute.TryParse(v1, out CrontabValueMinute minute))
            {
                CrontabValueHour  hour  = null;
                CrontabValueDay   day   = null;
                CrontabValueMonth month = null;
                CrontabValueWeek  week  = null;
                if (array.Length >= 2)
                {
                    if (!CrontabValueHour.TryParse(array[1], out hour))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 3)
                {
                    if (!CrontabValueDay.TryParse(array[2], out day))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 4)
                {
                    if (!CrontabValueMonth.TryParse(array[3], out month))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 5)
                {
                    if (!CrontabValueWeek.TryParse(array[4], out week))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 6)
                {
                    return(false);
                }
                var timeGroup = new CrontabValueHourMinuteGroup(minute, hour);
                var dateGroup = new CrontabValueMonthDayGroup(day, month);
                schedule = new CrontabSchedule(value, timeGroup, dateGroup, week);
                return(true);
            }
            else if (CrontabValueTimeRange.TryParse(v1, out CrontabValueTimeRange timeRange))
            {
                CrontabValueDay   day   = null;
                CrontabValueMonth month = null;
                CrontabValueWeek  week  = null;
                if (array.Length >= 2)
                {
                    if (!CrontabValueDay.TryParse(array[1], out day))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 3)
                {
                    if (!CrontabValueMonth.TryParse(array[2], out month))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 4)
                {
                    if (!CrontabValueWeek.TryParse(array[3], out week))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 5)
                {
                    return(false);
                }
                var timeGroup = new CrontabValueTimeRangeGroup(timeRange);
                var dateGroup = new CrontabValueMonthDayGroup(day, month);
                schedule = new CrontabSchedule(value, timeGroup, dateGroup, week);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
 public CrontabValueMonthDayGroup(CrontabValueDay day, CrontabValueMonth month)
 {
     this.day   = day;
     this.month = month;
 }
Пример #3
0
        public static bool TryParse(string value, out CrontabValueDay crontabValue)
        {
            crontabValue = null;
            var list    = new List <CrontabValueNode>();
            var listDyn = new List <CrontabDynamicEndValueNode>();
            var array   = value.Split(Separator, StringSplitOptions.RemoveEmptyEntries);

            foreach (var item in array)
            {
                if (item == "*" || item == "*/1")
                {
                    crontabValue = new CrontabValueDay(null, null);
                    return(true);
                }
                var endmatch = EndRegex.Match(item);
                if (endmatch.Success)
                {
                    var end = 0;
                    if (endmatch.Groups["end"].Success)
                    {
                        var i = Convert.ToInt32(endmatch.Groups["end"].Value);
                        if (i > max - 1)
                        {
                            return(false);
                        }
                        end = -1 * i;
                    }
                    listDyn.Add(new SingleCrontabDynamicValueNode(end));
                    continue;
                }
                if (int.TryParse(item, out int data))
                {
                    if (data >= min && data <= max)
                    {
                        list.Add(new SingleCrontabValueNode(data));
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }
                var aftmatch = AllFromToRegex.Match(item);
                if (aftmatch.Success)
                {
                    var interval = Convert.ToInt32(aftmatch.Groups["interval"].Value);
                    if (interval > 0)
                    {
                        list.Add(new RangeCrontabValueNode(min, max, min, max, interval));
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }
                var ftmatch = FromToRegex.Match(item);
                if (ftmatch.Success)
                {
                    var start    = Convert.ToInt32(ftmatch.Groups["start"].Value);
                    var end      = Convert.ToInt32(ftmatch.Groups["end"].Value);
                    var interval = 1;
                    if (ftmatch.Groups["interval"].Success)
                    {
                        interval = Convert.ToInt32(ftmatch.Groups["interval"].Value);
                    }
                    if (start >= min && start <= max && end >= min && end <= max && interval > 0)
                    {
                        list.Add(new RangeCrontabValueNode(min, max, start, end, interval));
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }
                var eftmatch = EndFromToRegex.Match(item);
                if (eftmatch.Success)
                {
                    var startv = eftmatch.Groups["start"].Value;
                    var endv   = eftmatch.Groups["end"].Value;
                    var start  = 0;
                    var end    = 0;
                    if (startv[0] == 'e')
                    {
                        if (startv.Length > 1)
                        {
                            var i = Convert.ToInt32(startv.Substring(1));
                            if (i > max - 1)
                            {
                                return(false);
                            }
                            start = -1 * i;
                        }
                    }
                    else
                    {
                        start = Convert.ToInt32(startv);
                        if (start == 0 || start > max)
                        {
                            return(false);
                        }
                    }
                    if (endv[0] == 'e')
                    {
                        if (endv.Length > 1)
                        {
                            var i = Convert.ToInt32(endv.Substring(1));
                            if (i > max - 1)
                            {
                                return(false);
                            }
                            end = -1 * i;
                        }
                    }
                    else
                    {
                        end = Convert.ToInt32(endv);
                        if (end == 0 || end > max)
                        {
                            return(false);
                        }
                    }
                    var interval = 1;
                    if (ftmatch.Groups["interval"].Success)
                    {
                        interval = Convert.ToInt32(ftmatch.Groups["interval"].Value);
                        if (interval == 0)
                        {
                            return(false);
                        }
                    }
                    listDyn.Add(new RangeCrontabDynamicValueNode(min, max, start, end, interval));
                    continue;
                }

                return(false);
            }
            if (list.Count == 0 && listDyn.Count == 0)
            {
                return(false);
            }
            else
            {
                crontabValue = new CrontabValueDay(list, listDyn);
                return(true);
            }
        }