8000 Fix `sleep()` in `MockClock` · symfony/symfony@381cdae · GitHub
[go: up one dir, main page]

Skip to content

Commit 381cdae

Browse files
committed
Fix sleep() in MockClock
1 parent f951250 commit 381cdae

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Symfony/Component/Clock/MockClock.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public function sleep(float|int $seconds): void
4646
$now[0] += $s;
4747
}
4848

49-
if (0 < ($us = $seconds - $s) && 1E6 <= $now[1] += $us * 1E6) {
49+
if (0 < ($us = $seconds - $s) && 1E6 <= $now[1] += round($us * 1E6)) {
5050
++$now[0];
5151
$now[1] -= 1E6;
5252
}
5353

5454
$datetime = '@'.$now[0].'.'.str_pad($now[1], 6, '0', \STR_PAD_LEFT);
5555
$timezone = $this->now->getTimezone();
5656

57-
$this->now = (new \DateTimeImmutable($datetime, $timezone))->setTimezone($timezone);
57+
$this->now = (new \DateTimeImmutable($datetime, $datetime))->setTimezone($timezone);
5858
}
5959

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

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,25 @@ 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('@123.999'))->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.002);
62+
$this->assertSame('126.001000', $clock->now()->format('U.u'));
6363
$this->assertSame($tz, $clock->now()->getTimezone()->getName());
6464
}
6565

66+
public function testPrecisionSleep()
67+
{
68+
$precision = \ini_get('precision');
69+
\ini_set('precision', -1);
70+
try {
71+
$this->testSleep();
72+
} finally {
66CC
73+
\ini_set('precision', $precision);
74+
}
75+
}
76+
6677
public function testWithTimeZone()
6778
{
6879
$clock = new MockClock();

0 commit comments

Comments
 (0)
0