Initial prototype of Duration and Clocks API

This commit is contained in:
Ilya Gorbunov
2019-04-03 20:43:22 +03:00
parent 7749afb13e
commit 37c5f2c54f
12 changed files with 533 additions and 0 deletions
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmMultifileClass()
@file:kotlin.jvm.JvmName("DurationUnitKt")
package kotlin.time
//Actual typealias 'DurationUnit' has no corresponding expected declaration
//The following declaration is incompatible because modality is different:
// public final expect enum class DurationUnit : Enum<DurationUnit>
@Suppress("ACTUAL_WITHOUT_EXPECT")
public actual typealias DurationUnit = java.util.concurrent.TimeUnit
public actual fun convertDurationUnit(value: Double, sourceUnit: DurationUnit, targetUnit: DurationUnit): Double {
val sourceInTargets = targetUnit.convert(1, sourceUnit)
if (sourceInTargets > 0)
return value * sourceInTargets
val otherInThis = sourceUnit.convert(1, targetUnit)
return value / otherInThis
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.time
public actual object MonoClock : LongReadingClock(), Clock { // TODO: interface should not be required here
override fun reading(): Long = System.nanoTime()
override fun toString(): String = "Clock(System.nanoTime())"
override val unit: DurationUnit = DurationUnit.NANOSECONDS
}
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.time
import java.util.*
internal actual fun formatToDecimals(value: Double, decimals: Int, unitName: String): String =
String.format(Locale.ROOT, "%.${decimals}f%s", value, unitName)