Переглянути джерело

Change Duration.asHumanReadable to Duration.toHumanReadableString for clearer semantics

Him188 5 роки тому
батько
коміт
95013a6ca2

+ 2 - 2
mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt

@@ -12,7 +12,7 @@
 package net.mamoe.mirai.contact
 
 import net.mamoe.mirai.message.data.Message
-import net.mamoe.mirai.utils.asHumanReadable
+import net.mamoe.mirai.utils.toHumanReadableString
 import kotlin.time.ExperimentalTime
 import kotlin.time.seconds
 
@@ -42,6 +42,6 @@ public class MessageTooLargeException(
 @OptIn(ExperimentalTime::class)
 public class BotIsBeingMutedException(
     public val target: Group
-) : RuntimeException("bot is being muted, remaining ${target.botMuteRemaining.seconds.asHumanReadable} seconds")
+) : RuntimeException("bot is being muted, remaining ${target.botMuteRemaining.seconds.toHumanReadableString()} seconds")
 
 public inline val BotIsBeingMutedException.botMuteRemaining: Int get() = target.botMuteRemaining

+ 4 - 4
mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt

@@ -22,10 +22,10 @@ internal class TimeTest {
                 20.toDuration(DurationUnit.HOURS) +
                 15.toDuration(DurationUnit.MINUTES) +
                 2057.toDuration(DurationUnit.MILLISECONDS)
-        println(time0.asHumanReadable)
-        assertTrue { time0.asHumanReadable == "1d 20h 15min 2.057s" }
+        println(time0.toHumanReadableString())
+        assertTrue { time0.toHumanReadableString() == "1d 20h 15min 2.057s" }
         val time1 = 1.toDuration(DurationUnit.DAYS) + 59.toDuration(DurationUnit.MINUTES)
-        println(time1.asHumanReadable)
-        assertTrue { time1.asHumanReadable == "1d 59min 0.0s" }
+        println(time1.toHumanReadableString())
+        assertTrue { time1.toHumanReadableString() == "1d 59min 0.0s" }
     }
 }

+ 11 - 12
mirai-core-utils/src/commonMain/kotlin/TimeUtils.kt

@@ -84,16 +84,15 @@ public inline val Int.monthsToSeconds: Long
 
 // @MiraiExperimentalApi
 @ExperimentalTime
-public val Duration.asHumanReadable: String
-    get() {
-        val days = toInt(DurationUnit.DAYS)
-        val hours = toInt(DurationUnit.HOURS) % 24
-        val minutes = toInt(DurationUnit.MINUTES) % 60
-        val s = floor(toDouble(DurationUnit.SECONDS) % 60 * 1000) / 1000
-        return buildString {
-            if (days != 0) append("${days}d ")
-            if (hours != 0) append("${hours}h ")
-            if (minutes != 0) append("${minutes}min ")
-            append("${s}s")
-        }
+public fun Duration.toHumanReadableString(): String {
+    val days = toInt(DurationUnit.DAYS)
+    val hours = toInt(DurationUnit.HOURS) % 24
+    val minutes = toInt(DurationUnit.MINUTES) % 60
+    val s = floor(toDouble(DurationUnit.SECONDS) % 60 * 1000) / 1000
+    return buildString {
+        if (days != 0) append("${days}d ")
+        if (hours != 0) append("${hours}h ")
+        if (minutes != 0) append("${minutes}min ")
+        append("${s}s")
     }
+}

+ 1 - 1
mirai-core/src/commonMain/kotlin/AbstractBot.kt

@@ -159,7 +159,7 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
                     }
 
                     if (!failed) {
-                        logger.info { "Reconnected successfully in ${time.asHumanReadable}" }
+                        logger.info { "Reconnected successfully in ${time.toHumanReadableString()}" }
                     }
                 }
                 is BotOfflineEvent.Active -> {