A cron expression parser and schedule calculator written in MoonBit.
let cron = CronExpr::parse("*/15 9-17 * * 1-5")
match cron {
Ok(expr) => {
// Check if a time matches
let now = DateTime::new(2026, 7, 27, 9, 0)
println(expr.matches(now).to_string()) // true
// Get English description
println(expr.describe_en())
// Get Chinese description
println(expr.describe_cn())
// Get next 5 execution times
let times = expr.next_n(DateTime::new(2026, 7, 25, 12, 0), 5)
for time in times {
println(time.to_string())
}
}
Err(err) => println(cron_error_to_string(err))
}# Validate an expression
moon run cmd/main -- check "*/15 9-18 * * 1-5"
# Get English description
moon run cmd/main -- explain "0 9 * * 1-5"
# Get Chinese description
moon run cmd/main -- describe "0 9 * * 1-5" --cn
# Get next execution times
moon run cmd/main -- next "0 9 * * 1-5" --count 5
# Check if a time matches
moon run cmd/main -- matches "0 9 * * 1-5" --time "2026-07-27 09:00"| Syntax | Example | Meaning |
|---|---|---|
| * | * | Any value |
| Value | 5 | Specific value |
| List | 1,3,5 | Multiple values |
| Range | 1-5 | Consecutive range |
| Step | */10 | Every N units |
| Range+step | 10-50/5 | Range with step |
| Alias | Expression | Meaning |
|---|---|---|
| @hourly | 0 * * * * | Every hour at :00 |
| @daily | 0 0 * * * | Every day at midnight |
| @weekly | 0 0 * * 0 | Every Sunday midnight |
| @monthly | 0 0 1 * * | 1st of each month |
| @yearly | 0 0 1 1 * | January 1st |
| Module | Purpose |
|---|---|
| cron_expr | Main CronExpr type and public API |
| cron_field | Field-level parsing and value expansion |
| parser | Full expression parsing with alias support |
| validator | Validation with detailed error messages |
| matcher | Time matching against cron expressions |
| calculator | Next execution time computation |
| calendar | Calendar calculations (leap years, etc.) |
| datetime | DateTime type and operations |
| descriptor | Human-readable descriptions (EN/ZH) |
| error | Error types and formatting |
Invalid cron expression '61 * * * *': invalid minute value 61: expected 0..59
Invalid cron expression '*/0 * * * *': step value cannot be 0 in field 'minute'
Invalid cron expression '1--5 * * * *': invalid range syntax in field 'minute'
Invalid cron expression '* * * * * *': expected 5 fields but got 6import {
"001-Elsa/mooncron@0.1.0"
}// In moon.pkg:
// import {
// "001-Elsa/mooncron" @mooncron,
// }
// In your code:
let cron = @mooncron.CronExpr::parse("*/15 * * * *")mooncron/
├── cmd/main/ # CLI tool
│ ├── main.mbt
│ └── moon.pkg
├── *.mbt # Library source files
├── *_test.mbt # Test files
├── moon.mod # Module metadata
├── moon.pkg # Package configuration
├── README.md
└── LICENSEpub fn CronExpr::parse(raw : String) -> Result[CronExpr, CronError]
pub fn CronExpr::is_valid(self : CronExpr) -> Bool
pub fn CronExpr::matches(self : CronExpr, dt : DateTime) -> Bool
pub fn CronExpr::next_after(self : CronExpr, after : DateTime) -> DateTime?
pub fn CronExpr::next_n(self : CronExpr, after : DateTime, n : Int) -> Array[DateTime]
pub fn CronExpr::describe_en(self : CronExpr) -> String
pub fn CronExpr::describe_cn(self : CronExpr) -> String
pub fn CronExpr::to_string(self : CronExpr) -> String
pub fn CronExpr::raw(self : CronExpr) -> Stringpub struct DateTime {
year : Int
month : Int // 1..12
day : Int // 1..31
hour : Int // 0..23
minute : Int // 0..59
}
pub fn DateTime::new(year : Int, month : Int, day : Int, hour : Int, minute : Int) -> DateTime
pub fn DateTime::eq(self : DateTime, other : DateTime) -> Bool
pub fn DateTime::lt(self : DateTime, other : DateTime) -> Bool
pub fn DateTime::gt(self : DateTime, other : DateTime) -> Bool
pub fn DateTime::to_string(self : DateTime) -> String
pub fn parse_datetime(s : String) -> DateTime?pub fn is_leap_year(year : Int) -> Bool
pub fn days_in_month(year : Int, month : Int) -> Int
pub fn day_of_week(year : Int, month : Int, day : Int) -> Int // 0=Sun..6=Sat
pub fn is_valid_date(year : Int, month : Int, day : Int) -> Bool
pub fn is_valid_datetime(dt : DateTime) -> Boolpub fn validate(raw : String) -> Result[Unit, CronError]
pub fn validate_expression(raw : String) -> Result[Unit, CronError]
pub fn cron_error_to_string(err : CronError) -> Stringmoon testTotal tests: 66, passed: 66, failed: 0.pub struct CronBuilder {
seconds : String
minutes : String
hours : String
dom : String
month : String
dow : String
include_seconds : Bool
}pub enum CronErrorKind {
EmptyExpression
WrongFieldCount(Int)
InvalidFieldValue(String)
FieldValueOutOfRange(String, Int, Int, Int)
StepCannotBeZero(String)
InvalidRange(String)
InvalidSyntax(String)
InvalidCharacter(String)
}pub struct DateTime {
year : Int
month : Int
day : Int
hour : Int
minute : Int
}pub enum FieldType {
Second
Minute
Hour
DayOfMonth
Month
DayOfWeek
}pub struct FrequencyAnalysis {
per_minute : Int
per_hour : Int
per_day : Int
per_week : Int
per_month : Int
per_year : Int
max_gap_minutes : Int
avg_gap_minutes : Int
}pub enum ScheduleDensity {
VerySparse
Sparse
Moderate
Frequent
Dense
}fn day_of_week(year : Int, month : Int, day : Int) -> Intfn days_in_month(year : Int, month : Int) -> Intfn is_leap_year(year : Int) -> Boolfn is_valid_date(year : Int, month : Int, day : Int) -> Boolfn monthly_at(day : Int, hour : Int, minute : Int) -> Stringfn weekly_at(day_of_week : Int, hour : Int, minute : Int) -> Stringfn yearly_at(month : Int, day : Int, hour : Int, minute : Int) -> StringInstall
Download zipA cron expression parser and schedule calculator written in MoonBit.