aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-02-13 20:25:33 +0100
committerhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-02-13 20:25:33 +0100
commita35b093c39c9dce7697d1daacecab199a44c9f40 (patch)
tree4c8a5b86e0ab38b898c28c49ba6509294070f006 /src/main/java/at/hannibal2/skyhanni/utils
parent5a75b69da9f8b1cfa7705c6d6b9850d72b5a0ab6 (diff)
downloadskyhanni-a35b093c39c9dce7697d1daacecab199a44c9f40.tar.gz
skyhanni-a35b093c39c9dce7697d1daacecab199a44c9f40.tar.bz2
skyhanni-a35b093c39c9dce7697d1daacecab199a44c9f40.zip
Show the time it takes to kill the Slayer boss.
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt40
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt59
2 files changed, 59 insertions, 40 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
index 877e0edf6..4b01f9499 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
@@ -1,12 +1,9 @@
package at.hannibal2.skyhanni.utils
import org.intellij.lang.annotations.Language
-import java.text.DecimalFormat
import java.util.*
object StringUtils {
- private val durationFormat = DecimalFormat("00")
-
fun String.firstLetterUppercase(): String {
if (isEmpty()) return this
@@ -35,43 +32,6 @@ object StringUtils {
return builder.toString()
}
- fun formatDuration(seconds: Long, decimalFormat: Boolean = false): 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 (decimalFormat) {
- return "$formatMinutes:$formatSeconds"
- }
-
- if (days > 0) {
- return "${days}d $formatHours:$formatMinutes:$formatSeconds"
- }
- if (hours > 0) {
- return "$formatHours:$formatMinutes:$formatSeconds".removeAtBeginning("0")
- }
- if (minutes > 0) {
- return "$formatMinutes:$formatSeconds".removeAtBeginning("0")
- }
- if (sec > 0) {
- return "${sec}s"
- }
-
- return "Now"
- }
-
/**
* From https://stackoverflow.com/questions/10711494/get-values-in-treemap-whose-string-keys-start-with-a-pattern
*/
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt
new file mode 100644
index 000000000..107395886
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt
@@ -0,0 +1,59 @@
+package at.hannibal2.skyhanni.utils
+
+object TimeUtils {
+ fun formatDuration(
+ millis: Long,
+ biggestUnit: TimeUnit = TimeUnit.YEAR,
+ showMilliSeconds: Boolean = false,
+ longName: Boolean = false
+ ): String {
+ var milliseconds = millis
+ val map = mutableMapOf<TimeUnit, Int>()
+ for (unit in TimeUnit.values()) {
+ if (unit.ordinal >= biggestUnit.ordinal) {
+ val factor = unit.factor
+ map[unit] = (milliseconds / factor).toInt()
+ milliseconds %= factor
+ }
+ }
+
+ val builder = StringBuilder()
+ for ((unit, value) in map.entries) {
+ if (value > 0 || builder.isNotEmpty() || unit == TimeUnit.SECOND) {
+ builder.append(value)
+ val name = if (longName) {
+ " " + unit.longName + if (value > 1) "s" else ""
+ } else {
+ unit.shortName
+ }
+
+ if (unit == TimeUnit.SECOND) {
+ if (showMilliSeconds) {
+ val formatMillis = milliseconds / 100
+ builder.append(".")
+ builder.append(formatMillis)
+ }
+ builder.append(name)
+ } else {
+ builder.append("$name ")
+ }
+ }
+ }
+ return builder.toString()
+ }
+}
+
+private const val FACTOR_SECONDS = 1000L
+private const val FACTOR_MINUTES = FACTOR_SECONDS * 60
+private const val FACTOR_HOURS = FACTOR_MINUTES * 60
+private const val FACTOR_DAYS = FACTOR_HOURS * 24
+private const val FACTOR_YEARS = (FACTOR_DAYS * 365.25).toLong()
+
+enum class TimeUnit(val factor: Long, val shortName: String, val longName: String) {
+ YEAR(FACTOR_YEARS, "y", "Year"),
+ DAY(FACTOR_DAYS, "d", "Day"),
+ HOUR(FACTOR_HOURS, "h", "Hour"),
+ MINUTE(FACTOR_MINUTES, "m", "Minute"),
+ SECOND(FACTOR_SECONDS, "s", "Second"),
+ ;
+} \ No newline at end of file