Add kotlin extension to get HR time

This commit is contained in:
Davide Depau 2018-08-14 18:48:44 +02:00
parent 20cb2466e2
commit aaa7493474

View file

@ -0,0 +1,31 @@
package eu.depau.ddroid.utils
private val timeStrings = arrayOf("s", "m", "h", "d")
private val timeDivs = arrayOf(60, 60, 24)
fun <T> humanReadableTimeDelta(time: T): String where T : Number {
var dbTime = time.toDouble()
var outString = ""
for (i in 0..(timeDivs.size-1)) {
val div = timeDivs[i]
val str = timeStrings[i]
outString = "${(dbTime % div).toInt()}$str$outString"
if (dbTime < div)
return outString
outString = " $outString"
dbTime /= div
}
return "${dbTime.toInt()}${timeStrings[-1]} $outString"
}
fun Long.toHRTime() = humanReadableTimeDelta(this)
fun Float.toHRTime() = humanReadableTimeDelta(this)
fun Double.toHRTime() = humanReadableTimeDelta(this)
fun Int.toHRTime() = humanReadableTimeDelta(this)
fun Byte.toHRTime() = humanReadableTimeDelta(this)
fun Short.toHRTime() = humanReadableTimeDelta(this)