Exemplo n.º 1
0
        public void Format(CrontabField field, TextWriter writer, bool noNames)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var next  = field.GetFirst();
            var count = 0;

            while (next != -1)
            {
                var first = next;
                int last;

                do
                {
                    last = next;
                    next = field.Next(last + 1);
                } while (next - last == 1);

                if (count == 0 &&
                    first == _minValue && last == _maxValue)
                {
                    writer.Write('*');
                    return;
                }

                if (count > 0)
                {
                    writer.Write(',');
                }

                if (first == last)
                {
                    FormatValue(first, writer, noNames);
                }
                else
                {
                    FormatValue(first, writer, noNames);
                    writer.Write('-');
                    FormatValue(last, writer, noNames);
                }

                count++;
            }
        }
        private CrontabSchedule(string expression)
        {
            Debug.Assert(expression != null);

            var fields = expression.Split((char[])Separators, StringSplitOptions.RemoveEmptyEntries);

            if (fields.Length != 5)
            {
                throw new FormatException(string.Format(
                                              "'{0}' is not a valid crontab expression. It must contain at least 5 components of a schedule "
                                              + "(in the sequence of minutes, hours, days, months, days of week).",
                                              expression));
            }

            _minutes    = CrontabField.Minutes(fields[0]);
            _hours      = CrontabField.Hours(fields[1]);
            _days       = CrontabField.Days(fields[2]);
            _months     = CrontabField.Months(fields[3]);
            _daysOfWeek = CrontabField.DaysOfWeek(fields[4]);
        }