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.
This commit is contained in:
dualfroz committed 2026-09-06 01:20:04 +02:00
1 parent 760a3e72b1
commit 49dbcb8c91
2 files changed
+16 -1

No files matched your search

+13
View File
@@ -53,6 +53,19 @@ class ChronosTimeTest extends TestCase
$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'));