Skip to content

Commit

Permalink
Merge pull request #846 from boekkooi/datetime-immutable
Browse files Browse the repository at this point in the history
Support \DateTimeImmutable and fixes timezone not being set correctly
  • Loading branch information
Hüseyin Mert committed Apr 5, 2016
2 parents 1858112 + 558714f commit 9844b5b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Grid/Column/DateTimeColumn.php
Expand Up @@ -111,14 +111,14 @@ public function getDisplayedValue($value)
/**
* DateTimeHelper::getDatetime() from SonataIntlBundle
*
* @param \Datetime|string|integer $data
* @param null|string timezone
* @param \Datetime|\DateTimeImmutable|string|integer $data
* @param \DateTimeZone timezone
* @return \Datetime
*/
protected function getDatetime($data, $timezone = null)
protected function getDatetime($data, \DateTimeZone $timezone)
{
if($data instanceof \DateTime) {
return $data;
if($data instanceof \DateTime || $data instanceof \DateTimeImmutable) {
return $data->setTimezone($timezone);
}

// the format method accept array or integer
Expand Down
66 changes: 66 additions & 0 deletions Tests/Grid/Column/DateTimeColumnTest.php
@@ -0,0 +1,66 @@
<?php
namespace APY\DataGridBundle\Tests\Grid\Column;

use APY\DataGridBundle\Grid\Column\DateTimeColumn;

class DateTimeColumnTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideDisplayInput
*/
public function testCorrectDisplayOut($value, $expectedOutput, $timeZone = null)
{
$column = new DateTimeColumn();
$column->setFormat('Y-m-d H:i:s');

if ($timeZone !== null) {
$column->setTimezone($timeZone);
}

$this->assertEquals(
$expectedOutput,
$column->getDisplayedValue($value)
);
}

public function testDisplayValueForDateTimeImmutable()
{
if (PHP_VERSION_ID < 50500) {
$this->markTestSkipped('\\DateTimeImmutable was introduced in PHP 5.5');
}

$now = new \DateTimeImmutable();

$column = new DateTimeColumn();
$column->setFormat('Y-m-d H:i:s');
$this->assertEquals(
$now->format('Y-m-d H:i:s'),
$column->getDisplayedValue($now)
);
}

public function testDateTimeZoneForDisplayValueIsTheSameAsTheColumn()
{
$column = new DateTimeColumn();
$column->setFormat('Y-m-d H:i:s');
$column->setTimezone('UTC');

$now = new \DateTime('2000-01-01 01:00:00', new \DateTimeZone('Europe/Amsterdam'));

$this->assertEquals(
'2000-01-01 00:00:00',
$column->getDisplayedValue($now)
);
}

public function provideDisplayInput()
{
$now = new \DateTime();

return array(
array($now, $now->format('Y-m-d H:i:s')),
array('2016/01/01 12:13:14', '2016-01-01 12:13:14'),
array(1, '1970-01-01 00:00:01', 'UTC')
);
}
}

0 comments on commit 9844b5b

Please sign in to comment.