-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add rescale option to Duration.normalize method #1299
Conversation
Ok, so this makes sense, but here are some changes I'd like. Essentially, I'd like to decompose this.
Does that make sense? |
@icambron I refactored the code as requested. |
Thanks! I'll release this soon |
@icambron Any ETA for the next release? |
@NGPixel it's released now |
Thanks! |
For the most part this looks great! It even handles negative numbers. Unfortunately, it does not handle zero. |
The Problem
There's a use case right now where you have a duration from a set of units but want them to be displayed to the user in a more "human readable" format.
For example, you might want to display the duration between various 2 datetimes, which can defer in hours or just a few seconds:
Because the duration can vary greatly between just a few seconds and many hours, the current output is completely useless.
One would expect the
normalize()
method to solve this problem but unfortunately, the current implementation only adjust the units currently in use.In this example, normalizing 90000 milliseconds will still get you... 90000 milliseconds. Not useful at all. Again, because the duration can vary greatly, using
shiftTo()
is not a viable option.The Solution
This PR adds a
rescale
option to thenormalize()
method which also shifts units to their largest representation, on top of the current normalization. This basically remove any units with values of 0, giving you a clean output.Notes
This could possibly be its own method (e.g.
rescale()
), but in my opinion, this is what you would expect thenormalize()
method to do. Therefore, I think it makes more sense to simply add an optional flag to the currentnormalize()
method.Thanks!