Suggest duration static factories instead of number extension properties
Introduce Duration companion functions to convert numbers to Duration. Deprecate number extension properties and propose to use these new functions instead.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2021 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.
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.time.*
|
||||
@ExperimentalTime
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun java.time.Duration.toKotlinDuration(): Duration =
|
||||
this.seconds.seconds + this.nano.nanoseconds
|
||||
Duration.seconds(this.seconds) + Duration.nanoseconds(this.nano)
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2021 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.
|
||||
*/
|
||||
|
||||
@@ -15,7 +15,9 @@ class DurationConversionTest {
|
||||
@Test
|
||||
fun twoWayConversion() {
|
||||
fun test(days: Int, hours: Int, minutes: Int, seconds: Int, millis: Int, nanos: Int) {
|
||||
val duration = days.days + hours.hours + minutes.minutes + seconds.seconds + millis.milliseconds + nanos.nanoseconds
|
||||
val duration = with(Duration) {
|
||||
days(days) + hours(hours) + minutes(minutes) + seconds(seconds) + milliseconds(millis) + nanoseconds(nanos)
|
||||
}
|
||||
val jtDuration = JTDuration.ZERO
|
||||
.plusDays(days.toLong())
|
||||
.plusHours(hours.toLong())
|
||||
@@ -44,12 +46,12 @@ class DurationConversionTest {
|
||||
fun javaToKotlinRounding() {
|
||||
val jtDuration = JTDuration.ofDays(105).plusNanos(1)
|
||||
val duration = jtDuration.toKotlinDuration()
|
||||
assertEquals(105.days, duration)
|
||||
assertEquals(Duration.days(105), duration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun kotlinToJavaClamping() {
|
||||
val duration = Long.MAX_VALUE.seconds * 5
|
||||
val duration = Duration.seconds(Long.MAX_VALUE) * 5
|
||||
val jtDuration = duration.toJavaDuration()
|
||||
assertEquals(JTDuration.ofSeconds(Long.MAX_VALUE), jtDuration)
|
||||
|
||||
@@ -60,7 +62,7 @@ class DurationConversionTest {
|
||||
@Test
|
||||
fun randomIsoConversionEquivalence() {
|
||||
repeat(100) {
|
||||
val duration = Random.nextLong(-(1 shl 53) + 1, 1 shl 53).nanoseconds
|
||||
val duration = Duration.nanoseconds(Random.nextLong(-(1 shl 53) + 1, 1 shl 53))
|
||||
val fromString = JTDuration.parse(duration.toIsoString())
|
||||
val fromDuration = duration.toJavaDuration()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user