TimeCrontab 3.4.0

dotnet add package TimeCrontab --version 3.4.0                
NuGet\Install-Package TimeCrontab -Version 3.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TimeCrontab" Version="3.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TimeCrontab --version 3.4.0                
#r "nuget: TimeCrontab, 3.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TimeCrontab as a Cake Addin
#addin nuget:?package=TimeCrontab&version=3.4.0

// Install TimeCrontab as a Cake Tool
#tool nuget:?package=TimeCrontab&version=3.4.0                

TimeCrontab

license nuget dotNET China

.NET 全能 Cron 表达式解析库,支持 Cron 所有特性。

TimeCrontab.drawio

特性

  • 支持 Cron 所有特性
  • 超高性能
  • 易拓展
  • 很小,仅 4KB
  • 无第三方依赖
  • 跨平台
  • 高质量代码和良好单元测试
  • 支持 .NET Framework 3.5+ 及后续版本

安装

Install-Package TimeCrontab
dotnet add package TimeCrontab

快速入门

我们在主页上有不少例子,这是让您入门的第一个:

常规格式:分 时 天 月 周

var crontab = Crontab.Parse("* * * * *");
var nextOccurrence = crontab.GetNextOccurrence(DateTime.Now);

支持年份:分 时 天 月 周 年

var crontab = Crontab.Parse("* * * * * *", CronStringFormat.WithYears);
var nextOccurrence = crontab.GetNextOccurrence(DateTime.Now);

支持秒数:秒 分 时 天 月 周

var crontab = Crontab.Parse("* * * * * *", CronStringFormat.WithSeconds);
var nextOccurrence = crontab.GetNextOccurrence(DateTime.Now);

支持秒和年:秒 分 时 天 月 周 年

var crontab = Crontab.Parse("* * * * * * *", CronStringFormat.WithSecondsAndYears);
var nextOccurrence = crontab.GetNextOccurrence(DateTime.Now);

获取休眠差实现简单定时任务

// 阻塞方式
var crontab = Crontab.Parse("* * * * * *", CronStringFormat.WithSeconds);
while(true)
{
    Thread.Sleep(crontab.GetSleepMilliseconds(DateTime.Now));
    Console.WriteLine(DateTime.Now.ToString("G"));
}

// 无阻塞方式
var crontab = Crontab.Parse("* * * * * *", CronStringFormat.WithSeconds);
Task.Factory.StartNew(async () =>
{
    while (true)
    {
        await Task.Delay(crontab.GetSleepMilliseconds(DateTime.Now));
        Console.WriteLine(DateTime.Now.ToString("G"));
    }
}, TaskCreationOptions.LongRunning);

BackgroundService 实现简单定时任务

using TimeCrontab;

namespace WorkerService;

public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    private readonly Crontab _crontab;

    public Worker(ILogger<Worker> logger)
    {
        _logger = logger;
        _crontab = Crontab.Parse("* * * * * *", CronStringFormat.WithSeconds);
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            var taskFactory = new TaskFactory(System.Threading.Tasks.TaskScheduler.Current);

            await taskFactory.StartNew(async () =>
            {
                // 你的业务代码写到这里面

                _logger.LogInformation("Worker running at: {time}", DateTime.Now);

                await Task.CompletedTask;
            }, stoppingToken);

            await Task.Delay(_crontab.GetSleepMilliseconds(DateTime.Now), stoppingToken);
        }
    }
}

Macro 标识符

// macro 字符串
var secondly = Crontab.Parse("@secondly");    // 每秒
var minutely = Crontab.Parse("@minutely");    // 每分钟
var hourly = Crontab.Parse("@hourly");    // 每小时
var daily = Crontab.Parse("@daily");  // 每天 00:00:00
var monthly = Crontab.Parse("@monthly");  // 每月 1 号 00:00:00
var weekly = Crontab.Parse("@weekly");    // 每周日 00:00:00
var yearly = Crontab.Parse("@yearly");    // 每年 1 月 1 号 00:00:00
var workday = Crontab.Parse("@workday");    // 每周一至周五 00:00:00

// 静态属性
var secondly = Crontab.Secondly;    // 每秒
var minutely = Crontab.Minutely;    // 每分钟
var hourly = Crontab.Hourly;    // 每小时
var daily = Crontab.Daily;  // 每天 00:00:00
var monthly = Crontab.Monthly;  // 每月 1 号 00:00:00
var weekly = Crontab.Weekly;    // 每周日 00:00:00
var yearly = Crontab.Yearly;    // 每年 1 月 1 号 00:00:00
var workday = Crontab.Workday;    // 每周一至周五 00:00:00

Macro At 标识符

// 每第 3 秒
var crontab = Crontab.SecondlyAt(3);
// 每第 3,5,6 秒
var crontab = Crontab.SecondlyAt(3, 5, 6);

// 每分钟第 3 秒
var crontab = Crontab.MinutelyAt(3);
// 每分钟第 3,5,6 秒
var crontab = Crontab.MinutelyAt(3, 5, 6);

// 每小时第 3 分钟
var crontab = Crontab.HourlyAt(3);
// 每小时第 3,5,6 分钟
var crontab = Crontab.HourlyAt(3, 5, 6);

// 每天第 3 小时正(点)
var crontab = Crontab.DailyAt(3);
// 每天第 3,5,6 小时正(点)
var crontab = Crontab.DailyAt(3, 5, 6);

// 每月第 3 天零点正
var crontab = Crontab.MonthlyAt(3);
// 每月第 3,5,6 天零点正
var crontab = Crontab.MonthlyAt(3, 5, 6);

// 每周星期 3 零点正
var crontab = Crontab.WeeklyAt(3);
var crontab = Crontab.WeeklyAt("WED");  // SUN(星期天),MON,TUE,WED,THU,FRI,SAT
// 每周星期 3,5,6 零点正
var crontab = Crontab.WeeklyAt(3, 5, 6);
var crontab = Crontab.WeeklyAt("WED", "FRI", "SAT");
// 还支持混合
var crontab = Crontab.WeeklyAt(3, "FRI", 6);

// 每年第 3 月 1 日零点正
var crontab = Crontab.YearlyAt(3);
var crontab = Crontab.YearlyAt("MAR");  // JAN(一月),FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
// 每年第 3,5,6 月 1 日零点正
var crontab = Crontab.YearlyAt(3);
var crontab = Crontab.YearlyAt(3, 5, 6);
var crontab = Crontab.YearlyAt("MAR", "MAY", "JUN");
// 还支持混合
var crontab = Crontab.YearlyAt(3, "MAY", 6);

更多文档

文档

您可以在主页找到 TimeCrontab 文档。

测试

public class TimeCrontabUnitTests
{
    [Theory]
    [InlineData("* * * * *", "* * * * *", CronStringFormat.Default)]
    [InlineData("0 0 31W * *", "0 0 31W * *", CronStringFormat.Default)]
    [InlineData("0 23 ? * MON-FRI", "0 23 ? * 1-5", CronStringFormat.Default)]
    [InlineData("*/5 * * * *", "*/5 * * * *", CronStringFormat.Default)]
    [InlineData("30 11 * * 1-5", "30 11 * * 1-5", CronStringFormat.Default)]
    [InlineData("23 12 * JAN *", "23 12 * 1 *", CronStringFormat.Default)]
    [InlineData("* * * * MON#3", "* * * * 1#3", CronStringFormat.Default)]
    [InlineData("*/5 * L JAN *", "*/5 * L 1 *", CronStringFormat.Default)]
    [InlineData("0 0 ? 1 MON#1", "0 0 ? 1 1#1", CronStringFormat.Default)]
    [InlineData("0 0 LW * *", "0 0 LW * *", CronStringFormat.Default)]
    [InlineData("0 30 10-13 ? * WED,FRI", "0 30 10-13 ? * 3,5", CronStringFormat.WithSeconds)]
    [InlineData("0 */5 * * * *", "0 */5 * * * *", CronStringFormat.WithSeconds)]
    [InlineData("0 0/1 * * * ?", "0 */1 * * * ?", CronStringFormat.WithSeconds)]
    [InlineData("5-10 30-35 10-12 * * *", "5-10 30-35 10-12 * * *", CronStringFormat.WithSeconds)]
    public void TestParse(string expression, string outputString, CronStringFormat format)
    {
        var output = Crontab.Parse(expression, format).ToString();
        Assert.Equal(outputString, output);
    }

    [Theory]
    [InlineData("* * * * *", "2022-01-01 00:01:00", CronStringFormat.Default)]
    [InlineData("0 0 31W * *", "2022-01-31 00:00:00", CronStringFormat.Default)]
    [InlineData("0 23 ? * MON-FRI", "2022-01-03 23:00:00", CronStringFormat.Default)]
    [InlineData("*/5 * * * *", "2022-01-01 00:05:00", CronStringFormat.Default)]
    [InlineData("30 11 * * 1-5", "2022-01-03 11:30:00", CronStringFormat.Default)]
    [InlineData("23 12 * JAN *", "2022-01-01 12:23:00", CronStringFormat.Default)]
    [InlineData("* * * * MON#3", "2022-01-17 00:00:00", CronStringFormat.Default)]
    [InlineData("*/5 * L JAN *", "2022-01-31 00:00:00", CronStringFormat.Default)]
    [InlineData("0 0 ? 1 MON#1", "2022-01-03 00:00:00", CronStringFormat.Default)]
    [InlineData("0 0 LW * *", "2022-01-31 00:00:00", CronStringFormat.Default)]
    [InlineData("0 30 10-13 ? * WED,FRI", "2022-01-05 10:30:00", CronStringFormat.WithSeconds)]
    [InlineData("0 */5 * * * *", "2022-01-01 00:05:00", CronStringFormat.WithSeconds)]
    [InlineData("0 0/1 * * * ?", "2022-01-01 00:01:00", CronStringFormat.WithSeconds)]
    [InlineData("5-10 30-35 10-12 * * *", "2022-01-01 10:30:05", CronStringFormat.WithSeconds)]
    public void TestGetNextOccurence(string expression, string nextOccurenceString, CronStringFormat format)
    {
        var beginTime = new DateTime(2022, 1, 1, 0, 0, 0);
        var crontab = Crontab.Parse(expression, format);
        var nextOccurence = crontab.GetNextOccurrence(beginTime);
        Assert.Equal(nextOccurenceString, nextOccurence.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}

贡献

该存储库的主要目的是继续发展 TimeCrontab 核心,使其更快、更易于使用。TimeCrontab 的开发在 Gitee 上公开进行,我们感谢社区贡献错误修复和改进。

许可证

TimeCrontab 采用 MIT 开源许可证。

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.0 is compatible.  netstandard1.1 was computed.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net35 is compatible.  net40 was computed.  net403 was computed.  net45 was computed.  net451 was computed.  net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wp8 was computed.  wp81 was computed.  wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETStandard 1.0

  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TimeCrontab:

Package Downloads
Sundial

.NET 功能齐全的开源分布式作业调度系统,可从最小的应用程序到大型企业系统使用。

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on TimeCrontab:

Repository Stars
YSGStudyHards/DotNetExercises
⚔【DotNetGuide专栏C#/.NET/.NET Core编程技巧练习集】C#/.NET/.NET Core编程常用语法、算法、技巧、中间件、类库、工作业务实操练习集,配套详细的文章教程讲解,助你快速掌握C#/.NET/.NET Core中各种编程常用语法、算法、技巧、中间件、类库、工作业务实操等等。
Version Downloads Last updated
3.4.0 2,912 3/19/2024
3.3.6 2,667 10/13/2023 3.3.6 is deprecated because it is no longer maintained and has critical bugs.
3.3.5 2,997 6/28/2023 3.3.5 is deprecated because it is no longer maintained and has critical bugs.
3.3.4 551 6/14/2023 3.3.4 is deprecated because it is no longer maintained and has critical bugs.
3.3.3 3,763 3/20/2023 3.3.3 is deprecated because it is no longer maintained and has critical bugs.
3.3.2 487 3/16/2023 3.3.2 is deprecated because it is no longer maintained and has critical bugs.
3.3.1 1,341 3/6/2023 3.3.1 is deprecated because it is no longer maintained and has critical bugs.
3.3.0 484 2/22/2023 3.3.0 is deprecated because it is no longer maintained and has critical bugs.
3.2.2 1,746 1/9/2023 3.2.2 is deprecated because it is no longer maintained and has critical bugs.
3.2.1 4,482 11/30/2022 3.2.1 is deprecated because it is no longer maintained and has critical bugs.
3.2.0 391 11/30/2022 3.2.0 is deprecated because it is no longer maintained and has critical bugs.
3.1.2 2,280 11/24/2022 3.1.2 is deprecated because it is no longer maintained and has critical bugs.
3.1.1 860 11/23/2022 3.1.1 is deprecated because it is no longer maintained and has critical bugs.
3.1.0 465 11/14/2022 3.1.0 is deprecated because it is no longer maintained and has critical bugs.
3.0.2 6,322 8/31/2022 3.0.2 is deprecated because it is no longer maintained and has critical bugs.
3.0.1 489 8/27/2022 3.0.1 is deprecated because it is no longer maintained and has critical bugs.
3.0.0 503 8/11/2022 3.0.0 is deprecated because it is no longer maintained and has critical bugs.
2.0.0 110,710 11/11/2021 2.0.0 is deprecated because it is no longer maintained and has critical bugs.
1.0.1 1,088 11/2/2021 1.0.1 is deprecated because it is no longer maintained and has critical bugs.