Skip to content

Commit b871fb8

Browse files
committed
Add more extensive duration formatter.
1 parent e96334a commit b871fb8

File tree

6 files changed

+702
-26
lines changed

6 files changed

+702
-26
lines changed

.github/workflows/gradle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ jobs:
3333
- name: Grant execute permission for gradlew
3434
run: chmod +x gradlew
3535
- name: Build the Jar
36-
run: './gradlew clean build'
36+
run: './gradlew clean test build'

eternalcode-commons-shared/src/main/java/com/eternalcode/commons/shared/DurationFormatter.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.eternalcode.commons.shared.time;
2+
3+
import java.time.Duration;
4+
import java.time.LocalDateTime;
5+
import java.time.temporal.ChronoUnit;
6+
import java.util.Map;
7+
8+
public class DurationParser extends TemporalAmountParser<Duration> {
9+
10+
public static final TemporalAmountParser<Duration> TIME_UNITS = new DurationParser()
11+
.withUnit("ms", ChronoUnit.MILLIS)
12+
.withUnit("s", ChronoUnit.SECONDS)
13+
.withUnit("m", ChronoUnit.MINUTES)
14+
.withUnit("h", ChronoUnit.HOURS);
15+
16+
public static final TemporalAmountParser<Duration> DATE_TIME_UNITS = new DurationParser()
17+
.withUnit("ns", ChronoUnit.NANOS)
18+
.withUnit("us", ChronoUnit.MICROS)
19+
.withUnit("ms", ChronoUnit.MILLIS)
20+
.withUnit("s", ChronoUnit.SECONDS)
21+
.withUnit("m", ChronoUnit.MINUTES)
22+
.withUnit("h", ChronoUnit.HOURS)
23+
.withUnit("d", ChronoUnit.DAYS)
24+
.withUnit("w", ChronoUnit.WEEKS)
25+
.withUnit("mo", ChronoUnit.MONTHS)
26+
.withUnit("y", ChronoUnit.YEARS);
27+
28+
public DurationParser() {
29+
super(LocalDateTimeProvider.now());
30+
}
31+
32+
public DurationParser(LocalDateTimeProvider localDateTimeProvider) {
33+
super(localDateTimeProvider);
34+
}
35+
36+
private DurationParser(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
37+
super(units, baseForTimeEstimation);
38+
}
39+
40+
@Override
41+
protected TemporalAmountParser<Duration> clone(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
42+
return new DurationParser(units, baseForTimeEstimation);
43+
}
44+
45+
@Override
46+
protected Duration plus(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount, TemporalEntry temporalEntry) {
47+
if (temporalEntry.getUnit().isDurationEstimated()) {
48+
LocalDateTime baseDateTime = baseForTimeEstimation.get();
49+
LocalDateTime estimatedDateTime = baseDateTime.plus(temporalEntry.getCount(), temporalEntry.getUnit());
50+
Duration estimatedDuration = Duration.between(baseDateTime, estimatedDateTime);
51+
52+
return temporalAmount.plus(estimatedDuration);
53+
}
54+
55+
return temporalAmount.plus(temporalEntry.getCount(), temporalEntry.getUnit());
56+
}
57+
58+
59+
@Override
60+
protected Duration negate(Duration temporalAmount) {
61+
return temporalAmount.negated();
62+
}
63+
64+
@Override
65+
protected Duration getZero() {
66+
return Duration.ZERO;
67+
}
68+
69+
@Override
70+
protected Duration toDuration(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount) {
71+
return temporalAmount;
72+
}
73+
74+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.eternalcode.commons.shared.time;
2+
3+
import java.time.Duration;
4+
import java.time.LocalDateTime;
5+
import java.time.Period;
6+
import java.time.temporal.ChronoUnit;
7+
import java.util.Map;
8+
9+
public class PeriodParser extends TemporalAmountParser<Period> {
10+
11+
public static final TemporalAmountParser<Period> DATE_UNITS = new PeriodParser()
12+
.withUnit("d", ChronoUnit.DAYS)
13+
.withUnit("w", ChronoUnit.WEEKS)
14+
.withUnit("mo", ChronoUnit.MONTHS)
15+
.withUnit("y", ChronoUnit.YEARS);
16+
17+
public PeriodParser() {
18+
super(LocalDateTimeProvider.now());
19+
}
20+
21+
public PeriodParser(LocalDateTimeProvider baseForTimeEstimation) {
22+
super(baseForTimeEstimation);
23+
}
24+
25+
private PeriodParser(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
26+
super(units, baseForTimeEstimation);
27+
}
28+
29+
@Override
30+
protected TemporalAmountParser<Period> clone(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
31+
return new PeriodParser(units, baseForTimeEstimation);
32+
}
33+
34+
@Override
35+
protected Period plus(LocalDateTimeProvider baseForTimeEstimation, Period temporalAmount, TemporalEntry temporalEntry) {
36+
int count = (int) temporalEntry.getCount();
37+
ChronoUnit unit = temporalEntry.getUnit();
38+
39+
if (unit == ChronoUnit.DAYS) {
40+
return temporalAmount.plus(Period.ofDays(count));
41+
}
42+
43+
if (unit == ChronoUnit.WEEKS) {
44+
return temporalAmount.plus(Period.ofWeeks(count));
45+
}
46+
47+
if (unit == ChronoUnit.MONTHS) {
48+
return temporalAmount.plus(Period.ofMonths(count));
49+
}
50+
51+
if (unit == ChronoUnit.YEARS) {
52+
return temporalAmount.plus(Period.ofYears(count));
53+
}
54+
55+
throw new IllegalArgumentException("Unsupported unit: " + unit);
56+
}
57+
58+
@Override
59+
protected Period negate(Period temporalAmount) {
60+
return temporalAmount.negated();
61+
}
62+
63+
@Override
64+
protected Period getZero() {
65+
return Period.ZERO;
66+
}
67+
68+
@Override
69+
protected Duration toDuration(LocalDateTimeProvider basisEstimation, Period period) {
70+
LocalDateTime localDate = basisEstimation.get();
71+
LocalDateTime estimatedDate = localDate.plus(period);
72+
73+
return Duration.between(localDate, estimatedDate);
74+
}
75+
76+
}

0 commit comments

Comments
 (0)