Created
January 9, 2020 12:09
-
-
Save zunjae/84657c3019abdf9787576cfb5cf47281 to your computer and use it in GitHub Desktop.
Seconds to HH:MM:SS in Kotlin
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
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