diff --git a/mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt b/mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt index aa0efa7e2..7616c0b48 100644 --- a/mirai-core-api/src/commonMain/kotlin/contact/Exceptions.kt +++ b/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 \ No newline at end of file diff --git a/mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt b/mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt index 4c72518b7..fd42075f5 100644 --- a/mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt +++ b/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" } } } \ No newline at end of file diff --git a/mirai-core-utils/src/commonMain/kotlin/TimeUtils.kt b/mirai-core-utils/src/commonMain/kotlin/TimeUtils.kt index b959ed943..e5f37a1dd 100644 --- a/mirai-core-utils/src/commonMain/kotlin/TimeUtils.kt +++ b/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") } +} diff --git a/mirai-core/src/commonMain/kotlin/AbstractBot.kt b/mirai-core/src/commonMain/kotlin/AbstractBot.kt index 7c3c20973..e4bd69339 100644 --- a/mirai-core/src/commonMain/kotlin/AbstractBot.kt +++ b/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 -> {