Implement DurationUnit in JS, add doc summary and conversion test

This commit is contained in:
Ilya Gorbunov
2019-04-04 21:43:26 +03:00
parent 37c5f2c54f
commit 7977b6cdb8
5 changed files with 80 additions and 34 deletions
@@ -1,37 +1,48 @@
/*
* 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.
* 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 enum class DurationUnit {
public actual enum class DurationUnit(internal val scale: Double) {
/**
* Time unit representing one nanosecond, which is 1/1000 of a microsecond.
*/
NANOSECONDS,
NANOSECONDS(1e0),
/**
* Time unit representing one microsecond, which is 1/1000 of a millisecond.
*/
MICROSECONDS,
MICROSECONDS(1e3),
/**
* Time unit representing one millisecond, which is 1/1000 of a second.
*/
MILLISECONDS,
MILLISECONDS(1e6),
/**
* Time unit representing one second.
*/
SECONDS,
SECONDS(1e9),
/**
* Time unit representing one minute.
*/
MINUTES,
MINUTES(60e9),
/**
* Time unit representing one hour.
*/
HOURS,
HOURS(3600e9),
/**
* Time unit representing one day, which always equals 24 hours.
* Time unit representing one day, which is always equal to 24 hours.
*/
DAYS;
}
DAYS(86400e9);
}
public actual fun convertDurationUnit(value: Double, sourceUnit: DurationUnit, targetUnit: DurationUnit): Double {
val sourceCompareTarget = sourceUnit.scale.compareTo(targetUnit.scale)
return when {
sourceCompareTarget > 0 -> value * (sourceUnit.scale / targetUnit.scale)
sourceCompareTarget < 0 -> value / (targetUnit.scale / sourceUnit.scale)
else -> value
}
}
@@ -1,10 +0,0 @@
/*
* 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.
*/
package kotlin.time
public actual fun convertDurationUnit(value: Double, sourceUnit: DurationUnit, targetUnit: DurationUnit): Double {
TODO("not implemented")
}
@@ -1,6 +1,6 @@
/*
* 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.
* 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.
*/
@file:kotlin.jvm.JvmMultifileClass()
@@ -8,14 +8,8 @@
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)
@@ -1,6 +1,6 @@
/*
* 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.
* 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.
*/
@file:kotlin.jvm.JvmMultifileClass()
@@ -9,6 +9,11 @@
package kotlin.time
/**
* The list of possible time measurement units, in which a duration can be expressed.
*
* The smallest time unit is [NANOSECONDS] and the largest is [DAYS], which corresponds to exactly 24 [HOURS].
*/
public expect enum class DurationUnit {
/**
* Time unit representing one nanosecond, which is 1/1000 of a microsecond.
@@ -35,12 +40,12 @@ public expect enum class DurationUnit {
*/
HOURS,
/**
* Time unit representing one day, which always equals 24 hours.
* Time unit representing one day, which is always equal to 24 hours.
*/
DAYS;
}
/** Converts the given time duration [value] expressed in the specified [sourceUnit] into the specified [targetUnit]. */
public expect fun convertDurationUnit(value: Double, sourceUnit: DurationUnit, targetUnit: DurationUnit): Double
@@ -0,0 +1,46 @@
/*
* 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 test.time
import kotlin.test.*
import kotlin.time.*
// TODO: Use star import after KT-30983 is fixed
import kotlin.time.DurationUnit.DAYS
import kotlin.time.DurationUnit.HOURS
import kotlin.time.DurationUnit.MINUTES
import kotlin.time.DurationUnit.SECONDS
import kotlin.time.DurationUnit.MILLISECONDS
import kotlin.time.DurationUnit.MICROSECONDS
import kotlin.time.DurationUnit.NANOSECONDS
class DurationUnitTest {
@Test
fun conversion() {
fun test(sourceValue: Double, sourceUnit: DurationUnit, targetValue: Double, targetUnit: DurationUnit) {
assertEquals(
targetValue, convertDurationUnit(sourceValue, sourceUnit, targetUnit),
"Expected $sourceValue $sourceUnit to be $targetValue $targetUnit"
)
assertEquals(
sourceValue, convertDurationUnit(targetValue, targetUnit, sourceUnit),
"Expected $targetValue $targetUnit to be $sourceValue $sourceUnit"
)
}
test(1.0, MINUTES, 60.0, SECONDS)
test(30.0, MINUTES, 0.5, HOURS)
test(12.0, HOURS, 0.5, DAYS)
test(720.0, MINUTES, 0.5, DAYS)
test(1.0, DAYS, 86400.0, SECONDS)
test(1.0, DAYS, 86400e9, NANOSECONDS)
test(50.0, NANOSECONDS, 0.05, MICROSECONDS)
test(50.0, NANOSECONDS, 50e-9, SECONDS)
test(16.0, MILLISECONDS, 0.016, SECONDS)
}
}