hako / durafmt

:clock8: Better time duration formatting in Go!
MIT License
496 stars 49 forks source link

durafmt incorrectly calculates the duration. #2

Closed adewale closed 7 years ago

adewale commented 8 years ago

As you can see from this gist: https://gist.github.com/adewale/da804cc1db00c26e4892eb0da6463789 durafmt incorrectly calculates the duration in some scenarios.

A duration that should be "1 year 1 day" becomes "1 year 1 month 3 days"

adewale commented 8 years ago

After thinking about this more (and with a nudge from Tom Insam on Twitter: https://twitter.com/tominsam/status/737849740168073217 ) I've realised that this problem can't be solved (with the current implementation and design) for the following units:

The above are why time.Duration: https://golang.org/pkg/time/#Duration doesn't handle any unit larger than hours.

One way to surface these issues is to add the following lines to the TestParse function in durafmt_test.go {8760 * time.Hour, "1 year"}, {17520 * time.Hour, "2 years"},

One workaround might be to remove the formatting of any unit larger than an hour.

hako commented 7 years ago

Resolved in https://github.com/hako/durafmt/commit/83a6d8dc879e5db09185e352561da4326f443de6