commit c6ee1e2850caeb1724edd0e0ee7137cde5f6e094
parent 00667074eaa9218dc9a2d7eaf102f2360c6a8505
Author: MS <ms@taler.net>
Date: Wed, 6 Jan 2021 18:08:24 +0100
Introducing global clock.
Diffstat:
2 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/util/src/main/kotlin/time.kt b/util/src/main/kotlin/time.kt
@@ -22,6 +22,15 @@ package tech.libeufin.util
import java.time.*
import java.time.format.DateTimeFormatter
+private var LIBEUFIN_CLOCK = Clock.system(ZoneId.systemDefault())
+
+fun setClock(rel: Duration) {
+ LIBEUFIN_CLOCK = Clock.offset(LIBEUFIN_CLOCK, rel)
+}
+fun getNow(): ZonedDateTime {
+ return ZonedDateTime.now(LIBEUFIN_CLOCK)
+}
+
fun LocalDateTime.toZonedString(): String {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.atZone(ZoneId.systemDefault()))
}
diff --git a/util/src/test/kotlin/TimeTest.kt b/util/src/test/kotlin/TimeTest.kt
@@ -1,4 +1,6 @@
import org.junit.Test
+import tech.libeufin.util.getNow
+import tech.libeufin.util.setClock
import java.time.*
import java.time.format.DateTimeFormatter
@@ -7,14 +9,9 @@ import java.time.format.DateTimeFormatter
class TimeTest {
@Test
fun mock() {
- // Using the system clock, thus not mocking at all.
- val systemClock = Clock.system(ZoneId.systemDefault())
- val realNow = ZonedDateTime.now(systemClock)
- println(realNow)
- // Moving four hours later.
- val offsetTime = Clock.offset(systemClock, Duration.ofHours(4))
- val offesetNow = ZonedDateTime.now(offsetTime)
- println(offesetNow)
+ println(getNow())
+ setClock(Duration.ofHours(2))
+ println(getNow())
}
@Test