diff --git a/dachlatten-datetime/build.gradle.kts b/dachlatten-datetime/build.gradle.kts index c0560d0..5f5333f 100644 --- a/dachlatten-datetime/build.gradle.kts +++ b/dachlatten-datetime/build.gradle.kts @@ -7,4 +7,6 @@ plugins { dependencies { compileOnly(libs.kotlinx.datetime) compileOnly(libs.android.support.annotations) + + testImplementation(libs.kotlinx.datetime) } diff --git a/dachlatten-datetime/src/main/kotlin/de/sipgate/dachlatten/datetime/FixedClock.kt b/dachlatten-datetime/src/main/kotlin/de/sipgate/dachlatten/datetime/FixedClock.kt new file mode 100644 index 0000000..2983640 --- /dev/null +++ b/dachlatten-datetime/src/main/kotlin/de/sipgate/dachlatten/datetime/FixedClock.kt @@ -0,0 +1,8 @@ +package de.sipgate.dachlatten.datetime + +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant + +fun Clock.Companion.Fixed(instant: Instant) = object : Clock { + override fun now(): Instant = instant +} diff --git a/dachlatten-datetime/src/test/kotlin/de/sipgate/dachlatten/datetime/FixedClockTest.kt b/dachlatten-datetime/src/test/kotlin/de/sipgate/dachlatten/datetime/FixedClockTest.kt new file mode 100644 index 0000000..97446ce --- /dev/null +++ b/dachlatten-datetime/src/test/kotlin/de/sipgate/dachlatten/datetime/FixedClockTest.kt @@ -0,0 +1,30 @@ +package de.sipgate.dachlatten.datetime + +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class FixedClockTest { + @Test + fun fixedClockReturnsFixedInstance() { + val instant = Instant.fromEpochSeconds(1713453869) + val fixedClock = Clock.Fixed(instant) + + val instantFromClock = fixedClock.now() + + assertEquals(instant, instantFromClock) + } + + @Test + fun fixedClockReturnsFixedInstanceWhenCalledMultipleTimes() { + val instant = Instant.fromEpochSeconds(1713453869) + val fixedClock = Clock.Fixed(instant) + + val firstInstantFromClock = fixedClock.now() + val secondInstantFromClock = fixedClock.now() + + assertEquals(instant, firstInstantFromClock) + assertEquals(firstInstantFromClock, secondInstantFromClock) + } +}