KCron
Cron realization for Kotlin Multiplatform

Features
- Kotlin Multiplatform library
- Parse Cron expressions
- Include L, W, LW, '?' and #
- Build Cron expression by smart builder functions:
builder
.seconds(10 at 0)
.minutes(5..25)
.hours(5, 12)
.daysOfWeek(7 on 5)
.years(2050)
- Build Cron expression via Kotlin style function:
cron {
seconds(10 at 0)
minutes(5..25)
hours(5, 12)
daysOfWeek(7 on 5)
years(2050)
}
- Support custom first week day
val builder = Builder(WeekDays.Sunday)
builder
.daysOfWeek(7 on 5)
- Parsing validation includes combination rules
- 'days' and 'days of week' could not be setup simultaneously
Usage
KCron-Common library as default implementation uses Kotlinx-DateTime library
Add with Gradle
kotlin {
sourceSets {
commonMain {
dependencies {
implementation 'com.ucasoft.kcron:kcron-common:0.31.7'
}
}
}
}
Build Cron expression
val builder = Cron.builder()
println(builder.expression)
builder
.seconds(10 at 0)
.minutes(5..25)
.hours(5, 12)
.daysOfWeek(7 on 5)
.years(2050)
println(builder.expression)
Parse as Classic as well as Modern Cron expressions
val builder = Cron.parseAndBuild("0/10 5-25 5,12 ? * 7#5 2050") {
it.firstDayOfWeek = WeekDays.Sunday
}
@OptIn(DelicateIterableApi::class)
println(builder.asIterable().take(10))
try {
val builder = Cron.parseAndBuild() {
it.version = Version.Classic
}
} (e: WrongCronExpression) {
println(e.message)
}
Days of week and months can be defined in a parsed expression as numbers as well as short names
builder = Cron.parseAndBuild("15/10 5-25 5 ? JAN,MAR 2,3,4,5 2050")
println(builder.nextRun)
builder = Cron.parseAndBuild("15/10 5-25 5 ? 2-4 MON 2050")
println(builder.nextRun)
Easy change any part of expression
val builder = Cron.parseAndBuild("0/10 5-25 5,12 ? * SUN#5 2050")
builder.years(2021..2025)
println(builder.expression)
Current status
This library is on beta version 0.31.7.
It is continuing to develop.
Check the news!