diff --git a/src/Driver/Driver.php b/src/Driver/Driver.php index d4d9a6de..7a7c5fe0 100644 --- a/src/Driver/Driver.php +++ b/src/Driver/Driver.php @@ -34,7 +34,7 @@ abstract class Driver implements DriverInterface, LoggerAwareInterface use LoggerAwareTrait; // DateTime format to be used to perform automatic conversion of DateTime objects. - protected const DATETIME = 'Y-m-d H:i:s'; + protected const DATETIME = 'Y-m-d H:i:s.u'; // Driver specific PDO options protected const DEFAULT_PDO_OPTIONS = [ @@ -605,13 +605,7 @@ protected function bindParameters(PDOStatement $statement, iterable $parameters) */ protected function formatDatetime(DateTimeInterface $value): string { - try { - $datetime = new DateTimeImmutable('now', $this->getTimezone()); - } catch (Throwable $e) { - throw new DriverException($e->getMessage(), $e->getCode(), $e); - } - - return $datetime->setTimestamp($value->getTimestamp())->format(static::DATETIME); + return (clone $value)->setTimezone($this->getTimezone())->format(static::DATETIME); } /** diff --git a/src/Driver/MySQL/Schema/MySQLColumn.php b/src/Driver/MySQL/Schema/MySQLColumn.php index b75f6f38..3bc1731b 100644 --- a/src/Driver/MySQL/Schema/MySQLColumn.php +++ b/src/Driver/MySQL/Schema/MySQLColumn.php @@ -13,6 +13,7 @@ use Spiral\Database\Driver\DriverInterface; use Spiral\Database\Exception\DefaultValueException; +use Spiral\Database\Exception\SchemaException; use Spiral\Database\Injection\Fragment; use Spiral\Database\Injection\FragmentInterface; use Spiral\Database\Schema\AbstractColumn; @@ -265,4 +266,25 @@ protected function formatDatetime(string $type, $value) return parent::formatDatetime($type, $value); } + + /** + * Set column type as datetime with specific size (precision). + * + * @param int $size + * @return self|$this + * + * @throws SchemaException + */ + public function datetime(int $size = 0): AbstractColumn + { + $this->type('datetime'); + + if ($size > 6) { + throw new SchemaException('Invalid size (precision) value; must be between 0 and 6.'); + } + + $this->size = (int)$size; + + return $this; + } }