https://github.com/aalpar/cronfab
Extensible crontab parser for Go — define custom calendar fields (moon phases, fiscal quarters, etc.)
https://github.com/aalpar/cronfab
cron crontab crontab-go custom-calendars extensible go golang parser scheduler
Last synced: 4 months ago
JSON representation
Extensible crontab parser for Go — define custom calendar fields (moon phases, fiscal quarters, etc.)
- Host: GitHub
- URL: https://github.com/aalpar/cronfab
- Owner: aalpar
- License: mit
- Created: 2020-10-12T00:19:59.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2026-02-05T02:56:52.000Z (5 months ago)
- Last Synced: 2026-02-05T14:39:32.573Z (5 months ago)
- Topics: cron, crontab, crontab-go, custom-calendars, extensible, go, golang, parser, scheduler
- Language: Go
- Homepage:
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Cronfab
=======
Cronfab is a crontab time-and-date specification parser and processor with a configurable calendar.
For projects that need extensibility beyond standard cron libraries like [robfig/cron](https://github.com/robfig/cron), cronfab exposes a configurable field system: define custom calendar fields (e.g. week-of-month, moon phase) with named ranges and arbitrary units — no forking required.
All the standard crontab features are supported:
- units may be specified by number or name (with prefix matching — `jan`, `mon`, etc.)
- lists and ranges are supported
- step values are supported
- aliases: `@yearly`, `@annually`, `@monthly`, `@weekly`, `@daily`, `@midnight`, `@hourly`
Cronfab does not support shell command execution.
Built-in Configs
----------------
- **`DefaultCrontabConfig`** — classic 5-field: minute, hour, day-of-month, month, day-of-week
- **`SecondCrontabConfig`** — 7-field: second, minute, hour, day-of-month, week-of-month, month, day-of-week
Both configs include aliases (`@daily`, `@hourly`, etc.) that expand to their corresponding expressions.
Example
-------
```go
markers, err := cronfab.DefaultCrontabConfig.ParseCronTab("*/5 * * * *")
if err != nil {
log.Fatal(err)
}
next, err := cronfab.DefaultCrontabConfig.Next(markers, time.Now())
if err != nil {
log.Fatal(err)
}
fmt.Println(next)
```
Aliases work the same way:
```go
markers, err := cronfab.DefaultCrontabConfig.ParseCronTab("@daily")
```
Custom Calendars
----------------
`FieldConfig` fields are exported, so any package can construct its own calendar by implementing the `Unit` interface and calling `NewCrontabConfig`:
```go
config, err := cronfab.NewCrontabConfig([]cronfab.FieldConfig{
{
Unit: cronfab.HourUnit{},
Name: "hour",
Min: 0,
Max: 23,
GetIndex: func(t time.Time) int { return t.Hour() },
},
{
Unit: MoonPhaseUnit{},
Name: "moon phase",
RangeNames: []string{"new", "waxingcrescent", "firstquarter", "waxinggibbous", "full", "waninggibbous", "thirdquarter", "waningcrescent"},
Min: 0,
Max: 7,
GetIndex: moonPhaseIndex,
},
})
```
Custom configs can also define their own aliases by setting the `Aliases` map on the returned `*CrontabConfig`.
A full working example is in [examples/lunar](examples/lunar).