Skip to content

Commit

Permalink
Fix for ocpsoft#5: calculatePreciseDuration() doesn't work for the past
Browse files Browse the repository at this point in the history
  • Loading branch information
nessumo committed Feb 28, 2012
1 parent b7780ea commit 601deeb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/ocpsoft/pretty/time/PrettyTime.java
Expand Up @@ -217,7 +217,7 @@ public List<Duration> calculatePreciseDuration(final Date then)
long difference = then.getTime() - reference.getTime();
Duration duration = calculateDuration(difference);
result.add(duration);
while (0 < duration.getDelta())
while (0 != duration.getDelta())
{
duration = calculateDuration(duration.getDelta());
result.add(duration);
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/ocpsoft/pretty/time/PrettyTimeTest.java
@@ -1,9 +1,11 @@
package com.ocpsoft.pretty.time;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.junit.After;
Expand Down Expand Up @@ -223,6 +225,27 @@ public void testWithinTwoHoursRounding() throws Exception
PrettyTime t = new PrettyTime();
assertEquals("2 hours ago", t.format(new Date(new Date().getTime() - 6543990)));
}

@Test
public void testPreciseInTheFuture() throws Exception
{
PrettyTime t = new PrettyTime();
List<Duration> durations = t.calculatePreciseDuration(new Date(new Date().getTime() + 1000*(10*60+5*60*60)));
assertTrue(durations.size() >= 2); // might be more because of milliseconds between date capturing and result calculation
assertEquals(5, durations.get(0).getQuantity());
assertEquals(10, durations.get(1).getQuantity());
}

@Test
public void testPreciseInThePast() throws Exception
{
PrettyTime t = new PrettyTime();
List<Duration> durations = t.calculatePreciseDuration(new Date(new Date().getTime() - 1000*(10*60+5*60*60)));
assertTrue(durations.size() >= 2); // might be more because of milliseconds between date capturing and result calculation
assertEquals(-5, durations.get(0).getQuantity());
assertEquals(-10, durations.get(1).getQuantity());
}

// Method tearDown() is called automatically after every test method
@After
public void tearDown() throws Exception
Expand Down

0 comments on commit 601deeb

Please sign in to comment.