Change Duration.INFINITE.toIsoString representation

This commit is contained in:
Ilya Gorbunov
2021-07-04 09:46:59 +03:00
committed by Space
parent ae3d9cc3cd
commit 7ab6f6c9b2
3 changed files with 10 additions and 4 deletions
@@ -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
+8 -2
View File
@@ -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) {
+1 -1
View File
@@ -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