示例#1
0
 public IntCronField(CronFieldType type, CronFieldKind kind, DateField dateField, string spec, params int[] values)
     : base(type, kind, dateField, spec)
 {
     Values = values;
     // For day field 31 matches last day of the month
     _matchLastDay = type == CronFieldType.Day && kind != CronFieldKind.AnyValue && values.Contains(31);
 }
 public CronFieldListItem(int fieldPos, string part)
 {
     FieldPos  = fieldPos;
     this.part = part;
     values    = new int[2] {
         -1, -1
     };
     if (part.IndexOf("0/") == 0)
     {
         Type = CronFieldType.Periodic;
         string s = part.Replace("0/", "");
         if (!int.TryParse(s, out values[0]))
         {
             throw new ArgumentException("Periodic value assigned is not numeric");
         }
     }
     else if (part.IndexOf('-') > 0)
     {
         Type = CronFieldType.Range;
         string[] array = part.Split('-');
         values[0] = CronUtils.ConvertCronFieldToNumber(fieldPos, array[0]);
         values[1] = CronUtils.ConvertCronFieldToNumber(fieldPos, array[1]);
     }
     else if (part == "*")
     {
         Type      = CronFieldType.Wildcard;
         values[0] = 0;
     }
     else
     {
         Type      = CronFieldType.Single;
         values[0] = CronUtils.ConvertCronFieldToNumber(fieldPos, part);
     }
 }
示例#3
0
 public CronField(CronFieldType type, CronFieldKind kind, DateField dateField, string spec)
 {
     Type      = type;
     Kind      = kind;
     DateField = dateField;
     Spec      = spec;
 }
示例#4
0
 public static DateField GetDateField(CronFieldType cronType)
 {
     _dateFieldsByCronFieldType = _dateFieldsByCronFieldType ??
                                  new DateField[] { DateFields.Year, DateFields.Month, DateFields.Day, DateFields.Day,
                                                    DateFields.Hour, DateFields.Minute };
     return(_dateFieldsByCronFieldType[(int)cronType]);
 }
示例#5
0
        static string[][] GetNameLists(CronFieldType type)
        {
            switch (type)
            {
            case CronFieldType.DayOfWeek: return(new[] { _dayNames, _dayNamesShort });

            case CronFieldType.Month: return(new[] { _monthNames, _monthNamesShort });

            default: return(null);
            }
        }
示例#6
0
        private static int[] GetListForDivisor(CronFieldType type, int div)
        {
            switch (type)
            {
            case CronFieldType.Day: return(GetDivisibles(1, 31, div));

            case CronFieldType.Hours: return(GetDivisibles(0, 23, div));

            case CronFieldType.Minutes: return(GetDivisibles(0, 59, div));

            case CronFieldType.Month: return(GetDivisibles(1, 12, div));

            case CronFieldType.Year: return(GetDivisibles(2001, 2100, div));

            default:
                Check(false, "Invalid CRON field, / is not allowed for field type " + type);
                return(null); //never happens
            }
        }
示例#7
0
 public static IntCronField ParseIntField(CronFieldType type, string spec)
 {
     try {
         var dateField = GetDateField(type);
         var nameLists = GetNameLists(type);
         if (spec == "*")
         {
             return(new IntCronField(type, CronFieldKind.AnyValue, dateField, spec));
         }
         if (spec.Contains(','))
         {
             var values = spec.Split(',').Select(s => ParseValue(s, nameLists)).ToArray();
             return(new IntCronField(type, CronFieldKind.List, dateField, spec, values));
         }
         if (spec.Contains('-'))
         {
             var values = spec.Split('-').Select(s => ParseValue(s, nameLists)).ToArray();
             Check(values.Length == 2, "Invalid cron list spec: '{0}', expected only 2 values separated by dash.", spec);
             Check(values[0] < values[1], "Invalid cron range spec: '{0}', first item must less than the second.", spec);
             return(new IntCronField(type, CronFieldKind.Range, dateField, spec, values[0], values[1]));
         }
         if (spec.Contains("/"))
         {
             var parts = spec.Split('/');
             Check(parts.Length == 2, "Invalid '/' CRON field: " + spec);
             var p0 = parts[0];
             Check(p0 == string.Empty || p0 == "*", "Invalid '/' CRON field, expected * as first symbol: " + spec);
             int div;
             Check(int.TryParse(parts[1], out div), "Invalid CRON field, expected int after / :" + spec);
             Check(div > 1 && div < 30, "Invalid divisor value in / CRON field, expected 2..30 :" + spec);
             var values = GetListForDivisor(type, div);
             return(new IntCronField(type, CronFieldKind.List, dateField, spec, values));
         }
         var value = ParseValue(spec, nameLists);
         return(new IntCronField(type, CronFieldKind.Value, dateField, spec, value));
     } catch (Exception ex) {
         ex.Data["CronField"]     = spec;
         ex.Data["CronFieldType"] = type;
         throw;
     }
 }//method
                public bool Validate(CronFieldType cft)
                {
                    switch (cft)
                    {
                    case CronFieldType.Minutes:
                        return(Array.TrueForAll <int>(vals, delegate(int i) { return i >= 0 && i <= 59; }));

                    case CronFieldType.Hours:
                        return(Array.TrueForAll <int>(vals, delegate(int i) { return i >= 0 && i <= 23; }));

                    case CronFieldType.Days:
                        return(Array.TrueForAll <int>(vals, delegate(int i) { return i >= 1 && i <= 31; }));

                    case CronFieldType.Months:
                        return(Array.TrueForAll <int>(vals, delegate(int i) { return i >= 1 && i <= 12; }));

                    case CronFieldType.DaysOfWeek:
                        return(Array.TrueForAll <int>(vals, delegate(int i) { return i >= 0 && i <= 6; }));

                    default:
                        break;
                    }
                    return(false);
                }
            private FieldVal ParseCronField(string str, CronFieldType cft)
            {
                FieldVal res = new FieldVal();

                if (string.IsNullOrEmpty(str))
                {
                    throw new ArgumentNullException("A crontab field value cannot be empty.");
                }

                // Look first for a list of values (e.g. 1,2,3).
                if (str.IndexOf(",") > 0)
                {
                    res.vals = Array.ConvertAll <string, int>(str.Split(','), delegate(string s) { return(ParseInt(s)); });
                    res.Validate(cft);
                    return(res);
                }

                // Look for stepping (e.g. */2 = every 2nd).
                res.step = 1;
                var slashIndex = str.IndexOf("/");

                if (slashIndex > 0)
                {
                    res.step = ParseInt(str.Substring(slashIndex + 1));
                    str      = str.Substring(0, slashIndex);
                }

                // Next, look for wildcard (*).
                if (str.Length == 1 && str[0] == '*')
                {
                    res.vals      = new int[0];
                    res.repeating = true;
                    return(res);
                }

                // Next, look for a range of values (e.g. 2-10).
                var dashIndex = str.IndexOf("-");

                if (dashIndex > 0)
                {
                    var first = ParseInt(str.Substring(0, dashIndex));
                    var last  = ParseInt(str.Substring(dashIndex + 1));
                    if (first >= last)
                    {
                        throw new ArgumentException("A crontab field value range must begin with a lower number");
                    }
                    res.range = true;
                    res.vals  = new int[] { first, last };
                    res.Validate(cft);
                    return(res);
                }

                // Check for "?" and substitute current time
                if (str.Length == 1 && str[0] == '?')
                {
                    DateTime now  = DateTime.Now;
                    int      nval = 0;
                    switch (cft)
                    {
                    case CronFieldType.Minutes:
                        nval = now.Minute;
                        break;

                    case CronFieldType.Hours:
                        nval = now.Hour;
                        break;

                    case CronFieldType.Days:
                        nval = now.Day;
                        break;

                    case CronFieldType.Months:
                        nval = now.Month;
                        break;

                    case CronFieldType.DaysOfWeek:
                        throw new ArgumentException("The fifth parameter representing the day of the week cannot be a '?'.");

                    default:
                        break;
                    }
                    res.vals = new int[] { nval };
                    res.step = 0;
                    res.Validate(cft);
                    return(res);
                }

                // Finally, handle the case where there is only one number.
                var value = ParseInt(str);

                res.vals = new int[] { value };
                res.step = 0;
                res.Validate(cft);

                return(res);
            }
示例#10
0
 private CronFieldBase GetField(CronFieldType type)
 {
     return(this.fields.First(f => f.CronFieldType == type));
 }
 public bool Validate(CronFieldType cft)
 {
     switch (cft)
     {
         case CronFieldType.Minutes:
             return Array.TrueForAll<int>(vals, delegate(int i) { return i >= 0 && i <= 59; });
         case CronFieldType.Hours:
             return Array.TrueForAll<int>(vals, delegate(int i) { return i >= 0 && i <= 23; });
         case CronFieldType.Days:
             return Array.TrueForAll<int>(vals, delegate(int i) { return i >= 1 && i <= 31; });
         case CronFieldType.Months:
             return Array.TrueForAll<int>(vals, delegate(int i) { return i >= 1 && i <= 12; });
         case CronFieldType.DaysOfWeek:
             return Array.TrueForAll<int>(vals, delegate(int i) { return i >= 0 && i <= 6; });
         default:
             break;
     }
     return false;
 }
            private FieldVal ParseCronField(string str, CronFieldType cft)
            {
                FieldVal res = new FieldVal();

                if (string.IsNullOrEmpty(str))
                    throw new ArgumentNullException("A crontab field value cannot be empty.");

                // Look first for a list of values (e.g. 1,2,3).
                if (str.IndexOf(",") > 0)
                {
                    res.vals = Array.ConvertAll<string, int>(str.Split(','), delegate(string s) { return ParseInt(s); });
                    res.Validate(cft);
                    return res;
                }

                // Look for stepping (e.g. */2 = every 2nd).
                res.step = 1;
                var slashIndex = str.IndexOf("/");
                if (slashIndex > 0)
                {
                    res.step = ParseInt(str.Substring(slashIndex + 1));
                    str = str.Substring(0, slashIndex);
                }

                // Next, look for wildcard (*).
                if (str.Length == 1 && str[0] == '*')
                {
                    res.vals = new int[0];
                    res.repeating = true;
                    return res;
                }

                // Next, look for a range of values (e.g. 2-10).
                var dashIndex = str.IndexOf("-");
                if (dashIndex > 0)
                {
                    var first = ParseInt(str.Substring(0, dashIndex));
                    var last = ParseInt(str.Substring(dashIndex + 1));
                    if (first >= last)
                        throw new ArgumentException("A crontab field value range must begin with a lower number");
                    res.range = true;
                    res.vals = new int[] { first, last };
                    res.Validate(cft);
                    return res;
                }

                // Check for "?" and substitute current time
                if (str.Length == 1 && str[0] == '?')
                {
                    DateTime now = DateTime.Now;
                    int nval = 0;
                    switch (cft)
                    {
                        case CronFieldType.Minutes:
                            nval = now.Minute;
                            break;
                        case CronFieldType.Hours:
                            nval = now.Hour;
                            break;
                        case CronFieldType.Days:
                            nval = now.Day;
                            break;
                        case CronFieldType.Months:
                            nval = now.Month;
                            break;
                        case CronFieldType.DaysOfWeek:
                            throw new ArgumentException("The fifth parameter representing the day of the week cannot be a '?'.");
                        default:
                            break;
                    }
                    res.vals = new int[] { nval };
                    res.step = 0;
                    res.Validate(cft);
                    return res;
                }

                // Finally, handle the case where there is only one number.
                var value = ParseInt(str);
                res.vals = new int[] { value };
                res.step = 0;
                res.Validate(cft);

                return res;
            }