mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 16:23:55 +00:00
add work with time zones
This commit is contained in:
parent
68c49faa1d
commit
5311e651f4
@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.krontab
|
package dev.inmo.krontab
|
||||||
|
|
||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
|
import com.soywiz.klock.DateTimeTz
|
||||||
import dev.inmo.krontab.internal.toNearDateTime
|
import dev.inmo.krontab.internal.toNearDateTime
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,9 +20,17 @@ interface KronScheduler {
|
|||||||
* @see dev.inmo.krontab.internal.CronDateTimeScheduler.next
|
* @see dev.inmo.krontab.internal.CronDateTimeScheduler.next
|
||||||
*/
|
*/
|
||||||
suspend fun next(relatively: DateTime = DateTime.now()): DateTime?
|
suspend fun next(relatively: DateTime = DateTime.now()): DateTime?
|
||||||
|
|
||||||
|
suspend fun next(relatively: DateTimeTz): DateTimeTz? {
|
||||||
|
return next(relatively.utc) ?.toOffset(relatively.offset)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun KronScheduler.nextOrRelative(relatively: DateTime = DateTime.now()): DateTime = next(relatively) ?: getAnyNext(relatively)
|
suspend fun KronScheduler.nextOrRelative(relatively: DateTime = DateTime.now()): DateTime = next(relatively) ?: getAnyNext(relatively)
|
||||||
|
suspend fun KronScheduler.nextOrRelative(relatively: DateTimeTz): DateTimeTz = next(relatively) ?: getAnyNext(relatively)
|
||||||
suspend fun KronScheduler.nextOrNow(): DateTime = DateTime.now().let {
|
suspend fun KronScheduler.nextOrNow(): DateTime = DateTime.now().let {
|
||||||
next(it) ?: getAnyNext(it)
|
next(it) ?: getAnyNext(it)
|
||||||
}
|
}
|
||||||
|
suspend fun KronScheduler.nextOrNowWithOffset(): DateTimeTz = DateTimeTz.nowLocal().let {
|
||||||
|
next(it) ?: getAnyNext(it)
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.krontab
|
package dev.inmo.krontab
|
||||||
|
|
||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
|
import com.soywiz.klock.DateTimeTz
|
||||||
import dev.inmo.krontab.builder.buildSchedule
|
import dev.inmo.krontab.builder.buildSchedule
|
||||||
import dev.inmo.krontab.internal.*
|
import dev.inmo.krontab.internal.*
|
||||||
import dev.inmo.krontab.internal.CronDateTime
|
import dev.inmo.krontab.internal.CronDateTime
|
||||||
@ -17,6 +18,7 @@ internal fun getAnyNext(relatively: DateTime) = anyCronDateTime.toNearDateTime(r
|
|||||||
val AnyTimeScheduler: KronScheduler by lazy {
|
val AnyTimeScheduler: KronScheduler by lazy {
|
||||||
CronDateTimeScheduler(listOf(anyCronDateTime))
|
CronDateTimeScheduler(listOf(anyCronDateTime))
|
||||||
}
|
}
|
||||||
|
internal suspend fun getAnyNext(relatively: DateTimeTz) = AnyTimeScheduler.next(relatively)!!
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [KronScheduler.next] will always return [com.soywiz.klock.DateTime.now] + one second
|
* [KronScheduler.next] will always return [com.soywiz.klock.DateTime.now] + one second
|
||||||
|
23
src/commonTest/kotlin/dev/inmo/krontab/utils/TimeZoneTest.kt
Normal file
23
src/commonTest/kotlin/dev/inmo/krontab/utils/TimeZoneTest.kt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package dev.inmo.krontab.utils
|
||||||
|
|
||||||
|
import com.soywiz.klock.*
|
||||||
|
import dev.inmo.krontab.builder.buildSchedule
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class TimeZoneTest {
|
||||||
|
@Test
|
||||||
|
fun testDifferentTimeZonesReturnsDifferentTimes() {
|
||||||
|
val scheduler = buildSchedule { seconds { every(1) } }
|
||||||
|
val now = DateTime.now()
|
||||||
|
runTest {
|
||||||
|
for (i in 0 .. 24) {
|
||||||
|
val nowTz = now.toOffset(i.hours)
|
||||||
|
val next = scheduler.next(nowTz)!!
|
||||||
|
assertEquals(
|
||||||
|
(nowTz + 1.seconds).utc.unixMillisLong, next.utc.unixMillisLong
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user