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)
This commit is contained in:
schroda
2023-07-27 01:28:13 +02:00
committed by GitHub
parent 7ebefa7c42
commit 6ac8f4c45d
2 changed files with 2 additions and 2 deletions
@@ -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) {
@@ -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 {