Revert "DateInterval Type now is stored as Y-m-d H:i:s interval representation."

This reverts commit aa4d483f.
parent aa4d483f
......@@ -10,8 +10,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*/
class DateIntervalType extends Type
{
const DATEINTERVAL_PATTERN = '#(?P<date>\d{4}-\d{2}-\d{2}).(?P<time>\d{2}:\d{2}:\d{2})#';
/**
* {@inheritdoc}
*/
......@@ -33,19 +31,8 @@ class DateIntervalType extends Type
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
$spec = null;
if ($value !== null) {
/** @var \DateInterval $value */
$spec = str_pad($value->y, 4, '0', STR_PAD_LEFT) . '-'
. $value->format('%M') . '-'
. $value->format('%D') . ' '
. $value->format('%H') . ':'
. $value->format('%I') . ':'
. $value->format('%S')
;
}
return $spec;
return ($value !== null)
? $value->format('P%yY%mM%dDT%hH%iM%sS') : null;
}
/**
......@@ -57,13 +44,10 @@ class DateIntervalType extends Type
return $value;
}
if (preg_match(self::DATEINTERVAL_PATTERN, $value, $parts) !== 1) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'Y-m-d H:i:s');
}
try {
$interval = new \DateInterval('P' . $parts['date'] . 'T' . $parts['time']);
$interval = new \DateInterval($value);
} catch (\Exception $e) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s');
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PxYxMxDTxHxMxS');
}
......
......@@ -19,9 +19,9 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public function testDateIntervalConvertsToDatabaseValue()
{
$interval = new \DateInterval('P2Y1DT1H2M3S');
$interval = new \DateInterval('P1DT1H2M3S');
$expected = '0002-00-01 01:02:03';
$expected = $interval->format('P%yY%mM%dDT%hH%iM%sS');
$actual = $this->_type->convertToDatabaseValue($interval, $this->_platform);
$this->assertEquals($expected, $actual);
......@@ -29,9 +29,9 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public function testDateIntervalConvertsToPHPValue()
{
$date = $this->_type->convertToPHPValue('0002-00-01 01:02:03', $this->_platform);
$date = $this->_type->convertToPHPValue('P1DT1H2M3S', $this->_platform);
$this->assertInstanceOf('DateInterval', $date);
$this->assertEquals('P2Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS'));
$this->assertEquals('P0Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS'));
}
public function testInvalidDateIntervalFormatConversion()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment