Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dfa2303

Browse filesBrowse files
committed
[Form][DateTimeImmutableToDateTimeTransformer] Preserve microseconds and use \DateTime::createFromImmutable() when available
1 parent bee0056 commit dfa2303
Copy full SHA for dfa2303

File tree

2 files changed

+31
-9
lines changed
Filter options

2 files changed

+31
-9
lines changed

‎src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeImmutableToDateTimeTransformer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeImmutableToDateTimeTransformer.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public function transform($value)
4040
throw new TransformationFailedException('Expected a \DateTimeImmutable.');
4141
}
4242

43-
return \DateTime::createFromFormat(\DateTime::RFC3339, $value->format(\DateTime::RFC3339));
43+
if (\PHP_VERSION_ID >= 70300) {
44+
return \DateTime::createFromImmutable($value);
45+
}
46+
47+
return \DateTime::createFromFormat('U.u', $value->format('U.u'))->setTimezone($value->getTimezone());
4448
}
4549

4650
/**

‎src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeImmutableToDateTimeTransformerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeImmutableToDateTimeTransformerTest.php
+26-8Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,33 @@
1616

1717
class DateTimeImmutableToDateTimeTransformerTest extends TestCase
1818
{
19-
public function testTransform()
19+
/**
20+
* @dataProvider provider
21+
*/
22+
public function testTransform(\DateTime $expectedOutput, \DateTimeImmutable $input)
2023
{
2124
$transformer = new DateTimeImmutableToDateTimeTransformer();
2225

23-
$input = new \DateTimeImmutable('2010-02-03 04:05:06 UTC');
24-
$expectedOutput = new \DateTime('2010-02-03 04:05:06 UTC');
2526
$actualOutput = $transformer->transform($input);
2627

27-
$this->assertInstanceOf(\DateTime::class, $actualOutput);
2828
$this->assertEquals($expectedOutput, $actualOutput);
29+
$this->assertEquals($expectedOutput->getTimezone(), $actualOutput->getTimezone());
30+
}
31+
32+
public function provider()
33+
{
34+
return [
35+
[
36+
new \DateTime('2010-02-03 04:05:06 UTC'),
37+
new \DateTimeImmutable('2010-02-03 04:05:06 UTC'),
38+
],
39+
[
40+
(new \DateTime('2019-10-07 +11:00'))
41+
->setTime(14, 27, 11, 10042),
42+
(new \DateTimeImmutable('2019-10-07 +11:00'))
43+
->setTime(14, 27, 11, 10042),
44+
],
45+
];
2946
}
3047

3148
public function testTransformEmpty()
@@ -43,16 +60,17 @@ public function testTransformFail()
4360
$transformer->transform(new \DateTime());
4461
}
4562

46-
public function testReverseTransform()
63+
/**
64+
* @dataProvider provider
65+
*/
66+
public function testReverseTransform(\DateTime $input, \DateTimeImmutable $expectedOutput)
4767
{
4868
$transformer = new DateTimeImmutableToDateTimeTransformer();
4969

50-
$input = new \DateTime('2010-02-03 04:05:06 UTC');
51-
$expectedOutput = new \DateTimeImmutable('2010-02-03 04:05:06 UTC');
5270
$actualOutput = $transformer->reverseTransform($input);
5371

54-
$this->assertInstanceOf(\DateTimeImmutable::class, $actualOutput);
5572
$this->assertEquals($expectedOutput, $actualOutput);
73+
$this->assertEquals($expectedOutput->getTimezone(), $actualOutput->getTimezone());
5674
}
5775

5876
public function testReverseTransformEmpty()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.