Skip to content

Commit

Permalink
fix: properly format duration with all its components
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMalch committed Dec 31, 2023
1 parent 4adf238 commit bd113ac
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import kotlin.time.Duration
fun formatDuration(duration: Duration, zero: String = seconds(0)): String {
if (duration == Duration.ZERO) return zero
return duration.toComponents { hours, minutes, seconds, _ ->
when {
seconds != 0 -> seconds(seconds)
minutes != 0 -> minutes(minutes)
else -> hours(hours)
buildString {
if (hours > 0) {
append(hours(hours))
}
if (minutes > 0) {
append(minutes(minutes))
}
if (seconds > 0 || isEmpty()) {
append(seconds(seconds))
}
}
}
}
Expand Down

0 comments on commit bd113ac

Please sign in to comment.