Skip to content

Commit f65e098

Browse files
committed
bug #48949 [HttpKernel] Use TZ from clock service in DateTimeValueResolver (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpKernel] Use TZ from clock service in DateTimeValueResolver | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Fixing #48098 + making tests green by using legit TZ names. Commits ------- 1acf90e [HttpKernel] Use TZ from clock service in DateTimeValueResolver
2 parents 675ee80 + 1acf90e commit f65e098

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/DateTimeValueResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
7373
}
7474

7575
if (null !== $format) {
76-
$date = $class::createFromFormat($format, $value);
76+
$date = $class::createFromFormat($format, $value, $this->clock?->now()->getTimeZone());
7777

7878
if (($class::getLastErrors() ?: ['warning_count' => 0])['warning_count']) {
7979
$date = false;
@@ -83,7 +83,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
8383
$value = '@'.$value;
8484
}
8585
try {
86-
$date = new $class($value);
86+
$date = new $class($value, $this->clock?->now()->getTimeZone());
8787
} catch (\Exception) {
8888
$date = false;
8989
}

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public function testUnsupportedArgument()
8484
*/
8585
public function testFullDate(string $timezone, bool $withClock)
8686
{
87-
date_default_timezone_set($timezone);
88-
$resolver = new DateTimeValueResolver($withClock ? new MockClock() : null);
87+
date_default_timezone_set($withClock ? 'UTC' : $timezone);
88+
$resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);
8989

9090
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null);
9191
$request = self::requestWithAttributes(['dummy' => '2012-07-21 00:00:00']);
@@ -103,7 +103,7 @@ public function testFullDate(string $timezone, bool $withClock)
103103
*/
104104
public function testUnixTimestamp(string $timezone, bool $withClock)
105105
{
106-
date_default_timezone_set($timezone);
106+
date_default_timezone_set($withClock ? 'UTC' : $timezone);
107107
$resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);
108108

109109
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null);
@@ -212,7 +212,7 @@ public function testCustomClass()
212212
*/
213213
public function testDateTimeImmutable(string $timezone, bool $withClock)
214214
{
215-
date_default_timezone_set($timezone);
215+
date_default_timezone_set($withClock ? 'UTC' : $timezone);
216216
$resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);
217217

218218
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null);
@@ -231,7 +231,7 @@ public function testDateTimeImmutable(string $timezone, bool $withClock)
231231
*/
232232
public function testWithFormat(string $timezone, bool $withClock)
233233
{
234-
date_default_timezone_set($timezone);
234+
date_default_timezone_set($withClock ? 'UTC' : $timezone);
235235
$resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);
236236

237237
$argument = new ArgumentMetadata('dummy', \DateTimeInterface::class, false, false, null, false, [

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