Skip to content

Instantly share code, notes, and snippets.

@afollestad
Created November 30, 2018 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afollestad/b8d9b8eb5a11d907338483c4f0a8eb42 to your computer and use it in GitHub Desktop.
Save afollestad/b8d9b8eb5a11d907338483c4f0a8eb42 to your computer and use it in GitHub Desktop.
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