Skip to content

Commit ba7d002

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

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/Symfony/Component/Clock/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
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+
if (\PHP_VERSION_ID >= 80300) {
25+
$now = Clock::get()->now()->modify($modifier);
26+
} else {
27+
$now = @Clock::get()->now()->modify($modifier) ?: throw new \DateMalformedStringException(error_get_last()['message'] ?? sprintf('Invalid date modifier "%s".', $modifier));
28+
}
29+
30+
return $now->setTimeZone((new \DateTimeImmutable($modifier, $now->getTimezone()))->getTimezone());
2431
}
2532
}

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

Lines changed: 14 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy