Skip to content

Commit

Permalink
add TemporalAmountParser for time manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
NONPLAYT committed Dec 3, 2024
1 parent bca6cfb commit e97e102
Show file tree
Hide file tree
Showing 4 changed files with 421 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package space.bxteam.commons.time;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Map;

public class DurationParser extends TemporalAmountParser<Duration> {
public DurationParser() {
super(LocalDateTimeProvider.now());
}

public DurationParser(LocalDateTimeProvider localDateTimeProvider) {
super(localDateTimeProvider);
}

private DurationParser(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
super(units, baseForTimeEstimation);
}

public static final TemporalAmountParser<Duration> TIME_UNITS = new DurationParser()
.withUnit("ms", ChronoUnit.MILLIS)
.withUnit("s", ChronoUnit.SECONDS)
.withUnit("m", ChronoUnit.MINUTES)
.withUnit("h", ChronoUnit.HOURS);

public static final TemporalAmountParser<Duration> DATE_TIME_UNITS = new DurationParser()
.withUnit("ns", ChronoUnit.NANOS)
.withUnit("us", ChronoUnit.MICROS)
.withUnit("ms", ChronoUnit.MILLIS)
.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);

@Override
protected TemporalAmountParser<Duration> clone(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
return new DurationParser(units, baseForTimeEstimation);
}

@Override
protected Duration plus(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount, TemporalEntry temporalEntry) {
if (temporalEntry.unit().isDurationEstimated()) {
LocalDateTime baseDateTime = baseForTimeEstimation.get();
LocalDateTime estimatedDateTime = baseDateTime.plus(temporalEntry.count(), temporalEntry.unit());
Duration estimatedDuration = Duration.between(baseDateTime, estimatedDateTime);

return temporalAmount.plus(estimatedDuration);
}

return temporalAmount.plus(temporalEntry.count(), temporalEntry.unit());
}


@Override
protected Duration negate(Duration temporalAmount) {
return temporalAmount.negated();
}

@Override
protected Duration getZero() {
return Duration.ZERO;
}

@Override
protected Duration toDuration(LocalDateTimeProvider baseForTimeEstimation, Duration temporalAmount) {
return temporalAmount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package space.bxteam.commons.time;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.Map;

public class PeriodParser extends TemporalAmountParser<Period> {
public PeriodParser() {
super(LocalDateTimeProvider.now());
}

public PeriodParser(LocalDateTimeProvider baseForTimeEstimation) {
super(baseForTimeEstimation);
}

private PeriodParser(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
super(units, baseForTimeEstimation);
}

public static final TemporalAmountParser<Period> DATE_UNITS = new PeriodParser()
.withUnit("d", ChronoUnit.DAYS)
.withUnit("w", ChronoUnit.WEEKS)
.withUnit("mo", ChronoUnit.MONTHS)
.withUnit("y", ChronoUnit.YEARS);

@Override
protected TemporalAmountParser<Period> clone(Map<String, ChronoUnit> units, LocalDateTimeProvider baseForTimeEstimation) {
return new PeriodParser(units, baseForTimeEstimation);
}

@Override
protected Period plus(LocalDateTimeProvider baseForTimeEstimation, Period temporalAmount, TemporalEntry temporalEntry) {
int count = (int) temporalEntry.count();
ChronoUnit unit = temporalEntry.unit();

if (unit == ChronoUnit.DAYS) {
return temporalAmount.plus(Period.ofDays(count));
}

if (unit == ChronoUnit.WEEKS) {
return temporalAmount.plus(Period.ofWeeks(count));
}

if (unit == ChronoUnit.MONTHS) {
return temporalAmount.plus(Period.ofMonths(count));
}

if (unit == ChronoUnit.YEARS) {
return temporalAmount.plus(Period.ofYears(count));
}

throw new IllegalArgumentException("Unsupported unit: " + unit);
}

@Override
protected Period negate(Period temporalAmount) {
return temporalAmount.negated();
}

@Override
protected Period getZero() {
return Period.ZERO;
}

@Override
protected Duration toDuration(LocalDateTimeProvider basisEstimation, Period period) {
LocalDateTime localDate = basisEstimation.get();
LocalDateTime estimatedDate = localDate.plus(period);

return Duration.between(localDate, estimatedDate);
}
}
Loading

0 comments on commit e97e102

Please sign in to comment.