krontab/src/commonMain/kotlin/com/insanusmokrassar/krontab/StringParser.kt

58 lines
1.7 KiB
Kotlin
Raw Normal View History

package com.insanusmokrassar.krontab
2019-10-05 05:09:22 +00:00
import com.insanusmokrassar.krontab.internal.*
2019-10-05 05:09:22 +00:00
2019-10-10 10:43:52 +00:00
/**
* Parse [incoming] string and adapt according to next format: "* * * * *" where order of things:
*
2020-06-03 14:41:54 +00:00
* * seconds
* * minutes
* * hours
* * dayOfMonth
* * month
2019-10-10 10:43:52 +00:00
*
* And each one have next format:
*
2020-07-24 08:18:18 +00:00
* `{number}[,{number},...]` or `*`
2019-10-10 10:43:52 +00:00
*
2020-06-03 14:41:54 +00:00
* and {number} here is one of
2019-10-10 10:43:52 +00:00
*
2020-06-03 14:41:54 +00:00
* * {int}-{int}
* * {int}/{int}
* * */{int}
* * {int}
2019-10-10 10:43:52 +00:00
*
2020-06-03 14:41:54 +00:00
* Additional info about ranges can be found in follow accordance:
*
* * Seconds ranges can be found in [secondsRange]
* * Minutes ranges can be found in [minutesRange]
* * Hours ranges can be found in [hoursRange]
* * Days of month ranges can be found in [dayOfMonthRange]
* * Months ranges can be found in [monthRange]
*
* Examples:
*
* * "0/5 * * * *" for every five seconds triggering
* * "0/15 30 * * *" for every 15th seconds in a half of each hour
* * "1 2 3 4 5" for triggering in near first second of second minute of third hour of fourth day of may
*
* @see com.insanusmokrassar.krontab.internal.createKronScheduler
2019-10-10 10:43:52 +00:00
*/
fun createSimpleScheduler(incoming: String): KronScheduler {
2019-10-05 10:40:51 +00:00
val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = incoming.split(" ")
val secondsParsed = parseSeconds(secondsSource)
val minutesParsed = parseMinutes(minutesSource)
val hoursParsed = parseHours(hoursSource)
val dayOfMonthParsed = parseDaysOfMonth(dayOfMonthSource)
val monthParsed = parseMonths(monthSource)
return createKronScheduler(
secondsParsed, minutesParsed, hoursParsed, dayOfMonthParsed, monthParsed
)
}
/**
2020-07-24 08:18:18 +00:00
* Shortcut for [createSimpleScheduler]
*/
fun buildSchedule(incoming: String): KronScheduler = createSimpleScheduler(incoming)