-
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 3 commits
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 |
---|---|---|
|
@@ -53,5 +53,19 @@ 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.asClock(offset: Instant = Instant.fromEpochSeconds(0)): Clock = object : Clock { | ||
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. We've deliberated internally about this function, and it does seem useful. A couple of notes:
|
||
private val startMark: TimeMark = markNow() | ||
override fun now() = offset + startMark.elapsedNow() | ||
} | ||
|
||
@Deprecated("Use Clock.todayIn instead", ReplaceWith("this.todayIn(timeZone)"), DeprecationLevel.WARNING) | ||
public fun Clock.todayAt(timeZone: TimeZone): LocalDate = todayIn(timeZone) |
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 timeSourceAsClock() { | ||
val timeSource = TestTimeSource() | ||
val clock = timeSource.asClock() | ||
|
||
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.asClock() | ||
|
||
assertEquals(0, clock1.now().epochSeconds) | ||
|
||
timeSource += 1.seconds | ||
assertEquals(1, clock1.now().epochSeconds) | ||
|
||
val clock2 = timeSource.asClock(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.asClock(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.