Skip to content

Commit a205140

Browse files
committed
Fix calculating custom precise durations.
1 parent 4bdb4ab commit a205140

5 files changed

Lines changed: 71 additions & 54 deletions

File tree

‎core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java‎

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class PrettyTime
6161
private volatile Date reference;
6262
private volatile Locale locale = Locale.getDefault();
6363
private volatile Map<TimeUnit, TimeFormat> units = new LinkedHashMap<TimeUnit, TimeFormat>();
64-
private List<TimeUnit> mCachedUnits;
64+
private List<TimeUnit> mCachedUnits;
6565

6666
/**
6767
* Default constructor
@@ -226,6 +226,14 @@ public List<Duration> calculatePreciseDuration(final Date then)
226226
while (0 != duration.getDelta())
227227
{
228228
duration = calculateDuration(duration.getDelta());
229+
if (result.size() > 0)
230+
{
231+
Duration last = result.get(result.size() - 1);
232+
if (last.getUnit().equals(duration.getUnit()))
233+
{
234+
break;
235+
}
236+
}
229237
result.add(duration);
230238
}
231239
return result;
@@ -246,11 +254,11 @@ public String format(Date then)
246254
Duration d = approximateDuration(then);
247255
return format(d);
248256
}
249-
257+
250258
/**
251-
* Format the given {@link Calendar} object. This method applies the {@code PrettyTime.approximateDuration(date)} method
252-
* to perform its calculation. If {@code then} is null, it will default to {@code new Date()}; also decorate for
253-
* past/future tense.
259+
* Format the given {@link Calendar} object. This method applies the {@code PrettyTime.approximateDuration(date)}
260+
* method to perform its calculation. If {@code then} is null, it will default to {@code new Date()}; also decorate
261+
* for past/future tense.
254262
*
255263
* @param duration the {@link Calendar} whose date is to be formatted
256264
* @return A formatted string representing {@code then}
@@ -333,43 +341,40 @@ public String format(final List<Duration> durations)
333341
duration = durations.get(i);
334342
format = getFormat(duration.getUnit());
335343

336-
boolean isLast = (i == durations.size() - 1);
337-
if (!isLast) {
338-
builder.append(format.formatUnrounded(duration));
339-
builder.append(" ");
340-
}
341-
else {
342-
builder.append(format.format(duration));
343-
}
344+
builder.append(format.formatUnrounded(duration));
345+
builder.append(" ");
344346
}
345347
result = format.decorateUnrounded(duration, builder.toString());
346348
}
349+
347350
return result;
348351
}
349352

350353
/**
351-
* Given a date, returns a non-relative format string for the
352-
* approximate duration of the difference between the date and now.
354+
* Given a date, returns a non-relative format string for the approximate duration of the difference between the date
355+
* and now.
353356
*
354357
* @param date the date to be formatted
355358
* @return A formatted string of the approximate duration
356359
*/
357-
public String formatApproximateDuration(Date date) {
358-
Duration duration = approximateDuration(date);
359-
return formatDuration(duration);
360+
public String formatApproximateDuration(Date date)
361+
{
362+
Duration duration = approximateDuration(date);
363+
return formatDuration(duration);
360364
}
361-
365+
362366
/**
363367
* Given a duration, returns a non-relative format string.
364368
*
365369
* @param duration the duration to be formatted
366370
* @return A formatted string of the duration
367371
*/
368-
public String formatDuration(Duration duration) {
369-
TimeFormat timeFormat = getFormat(duration.getUnit());
370-
return timeFormat.format(duration);
372+
public String formatDuration(Duration duration)
373+
{
374+
TimeFormat timeFormat = getFormat(duration.getUnit());
375+
return timeFormat.format(duration);
371376
}
372-
377+
373378
/**
374379
* Get the registered {@link TimeFormat} for the given {@link TimeUnit} or null if none exists.
375380
*/
@@ -417,14 +422,14 @@ public PrettyTime setReference(final Date timestamp)
417422
*/
418423
public List<TimeUnit> getUnits()
419424
{
420-
if (mCachedUnits == null) {
421-
List<TimeUnit> result = new ArrayList<TimeUnit>(units.keySet());
422-
Collections.sort(result, new TimeUnitComparator());
423-
mCachedUnits = Collections.unmodifiableList(result);
424-
}
425+
if (mCachedUnits == null) {
426+
List<TimeUnit> result = new ArrayList<TimeUnit>(units.keySet());
427+
Collections.sort(result, new TimeUnitComparator());
428+
mCachedUnits = Collections.unmodifiableList(result);
429+
}
425430

426-
return mCachedUnits;
427-
}
431+
return mCachedUnits;
432+
}
428433

429434
/**
430435
* Get the registered {@link TimeUnit} for the given {@link TimeUnit} type or null if none exists.
@@ -434,16 +439,16 @@ public List<TimeUnit> getUnits()
434439
@SuppressWarnings("unchecked")
435440
public <UNIT extends TimeUnit> UNIT getUnit(final Class<UNIT> unitType)
436441
{
437-
if (unitType == null)
438-
throw new IllegalArgumentException("Unit type to get must not be null.");
439-
440-
for (TimeUnit unit : units.keySet()) {
441-
if (unitType.isAssignableFrom(unit.getClass()))
442-
{
443-
return (UNIT) unit;
444-
}
445-
}
446-
return null;
442+
if (unitType == null)
443+
throw new IllegalArgumentException("Unit type to get must not be null.");
444+
445+
for (TimeUnit unit : units.keySet()) {
446+
if (unitType.isAssignableFrom(unit.getClass()))
447+
{
448+
return (UNIT) unit;
449+
}
450+
}
451+
return null;
447452
}
448453

449454
/**
@@ -458,9 +463,9 @@ public PrettyTime registerUnit(final TimeUnit unit, TimeFormat format)
458463
if (format == null)
459464
throw new IllegalArgumentException("Format to register must not be null.");
460465

461-
mCachedUnits = null;
466+
mCachedUnits = null;
462467

463-
units.put(unit, format);
468+
units.put(unit, format);
464469
if (unit instanceof LocaleAware)
465470
((LocaleAware<?>) unit).setLocale(locale);
466471
if (format instanceof LocaleAware)
@@ -481,9 +486,9 @@ public <UNIT extends TimeUnit> TimeFormat removeUnit(final Class<UNIT> unitType)
481486
for (TimeUnit unit : units.keySet()) {
482487
if (unitType.isAssignableFrom(unit.getClass()))
483488
{
484-
mCachedUnits = null;
489+
mCachedUnits = null;
485490

486-
return units.remove(unit);
491+
return units.remove(unit);
487492
}
488493
}
489494
return null;
@@ -499,9 +504,9 @@ public TimeFormat removeUnit(final TimeUnit unit)
499504
if (unit == null)
500505
throw new IllegalArgumentException("Unit to remove must not be null.");
501506

502-
mCachedUnits = null;
507+
mCachedUnits = null;
503508

504-
return units.remove(unit);
509+
return units.remove(unit);
505510
}
506511

507512
/**
@@ -545,8 +550,8 @@ public List<TimeUnit> clearUnits()
545550
{
546551
List<TimeUnit> result = getUnits();
547552

548-
mCachedUnits = null;
549-
units.clear();
553+
mCachedUnits = null;
554+
units.clear();
550555
return result;
551556
}
552557

‎core/src/main/java/org/ocpsoft/prettytime/impl/DurationImpl.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
public class DurationImpl implements Duration
2222
{
23+
2324
private long quantity;
2425
private long delta;
2526
private TimeUnit unit;
@@ -85,4 +86,10 @@ public long getQuantityRounded(int tolerance)
8586
}
8687
return quantity;
8788
}
89+
90+
@Override
91+
public String toString()
92+
{
93+
return "DurationImpl [" + quantity + " " + unit + ", delta=" + delta + "]";
94+
}
8895
}

‎core/src/main/java/org/ocpsoft/prettytime/impl/ResourcesTimeUnit.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.ocpsoft.prettytime.TimeUnit;
1919

20-
2120
/**
2221
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2322
*/
@@ -58,4 +57,10 @@ public void setMillisPerUnit(long millisPerUnit)
5857
this.millisPerUnit = millisPerUnit;
5958
}
6059

60+
@Override
61+
public String toString()
62+
{
63+
return getResourceKeyPrefix();
64+
}
65+
6166
}

‎core/src/main/java/org/ocpsoft/prettytime/units/Century.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
import org.ocpsoft.prettytime.TimeUnit;
1919
import org.ocpsoft.prettytime.impl.ResourcesTimeUnit;
2020

21-
22-
public class Century extends ResourcesTimeUnit implements TimeUnit {
23-
21+
public class Century extends ResourcesTimeUnit implements TimeUnit
22+
{
2423
public Century()
2524
{
2625
setMillisPerUnit(3155692597470L);
2726
}
2827

2928
@Override
30-
protected String getResourceKeyPrefix() {
29+
protected String getResourceKeyPrefix()
30+
{
3131
return "Century";
3232
}
3333
}

‎core/src/test/java/org/ocpsoft/prettytime/PrettyTimeI18n_Test.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testPrettyTimeSpanish()
7171
{
7272
// The Spanish resource bundle should be used
7373
PrettyTime p = new PrettyTime(new Locale("es"));
74-
assertEquals("en un instante", p.format(new Date()));
74+
assertEquals("hace un instante", p.format(new Date()));
7575
}
7676

7777
@Test

0 commit comments

Comments
 (0)