-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add duration formatted and editorconfig file
- Loading branch information
Showing
9 changed files
with
179 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.{kt,kts}] | ||
indent_size = 4 | ||
ktlint_code_style = intellij_idea | ||
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = unset | ||
ktlint_ignore_back_ticked_identifier = true | ||
ktlint_function_signature_body_expression_wrapping = default |
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
71 changes: 71 additions & 0 deletions
71
klog/src/main/kotlin/com/coditory/klog/format/DurationFormat.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.coditory.klog.format | ||
|
||
import java.time.Duration | ||
import kotlin.math.abs | ||
import kotlin.math.round | ||
|
||
object DurationFormat { | ||
fun format(duration: Duration): String { | ||
val ms = duration.toMillis() | ||
return if (duration.isZero || ms != 0L) { | ||
formatMillis(ms) | ||
} else { | ||
"${duration.toNanos()}ns" | ||
} | ||
} | ||
|
||
fun formatMillis(ms: Long): String { | ||
if (ms == 0L) { | ||
return "0.00s" | ||
} | ||
val seconds = ms / 1000 | ||
val minutes = seconds / 60 | ||
val hours = minutes / 60 | ||
val days = hours / 24 | ||
return if (abs(days) > 0) { | ||
val v = round(hours * 100 / 24.0) / 100.0 | ||
"%.2fd".format(v) | ||
} else if (abs(hours) > 0) { | ||
val v = round(minutes * 100 / 60.0) / 100.0 | ||
"%.2fh".format(v) | ||
} else if (abs(minutes) > 0) { | ||
val v = round(seconds * 100 / 60.0) / 100.0 | ||
"%.2fm".format(v) | ||
} else if (abs(seconds) > 0) { | ||
val v = round(ms * 100 / 1000.0) / 100.0 | ||
"%.2fs".format(v) | ||
} else { | ||
"${ms}ms" | ||
} | ||
} | ||
|
||
fun formatSignificant(duration: Duration): String { | ||
val ms = duration.toMillis() | ||
return if (duration.isZero || ms != 0L) { | ||
formatMillisSignificant(ms) | ||
} else { | ||
"${duration.toNanos()}ns" | ||
} | ||
} | ||
|
||
fun formatMillisSignificant(ms: Long): String { | ||
if (ms == 0L) { | ||
return "0s" | ||
} | ||
val seconds = ms / 1000 | ||
val minutes = seconds / 60 | ||
val hours = minutes / 60 | ||
val days = hours / 24 | ||
return if (abs(days) > 0) { | ||
"${days}d" | ||
} else if (abs(hours) > 0) { | ||
"${hours}h" | ||
} else if (abs(minutes) > 0) { | ||
"${minutes}m" | ||
} else if (abs(seconds) > 0) { | ||
"${seconds}s" | ||
} else { | ||
"${ms}ms" | ||
} | ||
} | ||
} |
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
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
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
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
62 changes: 62 additions & 0 deletions
62
klog/src/test/kotlin/com/coditory/klog/format/DurationFormatSpec.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.coditory.klog.format | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.tuple | ||
import io.kotest.matchers.shouldBe | ||
import java.time.Duration | ||
|
||
class DurationFormatSpec : FunSpec({ | ||
listOf( | ||
tuple(Duration.ofDays(0), "0.00s"), | ||
tuple(Duration.ofDays(-3), "-3.00d"), | ||
tuple(Duration.ofDays(3), "3.00d"), | ||
tuple(Duration.ofHours(36), "1.50d"), | ||
tuple(Duration.ofHours(24), "1.00d"), | ||
tuple(Duration.ofHours(16), "16.00h"), | ||
tuple(Duration.ofMinutes(110), "1.83h"), | ||
tuple(Duration.ofMinutes(60), "1.00h"), | ||
tuple(Duration.ofMinutes(35), "35.00m"), | ||
tuple(Duration.ofSeconds(110), "1.83m"), | ||
tuple(Duration.ofSeconds(60), "1.00m"), | ||
tuple(Duration.ofSeconds(35), "35.00s"), | ||
tuple(Duration.ofMillis(1100), "1.10s"), | ||
tuple(Duration.ofMillis(1000), "1.00s"), | ||
tuple(Duration.ofMillis(200), "200ms"), | ||
tuple(Duration.ofNanos(1100000), "1ms"), | ||
tuple(Duration.ofNanos(1000000), "1ms"), | ||
tuple(Duration.ofNanos(1000), "1000ns"), | ||
tuple(Duration.ofNanos(200), "200ns"), | ||
tuple(Duration.ofNanos(-200), "-200ns"), | ||
).forEach { | ||
test("format: ${it.a} -> \"${it.b}\"") { | ||
DurationFormat.format(it.a) shouldBe it.b | ||
} | ||
} | ||
|
||
listOf( | ||
tuple(Duration.ofDays(0), "0s"), | ||
tuple(Duration.ofDays(-3), "-3d"), | ||
tuple(Duration.ofDays(3), "3d"), | ||
tuple(Duration.ofHours(36), "1d"), | ||
tuple(Duration.ofHours(24), "1d"), | ||
tuple(Duration.ofHours(16), "16h"), | ||
tuple(Duration.ofMinutes(110), "1h"), | ||
tuple(Duration.ofMinutes(60), "1h"), | ||
tuple(Duration.ofMinutes(35), "35m"), | ||
tuple(Duration.ofSeconds(110), "1m"), | ||
tuple(Duration.ofSeconds(60), "1m"), | ||
tuple(Duration.ofSeconds(35), "35s"), | ||
tuple(Duration.ofMillis(1100), "1s"), | ||
tuple(Duration.ofMillis(1000), "1s"), | ||
tuple(Duration.ofMillis(200), "200ms"), | ||
tuple(Duration.ofNanos(1100000), "1ms"), | ||
tuple(Duration.ofNanos(1000000), "1ms"), | ||
tuple(Duration.ofNanos(1000), "1000ns"), | ||
tuple(Duration.ofNanos(200), "200ns"), | ||
tuple(Duration.ofNanos(-200), "-200ns"), | ||
).forEach { | ||
test("format significant: ${it.a} -> \"${it.b}\"") { | ||
DurationFormat.formatSignificant(it.a) shouldBe it.b | ||
} | ||
} | ||
}) |
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