Documentation
¶
Overview ¶
Package cronparser provides simple cron-string parsing.
Cron string is a string like '0 9 * * 6' that hold info about periodical date. The package provides the ability to check 1. whether a date is suitable for a cron string and 2. to find the nearest date that satisfies the cron string.
Examples ¶
check date:
actual, err := NewCronParser("/5 14 6-11 1 mon").IsMatch(time.Date(2025, 1, 6, 14, 35, 0, 0, time.UTC))
Output:
actual: true
err: <nil>
find nearest date:
actual, err := NewCronParser("/5 14 6-11 4,1 *").NearestDate(time.Date(2025, 1, 12, 15, 55, 0, 0, time.UTC))
Output:
actual: 2025-04-06 14:00:00 +0000 UTC
err: <nil>
Index ¶
Examples ¶
Constants ¶
const ( ErrorValidationEmptyString = "ErrorValidationEmptyString" ErrorValidationTokensCount = "ErrorValidationTokensCount" InvalidStepFormat = "InvalidStepFormat" InvalidStepSize = "InvalidStepSize" InvalidFromToValues = "InvalidFromToSize" InvalidFromToValuesSize = "InvalidFromToValuesSize" InvalidListValues = "InvalidListValues" InvalidSimpleValues = "InvalidSimpleValues" MinutesNotMatch = "MinutesNotMatch" HoursNotMatch = "HoursNotMatch" DayOfMonthNotMatch = "DayOfMonthNotMatch" MonthNotMatch = "MonthNotMatch" DayOfWeekNotMatch = "DayOfWeekNotMatch" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CronParser ¶
type CronParser struct {
// contains filtered or unexported fields
}
CronParser is a structure that provides the ability to manipulate a cron-string
func NewCronParser ¶
func NewCronParser(str string) CronParser
NewCronParser create new object that hold cron-string and provides function for madipulate with dates
func (CronParser) IsMatch ¶
func (crn CronParser) IsMatch(dateTime time.Time) (bool, error)
IsMatch checks whether the specified date matches the expression.
Parameters:
- dateTime: object of time.Time to check
Returns:
- the result of checking (true or false)
- error with failure case (if first result is false)
Example ¶
actual, err := NewCronParser("/5 14 6-11 1 mon").IsMatch(time.Date(2025, 1, 6, 14, 35, 0, 0, time.UTC))
fmt.Println("actual:", actual)
fmt.Println("err:", err)
Output: actual: true err: <nil>
Example (Second) ¶
actual, err := NewCronParser("/5 14 6-11 1 mon").IsMatch(time.Date(2025, 1, 7, 14, 35, 0, 0, time.UTC))
fmt.Println("actual:", actual)
fmt.Println("err:", err)
Output: actual: false err: DayOfWeekNotMatch
func (CronParser) NearestDate ¶
NearestDate return date that nearest for the specified date and matches the cron expression.
Parameters:
- dateTime: object of time.Time to check
Returns:
- nearest date
- error with failure case (if expression not valid)
Example ¶
actual, err := NewCronParser("/5 14 6-11 4,1 *").NearestDate(time.Date(2025, 1, 12, 15, 55, 0, 0, time.UTC))
fmt.Println("actual:", actual)
fmt.Println("err:", err)
Output: actual: 2025-04-06 14:00:00 +0000 UTC err: <nil>