Skip to content

Commit f91d84f

Browse files
authored
Merge pull request #703 from Slamdunk/frozenclock_systemtimezone
`FrozenClock`: add named constructor to system clock
2 parents b0cd180 + a3ecca8 commit f91d84f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/FrozenClock.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use DateTimeImmutable;
88
use DateTimeZone;
99

10+
use function date_default_timezone_get;
11+
1012
final class FrozenClock implements Clock
1113
{
1214
public function __construct(private DateTimeImmutable $now)
@@ -18,6 +20,11 @@ public static function fromUTC(): self
1820
return new self(new DateTimeImmutable('now', new DateTimeZone('UTC')));
1921
}
2022

23+
public static function fromSystemTimezone(): self
24+
{
25+
return new self(new DateTimeImmutable('now', new DateTimeZone(date_default_timezone_get())));
26+
}
27+
2128
public function setTo(DateTimeImmutable $now): void
2229
{
2330
$this->now = $now;

test/FrozenClockTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PHPUnit\Framework\Attributes\Test;
1010
use PHPUnit\Framework\TestCase;
1111

12+
use function date_default_timezone_get;
13+
1214
#[CoversClass(FrozenClock::class)]
1315
final class FrozenClockTest extends TestCase
1416
{
@@ -71,4 +73,13 @@ public function fromUTCCreatesClockFrozenAtCurrentSystemTimeInUTC(): void
7173

7274
self::assertSame('UTC', $now->getTimezone()->getName());
7375
}
76+
77+
#[Test]
78+
public function fromSystemTimezoneCreatesAnInstanceUsingTheDefaultTimezoneInSystem(): void
79+
{
80+
$clock = FrozenClock::fromSystemTimezone();
81+
$now = $clock->now();
82+
83+
self::assertSame(date_default_timezone_get(), $now->getTimezone()->getName());
84+
}
7485
}

0 commit comments

Comments
 (0)