IssueRaiser

`Duration#toHuman` does not print human-readable strings

Based on the name I would expect that `Duration#toHuman` returns strings that are optimal for human reading. But, IMHO this is not the case. The way I see it there are three issues. * If a `Duration` is created from a number of milliseconds then the milliseconds are printed as-is, which is not human-readable. ```js Duration.fromMillis(22140000).toHuman() //=> "22140000 milliseconds" ``` In this case, I would expect something like the string "6 hours and 9 minutes" which is what [humanize-duration](https://github.com/EvanHahn/HumanizeDuration.js) returns. * If a `Duration` is created with some units being zero then those units are printed even though they do not benefit humans. ```js Duration.fromMillis(3 * 60 * 1000).shiftTo("hours", "minutes").toHuman() //=> "0 hours, 3 minutes" Duration.fromObject({ days: 0, hours: 0, minutes: 12, seconds: 0 }).toHuman() //=> "0 days, 0 hours, 12 minutes, 0 seconds" ``` * If a `Duration` is created with an empty object then an empty string is returned. An empty string is not a human-readable duration. ```js Duration.fromObject({}).toHuman() //=> "" ``` It seems to me that `toHuman` only returns the internal representation of the duration with unit names attached and not actual human-friendly output. I think that either the `toHuman` method should be renamed as the current name is confusing or the above problems should be fixed. Units that are zero should not be printed and small units should be converted into larger units if possible.

Posted by paldepind on Feb 9, 2022, 10:15 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments

github (Software)

`Duration#toHuman` does not print human-readable strings

paldepindFeb 9, 2022, 10:15 AM

Based on the name I would expect that `Duration#toHuman` returns strings that are optimal for human reading. But, IMHO this is not the case. The way I see it there are three issues. * If a `Duration` is created from a number of milliseconds then the milliseconds are printed as-is, which is not human-readable. ```js Duration.fromMillis(22140000).toHuman() //=> "22140000 milliseconds" ``` In this case, I would expect something like the string "6 hours and 9 minutes" which is what [humanize-duration](https://github.com/EvanHahn/HumanizeDuration.js) returns. * If a `Duration` is created with some units being zero then those units are printed even though they do not benefit humans. ```js Duration.fromMillis(3 * 60 * 1000).shiftTo("hours", "minutes").toHuman() //=> "0 hours, 3 minutes" Duration.fromObject({ days: 0, hours: 0, minutes: 12, seconds: 0 }).toHuman() //=> "0 days, 0 hours, 12 minutes, 0 seconds" ``` * If a `Duration` is created with an empty object then an empty string is returned. An empty string is not a human-readable duration. ```js Duration.fromObject({}).toHuman() //=> "" ``` It seems to me that `toHuman` only returns the internal representation of the duration with unit names attached and not actual human-friendly output. I think that either the `toHuman` method should be renamed as the current name is confusing or the above problems should be fixed. Units that are zero should not be printed and small units should be converted into larger units if possible.

Comments (0)

No comments yet. Start the discussion.

Comment on this issue