-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathCronSchedule.cs
executable file
·203 lines (151 loc) · 5.99 KB
/
CronSchedule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using CronNET.Interfaces;
namespace CronNET
{
public class CronSchedule : ICronSchedule
{
#region Readonly Class Members
static readonly Regex DividedRegex = new Regex(@"(\*/\d+)");
static readonly Regex RangeRegex = new Regex(@"(\d+\-\d+)\/?(\d+)?");
static readonly Regex WildRegex = new Regex(@"(\*)");
static readonly Regex ListRegex = new Regex(@"(((\d+,)*\d+)+)");
static readonly Regex ValidationRegex = new Regex(DividedRegex + "|" + RangeRegex + "|" + WildRegex + "|" + ListRegex);
#endregion
#region Private Instance Members
private readonly string _expression;
private List<int> _minutes;
private List<int> _hours;
private List<int> _daysOfMonth;
private List<int> _months;
private List<int> _daysOfWeek;
#endregion
#region Public Constructors
public CronSchedule()
{
}
public CronSchedule(string expressions)
{
_expression = expressions;
Generate();
}
public List<int> Minutes => _minutes;
public List<int> Hours => _hours;
public List<int> DaysOfMonth => _daysOfMonth;
public List<int> Months => _months;
public List<int> DaysOfWeek => _daysOfWeek;
#endregion
#region Public Methods
private bool IsValid()
{
return IsValid(_expression);
}
public bool IsValid(string expression)
{
MatchCollection matches = ValidationRegex.Matches(expression);
return matches.Count > 0;//== 5;
}
public bool IsTime(DateTime dateTime)
{
return _minutes.Contains(dateTime.Minute) &&
_hours.Contains(dateTime.Hour) &&
_daysOfMonth.Contains(dateTime.Day) &&
_months.Contains(dateTime.Month) &&
_daysOfWeek.Contains((int)dateTime.DayOfWeek);
}
private void Generate()
{
if (!IsValid()) return;
MatchCollection matches = ValidationRegex.Matches(_expression);
generate_minutes(matches[0].ToString());
generate_hours(matches.Count > 1 ? matches[1].ToString() : "*");
generate_days_of_month(matches.Count > 2 ? matches[2].ToString() : "*");
generate_months(matches.Count > 3 ? matches[3].ToString() : "*");
generate_days_of_weeks(matches.Count > 4 ? matches[4].ToString() : "*");
}
private void generate_minutes(string match)
{
_minutes = generate_values(match, 0, 60);
}
private void generate_hours(string match)
{
_hours = generate_values(match, 0, 24);
}
private void generate_days_of_month(string match)
{
_daysOfMonth = generate_values(match, 1, 32);
}
private void generate_months(string match)
{
_months = generate_values(match, 1, 13);
}
private void generate_days_of_weeks(string match)
{
_daysOfWeek = generate_values(match, 0, 7);
}
private List<int> generate_values(string configuration, int start, int max)
{
if (DividedRegex.IsMatch(configuration)) return divided_array(configuration, start, max);
if (RangeRegex.IsMatch(configuration)) return range_array(configuration);
if (WildRegex.IsMatch(configuration)) return wild_array(configuration, start, max);
if (ListRegex.IsMatch(configuration)) return list_array(configuration);
return new List<int>();
}
private List<int> divided_array(string configuration, int start, int max)
{
if (!DividedRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();
string[] split = configuration.Split("/".ToCharArray());
int divisor = int.Parse(split[1]);
for (int i = start; i < max; ++i)
if (i % divisor == 0)
ret.Add(i);
return ret;
}
private List<int> range_array(string configuration)
{
if (!RangeRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();
string[] split = configuration.Split("-".ToCharArray());
int start = int.Parse(split[0]);
int end;
if (split[1].Contains("/"))
{
split = split[1].Split("/".ToCharArray());
end = int.Parse(split[0]);
int divisor = int.Parse(split[1]);
for (int i = start; i < end; ++i)
if (i % divisor == 0)
ret.Add(i);
return ret;
}
end = int.Parse(split[1]);
for (int i = start; i <= end; ++i)
ret.Add(i);
return ret;
}
private List<int> wild_array(string configuration, int start, int max)
{
if (!WildRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();
for (int i = start; i < max; ++i)
ret.Add(i);
return ret;
}
private List<int> list_array(string configuration)
{
if (!ListRegex.IsMatch(configuration))
return new List<int>();
List<int> ret = new List<int>();
string[] split = configuration.Split(",".ToCharArray());
foreach (string s in split)
ret.Add(int.Parse(s));
return ret;
}
#endregion
}
}