Skip to content

This library parse cron expression and Calculation of occurrences of time based on a cron expression

License

Notifications You must be signed in to change notification settings

zhurongbo111/CronParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

English | 中文

CronParser

Package NuGet Stable
CronParser CronParser

CI

Introduction

CronParser is a C# library designed to parse Cron expressions. It can convert a Cron expression string into a CronExpression object, supporting 5 - part (from minutes to days of the week), 6 - part (from seconds to days of the week), and 7 - part (from seconds to years) Cron expressions.

Features

  • Flexible Parsing: Supports Cron expressions of different lengths.
  • Input Validation: Rigorously validates the input Cron expression to ensure its validity.
  • Error Handling: Catches and handles potential errors during the parsing process, enhancing the robustness of the code.

The corn expression version supported by this library are following:

Five-part format
* * * * *
- - - - -
| | | | |
| | | | |
| | | | +----- Day of week(Range:0-6 or SUN-SAT)
| | | +------- Month(Range: 1-12 or JAN-DEC)
| | +--------- Day of month(Range:1-31)
| +----------- Hour(Range:0-23)
+------------- Minute(Range:0-59)
Six-part format(include second)
* * * * * *
- - - - - -
| | | | | |
| | | | | |
| | | | | +--- Day of week
| | | | +----- Month
| | | +------- Day of month
| | +--------- Hour
| +----------- Minute
+------------- Second(Range:0-59)
Seven-part format
* * * * * * *
- - - - - - -
| | | | | | |
| | | | | | |
| | | | | | +--- Year
| | | | | +----- Day of week
| | | | +------- Month
| | | +--------- Day of month
| | +----------- Hour
| +------------- Minute
+--------------- Second

Cron expression

Field Required Allowed values Allowed special characters Remarks
Second No 0-59 * , -,/ * stands every second
3-10 specifies the seconds from 3 to 10.
5/15 specifies the seconds: 5,20,35,50
3,5,7 specifies the seconds: 3,5,7
Minute Yes 0–59 * , -,/ Similar with above.
Hour Yes 0–23 * , -,/ Similar with above.
Day of month Yes 1–31 * , - L L specifies the last day of the month.
Month Yes 1–12 or JAN–DEC * , - / Similar with second second.
Day of week Yes 0–6 or SUN–SAT * , - / L # 5L specifies the last Friday.
5#3 specifies the third Friday of the month.
Year No 1970–2099 * , - / Similar with second.
DateTimeOffset time = new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero);
var cronExpression = CronExpressionParser.Parse("0 * * * * *");
var nextTime = cronExpression.GetNextAvaliableTime(time);
Console.WriteLine(nextTime.ToString("yyyy-MM-dd HH:mm:ss"));//2025-01-01 00:01:00

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Cron Wiki for more information on cron expressions.

About

This library parse cron expression and Calculation of occurrences of time based on a cron expression

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages