Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![JetBrains official project](https://jb.gg/badges/official.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
[![Maven Central](https://img.shields.io/maven-central/v/org.jetbrains.kotlinx/kotlinx-datetime.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22org.jetbrains.kotlinx%22%20AND%20a:%22kotlinx-datetime%22)
[![Kotlin](https://img.shields.io/badge/kotlin-1.7.0-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![Kotlin](https://img.shields.io/badge/kotlin-1.8.10-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![KDoc link](https://img.shields.io/badge/API_reference-KDoc-blue)](https://kotlinlang.org/api/kotlinx-datetime/)
[![Slack channel](https://img.shields.io/badge/chat-slack-blue.svg?logo=slack)](https://kotlinlang.slack.com/messages/kotlinx-datetime/)
[![TeamCity build](https://img.shields.io/teamcity/build/s/KotlinTools_KotlinxDatetime_Build_All.svg?server=http%3A%2F%2Fteamcity.jetbrains.com)](https://teamcity.jetbrains.com/viewType.html?buildTypeId=KotlinTools_KotlinxDatetime_Build_All&guest=1)
Expand Down
7 changes: 7 additions & 0 deletions core/common/src/Clock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public interface Clock {
}
}

/**
* Helper function to create a [Clock] instance with a custom [Instant] provider lambda.
*/
public fun Clock(instantProvider: () -> Instant): Clock = object : Clock {
override fun now(): Instant = instantProvider.invoke()
}

/**
* Returns the current date at the given [time zone][timeZone], according to [this Clock][this].
*/
Expand Down
14 changes: 6 additions & 8 deletions core/common/test/ClockTimeSourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ class ClockTimeSourceTest {

@Test
fun elapsed() {
val clock = object : Clock {
var instant = Clock.System.now()
override fun now(): Instant = instant
}
var instant = Clock.System.now()
val clock = Clock { instant }
val timeSource = clock.asTimeSource()
val mark = timeSource.markNow()
assertEquals(Duration.ZERO, mark.elapsedNow())

clock.instant += 1.days
instant += 1.days
assertEquals(1.days, mark.elapsedNow())

clock.instant -= 2.days
assertEquals(-1.days, mark.elapsedNow())
instant -= 2.days
assertEquals((-1).days, mark.elapsedNow())

clock.instant = Instant.MAX
instant = Instant.MAX
assertEquals(Duration.INFINITE, mark.elapsedNow())
}

Expand Down