Skip to content

Commit

Permalink
Merge 7e7e259 into 82abdea
Browse files Browse the repository at this point in the history
  • Loading branch information
z38 committed Jun 16, 2016
2 parents 82abdea + 7e7e259 commit d92a116
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Templating/Helper/DateTimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ public function getDatetime($data, $timezone = null)
return $data;
}

if ($data instanceof \DateTimeImmutable) {
$date = \DateTime::createFromFormat('U', $data->getTimestamp());

return $date;
}

// the format method accept array or integer
if (is_numeric($data)) {
$data = (int) $data;
Expand Down
21 changes: 21 additions & 0 deletions Tests/Helper/DateTimeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,25 @@ public function testLocaleTimezones()
$this->assertEquals('12:37', $helper->format($dateParis, 'HH:mm'), 'A date in the Europe/Paris timezone, should be corrected when formatted with timezone Europe/London.');
$this->assertEquals('13:37', $helperWithMapping->format($dateParis, 'HH:mm'), 'A date in the Europe/Paris timezone, should be corrected when formatted with timezone Europe/Paris.');
}

public function testImmutable()
{
if (!class_exists('\DateTimeImmutable')) {
$this->markTestSkipped('\DateTimeImmutable is not available.');
}

$localeDetector = $this->getMock('Sonata\IntlBundle\Locale\LocaleDetectorInterface');
$localeDetector->expects($this->any())
->method('getLocale')->will($this->returnValue('fr'));

$timezoneDetector = $this->getMock('Sonata\IntlBundle\Timezone\TimezoneDetectorInterface');
$timezoneDetector->expects($this->any())
->method('getTimezone')->will($this->returnValue('Europe/Paris'));

$helper = new DateTimeHelper($timezoneDetector, 'UTF-8', $localeDetector);

$date = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s T', '2009-02-15 15:16:17 HKT');

$this->assertEquals('08:16', $helper->format($date, 'HH:mm'));
}
}

0 comments on commit d92a116

Please sign in to comment.