Skip to content
Closed
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
27 changes: 27 additions & 0 deletions src/Driver/DateTimeFormatInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of Cycle ORM package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cycle\Database\Driver;

/**
* The methods of this interface should be part of Cycle\Database\Driver\DriverInterface,
* but adding new methods to the interface will break backward compatibility in projects
* that use their own driver implementations.
*
* The methods of this interface can be moved to the main driver interface when upgrading the project's major version.
*/
interface DateTimeFormatInterface
{
/**
* Returns DateTime format of the driver.
*/
public function getDateTimeFormat(): string;
}
14 changes: 10 additions & 4 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* Provides low level abstraction at top of
*/
abstract class Driver implements DriverInterface, NamedInterface, LoggerAwareInterface
abstract class Driver implements DriverInterface, NamedInterface, DateTimeFormatInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

Expand Down Expand Up @@ -578,9 +578,7 @@ protected function formatDatetime(\DateTimeInterface $value): string
throw new DriverException($e->getMessage(), (int) $e->getCode(), $e);
}

return $datetime->format(
$this->config->options['withDatetimeMicroseconds'] ? self::DATETIME_MICROSECONDS : self::DATETIME,
);
return $datetime->format($this->getDateTimeFormat());
}

/**
Expand Down Expand Up @@ -706,4 +704,12 @@ protected function defineLoggerContext(float $queryStart, \PDOStatement|PDOState

return $context;
}

/**
* Returns DateTime format of the driver.
*/
public function getDateTimeFormat(): string
{
return $this->config->options['withDatetimeMicroseconds'] ? self::DATETIME_MICROSECONDS : self::DATETIME;
}
}
18 changes: 18 additions & 0 deletions tests/Database/Functional/Driver/Common/Driver/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ public function datetimeDataProvider(): \Traversable
yield [new class('2000-01-23T01:23:45.678+09:00') extends \DateTime {}];
}

/**
* @dataProvider dateTimeFormatProvider
*/
public function testGetDateTimeFormat(bool $withDatetimeMicroseconds, string $format): void
{
$options = [
'withDatetimeMicroseconds' => $withDatetimeMicroseconds,
];
$driver = $this->db(driverConfig: ['options' => $options])->getDriver();
self::assertSame($format, $driver->getDateTimeFormat());
}

public function dateTimeFormatProvider(): \Traversable
{
yield [false, 'Y-m-d H:i:s'];
yield [true, 'Y-m-d H:i:s.u'];
}

public function testClearCache(): void
{
$driver = $this->mockDriver();
Expand Down
Loading