mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 16:23:55 +00:00
partial fix
This commit is contained in:
parent
28f84d4e3a
commit
b4790b892a
@ -2,6 +2,7 @@ package dev.inmo.krontab.internal
|
||||
|
||||
import com.soywiz.klock.*
|
||||
import dev.inmo.krontab.KronScheduler
|
||||
import dev.inmo.krontab.utils.copy
|
||||
|
||||
/**
|
||||
* @param daysOfWeek 0-6
|
||||
@ -32,14 +33,14 @@ internal data class CronDateTime(
|
||||
}
|
||||
|
||||
internal val calculators = listOf(
|
||||
years ?.let { NearDateTimeCalculatorYears(it) },
|
||||
daysOfWeek ?.let { NearDateTimeCalculatorWeekDays(it) },
|
||||
NearDateTimeCalculatorMillis(arrayOf(0)),
|
||||
seconds ?.let { NearDateTimeCalculatorSeconds(it) },
|
||||
minutes ?.let { NearDateTimeCalculatorMinutes(it) },
|
||||
hours ?.let { NearDateTimeCalculatorHours(it) },
|
||||
daysOfMonth ?.let { NearDateTimeCalculatorDays(it) },
|
||||
months ?.let { NearDateTimeCalculatorMonths(it) },
|
||||
years ?.let { NearDateTimeCalculatorYears(it) },
|
||||
daysOfWeek ?.let { NearDateTimeCalculatorWeekDays(it) },
|
||||
)
|
||||
|
||||
internal fun toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime? {
|
||||
|
@ -4,17 +4,27 @@ import com.soywiz.klock.*
|
||||
import dev.inmo.krontab.utils.copy
|
||||
import kotlin.math.min
|
||||
|
||||
internal class NearDateTimeCalculator<T>(
|
||||
private val times: Array<T>,
|
||||
private val partGetter: (DateTime) -> T,
|
||||
private val partSetter: (DateTime, T) -> DateTime?
|
||||
) where T : Comparable<T>, T : Number {
|
||||
fun interface NearDateTimeCalculator {
|
||||
/**
|
||||
* @return pair of near [DateTime] for this checker and [Boolean] flag that all previous calculations must be
|
||||
* recalculated
|
||||
*/
|
||||
fun calculateNearTime(
|
||||
relativelyTo: DateTime
|
||||
): Pair<DateTime, Boolean>?
|
||||
}
|
||||
|
||||
internal class CommonNearDateTimeCalculator<T>(
|
||||
private val times: Array<T>,
|
||||
private val partGetter: (DateTime) -> T,
|
||||
private val partSetter: (DateTime, T) -> DateTime?
|
||||
) : NearDateTimeCalculator where T : Comparable<T>, T : Number {
|
||||
/**
|
||||
* @return pair of near [DateTime] for this checker and [Boolean] flag that all previous calculations must be
|
||||
* recalculated
|
||||
*/
|
||||
override fun calculateNearTime(
|
||||
relativelyTo: DateTime
|
||||
): Pair<DateTime, Boolean>? {
|
||||
val currentData = partGetter(relativelyTo)
|
||||
val greaterOrEquals = times.firstOrNull { it >= currentData }
|
||||
@ -33,7 +43,7 @@ internal class NearDateTimeCalculator<T>(
|
||||
|
||||
internal fun NearDateTimeCalculatorMillis(
|
||||
times: Array<Short>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.milliseconds.toShort() },
|
||||
{ dateTime, newOne ->
|
||||
@ -47,7 +57,7 @@ internal fun NearDateTimeCalculatorMillis(
|
||||
|
||||
internal fun NearDateTimeCalculatorSeconds(
|
||||
times: Array<Byte>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.seconds.toByte() },
|
||||
{ dateTime, newOne ->
|
||||
@ -61,7 +71,7 @@ internal fun NearDateTimeCalculatorSeconds(
|
||||
|
||||
internal fun NearDateTimeCalculatorMinutes(
|
||||
times: Array<Byte>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.minutes.toByte() },
|
||||
{ dateTime, newOne ->
|
||||
@ -75,7 +85,7 @@ internal fun NearDateTimeCalculatorMinutes(
|
||||
|
||||
internal fun NearDateTimeCalculatorHours(
|
||||
times: Array<Byte>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.hours.toByte() },
|
||||
{ dateTime, newOne ->
|
||||
@ -89,7 +99,7 @@ internal fun NearDateTimeCalculatorHours(
|
||||
|
||||
internal fun NearDateTimeCalculatorDays(
|
||||
times: Array<Byte>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.dayOfMonth.toByte() },
|
||||
{ dateTime, newOne ->
|
||||
@ -109,7 +119,7 @@ internal fun NearDateTimeCalculatorDays(
|
||||
|
||||
internal fun NearDateTimeCalculatorMonths(
|
||||
times: Array<Byte>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.dayOfMonth.toByte() },
|
||||
{ dateTime, newOne ->
|
||||
@ -130,11 +140,12 @@ internal fun NearDateTimeCalculatorMonths(
|
||||
|
||||
internal fun NearDateTimeCalculatorWeekDays(
|
||||
times: Array<Byte>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.dayOfWeek.index0.toByte() },
|
||||
{ dateTime, newOne ->
|
||||
val currentDayOfWeek = dateTime.dayOfWeek.index0
|
||||
if (newOne.toInt() == currentDayOfWeek) return@CommonNearDateTimeCalculator dateTime
|
||||
(if (newOne < currentDayOfWeek) {
|
||||
dateTime.plus(7.days - (currentDayOfWeek - newOne).days)
|
||||
} else {
|
||||
@ -150,11 +161,12 @@ internal fun NearDateTimeCalculatorWeekDays(
|
||||
|
||||
internal fun NearDateTimeCalculatorYears(
|
||||
times: Array<Int>
|
||||
) = NearDateTimeCalculator(
|
||||
) = CommonNearDateTimeCalculator(
|
||||
times,
|
||||
{ it.yearInt },
|
||||
{ dateTime, newOne ->
|
||||
val currentYear = dateTime.yearInt
|
||||
if (newOne == currentYear) return@CommonNearDateTimeCalculator dateTime
|
||||
(if (newOne < currentYear) {
|
||||
null
|
||||
} else {
|
||||
|
@ -67,8 +67,8 @@ class StringParseTest {
|
||||
val flow = kronScheduler.asFlow()
|
||||
|
||||
runTest {
|
||||
val ranges = rangesEnds.map { it.first .. it.second }.flatten().toMutableList()
|
||||
val expectedCollects = rangesEnds.sumOf { it.second - it.first + 1 }
|
||||
val ranges = rangesEnds.map { it.first .. it.second }.flatten().distinct().toMutableList()
|
||||
val expectedCollects = ranges.size
|
||||
var collected = 0
|
||||
|
||||
flow.takeWhile { ranges.isNotEmpty() }.collect {
|
||||
|
Loading…
Reference in New Issue
Block a user