cronparser

package module
v0.0.0-...-ab49631 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2025 License: GPL-3.0 Imports: 8 Imported by: 0

README

[[http://godoc.org/github.com/AlexxSap/cronparser][http://godoc.org/github.com/AlexxSap/cronparser?status.png]]

*  cronparser

Simple cron parser for go.

Requires Go 1.24 or later.
Does not require external dependencies.

If you need more functionality, you can check out the wonderful repository https://github.com/robfig/cron .

cron-string look like this:
#+begin_src
0 9 * * 6
#+end_src

Which can be interpreted using the following:
#+begin_src
# ┌───────────── minute 
# │ ┌───────────── hour 
# │ │ ┌───────────── day of the month
# │ │ │ ┌───────────── month 
# │ │ │ │ ┌───────────── day of the week
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
#+end_src

For these fields the allowed values are:
| Field        | Required | Allowed values   | Allowed special characters |
| Minutes      | Yes      | 0–59             | * , -                      |
| Hours        | Yes      | 0–23             | * , -                      |
| Day of month | Yes      | 1–31             | * , -                      |
| Month        | Yes      | 1–12 (jan - dec) | * , -                      |
| Day of week  | Yes      | 1–7 (mon-sun)    | * , -                      |

Week begin from Monday. If you do not agree with this, you can create an issue.

Each section allows the following operators:
- *  All numbers in the value range
- /  How many numbers do you pass
- -  From X to Z
- ,  List of numbers

Examples:
- * * * * * - every minute
- * 9 * * SAT - every minute, between 9:00 and 9:59, on saturday
- 5 14-15 * 1,3 Mon-Fri - 14:05 and 15:05, on januar and march, from monday to friday

Usage:

install package
#+begin_src cmd
go get github.com/AlexxSap/cronparser
#+end_src

then import package in your file
#+begin_src go
import "github.com/AlexxSap/cronparser"
#+end_src

and use
#+begin_src go
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>
#+end_src

#+begin_src go
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>
#+end_src

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

View Source
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

func (crn CronParser) NearestDate(currentDate time.Time) (time.Time, error)

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>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL