TimeCrontab是.NET全能Cron表达式解析库,支持Cron完整特性。
特性支持Cron所有特性超高性能易拓展很小,仅 4KB无第三方依赖跨平台高质量代码和良好单元测试支持 .NETFramework3.5+ 及后续版本安装PackageManagerInstall-PackageTimeCrontab.NETCLIdotnetaddpackageTimeCrontab快速入门我们在主页上有不少例子,这是让您入门的第一个:常规格式:分时天月周varcrontab=Crontab.Parse("*****");varnextOccurrence=crontab.GetNextOccurrence(DateTime.Now);支持年份:分时天月周年varcrontab=Crontab.Parse("******",CronStringFormat.WithYears);varnextOccurrence=crontab.GetNextOccurrence(DateTime.Now);支持秒数:秒分时天月周varcrontab=Crontab.Parse("******",CronStringFormat.WithSeconds);varnextOccurrence=crontab.GetNextOccurrence(DateTime.Now);支持秒和年:秒分时天月周年varcrontab=Crontab.Parse("*******",CronStringFormat.WithSecondsAndYears);varnextOccurrence=crontab.GetNextOccurrence(DateTime.Now);获取休眠差实现定时任务//阻塞方式varcrontab=Crontab.Parse("******",CronStringFormat.WithSeconds);while(true){Thread.Sleep(crontab.GetSleepMilliseconds(DateTime.UtcNow));Console.WriteLine(DateTime.Now.ToString("G"));}//无阻塞方式varcrontab=Crontab.Parse("******",CronStringFormat.WithSeconds);Task.Factory.StartNew(async()=>{while(true){awaitTask.Delay(crontab.GetSleepMilliseconds(DateTime.UtcNow));Console.WriteLine(DateTime.Now.ToString("G"));}},TaskCreationOptions.LongRunning);BackgroundService 实现定时任务usingTimeCrontab;namespaceWorkerService;publicclassWorker:BackgroundService{privatereadonlyILogger<Worker>_logger;privatereadonlyCrontab_crontab;publicWorker(ILogger<Worker>logger){_logger=logger;_crontab=Crontab.Parse("******",CronStringFormat.WithSeconds);}protectedoverrideasyncTaskExecuteAsync(CancellationTokenstoppingToken){while(!stoppingToken.IsCancellationRequested){vartaskFactory=newTaskFactory(System.Threading.Tasks.TaskScheduler.Current);awaittaskFactory.StartNew(async()=>{//你的业务代码写到这里面_logger.LogInformation("Workerrunningat:{time}",DateTimeOffset.Now);awaitTask.CompletedTask;},stoppingToken);awaitTask.Delay(_crontab.GetSleepMilliseconds(DateTime.UtcNow),stoppingToken);}}}Macro标识符varsecondly=Crontab.Secondly;//每秒varminutely=Crontab.Minutely;//每分钟varhourly=Crontab.Hourly;//每小时vardaily=Crontab.Daily;//每天00:00:00varmonthly=Crontab.Monthly;//每月1号00:00:00varweekly=Crontab.Weekly;//每周日00:00:00varyearly=Crontab.Yearly;//每年1月1号00:00:00更多文档文档您可以在主页找到TimeCrontab文档。测试publicclassTimeCrontabUnitTests{[Theory][InlineData("*****","*****",CronStringFormat.Default)][InlineData("0031W**","0031W**",CronStringFormat.Default)][InlineData("023?*MON-FRI","023?*1-5",CronStringFormat.Default)][InlineData("*/5****","*/5****",CronStringFormat.Default)][InlineData("3011**1-5","3011**1-5",CronStringFormat.Default)][InlineData("2312*JAN*","2312*1*",CronStringFormat.Default)][InlineData("****MON#3","****1#3",CronStringFormat.Default)][InlineData("*/5*LJAN*","*/5*L1*",CronStringFormat.Default)][InlineData("00?1MON#1","00?11#1",CronStringFormat.Default)][InlineData("00LW**","00LW**",CronStringFormat.Default)][InlineData("03010-13?*WED,FRI","03010-13?*3,5",CronStringFormat.WithSeconds)][InlineData("0*/5****","0*/5****",CronStringFormat.WithSeconds)][InlineData("00/1***?","0*/1***?",CronStringFormat.WithSeconds)][InlineData("5-1030-3510-12***","5-1030-3510-12***",CronStringFormat.WithSeconds)]publicvoidTestParse(stringexpression,stringoutputString,CronStringFormatformat){varoutput=Crontab.Parse(expression,format).ToString();Assert.Equal(outputString,output);}[Theory][InlineData("*****","2021-01-0100:01:00",CronStringFormat.Default)][InlineData("0031W**","2021-01-2900:00:00",CronStringFormat.Default)][InlineData("023?*MON-FRI","2021-01-0123:00:00",CronStringFormat.Default)][InlineData("*/5****","2021-01-0100:05:00",CronStringFormat.Default)][InlineData("3011**1-5","2021-01-0111:30:00",CronStringFormat.Default)][InlineData("2312*JAN*","2021-01-0112:23:00",CronStringFormat.Default)][InlineData("****MON#3","2021-01-1800:00:00",CronStringFormat.Default)][InlineData("*/5*LJAN*","2021-01-3100:00:00",CronStringFormat.Default)][InlineData("00?1MON#1","2021-01-0400:00:00",CronStringFormat.Default)][InlineData("00LW**","2021-01-2900:00:00",CronStringFormat.Default)][InlineData("03010-13?*WED,FRI","2021-01-0110:30:00",CronStringFormat.WithSeconds)][InlineData("0*/5****","2021-01-0100:05:00",CronStringFormat.WithSeconds)][InlineData("00/1***?","2021-01-0100:01:00",CronStringFormat.WithSeconds)][InlineData("5-1030-3510-12***","2021-01-0110:30:05",CronStringFormat.WithSeconds)]publicvoidTestGetNextOccurence(stringexpression,stringnextOccurenceString,CronStringFormatformat){varbeginTime=newDateTime(2021,1,1,0,0,0);varcrontab=Crontab.Parse(expression,format);varnextOccurence=crontab.GetNextOccurrence(beginTime);Assert.Equal(nextOccurenceString,nextOccurence.ToString("yyyy-MM-ddHH
评论