cron

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 8 Imported by: 24

README

cron

GoDoc Reference 1.0.0 release Code Status Build Status Coverage Status

cron is a Go library that parses a cron expression and outputs a human readable description of the cron schedule.
For example, given the expression */5 * * * * it will output Every 5 minutes.

Translated to Go from cron-expression-descriptor (C#) via cRonstrue (Javascript).
Original Author & Credit: Brady Holt (http://www.geekytidbits.com).

Features

  • Zero dependencies
  • Supports all cron expression special characters including * / , - ? L W #
  • Supports 5, 6 (w/ seconds or year), or 7 (w/ seconds and year) part cron expressions
  • Supports Quartz Job Scheduler cron expressions
  • i18n support with 26 locales.

Installation

cron module can be used with both Go module (>= 1.11) and earlier Go versions.

go get -u -v github.com/lnquy/cron

Usage

// Init with default EN locale
exprDesc, _ := cron.NewDescriptor()

desc, _ := exprDesc.ToDescription("* * * * *", cron.Locale_en)
// "Every minute" 

desc, _ := exprDesc.ToDescription("0 23 ? * MON-FRI", cron.Locale_en)
// "At 11:00 PM, Monday through Friday" 

desc, _ := exprDesc.ToDescription("23 14 * * SUN#2", cron.Locale_en)
// "At 02:23 PM, on the second Sunday of the month"

// Init with custom configs
exprDesc, _ := cron.NewDescriptor(
    cron.Use24HourTimeFormat(true),
    cron.DayOfWeekStartsAtOne(true),
    cron.Verbose(true),
    cron.SetLogger(log.New(os.Stdout, "cron: ", 0)),
    cron.SetLocales(cron.Locale_en, cron.Locale_fr),
)

For more usage examples, including a demonstration of how cron can handle some very complex cron expressions, you can reference the unit tests or the example codes.

i18n

To use the i18n support, you must configure the locales when create a new ExpressionDescriptor via SetLocales() option.

exprDesc, _ := cron.NewDescriptor(
    cron.SetLocales(cron.Locale_en, cron.Locale_es, cron.Locale_fr),
)
// or load all cron.LocaleAll
exprDesc, _ := cron.NewDescriptor(cron.SetLocales(cron.LocaleAll))

desc, _ := exprDesc.ToDescription("* * * * *", cron.Locale_fr)
// Toutes les minutes

By default, ExpressionDescriptor always load the Locale_en. If you pass an unregistered locale into ToDescription() function, the result will be returned in English.

Supported Locales
Locale Code Language Contributors
cs Czech hanbar
da Danish Rasmus Melchior Jacobsen
de German Michael Schuler
en English Brady Holt
es Spanish Ivan Santos
fa Farsi A. Bahrami
fi Finnish Mikael Rosenberg
fr French Arnaud TAMAILLON
he Hebrew Ilan Firsov
it Italian rinaldihno
ja Japanese Alin Sarivan
ko Korean Ion Mincu
nb Norwegian Siarhei Khalipski
nl Dutch TotalMace
pl Polish foka
pt_BR Portuguese (Brazil) Renato Lima
ro Romanian Illegitimis
ru Russian LbISS
sk Slovakian hanbar
sl Slovenian Jani Bevk
sv Swedish roobin
sw Swahili Leylow Lujuo
tr Turkish Mustafa SADEDİL
uk Ukrainian Taras
zh_CN Chinese (Simplified) Star Peng
zh_TW Chinese (Traditional) Ricky Chiang

hcron

hcron is the CLI tool to convert the CRON expression to human readable string.
You can pass the CRON expressions as the program argument, piped hcron with stdin or given the path to crontab file.

Install

You can find the pre-built binaries for Linux, MacOS, FreeBSD and Windows from the Release.

For other OS or architecture, you can build the code using Go as below:

$ go get -u -v github.com/lnquy/cron/cmd/hcron

# or

$ git clone https://github.com/lnquy/cron
$ cd cron/cmd/hcron
$ go build
Usage
$ hcron -h
hcron converts the CRON expression to human readable description.

Usage:
  hcron [flags] [cron expression]

Flags:
  -24-hour
        Output description in 24 hour time format
  -dow-starts-at-one
        Is day of the week starts at 1 (Monday-Sunday: 1-7)
  -file string
        Path to crontab file
  -h    Print help then exit
  -locale string
        Output description in which locale (default "en")
  -v    Print app version then exit
  -verbose
        Output description in verbose format

Examples:
  $ hcron "0 15 * * 1-5"
  $ hcron "0 */10 9 * * 1-5 2020"
  $ hcron -locale fr "0 */10 9 * * 1-5 2020"
  $ hcron -file /var/spool/cron/crontabs/mycronfile
  $ another-app | hcron 
  $ another-app | hcron --dow-starts-at-one --24-hour -locale es

Project status

  • Port 1-1 code from cRonstrue Javascript
  • Port and pass all test cases from cRonstrue
  • i18n for 25 languages
  • Test cases i18n
  • Fix i18n issues of FA, HE, RO, RU, UK, ZH_CN and ZH_TW
  • hcron CLI tool
  • Performance improvement
  • Release v1.0.0

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidExprError           = errors.New("invalid expression")
	InvalidExprSecondError     = errors.New("invalid expression, second part")
	InvalidExprMinuteError     = errors.New("invalid expression, minute part")
	InvalidExprHourError       = errors.New("invalid expression, hour part")
	InvalidExprDayOfMonthError = errors.New("invalid expression, day of month part")
	InvalidExprMonthError      = errors.New("invalid expression, month part")
	InvalidExprDayOfWeekError  = errors.New("invalid expression, day of week part")
	InvalidExprYearError       = errors.New("invalid expression, year part")
)

Functions

This section is empty.

Types

type ExpressionDescriptor

type ExpressionDescriptor struct {
	// contains filtered or unexported fields
}

ExpressionDescriptor represents the CRON expression descriptor.

func NewDescriptor

func NewDescriptor(options ...Option) (exprDesc *ExpressionDescriptor, err error)

NewDescriptor returns a new CRON expression descriptor based on the list of options. If no options provided, a default CRON expression descriptor will be returned instead. By default, English (Locale_en) will always be loaded.

func (*ExpressionDescriptor) ToDescription

func (e *ExpressionDescriptor) ToDescription(expr string, loc LocaleType) (desc string, err error)

ToDescription converts the CRON expression to the human readable string in specified locale. If the specified locale had not been loaded by the CRON expression descriptor, the result will be returned in English (Locale_en) by default.

To configure supported locales of the CRON expression descriptor, please see the SetLocales() option.

type Locale

type Locale interface {
	GetLocaleType() (typ LocaleType)
	GetBool(key LocaleKey) (value bool)
	GetString(key LocaleKey) (value string)
	GetSlice(key LocaleKey) (values []string)
}

Locale is the interface to a specific i18n. The methods of Locale interface returns the corresponding i18n of a specific key.

func NewLocaleLoaders

func NewLocaleLoaders(types ...LocaleType) (loaders []Locale, err error)

type LocaleKey

type LocaleKey string

type LocaleLoader

type LocaleLoader struct {
	// contains filtered or unexported fields
}

LocaleLoader holds the map of i18n strings in a specific language (localType).

func (*LocaleLoader) GetBool

func (l *LocaleLoader) GetBool(key LocaleKey) (value bool)

func (*LocaleLoader) GetLocaleType

func (l *LocaleLoader) GetLocaleType() (typ LocaleType)

func (*LocaleLoader) GetSlice

func (l *LocaleLoader) GetSlice(key LocaleKey) (values []string)

func (*LocaleLoader) GetString

func (l *LocaleLoader) GetString(key LocaleKey) (value string)

type LocaleType

type LocaleType string
const (
	LocaleAll LocaleType = "all"

	Locale_cs    LocaleType = "cs"
	Locale_da    LocaleType = "da"
	Locale_de    LocaleType = "de"
	Locale_en    LocaleType = "en"
	Locale_es    LocaleType = "es"
	Locale_fa    LocaleType = "fa"
	Locale_fi    LocaleType = "fi"
	Locale_fr    LocaleType = "fr"
	Locale_he    LocaleType = "he"
	Locale_it    LocaleType = "it"
	Locale_ja    LocaleType = "ja"
	Locale_ko    LocaleType = "ko"
	Locale_nb    LocaleType = "nb"
	Locale_nl    LocaleType = "nl"
	Locale_pl    LocaleType = "pl"
	Locale_pt_BR LocaleType = "pt_BR"
	Locale_ro    LocaleType = "ro"
	Locale_ru    LocaleType = "ru"
	Locale_sk    LocaleType = "sk"
	Locale_sl    LocaleType = "sl"
	Locale_sv    LocaleType = "sv"
	Locale_sw    LocaleType = "sw"
	Locale_tr    LocaleType = "tr"
	Locale_uk    LocaleType = "uk"
	Locale_zh_CN LocaleType = "zh_CN"
	Locale_zh_TW LocaleType = "zh_TW"
)

func ParseLocale added in v0.0.3

func ParseLocale(s string) (l LocaleType, err error)

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger is the logging interface for expression descriptor.

type Option

type Option func(exprDesc *ExpressionDescriptor)

Option allows to configure expression descriptor.

func DayOfWeekStartsAtOne

func DayOfWeekStartsAtOne(v bool) Option

DayOfWeekStartsAtOne configures first day of the week is Monday (index 1) or Sunday (index 0, default).

func SetLocales

func SetLocales(locales ...LocaleType) Option

SetLocales initializes the list of initial locales that the expression descriptor will output in. By default, the expression descriptor always initialize English (Locale_en).

func SetLogger

func SetLogger(logger Logger) Option

SetLogger allows the expression descriptor to output log via logger.

func Use24HourTimeFormat

func Use24HourTimeFormat(v bool) Option

Use24HourTimeFormat configures the expression descriptor to output time in 24-hour format (14:00) or 12-hour format (2PM, default).

func Verbose

func Verbose(v bool) Option

Verbose sets the expression descriptor to output string in verbose format or not.

Example: cronExpression = "* * 5 * * * *"

  • verbose = false: Every second, between 05:00 and 05:59
  • verbose = true: Every second, every minute, between 05:00 and 05:59, every day

type Parser

type Parser interface {
	Parse(expr string) (exprParts []string, err error)
}

Parser represents the cron parser.

Directories

Path Synopsis
cmd
hcron command

Jump to

Keyboard shortcuts

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