Simplify time ago calculation.

This commit is contained in:
Isira Seneviratne 2023-03-25 16:19:14 +05:30
parent 44ae139d33
commit e68bd041c9
1 changed files with 3 additions and 27 deletions

View File

@ -105,34 +105,10 @@ public class TimeAgoParser {
}
private DateWrapper getResultFor(final int timeAgoAmount, final ChronoUnit chronoUnit) {
OffsetDateTime offsetDateTime = now;
boolean isApproximation = false;
switch (chronoUnit) {
case SECONDS:
case MINUTES:
case HOURS:
offsetDateTime = offsetDateTime.minus(timeAgoAmount, chronoUnit);
break;
case DAYS:
case WEEKS:
case MONTHS:
offsetDateTime = offsetDateTime.minus(timeAgoAmount, chronoUnit);
isApproximation = true;
break;
case YEARS:
// minusDays is needed to prevent `PrettyTime` from showing '12 months ago'.
offsetDateTime = offsetDateTime.minusYears(timeAgoAmount).minusDays(1);
isApproximation = true;
break;
}
if (isApproximation) {
OffsetDateTime offsetDateTime = now.minus(timeAgoAmount, chronoUnit);
if (chronoUnit.isDateBased()) {
offsetDateTime = offsetDateTime.truncatedTo(ChronoUnit.HOURS);
}
return new DateWrapper(offsetDateTime, isApproximation);
return new DateWrapper(offsetDateTime, chronoUnit.isDateBased());
}
}