Skip to content

Commit

Permalink
Merge pull request #103 from InsanusMokrassar/2.2.9
Browse files Browse the repository at this point in the history
2.2.9
  • Loading branch information
InsanusMokrassar authored Mar 16, 2024
2 parents 49f852f + 2efd7e6 commit 9546eb5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 21 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 2.2.9

* `Version`:
* `Kotlin`: `1.9.23`
* `Serialization`: `1.6.3`
* `Klock`: `5.3.2`
* Fixes in build-in schedulers

## 2.2.8

* `Version`:
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ android.useAndroidX=true
android.enableJetifier=false


kotlin_version=1.9.22
kotlin_version=1.9.23
kotlin_coroutines_version=1.8.0
kotlin_serialization_version=1.6.2
kotlin_serialization_version=1.6.3

dokka_version=1.9.20

klockVersion=5.3.1
klockVersion=5.4.0

## Github reease

Expand All @@ -36,5 +36,5 @@ androidx_work_version=2.9.0

## Common

version=2.2.8
android_code_version=38
version=2.2.9
android_code_version=39
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
35 changes: 30 additions & 5 deletions src/commonMain/kotlin/SchedulerShortcuts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,60 @@ val EverySecondScheduler: KronScheduler by lazy {
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one minute
*/
val EveryMinuteScheduler: KronScheduler by lazy {
buildSchedule { minutes { 0 every 1 } }
buildSchedule {
seconds { at(0) }
minutes { 0 every 1 }
}
}

/**
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one hour
*/
val EveryHourScheduler: KronScheduler by lazy {
buildSchedule { hours { 0 every 1 } }
buildSchedule {
seconds { at(0) }
minutes { at(0) }
hours { 0 every 1 }
}
}

/**
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one day
*/
val EveryDayOfMonthScheduler: KronScheduler by lazy {
buildSchedule { dayOfMonth { 0 every 1 } }
buildSchedule {
seconds { at(0) }
minutes { at(0) }
hours { at(0) }
dayOfMonth { 0 every 1 }
}
}

/**
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one month
*/
val EveryMonthScheduler: KronScheduler by lazy {
buildSchedule { months { 0 every 1 } }
buildSchedule {
seconds { at(0) }
minutes { at(0) }
hours { at(0) }
dayOfMonth { at(0) }
months { 0 every 1 }
}
}

/**
* [KronScheduler.next] will always return [korlibs.time.DateTime.now] + one year
*/
val EveryYearScheduler: KronScheduler by lazy {
buildSchedule { years { 0 every 1 } }
buildSchedule {
seconds { at(0) }
minutes { at(0) }
hours { at(0) }
dayOfMonth { at(0) }
months { at(0) }
years { 0 every 1 }
}
}

/**
Expand Down
29 changes: 19 additions & 10 deletions src/commonMain/kotlin/builder/SchedulerBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,24 @@ class SchedulerBuilder(
*/
fun build(): KronScheduler = offset ?.let {
createKronSchedulerWithOffset(
seconds,
minutes,
hours,
dayOfMonth,
month,
year,
dayOfWeek,
TimezoneOffset(it.minutes),
milliseconds ?: millisecondsArrayDefault
seconds = seconds,
minutes = minutes,
hours = hours,
dayOfMonth = dayOfMonth,
month = month,
years = year,
weekDays = dayOfWeek,
offset = TimezoneOffset(it.minutes),
milliseconds = milliseconds ?: millisecondsArrayDefault
)
} ?: createKronScheduler(seconds, minutes, hours, dayOfMonth, month, year, dayOfWeek, milliseconds ?: millisecondsArrayDefault)
} ?: createKronScheduler(
seconds = seconds,
minutes = minutes,
hours = hours,
dayOfMonth = dayOfMonth,
month = month,
years = year,
weekDays = dayOfWeek,
milliseconds = milliseconds ?: millisecondsArrayDefault
)
}
38 changes: 38 additions & 0 deletions src/commonTest/kotlin/BuildersTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.inmo.krontab.utils

import dev.inmo.krontab.*
import korlibs.time.*
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

class BuildersTest {
@Test
fun presetsWorksCorrectly() {
val data = mapOf(
EverySecondScheduler to { it: DateTime -> if (it.milliseconds > 0 ) it + 1.seconds - it.milliseconds.milliseconds else it },
EveryMinuteScheduler to { it: DateTime -> if (it.seconds > 0 || it.milliseconds > 0 ) it + 1.minutes - it.seconds.seconds - it.milliseconds.milliseconds else it },
EveryHourScheduler to { it: DateTime -> if (it.minutes > 0 || it.seconds > 0 || it.milliseconds > 0 ) it + 1.hours - it.minutes.minutes - it.seconds.seconds - it.milliseconds.milliseconds else it },
EveryDayOfMonthScheduler to { it: DateTime -> if (it.hours > 0 || it.minutes > 0 || it.seconds > 0 || it.milliseconds > 0 ) it + 1.days - it.hours.hours - it.minutes.minutes - it.seconds.seconds - it.milliseconds.milliseconds else it },
EveryMonthScheduler to { it: DateTime -> if (it.dayOfMonth > 1 || it.hours > 0 || it.minutes > 0 || it.seconds > 0 || it.milliseconds > 0 ) (it + 1.months).copy(dayOfMonth = 1, hour = 0, minute = 0, second = 0, milliseconds = 0) else it },
)
val samples = 10000

runTest {
var now = DateTime.now()
for (i in 0 until samples) {
data.forEach { (scheduler, expectCalculator) ->
val expectValue = expectCalculator(now)
val newNow = scheduler.nextOrRelative(now)

assertEquals(expectValue, newNow, "For time ${now.toStringDefault()} calculated wrong value: ${newNow.toStringDefault()} is not equal to ${expectValue.toStringDefault()}")

now = newNow
}
}
}
}
}

0 comments on commit 9546eb5

Please sign in to comment.