Skip to content

[Form] DateTimeToLocalizedStringTransformer does not use timezone when using date only #21218

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 1 commit into from
Jan 12, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public function reverseTransform($value)
try {
if ($dateOnly) {
// we only care about year-month-date, which has been delivered as a timestamp pointing to UTC midnight
return new \DateTime(gmdate('Y-m-d', $timestamp), new \DateTimeZone($this->inputTimezone));
$dateTime = new \DateTime(gmdate('Y-m-d', $timestamp), new \DateTimeZone($this->outputTimezone));
Copy link
Member

@nicolas-grekas nicolas-grekas Jan 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, @tkleinhakisa is right! this should be inputTimezone instead of outputTimezone!
when dateOnly is true, the DateTime should not depend on the current hour!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not my point ! for me it shoud be $dateTime = new \DateTime(gmdate('Y-m-d', $timestamp)) so that the date i write/select in my form is the same as the one i get back in the code

} else {
// read timestamp into DateTime object - the formatter delivers a timestamp
$dateTime = new \DateTime(sprintf('@%s', $timestamp));
}

// read timestamp into DateTime object - the formatter delivers a timestamp
$dateTime = new \DateTime(sprintf('@%s', $timestamp));
// set timezone separately, as it would be ignored if set via the constructor,
// see http://php.net/manual/en/datetime.construct.php
$dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ public function testReverseTransformWithDifferentTimezones()
$this->assertDateTimeEquals($dateTime, $transformer->reverseTransform('03.02.2010, 04:05'));
}

public function testReverseTransformOnlyDateWithDifferentTimezones()
{
$transformer = new DateTimeToLocalizedStringTransformer('Europe/Berlin', 'Pacific/Tahiti', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');

$dateTime = new \DateTime('2017-01-10 11:00', new \DateTimeZone('Europe/Berlin'));

$this->assertDateTimeEquals($dateTime, $transformer->reverseTransform('2017-01-10'));
}

public function testReverseTransformWithDifferentPatterns()
{
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'MM*yyyy*dd HH|mm|ss');
Expand Down