asClock
Creates a Clock that uses the TimeMark.markNow to determine how far the current moment is from the origin.
This clock stores the TimeMark at the moment of creation, so repeatedly creating Clocks from the same TimeSource results in different Instants iff the time of the TimeSource was increased. To sync different Clocks, use the origin parameter.
Since Kotlin
2.2Samples
import kotlin.time.*
import kotlin.time.Duration.Companion.seconds
fun main() {
//sampleStart
// Creating a TimeSource
// When testing a Clock in combination of kotlinx-coroutines-test, use the testTimeSource of the TestDispatcher.
val timeSource = TestTimeSource()
// Creating a clock by setting the specified origin
val clock = timeSource.asClock(origin = Instant.parse("2023-01-02T22:00:00Z"))
println(clock.now()) // 2023-01-02T22:00:00Z
// Advancing time in the timeSource
timeSource += 10.seconds
println(clock.now()) // 2023-01-02T22:00:10Z
//sampleEnd
}