Skip to content

Instantly share code, notes, and snippets.

@zunjae
Created January 9, 2020 12:09
Show Gist options
  • Save zunjae/84657c3019abdf9787576cfb5cf47281 to your computer and use it in GitHub Desktop.
Save zunjae/84657c3019abdf9787576cfb5cf47281 to your computer and use it in GitHub Desktop.
Seconds to HH:MM:SS in Kotlin
data class HoursMinutesSeconds(val hours: Int, val minutes: Int, val seconds: Int)
fun Int.toHoursMinuteSeconds(): HoursMinutesSeconds {
val hours = this / 3600f
val fullHours = hours.toInt()
val minutes = (hours - fullHours) * 60f
val fullMinutes = minutes.toInt()
val seconds = (minutes - fullMinutes) * 60f
val fullSeconds = seconds.toInt()
return HoursMinutesSeconds(fullHours, fullMinutes, fullSeconds)
}
val result = 8_274.toHoursMinuteSeconds()
assert(result.seconds == 54)
assert(result.minutes == 17)
assert(result.hours == 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment