Skip to content

Commit

Permalink
Leverage kotlinx-datetime for date and time API
Browse files Browse the repository at this point in the history
For now, common classes are copied from
https://github.com/Kotlin/kotlinx-datetime and
WASI implementation of currentTime() is provided.

Timezones are not managed yet.

Closes #9
  • Loading branch information
sdeleuze committed Jan 21, 2023
1 parent c7fab0a commit 983a569
Show file tree
Hide file tree
Showing 30 changed files with 4,189 additions and 28 deletions.
3 changes: 2 additions & 1 deletion samples/wasi-sample/src/wasmMain/kotlin/WasiSample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

import kotlinx.datetime.Clock
import org.kowasm.wasi.*

fun main() {
val wasi: Wasi = DefaultWasi
wasi.println("Hello, world!")
wasi.println(wasi.now().toString())
wasi.println(Clock.System.now().toString())
wasi.createDirectory("testDir")
wasi.createFile("testFile")
wasi.listDirectoryEntries(".").forEach(wasi::println)
Expand Down
57 changes: 57 additions & 0 deletions wasi/src/commonMain/kotlin/kotlinx/datetime/Clock.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2019-2020 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

import kotlin.time.*

/**
* A source of [Instant] values.
*
* See [Clock.System][Clock.System] for the clock instance that queries the operating system.
*/
public interface Clock {
/**
* Returns the [Instant] corresponding to the current time, according to this clock.
*/
public fun now(): Instant

/**
* The [Clock] instance that queries the operating system as its source of knowledge of time.
*/
public object System : Clock {
override fun now(): Instant = @Suppress("DEPRECATION_ERROR") Instant.now()
}

public companion object {

}
}

/**
* Returns the current date at the given [time zone][timeZone], according to [this Clock][this].
*/
public fun Clock.todayIn(timeZone: TimeZone): LocalDate =
now().toLocalDateTime(timeZone).date

/**
* Returns a [TimeSource] that uses this [Clock] to mark a time instant and to find the amount of time elapsed since that mark.
*/
@ExperimentalTime
public fun Clock.asTimeSource(): TimeSource = object : TimeSource {
override fun markNow(): TimeMark = InstantTimeMark(now(), this@asTimeSource)
}

@ExperimentalTime
private class InstantTimeMark(private val instant: Instant, private val clock: Clock) : TimeMark {
override fun elapsedNow(): Duration = clock.now() - instant

override fun plus(duration: Duration): TimeMark = InstantTimeMark(instant + duration, clock)

override fun minus(duration: Duration): TimeMark = InstantTimeMark(instant - duration, clock)
}

@Deprecated("Use Clock.todayIn instead", ReplaceWith("this.todayIn(timeZone)"), DeprecationLevel.WARNING)
public fun Clock.todayAt(timeZone: TimeZone): LocalDate = todayIn(timeZone)
Loading

0 comments on commit 983a569

Please sign in to comment.