fix: parse fractional seconds in ChronosTime as a fraction of a second - #531
Merged
dereuromark merged 2 commits intoSep 6, 2026
Merged
Conversation
ChronosTime::parseString() passed the fractional-seconds digits straight to (int), so a short fraction such as .5 was read as 5 microseconds instead of 500000 (half a second), diverging from DateTime. Right-pad the fractional digits to microseconds before converting.
dualfroz
force-pushed
the
dualfroz/fix-chronostime-fractional-seconds
branch
from
September 5, 2026 23:20
540a12e to
cdfce65
Compare
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.
Problem
ChronosTimemisinterprets a time string whose fractional-seconds part has fewerthan six digits. The fractional part is treated as a literal microsecond count
instead of a fraction of a second:
So
12:00:00.5becomes 5 microseconds rather than 500000, diverging from PHP'sown
DateTimeand from the round-trip used when constructing aChronosTimefrom a
DateTimeInterface(which formats to six digits).Root cause
src/ChronosTime.php,parseString():$matches[4]is the digit run after the decimal point. Passing it straight to(int)reads"5"as5, ignoring that these are the most-significant digits ofa fractional second. Only six-digit input (for example
.500000) happened towork, which is why the issue was not caught: every existing test uses a full
six-digit fraction (or a seven-digit one that is truncated to six).
The fix
Right-pad the (at most six) fractional digits to microseconds before converting:
.5->500000.05->50000.500000->500000(unchanged).000005->5(unchanged).9999991-> truncated to999999(unchanged; existing behaviour preserved)This matches how
DateTimeinterprets fractional seconds.Test
Added
ChronosTimeTest::testConstructFromStringWithFractionalSeconds, assertingthat
12:00:00.5,12:00:00.05and12:00:00.000005produce500000,50000and5microseconds respectively.