Change Duration.INFINITE.toIsoString representation
This commit is contained in:
@@ -20,7 +20,7 @@ class Durations {
|
||||
assertPrints(Duration.seconds(86420).toIsoString(), "PT24H0M20S")
|
||||
assertPrints(Duration.days(2).toIsoString(), "PT48H")
|
||||
assertPrints(Duration.ZERO.toIsoString(), "PT0S")
|
||||
assertPrints(Duration.INFINITE.toIsoString(), "PT2147483647H")
|
||||
assertPrints(Duration.INFINITE.toIsoString(), "PT9999999999999H")
|
||||
}
|
||||
|
||||
@Sample
|
||||
|
||||
@@ -764,8 +764,14 @@ public value class Duration internal constructor(private val rawValue: Long) : C
|
||||
public fun toIsoString(): String = buildString {
|
||||
if (isNegative()) append('-')
|
||||
append("PT")
|
||||
absoluteValue.toComponents { hours, minutes, seconds, nanoseconds ->
|
||||
val hasHours = hours != 0
|
||||
val absoluteValue = this@Duration.absoluteValue
|
||||
absoluteValue.toComponents { _, minutes, seconds, nanoseconds ->
|
||||
var hours = absoluteValue.inWholeHours
|
||||
if (isInfinite()) {
|
||||
// use large enough value instead of Long.MAX_VALUE
|
||||
hours = 9_999_999_999_999
|
||||
}
|
||||
val hasHours = hours != 0L
|
||||
val hasSeconds = seconds != 0 || nanoseconds != 0
|
||||
val hasMinutes = minutes != 0 || (hasSeconds && hasHours)
|
||||
if (hasHours) {
|
||||
|
||||
@@ -485,7 +485,7 @@ class DurationTest {
|
||||
assertEquals("-PT24H15M", (-Duration.days(1) - Duration.minutes(15)).toIsoString())
|
||||
|
||||
// infinite
|
||||
assertEquals("PT2147483647H", Duration.INFINITE.toIsoString())
|
||||
assertEquals("PT9999999999999H", Duration.INFINITE.toIsoString())
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user