2019-10-08 17:17:51 +00:00
|
|
|
package com.insanusmokrassar.krontab.internal
|
2019-10-04 18:04:00 +00:00
|
|
|
|
2019-10-08 17:17:51 +00:00
|
|
|
import com.insanusmokrassar.krontab.utils.clamp
|
2019-10-05 10:29:59 +00:00
|
|
|
import com.soywiz.klock.DateTime
|
|
|
|
import com.soywiz.klock.DateTimeSpan
|
2019-10-04 18:04:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [month] 0-11
|
|
|
|
* [dayOfMonth] 0-31
|
2019-10-04 18:09:39 +00:00
|
|
|
* [hours] 0-23
|
|
|
|
* [minutes] 0-59
|
|
|
|
* [seconds] 0-59
|
2019-10-04 18:04:00 +00:00
|
|
|
*/
|
2019-10-05 05:23:48 +00:00
|
|
|
internal data class CronDateTime(
|
2019-10-04 18:04:00 +00:00
|
|
|
val month: Byte? = null,
|
|
|
|
val dayOfMonth: Byte? = null,
|
2019-10-04 18:09:39 +00:00
|
|
|
val hours: Byte? = null,
|
|
|
|
val minutes: Byte? = null,
|
|
|
|
val seconds: Byte? = null
|
2019-10-04 18:04:00 +00:00
|
|
|
) {
|
|
|
|
init {
|
2020-06-03 14:11:42 +00:00
|
|
|
check(month ?.let { it in monthRange } ?: true)
|
|
|
|
check(dayOfMonth ?.let { it in dayOfMonthRange } ?: true)
|
|
|
|
check(hours?.let { it in hoursRange } ?: true)
|
|
|
|
check(minutes?.let { it in minutesRange } ?: true)
|
|
|
|
check(seconds?.let { it in secondsRange } ?: true)
|
2019-10-04 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal val klockDayOfMonth = dayOfMonth ?.plus(1)
|
2019-10-05 04:55:01 +00:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
fun create(
|
|
|
|
month: Int? = null,
|
|
|
|
dayOfMonth: Int? = null,
|
|
|
|
hours: Int? = null,
|
|
|
|
minutes: Int? = null,
|
|
|
|
seconds: Int? = null
|
|
|
|
) = CronDateTime(
|
2020-06-03 14:11:42 +00:00
|
|
|
month ?.clamp(monthRange) ?.toByte(),
|
|
|
|
dayOfMonth ?.clamp(dayOfMonthRange) ?.toByte(),
|
|
|
|
hours ?.clamp(hoursRange) ?.toByte(),
|
|
|
|
minutes ?.clamp(minutesRange) ?.toByte(),
|
|
|
|
seconds ?.clamp(secondsRange) ?.toByte()
|
2019-10-05 04:55:01 +00:00
|
|
|
)
|
|
|
|
}
|
2019-10-04 18:04:00 +00:00
|
|
|
}
|
2019-10-05 10:29:59 +00:00
|
|
|
|
|
|
|
internal 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
|
|
|
|
}
|