mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 16:23:55 +00:00
add base scheduling
This commit is contained in:
parent
e5e4770346
commit
fa68bf72dc
@ -46,34 +46,3 @@ data class CronDateTime(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime {
|
||||
var current = relativelyTo
|
||||
|
||||
seconds?.let {
|
||||
val left = it - current.seconds
|
||||
current += DateTimeSpan(minutes = if (left < 0) 1 else 0, seconds = left)
|
||||
}
|
||||
|
||||
minutes?.let {
|
||||
val left = it - current.minutes
|
||||
current += DateTimeSpan(hours = if (left < 0) 1 else 0, minutes = left)
|
||||
}
|
||||
|
||||
hours?.let {
|
||||
val left = it - current.hours
|
||||
current += DateTimeSpan(days = if (left < 0) 1 else 0, hours = left)
|
||||
}
|
||||
|
||||
klockDayOfMonth ?.let {
|
||||
val left = it - current.dayOfMonth
|
||||
current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left)
|
||||
}
|
||||
|
||||
month ?.let {
|
||||
val left = it - current.month0
|
||||
current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left)
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.github.insanusmokrassar.krontab
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
private val anyCronDateTime by lazy {
|
||||
CronDateTime()
|
||||
}
|
||||
|
||||
data class CronDateTimeScheduler(
|
||||
internal val cronDateTimes: List<CronDateTime>
|
||||
)
|
||||
|
||||
internal fun CronDateTimeScheduler.next(relatively: DateTime = DateTime.now()): DateTime {
|
||||
return cronDateTimes.map { it.toNearDateTime(relatively) }.min() ?: anyCronDateTime.toNearDateTime(relatively)
|
||||
}
|
||||
|
||||
suspend fun CronDateTimeScheduler.doInLoop(block: () -> Boolean) {
|
||||
do {
|
||||
delay(next().unixMillisLong - DateTime.now().unixMillisLong)
|
||||
} while (block())
|
||||
}
|
@ -1,3 +1,37 @@
|
||||
package com.github.insanusmokrassar.krontab
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import com.soywiz.klock.DateTimeSpan
|
||||
|
||||
|
||||
|
||||
fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime {
|
||||
var current = relativelyTo
|
||||
|
||||
seconds?.let {
|
||||
val left = it - current.seconds
|
||||
current += DateTimeSpan(minutes = if (left < 0) 1 else 0, seconds = left)
|
||||
}
|
||||
|
||||
minutes?.let {
|
||||
val left = it - current.minutes
|
||||
current += DateTimeSpan(hours = if (left < 0) 1 else 0, minutes = left)
|
||||
}
|
||||
|
||||
hours?.let {
|
||||
val left = it - current.hours
|
||||
current += DateTimeSpan(days = if (left < 0) 1 else 0, hours = left)
|
||||
}
|
||||
|
||||
klockDayOfMonth ?.let {
|
||||
val left = it - current.dayOfMonth
|
||||
current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left)
|
||||
}
|
||||
|
||||
month ?.let {
|
||||
val left = it - current.month0
|
||||
current += DateTimeSpan(months = if (left < 0) 1 else 0, days = left)
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user