diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index 7d68afea6..ea88f8eb8 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -1,7 +1,11 @@ package at.hannibal2.skyhanni.utils +import java.text.DecimalFormat + object StringUtils { + private val durationFormat = DecimalFormat("00") + fun String.firstLetterUppercase(): String { if (isEmpty()) return this @@ -30,7 +34,40 @@ object StringUtils { return builder.toString() } -// fun cleanColour(`in`: String): String? { -// return `in`.replace("(?i)\\u00A7.".toRegex(), "") -// } + fun formatDuration(seconds: Long): String { + var sec: Long = seconds + + var minutes: Long = sec / 60 + sec %= 60 + + var hours = minutes / 60 + minutes %= 60 + + val days = hours / 24 + hours %= 24 + + + val formatHours = durationFormat.format(hours) + val formatMinutes = durationFormat.format(minutes) + val formatSeconds = durationFormat.format(sec) + + if (days > 0) { + return "" + days + "d " + formatHours + ":" + formatMinutes + ":" + formatSeconds + } + if (hours > 0) { + return "$formatHours:$formatMinutes:$formatSeconds" + } + if (minutes > 0) { + return "$formatMinutes:$formatSeconds" + } + if (sec > 0) { + return if (sec == 1L) { + "$formatSeconds second" + } else { + "$formatSeconds seconds" + } + } + + return "Now" + } }
\ No newline at end of file |