HumanizeDuration.js icon indicating copy to clipboard operation
HumanizeDuration.js copied to clipboard

Round is not working correctly

Open schoofseggl opened this issue 7 years ago • 16 comments

I think there is an issue with the round function. See this plunker:

https://plnkr.co/edit/RRyKjyroA2ho9wZcO1gn?p=preview

For 7200 sec, the result will be 2 hours. Fine. For 7199 sec the result is now 1 hour for options.largest =1. Shouldn't it be 2 hours?

schoofseggl avatar Aug 07 '18 09:08 schoofseggl

Sorry to take a few days to respond to this! Could you post a snippet of JavaScript that reproduces this problem?

EvanHahn avatar Aug 08 '18 23:08 EvanHahn

Have you noticed the plunk above? It's a small angular filter calls the shortEn humanizer (took it from your website)

schoofseggl avatar Aug 09 '18 05:08 schoofseggl

I have but I didn't see how it reproduced the problem (the largest option didn't appear to be set). Any chance you could do something that isn't Angular-specific?

I'm happy to dig into this further but am busy so it might take me awhile to get to this.

EvanHahn avatar Aug 09 '18 18:08 EvanHahn

Vanilla plunk:

https://plnkr.co/edit/KYn1iltDBl4viEDQE4Ig

schoofseggl avatar Aug 10 '18 06:08 schoofseggl

Hmm, that didn't appear to work for me. Could you just show a call to humanizeDuration that breaks for you? It doesn't need to be in a Plunker...something like:

humanizeDuration(7200, {largest: 1, round: true}) // => whatever you get

EvanHahn avatar Aug 10 '18 18:08 EvanHahn

Arg, forgot to save the plunk. New one: https://plnkr.co/edit/WwL30XZbp8kffm5EN22I?p=preview

For 7200 seconds I get

with round, largest 2: 2 hours with round, largest 1: 2 hours

Which is correct obviously

However 7199 seconds yields

with round, largest 2: 1h 59m (correct) with round, largest 1: 1h

But shouldn't this be rounded to 2h?

schoofseggl avatar Aug 13 '18 05:08 schoofseggl

Yes, I think you're right.

Any chance you would be able to submit a pull request for this? I am pretty busy and likely won't be able to get to this too soon.

EvanHahn avatar Aug 24 '18 19:08 EvanHahn

@EvanHahn I can help in this if you're still busy

2bezzat avatar Sep 10 '18 11:09 2bezzat

@A-ezzat1997 That'd be great!

EvanHahn avatar Sep 10 '18 14:09 EvanHahn

@A-ezzat1997 Are you able to make a pull request for this?

EvanHahn avatar Feb 07 '19 21:02 EvanHahn

@EvanHahn I'm sorry I was very busy the last couple of months and the coming months will be so as well, once I can work on it and there's no one already fix it I'll work on it

2bezzat avatar May 10 '19 09:05 2bezzat

@A-ezzat1997 Any chance you're able to make a pull request here? (Or anyone else?)

EvanHahn avatar Jul 30 '19 15:07 EvanHahn

@EvanHahn FYI ~ This bug was squashed in v3.6.1, specifically this commit

lharress avatar Aug 03 '19 16:08 lharress

@lharress That was committed over 3 years ago, but this this report was from about 1 year ago. Are you sure that commit squashed this bug?

EvanHahn avatar Aug 03 '19 22:08 EvanHahn

@EvanHahn The initial issue is gone, here's an updated plunk with the latest code from master;

https://next.plnkr.co/edit/I3YBFw1VaZw4MekP

One thing I'd note (which is correct IMO) is that using 7199 with { largest: 2, round: true } gives 2 hours given that 1hr, 59mins and 59 seconds rounded to the nearest minute bumps it up to 2 hours.

Above @schoofseggl mentions;

However 7199 seconds yields with round, largest 2: 1h 59m (correct)

Which was in fact incorrect as well.

lharress avatar Aug 04 '19 20:08 lharress

Wrote some tests, I don't think there is any issue anymore, everything is passing.

  it('can handle rounding with the "largest" option without truncating the largest units', () => {
    const h = humanizer({ round: true })
    assert.strictEqual(h(739160000, { largest: 2 }), '1 week, 2 days')
    assert.strictEqual(h(739160000, { largest: 2 }), '1 week, 2 days')
    assert.strictEqual(h(7199000, { largest: 2 }), '2 hours')
    assert.strictEqual(h(7199000, { largest: 3 }), '1 hour, 59 minutes, 59 seconds')
    assert.strictEqual(h(7201000, { largest: 2 }), '2 hours')
    assert.strictEqual(h(7201000, { largest: 3 }), '2 hours, 1 second')
  })

HHK1 avatar May 18 '20 20:05 HHK1

It's been a few years but I'm finally getting around to this.

Could you try this on the latest version of humanize-duration to see if this is still a problem? I think I may have fixed it.

EvanHahn avatar Jul 09 '23 22:07 EvanHahn

At least for the issue I reported initially I can confirm it's fixed, thanks :)

schoofseggl avatar Jul 10 '23 06:07 schoofseggl

Great! I'm going to close this issue because I think it's (finally) solved.

Thanks for your patience!

EvanHahn avatar Jul 10 '23 11:07 EvanHahn