me.jakejmattson.kutils.api.extensions.stdlib.LongExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of KUtils Show documentation
Show all versions of KUtils Show documentation
A Discord bot framework for Kotlin.
@file:Suppress("unused")
package me.jakejmattson.kutils.api.extensions.stdlib
import java.time.Duration
import java.time.temporal.ChronoUnit
fun Number.pluralize(unit: String) = "$this ${if (this.toLong() == 1L) unit else "${unit}s"}"
fun Long.toTimeString(trimFront: Boolean = true, trimBack: Boolean = true) = with(Duration.of(this, ChronoUnit.SECONDS)) {
val timeString =
"${toDaysPart().pluralize("day")} " +
"${toHoursPart().pluralize("hour")} " +
"${toMinutesPart().pluralize("minute")} " +
toSecondsPart().pluralize("second")
var grouped = group(timeString)
if (trimFront)
grouped = grouped.trimFront()
if (trimBack)
grouped = grouped.trimBack()
assemble(grouped).ifBlank { "0 seconds" }
}
private fun group(timeString: String) = timeString.split(" ").chunked(2).map { it[0] to it[1] }
private fun assemble(timeElements: List>) = timeElements.joinToString(" ") { it.first + " " + it.second }
private fun List>.trimFront() = dropWhile { it.first.toDouble() == 0.0 }
private fun List>.trimBack() = reversed().trimFront().reversed()
© 2015 - 2024 Weber Informatics LLC | Privacy Policy