Human-readable duration formatting and parsing for Kotlin
implementation com.philiprehberger:duration-fmtHuman-readable duration formatting and parsing for Kotlin.
implementation("com.philiprehberger:duration-fmt:0.2.0")
<dependency>
<groupId>com.philiprehberger</groupId>
<artifactId>duration-fmt</artifactId>
<version>0.2.0</version>
</dependency>
import com.philiprehberger.durationfmt.*
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes
val duration = 2.hours + 30.minutes
duration.humanize() // "2 hours, 30 minutes"
duration.humanize(style = Style.SHORT) // "2h 30m"
duration.humanize(style = Style.NARROW) // "2h30m"
duration.humanize(maxUnits = 1) // "2 hours"
parseDuration("2h 30m") // 2.hours + 30.minutes
parseDuration("1 day, 3 hours") // 1.days + 3.hours
parseDuration("500ms") // 500.milliseconds
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.seconds
import kotlin.time.Duration.Companion.milliseconds
val c = (1.days + 2.hours + 3.minutes + 4.seconds + 500.milliseconds).components()
c.days // 1
c.hours // 2
c.minutes // 3
c.seconds // 4
c.millis // 500
import kotlin.time.Duration.Companion.seconds
30.seconds.timeAgo() // "30 seconds ago"
2.hours.timeAgo() // "2 hours ago"
30.seconds.fromNow() // "in 30 seconds"
| Function / Type | Description |
|---|---|
Duration.humanize(maxUnits, style) | Format duration as human-readable string |
parseDuration(input) | Parse human-readable string to Duration |
Duration.components() | Decompose into DurationComponents (days, hours, minutes, seconds, millis) |
Duration.timeAgo() | Format as relative past time |
Duration.fromNow() | Format as relative future time |
Style.LONG | Full words: "2 hours, 30 minutes" |
Style.SHORT | Abbreviated: "2h 30m" |
Style.NARROW | Compact: "2h30m" |
./gradlew test # Run tests
./gradlew check # Run all checks
./gradlew build # Build JAR
If you find this project useful: