Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/gradle/dev.rollczi-litecommands-b…
Browse files Browse the repository at this point in the history
…ukkit-3.4.0
  • Loading branch information
imDMK authored Mar 5, 2024
2 parents c9e76b7 + 48f6e1e commit 70767f7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/github/imdmk/spenttime/util/DurationUtil.java
Original file line number Diff line number Diff line change
@@ -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<Duration> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70767f7

Please sign in to comment.