8000 [Form][DateTimeImmutableToDateTimeTransformer] Preserve microseconds and use \DateTime::createFromImmutable() when available by fancyweb · Pull Request #33885 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form][DateTimeImmutableToDateTimeTransformer] Preserve microseconds and use \DateTime::createFromImmutable() when available #33885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form][DateTimeImmutableToDateTimeTransformer] Preserve microseconds …
…and use \DateTime::createFromImmutable() when available
  • Loading branch information
< 8000 div data-show-on-forbidden-error hidden>

Uh oh!

There was an error while loading. Please reload this page.

fancyweb committed Oct 7, 2019
commit dfa23034c3885f0d6e63eb43648027f262b9a0fe
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public function transform($value)
throw new TransformationFailedException('Expected a \DateTimeImmutable.');
}

return \DateTime::createFromFormat(\DateTime::RFC3339, $value->format(\DateTime::RFC3339));
if (\PHP_VERSION_ID >= 70300) {
return \DateTime::createFromImmutable($value);
}

return \DateTime::createFromFormat('U.u', $value->format('U.u'))->setTimezone($value->getTimezone());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,33 @@

class DateTimeImmutableToDateTimeTransformerTest extends TestCase
{
public function testTransform()
/**
* @dataProvider provider
*/
public function testTransform(\DateTime $expectedOutput, \DateTimeImmutable $input)
{
$transformer = new DateTimeImmutableToDateTimeTransformer();

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

$this->assertInstanceOf(\DateTime::class, $actualOutput);
$this->assertEquals($expectedOutput, $actualOutput);
$this->assertEquals($expectedOutput->getTimezone(), $actualOutput->getTimezone());
}

public function provider()
{
return [
[
new \DateTime('2010-02-03 04:05:06 UTC'),
new \DateTimeImmutable('2010-02-03 04:05:06 UTC'),
],
[
(new \DateTime('2019-10-07 +11:00'))
->setTime(14, 27, 11, 10042),
(new \DateTimeImmutable('2019-10-07 +11:00'))
->setTime(14, 27, 11, 10042),
],
];
}

public function testTransformEmpty()
Expand All @@ -43,16 +60,17 @@ public function testTransformFail()
$transformer->transform(new \DateTime());
}

public function testReverseTransform()
/**
* @dataProvider provider
*/
public function testReverseTransform(\DateTime $input, \DateTimeImmutable $expectedOutput)
{
$transformer = new DateTimeImmutableToDateTimeTransformer();

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

$this->assertInstanceOf(\DateTimeImmutable::class, $actualOutput);
$this->assertEquals($expectedOutput, $actualOutput);
$this->assertEquals($expectedOutput->getTimezone(), $actualOutput->getTimezone());
}

public function testReverseTransformEmpty()
Expand Down
0