Skip to content

Commit

Permalink
[improv]解析Cron表达式时,兼容支持各段之间多个空格。标准Cron表达式要求各段之间只有一个空格,然而在web中编辑Cron表达…
Browse files Browse the repository at this point in the history
…式时(例如蚂蚁调度AntJob),可能不小心多敲了一个空格,不仔细还看不见。该改进将支持这种场景。
  • Loading branch information
nnhy committed Sep 4, 2024
1 parent 0aa7e11 commit 35d8304
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NewLife.Core/Threading/Cron.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public Boolean IsTime(DateTime time)
/// <returns></returns>
public Boolean Parse(String expression)
{
var ss = expression.Split(' ');
var ss = expression.Split([' '], StringSplitOptions.RemoveEmptyEntries);
if (ss.Length == 0) return false;

if (!TryParse(ss[0], 0, 60, out var vs)) return false;
Expand Down
13 changes: 11 additions & 2 deletions XUnitTest.Core/Threading/CronTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public void GetPreviousTest()
//Assert.False(cron.IsTime(DateTime.Parse("11:01:00 10/12/2008")));
}



[Theory]
[InlineData("*/2")]
[InlineData("* * * * *")]
Expand All @@ -38,6 +36,17 @@ public void GetPreviousTest()
[InlineData("1,10,20 * * * *")]
[InlineData("* 1,10,20 * * *")]
[InlineData("* 1-10,13,5/20 * * *")]
[InlineData("*/2 ")]
[InlineData("* * * * *")]
[InlineData("0 * * * *")]
[InlineData("0,1,2 * * * *")]
[InlineData("*/2 * * * * ")]
[InlineData("5/20 * * * *")]
[InlineData("1-4 * * * * ")]
[InlineData("1-55/3 * * * * ")]
[InlineData("1,10,20 * * * *")]
[InlineData(" * 1,10,20 * * *")]
[InlineData(" * 1-10,13,5/20 * * *")]
public void Valid(String expression)
{
var cron = new Cron();
Expand Down

0 comments on commit 35d8304

Please sign in to comment.