Clamp rounded fractional seconds to the field width#1292
Open
vineethsaivs wants to merge 1 commit into
Open
Conversation
format_frac_seconds rounds the microseconds to the requested number of digits, but the rounding can carry up to a whole second (0.999 -> 1.0), producing a value with one digit too many. self.format then prints all of them, overflowing the fractional field (S rendered '10', SS rendered '100'). Clamp the rounded value to the maximum in-field value so it stays within the declared width.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DateTimeFormat.format_frac_secondscan render a fractional-seconds field that is wider than its declared number of digits.The method rounds the microseconds to the requested precision:
round(value, num)can carry up to a whole second (for example0.999rounds to1.0). Then1.0 * 10**num == 10**num, which hasnum + 1digits, andself.format('%0*d' % (num, value)) prints all of them. Babel intentionally does not carry into the seconds field, so the fractional field simply overflows its width.A single
Sdigit must be 0 to 9, but it can render10.Fix: clamp the rounded value to the largest in-field value so it stays within the declared width:
This only changes the overflow case (the value is clamped to all nines, the closest representation without carrying into seconds). Every existing
test_fractional_secondsexpectation, including the deliberately rounded['SSSS'] == '0346', is unchanged. Added a regression test covering theS,SS, andSSSSoverflow inputs.