From 7977b6cdb8794e59b551d693385dbff0c9b9b994 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 4 Apr 2019 21:43:26 +0300 Subject: [PATCH] Implement DurationUnit in JS, add doc summary and conversion test --- .../stdlib/js/src/kotlin/time/DurationUnit.kt | 35 +++++++++----- .../stdlib/js/src/kotlin/time/conversions.kt | 10 ---- .../jvm/src/kotlin/time/DurationUnitJvm.kt | 10 +--- .../stdlib/src/kotlin/time/DurationUnit.kt | 13 ++++-- .../stdlib/test/time/DurationUnitTest.kt | 46 +++++++++++++++++++ 5 files changed, 80 insertions(+), 34 deletions(-) delete mode 100644 libraries/stdlib/js/src/kotlin/time/conversions.kt create mode 100644 libraries/stdlib/test/time/DurationUnitTest.kt diff --git a/libraries/stdlib/js/src/kotlin/time/DurationUnit.kt b/libraries/stdlib/js/src/kotlin/time/DurationUnit.kt index 6f0f161665a..af1028026d5 100644 --- a/libraries/stdlib/js/src/kotlin/time/DurationUnit.kt +++ b/libraries/stdlib/js/src/kotlin/time/DurationUnit.kt @@ -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; -} \ No newline at end of file + 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 + } +} + diff --git a/libraries/stdlib/js/src/kotlin/time/conversions.kt b/libraries/stdlib/js/src/kotlin/time/conversions.kt deleted file mode 100644 index 98021b14bbf..00000000000 --- a/libraries/stdlib/js/src/kotlin/time/conversions.kt +++ /dev/null @@ -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") -} \ No newline at end of file diff --git a/libraries/stdlib/jvm/src/kotlin/time/DurationUnitJvm.kt b/libraries/stdlib/jvm/src/kotlin/time/DurationUnitJvm.kt index 578b136c60d..466f5e273cd 100644 --- a/libraries/stdlib/jvm/src/kotlin/time/DurationUnitJvm.kt +++ b/libraries/stdlib/jvm/src/kotlin/time/DurationUnitJvm.kt @@ -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 - -@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) diff --git a/libraries/stdlib/src/kotlin/time/DurationUnit.kt b/libraries/stdlib/src/kotlin/time/DurationUnit.kt index 1cfcd2f2d14..9bdc5a73ae8 100644 --- a/libraries/stdlib/src/kotlin/time/DurationUnit.kt +++ b/libraries/stdlib/src/kotlin/time/DurationUnit.kt @@ -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 diff --git a/libraries/stdlib/test/time/DurationUnitTest.kt b/libraries/stdlib/test/time/DurationUnitTest.kt new file mode 100644 index 00000000000..566e2e869a3 --- /dev/null +++ b/libraries/stdlib/test/time/DurationUnitTest.kt @@ -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) + } + + + +} \ No newline at end of file