8000 bug #19879 [Form] Incorrect timezone with DateTimeLocalizedStringTran… · symfony/symfony@d893192 · GitHub
[go: up one dir, main page]

Skip to content

Commit d893192

Browse files
committed
bug #19879 [Form] Incorrect timezone with DateTimeLocalizedStringTransformer (mbeccati)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Incorrect timezone with DateTimeLocalizedStringTransformer | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19721 | License | MIT | Doc PR | n.a. Issue was introduced in #19541 Commits ------- bf6691c Fix #19721
2 parents be2a6d1 + bf6691c commit d893192

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ public function reverseTransform($value)
134134
}
135135

136136
// read timestamp into DateTime object - the formatter delivers a timestamp
137-
$dateTime = new \DateTime(sprintf('@%s', $timestamp), new \DateTimeZone($this->outputTimezone));
137+
$dateTime = new \DateTime(sprintf('@%s', $timestamp));
138+
// set timezone separately, as it would be ignored if set via the constructor,
139+
// see http://php.net/manual/en/datetime.construct.php
140+
$dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
138141
} catch (\Exception $e) {
139142
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
140143
}

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ public function testTransformWithDifferentTimezones()
134134
$this->assertEquals($dateTime->format('d.m.Y, H:i'), $transformer->transform($input));
135135
}
136136

137+
public function testReverseTransformWithNoConstructorParameters()
138+
{
139+
$tz = date_default_timezone_get();
140+
date_default_timezone_set('Europe/Rome');
141+
142+
$transformer = new DateTimeToLocalizedStringTransformer();
143+
144+
$dateTime = new \DateTime('2010-02-03 04:05');
145+
146+
$this->assertEquals(
147+
$dateTime->format('c'),
148+
$transformer->reverseTransform('03.02.2010, 04:05')->format('c')
149+
);
150+
151+
date_default_timezone_set($tz);
152+
}
153+
137154
public function testTransformWithDifferentPatterns()
138155
{
139156
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'MM*yyyy*dd HH|mm|ss');

0 commit comments

Comments
 (0)
0