Home

Awesome

cron

<p align="left"> <a href="https://godoc.org/github.com/lnquy/cron" title="GoDoc Reference" rel="nofollow"><img src="https://img.shields.io/badge/go-documentation-blue.svg?style=flat" alt="GoDoc Reference"></a> <a href="https://github.com/github.com/lnquy/cron/releases/tag/v1.0.0" title="1.0.0 Release" rel="nofollow"><img src="https://img.shields.io/badge/version-1.0.0-blue.svg?style=flat" alt="1.0.0 release"></a> <a href="https://goreportcard.com/report/github.com/lnquy/cron"><img src="https://goreportcard.com/badge/github.com/lnquy/cron" alt="Code Status" /></a> <a href="https://travis-ci.org/lnquy/cron"><img src="https://travis-ci.org/lnquy/cron.svg?branch=master" alt="Build Status" /></a> <a href='https://coveralls.io/github/lnquy/cron?branch=master'><img src='https://coveralls.io/repos/github/lnquy/cron/badge.svg?branch=master' alt='Coverage Status' /></a> <br /> </p>

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

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 CodeLanguageContributors
csCzechhanbar
daDanishRasmus Melchior Jacobsen
deGermanMichael Schuler
enEnglishBrady Holt
esSpanishIvan Santos
faFarsiA. Bahrami
fiFinnishMikael Rosenberg
frFrenchArnaud TAMAILLON
heHebrewIlan Firsov
itItalianrinaldihno
jaJapaneseAlin Sarivan
koKoreanIon Mincu
nbNorwegianSiarhei Khalipski
nlDutchTotalMace
plPolishfoka
pt_BRPortuguese (Brazil)Renato Lima
roRomanianIllegitimis
ruRussianLbISS
skSlovakianhanbar
slSlovenianJani Bevk
svSwedishroobin
swSwahiliLeylow Lujuo
trTurkishMustafa SADEDİL
ukUkrainianTaras
zh_CNChinese (Simplified)Star Peng
zh_TWChinese (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")
  -print-all
        Also print all the lines which is not a valid cron
  -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

License

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