10000 bug #29413 [Serializer] fixed DateTimeNormalizer to maintain microsec… · symfony/symfony@c2d2f5b · GitHub
[go: up one dir, main page]

Skip to content

Commit c2d2f5b

Browse files
bug #29413 [Serializer] fixed DateTimeNormalizer to maintain microseconds when a different timezone required (rvitaliy)
This PR was squashed before being merged into the 3.4 branch (closes #29413). Discussion ---------- [Serializer] fixed DateTimeNormalizer to maintain microseconds when a different timezone required | Q | A | ------------- | --- | Branch? | 3.4 up to 4.2 for bug fixes | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | # | License | MIT | Doc PR | none fixed `DateTimeNormalizer::normalizer()` when `$object` is mutated internally to change timezone we lost microseconds of origina `$object` Commits ------- 2bf8a1c [Serializer] fixed DateTimeNormalizer to maintain microseconds when a different timezone required
2 parents 17874f6 + 2bf8a1c commit c2d2f5b

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function normalize($object, $format = null, array $context = array())
5959
$timezone = $this->getTimezone($context);
6060

6161
if (null !== $timezone) {
62-
$object = (new \DateTimeImmutable('@'.$object->getTimestamp()))->setTimezone($timezone);
62+
$object = clone $object;
63+
$object = $object->setTimezone($timezone);
6364
}
6465

6566
return $object->format($format);

src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,82 @@ public function normalizeUsingTimeZonePassedInContextProvider()
7878
yield array('2016-12-01T09:00:00+09:00', new \DateTimeImmutable('2016/12/01', new \DateTimeZone('UTC')), new \DateTimeZone('Japan'));
7979
}
10000
8080

81+
/**
82+
* @dataProvider normalizeUsingTimeZonePassedInContextAndExpectedFormatWithMicrosecondsProvider
83+
*/
84+
public function testNormalizeUsingTimeZonePassedInContextAndFormattedWithMicroseconds($expected, $expectedFormat, $input, $timezone)
85+
{
86+
$this->assertSame(
87+
$expected,
88+
$this->normalizer->normalize(
89+
$input,
90+
null,
91+
array(
92+
DateTimeNormalizer::TIMEZONE_KEY => $timezone,
93+
DateTimeNormalizer::FORMAT_KEY => $expectedFormat,
94+
)
95+
)
96+
);
97+
}
98+
99+
public function normalizeUsingTimeZonePassedInContextAndExpectedFormatWithMicrosecondsProvider()
100+
{
101+
yield array(
102+
'2018-12-01T18:03:06.067634',
103+
'Y-m-d\TH:i:s.u',
104+
\DateTime::createFromFormat(
105+
'Y-m-d\TH:i:s.u',
106+
'2018-12-01T18:03:06.067634',
107+
new \DateTimeZone('UTC')
108+
),
109+
null,
110+
);
111+
112+
yield array(
113+
'2018-12-01T18:03:06.067634',
114+
'Y-m-d\TH:i:s.u',
115+
\DateTime::createFromFormat(
116+
'Y-m-d\TH:i:s.u',
117+
'2018-12-01T18:03:06.067634',
118+
new \DateTimeZone('UTC')
119+
),
120+
new \DateTimeZone('UTC'),
121+
);
122+
123+
yield array(
124+
'2018-12-01T19:03:06.067634+01:00',
125+
'Y-m-d\TH:i:s.uP',
126+
\DateTimeImmutable::createFromFormat(
127+
'Y-m-d\TH:i:s.u',
128+
'2018-12-01T18:03:06.067634',
129+
new \DateTimeZone('UTC')
130+
),
131+
new \DateTimeZone('Europe/Rome'),
132+
);
133+
134+
yield array(
135+
'2018-12-01T20:03:06.067634+02:00',
136+
'Y-m-d\TH:i:s.uP',
137+
\DateTime::createFromFormat(
138+
'Y-m-d\TH:i:s.u',
139+
'2018-12-01T18:03:06.067634',
140+
new \DateTimeZone('UTC')
141+
),
142+
new \DateTimeZone('Europe/Kiev'),
143+
);
144+
145+
yield array(
146+
'2018-12-01T21:03:06.067634',
147+
'Y-m-d\TH:i:s.u',
148+
\DateTime::createFromFormat(
149+
'Y-m-d\TH:i:s.u',
150+
'2018-12-01T18:03:06.067634',
151+
new \DateTimeZone('UTC')
152+
),
153+
new \Da 57AE teTimeZone('Europe/Moscow'),
154+
);
155+
}
156+
81157
/**
82158
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
83159
* @expectedExceptionMessage The object must implement the "\DateTimeInterface".

0 commit comments

Comments
 (0)
0