Use days component in the default duration format KT-42851
This commit is contained in:
@@ -25,8 +25,8 @@ class Durations {
|
||||
|
||||
@Sample
|
||||
fun toStringDefault() {
|
||||
assertPrints(Duration.days(45), "1080h")
|
||||
assertPrints(Duration.days(1.5), "36h")
|
||||
assertPrints(Duration.days(45), "45d")
|
||||
assertPrints(Duration.days(1.5), "1d 12h")
|
||||
assertPrints(Duration.minutes(1230), "20h 30m")
|
||||
assertPrints(Duration.minutes(920), "15h 20m")
|
||||
assertPrints(Duration.seconds(1.546), "1.546s")
|
||||
|
||||
@@ -670,15 +670,22 @@ public value class Duration internal constructor(private val rawValue: Long) : C
|
||||
buildString {
|
||||
if (isNegative()) append('-')
|
||||
absoluteValue.run {
|
||||
toComponents { _, minutes, seconds, nanoseconds ->
|
||||
val hours = inWholeHours
|
||||
val hasHours = hours != 0L
|
||||
toComponents { _, hours, minutes, seconds, nanoseconds ->
|
||||
val days = inWholeDays
|
||||
val hasDays = days != 0L
|
||||
val hasHours = hours != 0
|
||||
val hasMinutes = minutes != 0
|
||||
val hasSeconds = seconds != 0 || nanoseconds != 0
|
||||
if (hasHours) {
|
||||
if (hasDays) {
|
||||
append(days)
|
||||
append('d')
|
||||
}
|
||||
if (hasHours || (hasDays && (hasMinutes || hasSeconds))) {
|
||||
if (length > 1) append(' ')
|
||||
append(hours)
|
||||
append('h')
|
||||
}
|
||||
if (minutes != 0 || (hasSeconds && hasHours)) {
|
||||
if (hasMinutes || (hasSeconds && (hasHours || hasDays))) {
|
||||
if (length > 1) append(' ')
|
||||
append(minutes)
|
||||
append('m')
|
||||
|
||||
@@ -543,13 +543,17 @@ class DurationTest {
|
||||
assertEquals("-$actual", (-duration).toString())
|
||||
}
|
||||
|
||||
test(Duration.days(101), "2424h")
|
||||
test(Duration.days(45.3), "1087h 12m") // 1080h + 7.2h
|
||||
test(Duration.days(45), "1080h")
|
||||
test(Duration.days(101), "101d")
|
||||
test(Duration.days(45.3), "45d 7h 12m") // 0.3d == 7.2h
|
||||
test(Duration.days(45), "45d")
|
||||
|
||||
test(Duration.days(40.5), "972h")
|
||||
test(Duration.hours(40) + Duration.minutes(15), "40h 15m")
|
||||
test(Duration.hours(40), "40h")
|
||||
test(Duration.days(40.5), "40d 12h")
|
||||
test(Duration.days(40) + Duration.minutes(20), "40d 0h 20m")
|
||||
test(Duration.days(40) + Duration.seconds(20), "40d 0h 0m 20s")
|
||||
test(Duration.days(40) + Duration.nanoseconds(100), "40d 0h 0m 0.000000100s")
|
||||
|
||||
test(Duration.hours(40) + Duration.minutes(15), "1d 16h 15m")
|
||||
test(Duration.hours(40), "1d 16h")
|
||||
|
||||
test(Duration.hours(12.5), "12h 30m")
|
||||
test(Duration.hours(12) + Duration.seconds(15), "12h 0m 15s")
|
||||
@@ -589,9 +593,10 @@ class DurationTest {
|
||||
// test(Duration.nanoseconds(0.0000035), "0.0000035ns")
|
||||
|
||||
test(Duration.ZERO, "0s")
|
||||
test(Duration.days(365) * 10000, "87600000h")
|
||||
test(Duration.days(300) * 100000, "720000000h")
|
||||
test(Duration.days(365) * 100000, "876000000h")
|
||||
test(Duration.days(365) * 10000, "3650000d")
|
||||
test(Duration.days(300) * 100000, "30000000d")
|
||||
test(Duration.days(365) * 100000, "36500000d")
|
||||
test(Duration.milliseconds(MAX_MILLIS - 1), "53375995583d 15h 36m 27.902s") // max finite value
|
||||
|
||||
// all infinite
|
||||
// val universeAge = Duration.days(365.25) * 13.799e9
|
||||
|
||||
Reference in New Issue
Block a user