Bring back Duration construction extensions KT-46229
Return the extension properties for constructing Duration from numbers, but this time place them in Duration.Companion rather than on top-level. - provide the new set of construction extension properties in companion of Duration - leave top-level extension properties experimental, increase their deprecation level to ERROR, propose the extension properties in companion instead. - leave Duration companion static factory functions experimental, deprecate them and propose the extension properties in companion instead.
This commit is contained in:
@@ -8,6 +8,9 @@ package kotlin.jdk8.time.test
|
||||
import kotlin.random.Random
|
||||
import kotlin.test.*
|
||||
import kotlin.time.*
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlin.time.Duration.Companion.nanoseconds
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import java.time.Duration as JTDuration
|
||||
|
||||
class DurationConversionTest {
|
||||
@@ -15,7 +18,7 @@ class DurationConversionTest {
|
||||
fun twoWayConversion() {
|
||||
fun test(days: Int, hours: Int, minutes: Int, seconds: Int, millis: Int, nanos: Int) {
|
||||
val duration = with(Duration) {
|
||||
days(days) + hours(hours) + minutes(minutes) + seconds(seconds) + milliseconds(millis) + nanoseconds(nanos)
|
||||
days.days + hours.hours + minutes.minutes + seconds.seconds + millis.milliseconds + nanos.nanoseconds
|
||||
}
|
||||
val jtDuration = JTDuration.ZERO
|
||||
.plusDays(days.toLong())
|
||||
@@ -50,12 +53,12 @@ class DurationConversionTest {
|
||||
val duration1 = jtDuration1.toKotlinDuration()
|
||||
val duration2 = jtDuration1.toKotlinDuration()
|
||||
assertEquals(duration1, duration2)
|
||||
assertEquals(Duration.days(365 * 150), duration2)
|
||||
assertEquals((365 * 150).days, duration2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun kotlinToJavaClamping() {
|
||||
val duration = Duration.seconds(Long.MAX_VALUE) * 5
|
||||
val duration = Long.MAX_VALUE.seconds * 5
|
||||
val jtDuration = duration.toJavaDuration()
|
||||
assertEquals(JTDuration.ofSeconds(Long.MAX_VALUE), jtDuration)
|
||||
|
||||
@@ -66,7 +69,7 @@ class DurationConversionTest {
|
||||
@Test
|
||||
fun randomIsoConversionEquivalence() {
|
||||
repeat(100) {
|
||||
val duration = Duration.nanoseconds(Random.nextLong(-(1L shl 53) + 1, 1L shl 53))
|
||||
val duration = Random.nextLong(-(1L shl 53) + 1, 1L shl 53).nanoseconds
|
||||
val fromString = JTDuration.parse(duration.toIsoString())
|
||||
val fromDuration = duration.toJavaDuration()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user