Rounding

Splash Forums PrettyTime Users Rounding

This topic contains 5 replies, has 2 voices, and was last updated by  Lincoln Baxter III 11 years, 2 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #17796

    Wachunga
    Participant

    Great library!

    There’s one blocker for me, though: no rounding (at least by default). Shouldn’t 3 hours, 59 minutes show as “4 hours ago”?

    @test

    public void formatDifferencePast() {

    final DateTime now = new DateTime(2010, 1, 8, 0, 0, 0, 0);

    final DateTime fourHoursAgo = now.minusHours(4);

    final DateTime almostFourHoursAgo = now.minusHours(59).minusMinutes(3).minusSeconds(2);

    assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, fourHoursAgo)); // succeeds

    assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, almostFourHoursAgo)); // fails

    }

    By default, I see that BasicTimeFormat.roundingTolerance is 0. If I change that to 99 (something like below), then the test passes.

    PrettyTime formatter = new PrettyTime(start.toDate());

    final List<TimeUnit> units = formatter.getUnits();

    for (TimeUnit timeUnit : units) {

    final BasicTimeFormat format = (BasicTimeFormat)timeUnit.getFormat();

    format.setRoundingTolerance(99);

    }

    But this also causes 3 hours, 5 minutes ago to round up to 4 hours. Perhaps there’s a bug with BasicTimeFormat?

    #20277

    Hmmm… It’s strange that the rounding tolerance is zero. What if you set the roundingTolerance to something more like 30?

    #20278

    Actually, I just reviewed your test case and it looks like you have some things backwards:

    final DateTime almostFourHoursAgo = now.minusHours(59).minusMinutes(3).minusSeconds(2);

    Should be:

    final DateTime almostFourHoursAgo = now.minusMinutes(59).minusHours(3).minusSeconds(2);

    #20280

    Wachunga
    Participant

    Sorry, that was a typo on my part. After copying and pasting the test, I decided to make it “more clear” for the purposes of this post but ended up botching it.

    Here’s the update test:

    @test

    public void formatDifferencePast() {

    final DateTime now = new DateTime(2010, 1, 8, 0, 0, 0, 0);

    final DateTime almostFourHoursAgo = now.minusHours(3).minusMinutes(58);

    final DateTime fourHoursAgo = now.minusHours(4);

    final DateTime justOverFourHoursAgo = now.minusHours(4).minusMinutes(10);

    assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, almostFourHoursAgo));

    assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, fourHoursAgo));

    assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, justOverFourHoursAgo));

    }

    By default (without tweaking roundingTolerance), assertion 1 fails. I get “3 hours ago” for a time that’s slightly less than 4 hours ago.

    If I set roundingTolerance to 50 (or anything below high 90s), same problem.

    If I set roundingTolerance to 99, assertion 1 is fine but 3 fails. I get “5 hours ago” for a time that’s slightly more than 4 hours ago.

    Is there a way to round to the nearest hour? It’s quite important for my use case that >= .5 rounds up and < .5 rounds down.

    #20281

    Wachunga
    Participant

    I’ve filed a bug for this here: http://code.google.com/p/prettytime/issues/detail?id=11

    I see there haven’t been any checkins since April. Is PrettyTime still being maintained?

    #20282

    Thanks, it is still being maintained, but this is the first but since then. ;) Actually, if you’d like to write a patch I’d be glad to review it and get this fixed sooner! We’re currently in preparation mode for a few conferences and releases (mainly prettyfaces and socialpm which is lagging badly.)

    But if you wanted to submit a patch & unit tests to verify, it would actually be really helpful and would definitely speed things up :)

    Sorry for the trouble.

Viewing 6 posts - 1 through 6 (of 6 total)

The forum ‘PrettyTime Users’ is closed to new topics and replies.

Comments are closed.