TimeCrontab .NET 全能 Cron 表达式解析库开源项目

我要开发同款
匿名用户2021年11月02日
55阅读
开发技术C#
所属分类程序开发、作业/任务调度
授权协议Mulan

作品详情

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
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论