From cdfce65279fb7288e1e952a5b3370c7be79e40ad Mon Sep 17 00:00:00 2001 From: dualfroz Date: Fri, 4 Sep 2026 09:47:39 +0200 Subject: [PATCH 1/2] fix: parse fractional seconds in ChronosTime as a fraction of a second 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. --- src/ChronosTime.php | 4 +++- tests/TestCase/ChronosTimeTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ChronosTime.php b/src/ChronosTime.php index f066978..629736e 100644 --- a/src/ChronosTime.php +++ b/src/ChronosTime.php @@ -122,7 +122,9 @@ protected static function parseString(string $time): int $hours = (int)$matches[1]; $minutes = (int)$matches[2]; $seconds = (int)($matches[3] ?? 0); - $microseconds = (int)substr($matches[4] ?? '', 0, 6); + // The fraction is of a second, so pad on the right to microseconds: + // without it ".5" reads as 5us instead of 500000 (half a second). + $microseconds = (int)str_pad(substr($matches[4] ?? '', 0, 6), 6, '0', STR_PAD_RIGHT); if ($hours > 24 || $minutes > 59 || $seconds > 59 || $microseconds > 999_999) { throw new InvalidArgumentException(sprintf('Time string `%s` contains invalid values.', $time)); diff --git a/tests/TestCase/ChronosTimeTest.php b/tests/TestCase/ChronosTimeTest.php index 1be8a37..9f8f233 100644 --- a/tests/TestCase/ChronosTimeTest.php +++ b/tests/TestCase/ChronosTimeTest.php @@ -53,6 +53,19 @@ public function testConstructFromString(): void $this->assertSame('00:59:59.999999', $t->format('H:i:s.u')); } + public function testConstructFromStringWithFractionalSeconds(): void + { + // The fractional part is a fraction of a second, matching DateTime. + $t = new ChronosTime('12:00:00.5'); + $this->assertSame('12:00:00.500000', $t->format('H:i:s.u')); + + $t = new ChronosTime('12:00:00.05'); + $this->assertSame('12:00:00.050000', $t->format('H:i:s.u')); + + $t = new ChronosTime('12:00:00.000005'); + $this->assertSame('12:00:00.000005', $t->format('H:i:s.u')); + } + public function testConstructFromInstance(): void { $t = new ChronosTime(new DateTimeImmutable('23:59:59.999999')); From db74f49059fde46bf0ea9d8985df593334426959 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Sun, 6 Sep 2026 04:28:24 +0200 Subject: [PATCH 2/2] Fix Rector dependency compatibility --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 95feefc..603bc69 100644 --- a/composer.json +++ b/composer.json @@ -62,7 +62,7 @@ "stan": "@phpstan", "stan-baseline": "tools/phpstan --generate-baseline", "stan-setup": "phive install", - "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json", + "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" phpstan/phpstan:\"~2.1.40\" && mv composer.backup composer.json", "rector-check": "vendor/bin/rector process --dry-run", "rector-fix": "vendor/bin/rector process", "test": "phpunit"