8000 [Clock] Add $modifier argument to now() helper · symfony/symfony@1a616c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a616c1

Browse files
[Clock] Add $modifier argument to now() helper
1 parent bb8c76d commit 1a616c1

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

src/Symfony/Component/Clock/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Throw `DateMalformedStringException`/`DateInvalidTimeZoneException` when appropriate
8+
* Add `$modifier` argument to the `now()` helper
89

910
6.3
1011
---

src/Symfony/Component/Clock/Resources/now.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,29 @@
1313

1414
if (!\function_exists(now::class)) {
1515
/**
16-
* Returns the current time as a DateTimeImmutable.
17-
*
18-
* Note that you should prefer injecting a ClockInterface or using
19-
* ClockAwareTrait when possible instead of using this function.
16+
* @throws \DateMalformedStringException When the modifier is invalid
2017
*/
21-
function now(): \DateTimeImmutable
18+
function now(string $modifier = null): \DateTimeImmutable
2219
{
23-
return Clock::get()->now();
20+
if (null === $modifier || 'now' === $modifier) {
21+
return Clock::get()->now();
22+
}
23+
24+
$now = Clock::get()->now();
25+
26+
if (\PHP_VERSION_ID < 80300) {
27+
try {
28+
$tz = (new \DateTimeImmutable($modifier, $now->getTimezone()))->getTimezone();
29+
} catch (\Exception $e) {
30+
throw new \DateMalformedStringException($e->getMessage(), $e->getCode(), $e);
31+
}
32+
$now = $now->setTimezone($tz);
33+
34+
return @$now->modify($modifier) ?: throw new \DateMalformedStringException(error_get_last()['message'] ?? sprintf('Invalid date modifier "%s".', $modifier));
35+
}
36+
37+
$tz = (new \DateTimeImmutable($modifier, $now->getTimezone()))->getTimezone();
38+
39+
return $now->setTimezone($tz)->modify($modifier);
2440
}
2541
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ public function testNativeClock()
3939
$this->assertInstanceOf(NativeClock::class, Clock::get());
4040
}
4141

42+
public function testNowModifier()
43+
{
44+
$this->assertSame('2023-08-14', now('2023-08-14')->format('Y-m-d'));
45+
$this->assertSame('Europe/Paris', now('Europe/Paris')->getTimezone()->getName());
46+
$this->assertSame('UTC', now('UTC')->getTimezone()->getName());
47+
}
48+
49+
public function testInvalidNowModifier()
50+
{
51+
$this->expectException(\DateMalformedStringException::class);
52+
now('invalid date');
53+
}
54+
4255
public function testMockClockDisable()
4356
{
4457
$this->assertInstanceOf(NativeClock::class, Clock::get());
@@ -52,6 +65,7 @@ public function testMockClockFreeze()
5265
self::mockTime(new \DateTimeImmutable('2021-12-19'));
5366

5467
$this->assertSame('2021-12-19', now()->format('Y-m-d'));
68+
$this->assertSame('2021-12-20', now('+1 days')->format('Y-m-d'));
5569

5670
self::mockTime('+1 days');
5771
$this->assertSame('2021-12-20', now()->format('Y-m-d'));

0 commit comments

Comments
 (0)
0