diff --git a/src/commonMain/kotlin/dev/inmo/krontab/KronScheduler.kt b/src/commonMain/kotlin/dev/inmo/krontab/KronScheduler.kt index b7e214a..53af1c5 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/KronScheduler.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/KronScheduler.kt @@ -1,6 +1,7 @@ package dev.inmo.krontab import com.soywiz.klock.DateTime +import com.soywiz.klock.DateTimeTz import dev.inmo.krontab.internal.toNearDateTime /** @@ -19,9 +20,17 @@ interface KronScheduler { * @see dev.inmo.krontab.internal.CronDateTimeScheduler.next */ 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: DateTimeTz): DateTimeTz = next(relatively) ?: getAnyNext(relatively) suspend fun KronScheduler.nextOrNow(): DateTime = DateTime.now().let { next(it) ?: getAnyNext(it) } +suspend fun KronScheduler.nextOrNowWithOffset(): DateTimeTz = DateTimeTz.nowLocal().let { + next(it) ?: getAnyNext(it) +} diff --git a/src/commonMain/kotlin/dev/inmo/krontab/SchedulerShortcuts.kt b/src/commonMain/kotlin/dev/inmo/krontab/SchedulerShortcuts.kt index ed641e6..4b7945c 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/SchedulerShortcuts.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/SchedulerShortcuts.kt @@ -1,6 +1,7 @@ package dev.inmo.krontab import com.soywiz.klock.DateTime +import com.soywiz.klock.DateTimeTz import dev.inmo.krontab.builder.buildSchedule import dev.inmo.krontab.internal.* import dev.inmo.krontab.internal.CronDateTime @@ -17,6 +18,7 @@ internal fun getAnyNext(relatively: DateTime) = anyCronDateTime.toNearDateTime(r val AnyTimeScheduler: KronScheduler by lazy { CronDateTimeScheduler(listOf(anyCronDateTime)) } +internal suspend fun getAnyNext(relatively: DateTimeTz) = AnyTimeScheduler.next(relatively)!! /** * [KronScheduler.next] will always return [com.soywiz.klock.DateTime.now] + one second diff --git a/src/commonTest/kotlin/dev/inmo/krontab/utils/TimeZoneTest.kt b/src/commonTest/kotlin/dev/inmo/krontab/utils/TimeZoneTest.kt new file mode 100644 index 0000000..0b3bc2b --- /dev/null +++ b/src/commonTest/kotlin/dev/inmo/krontab/utils/TimeZoneTest.kt @@ -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 + ) + } + } + } +} \ No newline at end of file