diff --git a/build.gradle.kts b/build.gradle.kts index 5086504..caaacd7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -32,7 +32,7 @@ dependencies { implementation("eu.okaeri:okaeri-configs-serdes-bukkit:5.0.0-beta.5") implementation("net.kyori:adventure-platform-bukkit:4.3.2") - implementation("net.kyori:adventure-text-minimessage:4.15.0") + implementation("net.kyori:adventure-text-minimessage:4.16.0") implementation("dev.rollczi:litecommands-bukkit:3.4.0") implementation("dev.rollczi:litecommands-annotations:3.3.2") diff --git a/src/main/java/com/github/imdmk/spenttime/util/DurationUtil.java b/src/main/java/com/github/imdmk/spenttime/util/DurationUtil.java index b6dee93..1637146 100644 --- a/src/main/java/com/github/imdmk/spenttime/util/DurationUtil.java +++ b/src/main/java/com/github/imdmk/spenttime/util/DurationUtil.java @@ -1,24 +1,32 @@ package com.github.imdmk.spenttime.util; +import dev.rollczi.litecommands.time.DurationParser; +import dev.rollczi.litecommands.time.TemporalAmountParser; + import java.time.Duration; +import java.time.temporal.ChronoUnit; public class DurationUtil { + public static TemporalAmountParser DATE_TIME_PARSER = new DurationParser() + .withUnit("s", ChronoUnit.SECONDS) + .withUnit("m", ChronoUnit.MINUTES) + .withUnit("h", ChronoUnit.HOURS) + .withUnit("d", ChronoUnit.DAYS) + .withUnit("w", ChronoUnit.WEEKS) + .withUnit("mo", ChronoUnit.MONTHS) + .withUnit("y", ChronoUnit.YEARS); + private DurationUtil() { throw new UnsupportedOperationException("This is utility class."); } public static String toHumanReadable(Duration duration) { - Duration ofSeconds = Duration.ofSeconds(duration.toSeconds()); - - if (ofSeconds.isZero() || ofSeconds.isNegative()) { + if (duration.isNegative()) { return "<1s"; } - return ofSeconds.toString() - .substring(2) - .replaceAll("(\\d[HMS])(?!$)", "$1 ") - .toLowerCase(); + return DATE_TIME_PARSER.format(duration); } public static long toTicks(Duration duration) { diff --git a/src/test/java/com/github/imdmk/spenttime/util/DurationUtilTest.java b/src/test/java/com/github/imdmk/spenttime/util/DurationUtilTest.java index 1341348..81b4306 100644 --- a/src/test/java/com/github/imdmk/spenttime/util/DurationUtilTest.java +++ b/src/test/java/com/github/imdmk/spenttime/util/DurationUtilTest.java @@ -10,26 +10,41 @@ public class DurationUtilTest { @Test void testHumanReadable() { - Duration negative = Duration.ZERO.minusDays(5L); + Duration negative = Duration.ZERO.minusDays(5); String negativeExcepted = "<1s"; String negativeResult = DurationUtil.toHumanReadable(negative); - Duration ofSeconds = Duration.ofSeconds(5L); + Duration ofSeconds = Duration.ofSeconds(5); String secondsExcepted = "5s"; String secondsResult = DurationUtil.toHumanReadable(ofSeconds); - Duration ofMinutes = Duration.ofMinutes(5L); + Duration ofMinutes = Duration.ofMinutes(5); String minutesExcepted = "5m"; String minutesResult = DurationUtil.toHumanReadable(ofMinutes); - Duration ofHours = Duration.ofHours(10L); + Duration ofHours = Duration.ofHours(10); String hoursExcepted = "10h"; String hoursResult = DurationUtil.toHumanReadable(ofHours); + Duration ofDays = Duration.ofDays(5); + String daysExcepted = "5d"; + String daysResult = DurationUtil.toHumanReadable(ofDays); + + Duration ofWeeks = Duration.ofDays(14); + String weeksExcepted = "2w"; + String weeksResult = DurationUtil.toHumanReadable(ofWeeks); + + Duration ofMonths = Duration.ofDays(60); + String monthsExcepted = "2mo"; + String monthsResult = DurationUtil.toHumanReadable(ofMonths); + assertEquals(negativeExcepted, negativeResult); assertEquals(secondsExcepted, secondsResult); assertEquals(minutesExcepted, minutesResult); assertEquals(hoursExcepted, hoursResult); + assertEquals(daysExcepted, daysResult); + assertEquals(weeksExcepted, weeksResult); + assertEquals(monthsExcepted, monthsResult); } @Test