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