From 6ac8f4c45d4be32065ef1c73396bb2c78060cce5 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Thu, 27 Jul 2023 01:28:13 +0200 Subject: [PATCH] Use mathematical modulo implementation for calculations (#616) See documentation (%/rem, mod) for differences. Example for "issue" that occurred: mathematical: -4 % 6 = 2 (expected) kotlin: -4 % 6 = -4 (unexpected) --- .../main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt | 2 +- server/src/main/kotlin/suwayomi/tachidesk/util/HAScheduler.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt index ecd754bc..3a526d0a 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt @@ -75,7 +75,7 @@ class Updater : IUpdater { val updateInterval = serverConfig.globalUpdateInterval.hours.coerceAtLeast(6.hours).inWholeMilliseconds val lastAutomatedUpdate = preferences.getLong(lastAutomatedUpdateKey, 0) - val timeToNextExecution = updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate) % updateInterval + val timeToNextExecution = (updateInterval - (System.currentTimeMillis() - lastAutomatedUpdate)).mod(updateInterval) val wasPreviousUpdateTriggered = System.currentTimeMillis() - (if (lastAutomatedUpdate > 0) lastAutomatedUpdate else System.currentTimeMillis()) < updateInterval if (!wasPreviousUpdateTriggered) { diff --git a/server/src/main/kotlin/suwayomi/tachidesk/util/HAScheduler.kt b/server/src/main/kotlin/suwayomi/tachidesk/util/HAScheduler.kt index 6ce4616c..966bae2a 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/util/HAScheduler.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/util/HAScheduler.kt @@ -62,7 +62,7 @@ class HATask(id: String, val interval: Long, execute: () -> Unit, val timerTask: private fun getElapsedTimeOfCurrentInterval(): Long { val timeSinceFirstExecution = System.currentTimeMillis() - firstExecutionTime - return timeSinceFirstExecution % interval + return timeSinceFirstExecution.mod(interval) } override fun getLastExecutionTime(): Long {