将日判断和星期判断分开
This commit is contained in:
parent
fb3544ec89
commit
f3096745e3
|
@ -13,31 +13,36 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 可选的秒范围枚举
|
/// 可选的秒范围枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<int> Seconds { get; }
|
public List<int> Seconds { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 可选的分范围枚举
|
/// 可选的分范围枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<int> Minutes { get; }
|
public List<int> Minutes { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 可选的时范围枚举
|
/// 可选的时范围枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<int> Hours { get; }
|
public List<int> Hours { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 可选的天范围枚举
|
/// 可选的天范围枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<int> DaysOfMonth { get; }
|
public List<int> DaysOfMonth { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 可选的月范围枚举
|
/// 可选的月范围枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<int> Months { get; }
|
public List<int> Months { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 可选的星期范围枚举
|
/// 可选的星期范围枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<int> DaysOfWeek { get; }
|
public List<int> DaysOfWeek { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 可选的年范围枚举
|
/// 可选的年范围枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<int> Years { get; set; }
|
public List<int> Years { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析配置
|
||||||
|
/// </summary>
|
||||||
|
public CronOptions Options { get; set; } = new CronOptions();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通过提供cron表达式构造对象
|
/// 通过提供cron表达式构造对象
|
||||||
|
@ -59,13 +64,35 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
var fields = cronExpression.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
|
var fields = cronExpression.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
Seconds = fields.Length > 0 ? GetRange(fields[0],0,59) : Enumerable.Range(0,60).ToList();
|
Seconds = fields.Length > 0 ? GetRange(fields[0],0,59) : Enumerable.Range(0,60).ToList();
|
||||||
|
if(Seconds.Count == 0) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(CronExpression),"表达式没有任何可以匹配的秒,无法触发!");
|
||||||
|
}
|
||||||
Minutes = fields.Length > 1 ? GetRange(fields[1],0,59) : Enumerable.Range(0,60).ToList();
|
Minutes = fields.Length > 1 ? GetRange(fields[1],0,59) : Enumerable.Range(0,60).ToList();
|
||||||
|
if(Minutes.Count == 0) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(CronExpression),"表达式没有任何可以匹配的分,无法触发!");
|
||||||
|
}
|
||||||
Hours = fields.Length > 2 ? GetRange(fields[2],0,23) : Enumerable.Range(0,24).ToList();
|
Hours = fields.Length > 2 ? GetRange(fields[2],0,23) : Enumerable.Range(0,24).ToList();
|
||||||
|
if(Hours.Count == 0) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(CronExpression),"表达式没有任何可以匹配的时,无法触发!");
|
||||||
|
}
|
||||||
DaysOfMonth = fields.Length > 3 ? GetRange(fields[3],1,31) : Enumerable.Range(1,31).ToList();
|
DaysOfMonth = fields.Length > 3 ? GetRange(fields[3],1,31) : Enumerable.Range(1,31).ToList();
|
||||||
|
if(DaysOfMonth.Count == 0) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(CronExpression),"表达式没有任何可以匹配的日,无法触发!");
|
||||||
|
}
|
||||||
Months = fields.Length > 4 ? GetRange(fields[4],1,12) : Enumerable.Range(1,12).ToList();
|
Months = fields.Length > 4 ? GetRange(fields[4],1,12) : Enumerable.Range(1,12).ToList();
|
||||||
|
if(Months.Count == 0) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(CronExpression),"表达式没有任何可以匹配的月份,无法触发!");
|
||||||
|
}
|
||||||
DaysOfWeek = fields.Length > 5 ? GetRange(fields[5],0,6) : Enumerable.Range(0,7).ToList();
|
DaysOfWeek = fields.Length > 5 ? GetRange(fields[5],0,6) : Enumerable.Range(0,7).ToList();
|
||||||
|
if(DaysOfWeek.Count == 0) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(CronExpression),"表达式没有任何可以匹配的星期几,无法触发!");
|
||||||
|
}
|
||||||
var nowYear = DateTime.Now.Year;
|
var nowYear = DateTime.Now.Year;
|
||||||
Years = fields.Length > 6 ? GetRange(fields[6],nowYear,nowYear + 2) : Enumerable.Range(nowYear,2).ToList();
|
var endYear = Options.MaxYear;
|
||||||
|
Years = fields.Length > 6 ? GetRange(fields[6],nowYear,endYear) : Enumerable.Range(nowYear,2).ToList();
|
||||||
|
if(Years.Count == 0) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(CronExpression),"表达式没有任何可以匹配的年份,无法触发!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -91,15 +118,21 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(!ct.IsDayOfWeekAdjust) {
|
||||||
|
AdjustDayOfWeek(ct);
|
||||||
|
if(!ct.IsYearAdjust || !ct.IsMonthAdjust || !ct.IsDayAdjust || !ct.IsDayOfWeekAdjust) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
if(!ct.IsHourAdjust) {
|
if(!ct.IsHourAdjust) {
|
||||||
AdjustHour(ct);
|
AdjustHour(ct);
|
||||||
if(!ct.IsYearAdjust || !ct.IsMonthAdjust || !ct.IsDayAdjust) {
|
if(!ct.IsYearAdjust || !ct.IsMonthAdjust || !ct.IsDayAdjust || !ct.IsDayOfWeekAdjust) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!ct.IsMinuteAdjust) {
|
if(!ct.IsMinuteAdjust) {
|
||||||
AdjustMinute(ct);
|
AdjustMinute(ct);
|
||||||
if(!ct.IsYearAdjust || !ct.IsMonthAdjust || !ct.IsDayAdjust || !ct.IsHourAdjust) {
|
if(!ct.IsYearAdjust || !ct.IsMonthAdjust || !ct.IsDayAdjust || !ct.IsDayOfWeekAdjust || !ct.IsHourAdjust) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,19 +178,29 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
int month = dt.Month;
|
int month = dt.Month;
|
||||||
int day = dt.Day;
|
int day = dt.Day;
|
||||||
int maxDayInMonth = DateTime.DaysInMonth(year,month);
|
int maxDayInMonth = DateTime.DaysInMonth(year,month);
|
||||||
var next = DaysOfMonth.Where(a => a >= day && a <= maxDayInMonth && DaysOfWeek.Contains((int)dt.DayOfWeek));
|
|
||||||
|
var next = DaysOfMonth.Where(a => a >= day && a <= maxDayInMonth);
|
||||||
if(next.Any()) {
|
if(next.Any()) {
|
||||||
date.SetAdjustTime(dt.AddDays(next.First() - day),TimePartEnum.YearDay);
|
date.SetAdjustTime(dt.AddDays(next.First() - day),TimePartEnum.YearDay);
|
||||||
date.IsDayAdjust = true;
|
date.IsDayAdjust = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(!DaysOfWeek.Contains((int)dt.DayOfWeek)) {
|
|
||||||
date.SetAdjustTime(dt.AddDays(1),TimePartEnum.YearDay);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
date.SetAdjustTime(dt.AddMonths(1),TimePartEnum.YearMonth);
|
date.SetAdjustTime(dt.AddMonths(1),TimePartEnum.YearMonth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AdjustDayOfWeek(CronResult date) {
|
||||||
|
var dw = (int)date.AdjustTime.DayOfWeek;
|
||||||
|
var next = DaysOfWeek.Where(a => a >= dw);
|
||||||
|
if(next.Any()) {
|
||||||
|
date.SetAdjustTime(date.AdjustTime.AddDays(next.First() - dw),TimePartEnum.YearDay);
|
||||||
|
date.IsDayOfWeekAdjust = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
date.SetAdjustTime(date.AdjustTime.AddDays(7 - dw + DaysOfWeek.First()),TimePartEnum.YearDay);
|
||||||
|
date.IsDayOfWeekAdjust = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void AdjustHour(CronResult date) {
|
private void AdjustHour(CronResult date) {
|
||||||
var next = Hours.Where(m => m >= date.AdjustTime.Hour);
|
var next = Hours.Where(m => m >= date.AdjustTime.Hour);
|
||||||
if(next.Any()) {
|
if(next.Any()) {
|
||||||
|
@ -195,9 +238,6 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
if(outRangeChars.Any()) {
|
if(outRangeChars.Any()) {
|
||||||
throw new ArgumentOutOfRangeException(nameof(exp),$"非法字符{new string(outRangeChars)} 在表达式 {exp} 中");
|
throw new ArgumentOutOfRangeException(nameof(exp),$"非法字符{new string(outRangeChars)} 在表达式 {exp} 中");
|
||||||
}
|
}
|
||||||
//if(exp.ToCharArray().Any(a => a.NotIn(charList))) {
|
|
||||||
// throw new ArgumentOutOfRangeException(nameof(exp));
|
|
||||||
//}
|
|
||||||
var list = new List<int>();
|
var list = new List<int>();
|
||||||
if(exp == "*") {
|
if(exp == "*") {
|
||||||
return Enumerable.Range(min,max - min + 1).ToList();
|
return Enumerable.Range(min,max - min + 1).ToList();
|
||||||
|
|
13
Falcon.SugarApi/TimedBackgroundTask/CronOptions.cs
Normal file
13
Falcon.SugarApi/TimedBackgroundTask/CronOptions.cs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Cron表达式解析选项
|
||||||
|
/// </summary>
|
||||||
|
public class CronOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 最大支持的年份
|
||||||
|
/// </summary>
|
||||||
|
public int MaxYear { get; set; } = 2099;
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,12 +8,13 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
public bool IsYearAdjust { get; set; } = false;
|
public bool IsYearAdjust { get; set; } = false;
|
||||||
public bool IsMonthAdjust { get; set; } = false;
|
public bool IsMonthAdjust { get; set; } = false;
|
||||||
public bool IsDayAdjust { get; set; } = false;
|
public bool IsDayAdjust { get; set; } = false;
|
||||||
|
public bool IsDayOfWeekAdjust { get; set; } = false;
|
||||||
public bool IsHourAdjust { get; set; } = false;
|
public bool IsHourAdjust { get; set; } = false;
|
||||||
public bool IsMinuteAdjust { get; set; } = false;
|
public bool IsMinuteAdjust { get; set; } = false;
|
||||||
public bool IsSecondAdjust { get; set; } = false;
|
public bool IsSecondAdjust { get; set; } = false;
|
||||||
|
|
||||||
public bool IsAllAdjust
|
public bool IsAllAdjust
|
||||||
=> (IsYearAdjust && IsMonthAdjust && IsDayAdjust
|
=> (IsYearAdjust && IsMonthAdjust && IsDayAdjust && IsDayOfWeekAdjust
|
||||||
&& IsHourAdjust && IsMinuteAdjust && IsSecondAdjust) || IsNullVal;
|
&& IsHourAdjust && IsMinuteAdjust && IsSecondAdjust) || IsNullVal;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -32,14 +33,12 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
IsYearAdjust = false;
|
IsYearAdjust = false;
|
||||||
IsMonthAdjust = false;
|
IsMonthAdjust = false;
|
||||||
IsDayAdjust = false;
|
IsDayAdjust = false;
|
||||||
|
IsDayOfWeekAdjust = false;
|
||||||
IsHourAdjust = false;
|
IsHourAdjust = false;
|
||||||
IsMinuteAdjust = false;
|
IsMinuteAdjust = false;
|
||||||
IsSecondAdjust = false;
|
IsSecondAdjust = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//else {
|
|
||||||
// IsYearAdjust = true;
|
|
||||||
//}
|
|
||||||
if(!part.HasFlag(TimePartEnum.Month)) {
|
if(!part.HasFlag(TimePartEnum.Month)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -47,28 +46,24 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
this.AdjustTime = new DateTime(dt.Year,dt.Month,1,0,0,0);
|
this.AdjustTime = new DateTime(dt.Year,dt.Month,1,0,0,0);
|
||||||
IsMonthAdjust = false;
|
IsMonthAdjust = false;
|
||||||
IsDayAdjust = false;
|
IsDayAdjust = false;
|
||||||
|
IsDayOfWeekAdjust = false;
|
||||||
IsHourAdjust = false;
|
IsHourAdjust = false;
|
||||||
IsMinuteAdjust = false;
|
IsMinuteAdjust = false;
|
||||||
IsSecondAdjust = false;
|
IsSecondAdjust = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//else {
|
|
||||||
// IsMonthAdjust = true;
|
|
||||||
//}
|
|
||||||
if(!part.HasFlag(TimePartEnum.Day)) {
|
if(!part.HasFlag(TimePartEnum.Day)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(dt.Day != this.AdjustTime.Day) {
|
if(dt.Day != this.AdjustTime.Day) {
|
||||||
this.AdjustTime = new DateTime(dt.Year,dt.Month,dt.Day,0,0,0);
|
this.AdjustTime = new DateTime(dt.Year,dt.Month,dt.Day,0,0,0);
|
||||||
IsDayAdjust = false;
|
IsDayAdjust = false;
|
||||||
|
IsDayOfWeekAdjust = false;
|
||||||
IsHourAdjust = false;
|
IsHourAdjust = false;
|
||||||
IsMinuteAdjust = false;
|
IsMinuteAdjust = false;
|
||||||
IsSecondAdjust = false;
|
IsSecondAdjust = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//else {
|
|
||||||
// IsDayAdjust = true;
|
|
||||||
//}
|
|
||||||
if(!part.HasFlag(TimePartEnum.Hour)) {
|
if(!part.HasFlag(TimePartEnum.Hour)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -79,9 +74,6 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
IsSecondAdjust = false;
|
IsSecondAdjust = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//else {
|
|
||||||
// IsHourAdjust = true;
|
|
||||||
//}
|
|
||||||
if(!part.HasFlag(TimePartEnum.Minute)) {
|
if(!part.HasFlag(TimePartEnum.Minute)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -91,9 +83,6 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
IsSecondAdjust = false;
|
IsSecondAdjust = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//else {
|
|
||||||
// IsMinuteAdjust = true;
|
|
||||||
//}
|
|
||||||
if(!part.HasFlag(TimePartEnum.Second)) {
|
if(!part.HasFlag(TimePartEnum.Second)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -102,9 +91,6 @@ namespace Falcon.SugarApi.TimedBackgroundTask
|
||||||
IsSecondAdjust = false;
|
IsSecondAdjust = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//else {
|
|
||||||
// IsSecondAdjust = true;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user