Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0b82ee

Browse files
bug #47615 [Clock] Fix sleep() in MockClock (upyx)
This PR was merged into the 6.2 branch. Discussion ---------- [Clock] Fix `sleep()` in `MockClock` | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | none | License | MIT | Doc PR | none It fixes losing precision in `$us = $seconds - $s` that occurs because the `$seconds` is float. Anyway, why so many calculation? Double precision is enough to represent any valid date and time: ```php public function sleep(float|int $seconds): void { $datetime = '@'. ((float)$this->now->format('U.u') + $seconds); $timezone = $this->now->getTimezone(); $this->now = (new \DateTimeImmutable($datetime))->setTimezone($timezone); } ``` Why is cloning needed for immutable? The beauty of immutables is that they don't need to be copied. ```php public function now(): \DateTimeImmutable { return clone $this->now; } ``` Commits ------- 066ed61 Fix `sleep()` in `MockClock`
2 parents 889f4d6 + 066ed61 commit a0b82ee

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

src/Symfony/Component/Clock/MockClock.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,11 @@ public function now(): \DateTimeImmutable
4040

4141
public function sleep(float|int $seconds): void
4242
{
43-
$now = explode('.', $this->now->format('U.u'));
44-
45-
if (0 < $s = (int) $seconds) {
46-
$now[0] += $s;
47-
}
48-
49-
if (0 < ($us = $seconds - $s) && 1E6 <= $now[1] += $us * 1E6) {
50-
++$now[0];
51-
$now[1] -= 1E6;
52-
}
53-
54-
$datetime = '@'.$now[0].'.'.str_pad($now[1], 6, '0', \STR_PAD_LEFT);
43+
$now = (float) $this->now->format('Uu') + $seconds * 1e6;
44+
$now = substr_replace(sprintf('@%07.0F', $now), '.', -6, 0);
5545
$timezone = $this->now->getTimezone();
5646

57-
$this->now = (new \DateTimeImmutable($datetime, $timezone))->setTimezone($timezone);
47+
$this->now = (new \DateTimeImmutable($now, $timezone))->setTimezone($timezone);
5848
}
5949

6050
public function withTimeZone(\DateTimeZone|string $timezone): static

src/Symfony/Component/Clock/Tests/MockClockTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function testNow()
5555

5656
public function testSleep()
5757
{
58-
$clock = new MockClock((new \DateTimeImmutable('@123.456'))->setTimezone(new \DateTimeZone('UTC')));
58+
$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));
5959
$tz = $clock->now()->getTimezone()->getName();
6060

61-
$clock->sleep(4.999);
62-
$this->assertSame('128.455000', $clock->now()->format('U.u'));
61+
$clock->sleep(2.002001);
62+
$this->assertSame('2112-09-17 23:53:03.001001', $clock->now()->format('Y-m-d H:i:s.u'));
6363
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
6464
}
6565

0 commit comments

Comments
 (0)