fix(naive_time): enforce exact width when parsing %H%M%S #1710
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.
Summary
Fixes #1697: NaiveTime::parse_from_str was accepting inputs shorter than the expected length for format strings like "%H%M%S". For example, "01023" was being parsed as 01:02:03, which is incorrect.
According to the documentation:
Bug
However,
NaiveTime::parse_from_str("01023", "%H%M%S")
silently succeeds, returning01:02:03
. I expected it to fail due to input being too short for%H%M%S
(which should be 6 digits). This can lead to subtle bugs if users pass malformed data.Examples
"010233"
%H%M%S
01:02:33
✅"0102334"
%H%M%S
"01023"
%H%M%S
01:02:03
❌Fix
I modified
parse_internal
to enforce exact width parsing for numeric fields like%H
,%M
,%S
, etc., rejecting inputs that are too short or too long.I also updated/added test cases to reflect this.
Open Questions
strict-numeric
) to avoid breaking changes?Why This Matters
The current behavior contradicts the documentation and can lead to unexpected results when parsing time strings. Enforcing exact width aligns with user expectations and format specifiers.
Please let me know if this direction makes sense. I’d be happy to:
Closes #1697