diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt | 12 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt | 14 |
2 files changed, 20 insertions, 6 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt index e79599cae..eebcc5f06 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SimpleTimeMark.kt @@ -7,18 +7,28 @@ import kotlin.time.Duration.Companion.milliseconds @JvmInline value class SimpleTimeMark(private val millis: Long) { +// operator fun plus(other: SimpleTimeMark) = +// (millis + other.millis).milliseconds + operator fun minus(other: SimpleTimeMark) = (millis - other.millis).milliseconds operator fun plus(other: Duration) = SimpleTimeMark(millis + other.inWholeMilliseconds) + operator fun minus(other: Duration) = plus(-other) + +// operator fun minus(other: Duration) = +// SimpleTimeMark(millis - other.inWholeMilliseconds) + fun passedSince() = if (millis == 0L) Duration.INFINITE else now() - this fun timeUntil() = -passedSince() fun isInPast() = timeUntil().isNegative() + fun isFarPast() = millis == 0L + override fun toString(): String { if (millis == 0L) return "The Far Past" return Instant.ofEpochMilli(millis).toString() @@ -33,4 +43,4 @@ value class SimpleTimeMark(private val millis: Long) { fun Long.asTimeMark() = SimpleTimeMark(this) fun SkyBlockTime.asTimeMark() = SimpleTimeMark(toMillis()) } -}
\ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt index 3a32b3de6..5bf6bfcf0 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt @@ -4,6 +4,8 @@ import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import io.github.moulberry.notenoughupdates.util.SkyBlockTime import kotlin.time.Duration +import kotlin.time.DurationUnit +import kotlin.time.toDuration object TimeUtils { private val pattern = @@ -73,8 +75,10 @@ object TimeUtils { return builder.toString().trim() } - // TODO: use kotlin Duration - fun getMillis(string: String) = getMillis_(string.replace("m", "m ").replace(" ", " ").trim()) + @Deprecated("Do no longer use long for time", ReplaceWith("getDuration()")) + fun getMillis(string: String) = getDuration(string).inWholeMilliseconds + + fun getDuration(string: String) = getMillis_(string.replace("m", "m ").replace(" ", " ").trim()) private fun getMillis_(string: String) = pattern.matchMatcher(string.lowercase().trim()) { val years = group("y")?.toLong() ?: 0L @@ -90,10 +94,10 @@ object TimeUtils { millis += days * 24 * 60 * 60 * 1000 millis += (years * 365.25 * 24 * 60 * 60 * 1000).toLong() - millis + millis.toDuration(DurationUnit.MILLISECONDS) } ?: tryAlternativeFormat(string) - private fun tryAlternativeFormat(string: String): Long { + private fun tryAlternativeFormat(string: String): Duration { val split = string.split(":") return when (split.size) { 3 -> { @@ -116,7 +120,7 @@ object TimeUtils { else -> { throw RuntimeException("Invalid format: '$string'") } - }.toLong() + }.toLong().toDuration(DurationUnit.MILLISECONDS) } fun SkyBlockTime.formatted(): String { |