forked from bradymholt/cron-expression-descriptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionParser.cs
264 lines (230 loc) · 9.17 KB
/
ExpressionParser.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Globalization;
namespace CronExpressionDescriptor
{
/// <summary>
/// Cron Expression Parser
/// </summary>
public class ExpressionParser
{
/* Cron reference
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday; 7 is also Sunday on some systems)
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * * command to execute
*/
private string m_expression;
private Options m_options;
private CultureInfo m_en_culture;
/// <summary>
/// Initializes a new instance of the <see cref="ExpressionParser"/> class
/// </summary>
/// <param name="expression">The cron expression string</param>
/// <param name="options">Parsing options</param>
public ExpressionParser(string expression, Options options)
{
m_expression = expression;
m_options = options;
m_en_culture = new CultureInfo("en-US"); //Default to English
}
/// <summary>
/// Parses the cron expression string
/// </summary>
/// <returns>A 7 part string array, one part for each component of the cron expression (seconds, minutes, etc.)</returns>
public string[] Parse()
{
// Initialize all elements of parsed array to empty strings
string[] parsed = new string[7].Select(el => "").ToArray();
if (string.IsNullOrEmpty(m_expression))
{
#if NET_STANDARD_1X
throw new Exception("Field 'expression' not found.");
#else
throw new MissingFieldException("Field 'expression' not found.");
#endif
}
else
{
string[] expressionPartsTemp = m_expression.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (expressionPartsTemp.Length < 5)
{
throw new FormatException(string.Format("Error: Expression only has {0} parts. At least 5 part are required.", expressionPartsTemp.Length));
}
else if (expressionPartsTemp.Length == 5)
{
//5 part cron so shift array past seconds element
Array.Copy(expressionPartsTemp, 0, parsed, 1, 5);
}
else if (expressionPartsTemp.Length == 6)
{
/* We will detect if this 6 part expression has a year specified and if so we will shift the parts and treat the
first part as a minute part rather than a second part.
Ways we detect:
1. Last part is a literal year (i.e. 2020)
2. 3rd or 5th part is specified as "?" (DOM or DOW)
*/
bool isYearWithNoSecondsPart = Regex.IsMatch(expressionPartsTemp[5], "\\d{4}$") || expressionPartsTemp[4] == "?" || expressionPartsTemp[2] == "?";
if (isYearWithNoSecondsPart)
{
// Shift parts over by one
Array.Copy(expressionPartsTemp, 0, parsed, 1, 6);
}
else
{
Array.Copy(expressionPartsTemp, 0, parsed, 0, 6);
}
}
else if (expressionPartsTemp.Length == 7)
{
parsed = expressionPartsTemp;
}
else
{
throw new FormatException(string.Format("Error: Expression has too many parts ({0}). Expression must not have more than 7 parts.", expressionPartsTemp.Length));
}
}
NormalizeExpression(parsed);
return parsed;
}
/// <summary>
/// Converts cron expression components into consistent, predictable formats.
/// </summary>
/// <param name="expressionParts">A 7 part string array, one part for each component of the cron expression</param>
private void NormalizeExpression(string[] expressionParts)
{
// Convert ? to * only for DOM and DOW
expressionParts[3] = expressionParts[3].Replace("?", "*");
expressionParts[5] = expressionParts[5].Replace("?", "*");
// Convert 0/, 1/ to */
if (expressionParts[0].StartsWith("0/"))
{
// Seconds
expressionParts[0] = expressionParts[0].Replace("0/", "*/");
}
if (expressionParts[1].StartsWith("0/"))
{
// Minutes
expressionParts[1] = expressionParts[1].Replace("0/", "*/");
}
if (expressionParts[2].StartsWith("0/"))
{
// Hours
expressionParts[2] = expressionParts[2].Replace("0/", "*/");
}
if (expressionParts[3].StartsWith("1/"))
{
// DOM
expressionParts[3] = expressionParts[3].Replace("1/", "*/");
}
if (expressionParts[4].StartsWith("1/"))
{
// Month
expressionParts[4] = expressionParts[4].Replace("1/", "*/");
}
if (expressionParts[5].StartsWith("1/"))
{
// DOW
expressionParts[5] = expressionParts[5].Replace("1/", "*/");
}
if (expressionParts[6].StartsWith("1/"))
{
// Years
expressionParts[6] = expressionParts[6].Replace("1/", "*/");
}
// Adjust DOW based on dayOfWeekStartIndexZero option
expressionParts[5] = Regex.Replace(expressionParts[5], @"(^\d)|([^#/\s]\d)", t =>
{ //skip anything preceding by # or /
string dowDigits = Regex.Replace(t.Value, @"\D", ""); // extract digit part (i.e. if "-2" or ",2", just take 2)
string dowDigitsAdjusted = dowDigits;
if (m_options.DayOfWeekStartIndexZero)
{
// "7" also means Sunday so we will convert to "0" to normalize it
if (dowDigits == "7")
{
dowDigitsAdjusted = "0";
}
}
else
{
// If dayOfWeekStartIndexZero==false, Sunday is specified as 1 and Saturday is specified as 7.
// To normalize, we will shift the DOW number down so that 1 becomes 0, 2 becomes 1, and so on.
dowDigitsAdjusted = (Int32.Parse(dowDigits) - 1).ToString();
}
return t.Value.Replace(dowDigits, dowDigitsAdjusted);
});
// Convert DOM '?' to '*'
if (expressionParts[3] == "?")
{
expressionParts[3] = "*";
}
// Convert SUN-SAT format to 0-6 format
for (int i = 0; i <= 6; i++)
{
DayOfWeek currentDay = (DayOfWeek)i;
string currentDayOfWeekDescription = currentDay.ToString().Substring(0, 3).ToUpperInvariant();
expressionParts[5] = Regex.Replace(expressionParts[5], currentDayOfWeekDescription, i.ToString(), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
// Convert JAN-DEC format to 1-12 format
for (int i = 1; i <= 12; i++)
{
DateTime currentMonth = new DateTime(DateTime.Now.Year, i, 1);
string currentMonthDescription = currentMonth.ToString("MMM", m_en_culture).ToUpperInvariant();
expressionParts[4] = Regex.Replace(expressionParts[4], currentMonthDescription, i.ToString(), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
// Convert 0 second to (empty)
if (expressionParts[0] == "0")
{
expressionParts[0] = string.Empty;
}
// If time interval is specified for seconds or minutes and next time part is single item, make it a "self-range" so
// the expression can be interpreted as an interval 'between' range.
// For example:
// 0-20/3 9 * * * => 0-20/3 9-9 * * * (9 => 9-9)
// */5 3 * * * => */5 3-3 * * * (3 => 3-3)
if (expressionParts[2].IndexOfAny(new char[] { '*', '-', ',', '/' }) == -1
&& (Regex.IsMatch(expressionParts[1], @"\*|\/") || Regex.IsMatch(expressionParts[0], @"\*|\/")))
{
expressionParts[2] += $"-{expressionParts[2]}";
}
// Loop through all parts and apply global normalization
for (int i = 0; i < expressionParts.Length; i++)
{
// convert all '*/1' to '*'
if (expressionParts[i] == "*/1")
{
expressionParts[i] = "*";
}
/* Convert Month,DOW,Year step values with a starting value (i.e. not '*') to between expressions.
This allows us to reuse the between expression handling for step values.
For Example:
- month part '3/2' will be converted to '3-12/2' (every 2 months between March and December)
- DOW part '3/2' will be converted to '3-6/2' (every 2 days between Tuesday and Saturday)
*/
if (expressionParts[i].Contains("/")
&& expressionParts[i].IndexOfAny(new char[] { '*', '-', ',' }) == -1)
{
string stepRangeThrough = null;
switch (i)
{
case 4: stepRangeThrough = "12"; break;
case 5: stepRangeThrough = "6"; break;
case 6: stepRangeThrough = "9999"; break;
default: stepRangeThrough = null; break;
}
if (stepRangeThrough != null)
{
string[] parts = expressionParts[i].Split('/');
expressionParts[i] = string.Format("{0}-{1}/{2}", parts[0], stepRangeThrough, parts[1]);
}
}
}
}
}
}