Created
November 30, 2018 22:22
-
-
Save afollestad/b8d9b8eb5a11d907338483c4f0a8eb42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.math.ceil | |
const val SECOND: Long = 1000 | |
const val MINUTE = SECOND * 60 | |
const val HOUR = MINUTE * 60 | |
const val DAY = HOUR * 24 | |
const val WEEK = DAY * 7 | |
const val MONTH = WEEK * 4 | |
fun Long.timeString() = when { | |
this <= 0 -> "" | |
this >= MONTH -> | |
"${ceil((this.toFloat() / MONTH.toFloat()).toDouble()).toInt()}mo" | |
this >= WEEK -> | |
"${ceil((this.toFloat() / WEEK.toFloat()).toDouble()).toInt()}w" | |
this >= DAY -> | |
"${ceil((this.toFloat() / DAY.toFloat()).toDouble()).toInt()}d" | |
this >= HOUR -> | |
"${ceil((this.toFloat() / HOUR.toFloat()).toDouble()).toInt()}h" | |
this >= MINUTE -> | |
"${ceil((this.toFloat() / MINUTE.toFloat()).toDouble()).toInt()}m" | |
else -> "<1m" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment