-
Notifications
You must be signed in to change notification settings - Fork 110
Add TimeSource asClock converter #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b544b69
717d964
7175420
da6dfb9
17dfaad
b0606d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,17 @@ private class InstantTimeMark(private val instant: Instant, private val clock: C | |
|
||
override fun minus(duration: Duration): TimeMark = InstantTimeMark(instant - duration, clock) | ||
} | ||
|
||
/** | ||
* Returns the [Clock] by storing an initial [TimeMark] using [TimeSource.markNow] and [returns][Clock.now] the elapsed | ||
* time using [TimeMark.elapsedNow] plus the provided [offset]. | ||
* | ||
* This clock stores the initial [TimeMark], so repeatedly creating [Clock]s from the same [TimeSource] results | ||
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* into different [Instant]s iff the time of the [TimeSource] was increased. To sync different [Clock]s, use the [offset] | ||
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* parameter. | ||
*/ | ||
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@ExperimentalTime | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like, after all this time, this is no longer needed!
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public fun TimeSource.toClock(offset: Instant = Instant.fromEpochSeconds(0)): Clock = object : Clock { | ||
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered some other default value for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did. The main use-case is converting the testTimeSource of coroutines to a Clock for high-level datetime usage, like Instant, with coroutines sync support, like |
||
private val startMark: TimeMark = markNow() | ||
override fun now() = offset + startMark.elapsedNow() | ||
} |
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2019-2021 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime.test | ||
|
||
import kotlinx.datetime.* | ||
import kotlin.test.* | ||
import kotlin.time.* | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@ExperimentalTime | ||
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class ClockTest { | ||
@Test | ||
fun timeSourceToClock() { | ||
val timeSource = TestTimeSource() | ||
val clock = timeSource.toClock() | ||
|
||
assertEquals(Instant.fromEpochSeconds(0), clock.now()) | ||
assertEquals(Instant.fromEpochSeconds(0), clock.now()) | ||
|
||
timeSource += 1.seconds | ||
assertEquals(Instant.fromEpochSeconds(1), clock.now()) | ||
assertEquals(Instant.fromEpochSeconds(1), clock.now()) | ||
} | ||
|
||
@Test | ||
fun syncMultipleClocksFromTimeSource() { | ||
val timeSource = TestTimeSource() | ||
val clock1 = timeSource.toClock() | ||
|
||
assertEquals(0, clock1.now().epochSeconds) | ||
|
||
timeSource += 1.seconds | ||
assertEquals(1, clock1.now().epochSeconds) | ||
|
||
val clock2 = timeSource.toClock(offset = Instant.fromEpochSeconds(1)) | ||
assertEquals(clock1.now(), clock2.now()) | ||
|
||
timeSource += 1.seconds | ||
assertEquals(2, clock1.now().epochSeconds) | ||
assertEquals(clock1.now(), clock2.now()) | ||
|
||
val clock3 = timeSource.toClock(offset = clock2.now()) | ||
timeSource += 1.seconds | ||
assertEquals(3, clock3.now().epochSeconds) | ||
assertEquals(clock1.now(), clock2.now()) | ||
assertEquals(clock1.now(), clock3.now()) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.