Skip to content

Commit

Permalink
implemented MySqliDriver::escapeDateInterval()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 30, 2019
1 parent e2dc926 commit 7f86cf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Dibi/Drivers/MySqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@ public function escapeDateTime(\DateTimeInterface $value): string

public function escapeDateInterval(\DateInterval $value): string
{
throw new Dibi\NotImplementedException;
if ($value->y || $value->m || $value->d) {
throw new Dibi\NotSupportedException('Only time interval is supported.');
}
return $value->format('%r%H:%I:%S.%f');
}


Expand Down
8 changes: 8 additions & 0 deletions tests/dibi/Translator.DateInterval.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ $conn = new Dibi\Connection($config);
$translator = new Dibi\Translator($conn);

switch ($config['system']) {
case 'mysql':
Assert::equal('10:20:30.0', $translator->formatValue(new DateInterval('PT10H20M30S'), null));
Assert::equal('-1:00:00.0', $translator->formatValue(DateInterval::createFromDateString('-1 hour'), null));
Assert::exception(function () use ($translator) {
$translator->formatValue(new DateInterval('P2Y4DT6H8M'), null);
}, Dibi\NotSupportedException::class, 'Only time interval is supported.');
break;

default:
Assert::exception(function () use ($translator) {
$translator->formatValue(new DateInterval('PT10H20M30S'), null);
Expand Down

0 comments on commit 7f86cf0

Please sign in to comment.