refactor: prefer using Duration than milliseconds

This commit is contained in:
2026-07-03 11:17:15 +02:00
parent 16bb6b4c6b
commit beb1973978
8 changed files with 43 additions and 33 deletions
@@ -1,6 +1,9 @@
package fr.ajaury.gwenedeg.subtitle.data
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
/**
* Parses [LRC](https://en.wikipedia.org/wiki/LRC_(file_format)) subtitle content.
@@ -26,15 +29,15 @@ internal class LrcParser {
.findAll(tags.value)
.mapNotNull { match ->
TimeCodedText(
startTime = match.toMs().milliseconds,
startTime = match.asDuration(),
text = text,
).takeIf { text.isNotBlank() }
}.toList()
}
private fun MatchResult.toMs(): Long {
private fun MatchResult.asDuration(): Duration {
val (minutes, seconds, fraction) = destructured
val fractionMs = if (fraction.length == 2) fraction.toLong() * 10 else fraction.toLong()
return minutes.toLong() * 60_000 + seconds.toLong() * 1_000 + fractionMs
return minutes.toInt().minutes + seconds.toInt().seconds + fractionMs.toInt().milliseconds
}
}