File tree 3 files changed +37
-6
lines changed
src/Symfony/Component/Clock
3 files changed +37
-6
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ CHANGELOG
5
5
---
6
6
7
7
* Throw ` DateMalformedStringException ` /` DateInvalidTimeZoneException ` when appropriate
8
+ * Add ` $modifier ` argument to the ` now() ` helper
8
9
9
10
6.3
10
11
---
Original file line number Diff line number Diff line change 13
13
14
14
if (!\function_exists (now::class)) {
15
15
/**
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
20
17
*/
21
- function now (): \DateTimeImmutable
18
+ function now (string $ modifier = null ): \DateTimeImmutable
22
19
{
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 );
24
40
}
25
41
}
Original file line number Diff line number Diff line change @@ -39,6 +39,19 @@ public function testNativeClock()
39
39
$ this ->assertInstanceOf (NativeClock::class, Clock::get ());
40
40
}
41
41
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
+
42
55
public function testMockClockDisable ()
43
56
{
44
57
$ this ->assertInstanceOf (NativeClock::class, Clock::get ());
@@ -52,6 +65,7 @@ public function testMockClockFreeze()
52
65
self ::mockTime (new \DateTimeImmutable ('2021-12-19 ' ));
53
66
54
67
$ this ->assertSame ('2021-12-19 ' , now ()->format ('Y-m-d ' ));
68
+ $ this ->assertSame ('2021-12-20 ' , now ('+1 days ' )->format ('Y-m-d ' ));
55
69
56
70
self ::mockTime ('+1 days ' );
57
71
$ this ->assertSame ('2021-12-20 ' , now ()->format ('Y-m-d ' ));
You can’t perform that action at this time.
0 commit comments