From 93806a80369a84a904f41c76ec3467fa31a54c8c Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Thu, 27 Aug 2015 15:39:26 +0900 Subject: [PATCH 1/5] Update DateTime cookbook code It seems that existing code was outdated and lacked ``use`` statement in order to work correctly. We also added instructions on how to configure the new type, since it may not be straightforward for newcomers. --- docs/en/cookbook/working-with-datetime.rst | 47 +++++++++++++++------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/docs/en/cookbook/working-with-datetime.rst b/docs/en/cookbook/working-with-datetime.rst index fc548dac0..12757b742 100644 --- a/docs/en/cookbook/working-with-datetime.rst +++ b/docs/en/cookbook/working-with-datetime.rst @@ -85,43 +85,60 @@ the UTC time at the time of the booking and the timezone the event happened in. use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ConversionException; + use Doctrine\DBAL\Types\DateTimeType; class UTCDateTimeType extends DateTimeType { static private $utc = null; - public function convertToDatabaseValue($value, AbstractPlatform $platform) + private static function getUtc() { - if ($value === null) { - return null; + if (!self::$utc) { + self::$utc = new \DateTimeZone('UTC'); } + return self::$utc; + } - return $value->format($platform->getDateTimeFormatString(), - (self::$utc) ? self::$utc : (self::$utc = new \DateTimeZone('UTC')) - ); + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + if ($value) { + $value->setTimezone(self::getUtc()); + } + + return parent::convertToDatabaseValue($value, $platform); } public function convertToPHPValue($value, AbstractPlatform $platform) { - if ($value === null) { - return null; + if ($value === null || $value instanceof \DateTime) { + return $value; } - $val = \DateTime::createFromFormat( - $platform->getDateTimeFormatString(), - $value, - (self::$utc) ? self::$utc : (self::$utc = new \DateTimeZone('UTC')) - ); + $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::getUtc()); + if (!$val) { - throw ConversionException::conversionFailed($value, $this->getName()); + throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); } + return $val; } + } This database type makes sure that every DateTime instance is always saved in UTC, relative -to the current timezone that the passed DateTime instance has. To be able to transform these values +to the current timezone that the passed DateTime instance has. To actually use that new type, configure it in ``config.yml`` like so: + +.. code-block:: yml + + doctrine: + dbal: + types: + datetime: DoctrineExtensions\DBAL\Types\UTCDateTimeType + datetimetz: DoctrineExtensions\DBAL\Types\UTCDateTimeType + + +To be able to transform these values back into their real timezone you have to save the timezone in a separate field of the entity requiring timezoned datetimes: From 5b22e59383d7dc6dcc9e3247c10656052cf2d3fb Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 11 Dec 2015 18:43:53 +0100 Subject: [PATCH 2/5] Removing symfony-specific documentation --- docs/en/cookbook/working-with-datetime.rst | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/docs/en/cookbook/working-with-datetime.rst b/docs/en/cookbook/working-with-datetime.rst index 12757b742..4272c3a40 100644 --- a/docs/en/cookbook/working-with-datetime.rst +++ b/docs/en/cookbook/working-with-datetime.rst @@ -127,16 +127,7 @@ the UTC time at the time of the booking and the timezone the event happened in. } This database type makes sure that every DateTime instance is always saved in UTC, relative -to the current timezone that the passed DateTime instance has. To actually use that new type, configure it in ``config.yml`` like so: - -.. code-block:: yml - - doctrine: - dbal: - types: - datetime: DoctrineExtensions\DBAL\Types\UTCDateTimeType - datetimetz: DoctrineExtensions\DBAL\Types\UTCDateTimeType - +to the current timezone that the passed DateTime instance has. To be able to transform these values back into their real timezone you have to save the timezone in a separate field of the entity From a130ff96ba8933c7a760edf9b828fd766fe47ed1 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 11 Dec 2015 18:48:23 +0100 Subject: [PATCH 3/5] #1497 - Using a PHP code block to describe how a datetime+utc type can be set up --- docs/en/cookbook/working-with-datetime.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/en/cookbook/working-with-datetime.rst b/docs/en/cookbook/working-with-datetime.rst index 4272c3a40..67a78b258 100644 --- a/docs/en/cookbook/working-with-datetime.rst +++ b/docs/en/cookbook/working-with-datetime.rst @@ -129,6 +129,20 @@ the UTC time at the time of the booking and the timezone the event happened in. This database type makes sure that every DateTime instance is always saved in UTC, relative to the current timezone that the passed DateTime instance has. +To actually use this new type instead of the default ``datetime`` type, you need to run following +code before bootstrapping the ORM: + +.. code-block:: php + + Date: Fri, 11 Dec 2015 18:52:26 +0100 Subject: [PATCH 4/5] #1497 - simplifying UTC datetime handling logic in the examples Highly performance sensitive code should avoid method calls, sadly --- docs/en/cookbook/working-with-datetime.rst | 29 +++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/docs/en/cookbook/working-with-datetime.rst b/docs/en/cookbook/working-with-datetime.rst index 67a78b258..2b5317c69 100644 --- a/docs/en/cookbook/working-with-datetime.rst +++ b/docs/en/cookbook/working-with-datetime.rst @@ -91,18 +91,9 @@ the UTC time at the time of the booking and the timezone the event happened in. { static private $utc = null; - private static function getUtc() - { - if (!self::$utc) { - self::$utc = new \DateTimeZone('UTC'); - } - - return self::$utc; - } - public function convertToDatabaseValue($value, AbstractPlatform $platform) { - if ($value) { + if ($value instanceof \DateTime) { $value->setTimezone(self::getUtc()); } @@ -111,17 +102,25 @@ the UTC time at the time of the booking and the timezone the event happened in. public function convertToPHPValue($value, AbstractPlatform $platform) { - if ($value === null || $value instanceof \DateTime) { + if (null === $value || $value instanceof \DateTime) { return $value; } - $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::getUtc()); + $converted = \DateTime::createFromFormat( + $platform->getDateTimeFormatString(), + $value, + self::$utc ? self::$utc : self::$utc = new \DateTimeZone('UTC') + ); - if (!$val) { - throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); + if (! $converted) { + throw ConversionException::conversionFailedFormat( + $value, + $this->getName(), + $platform->getDateTimeFormatString() + ); } - return $val; + return $converted; } } From 8c086d1a6ea147fa40c1ed6b5cf4a0e95f2c6594 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 11 Dec 2015 18:53:18 +0100 Subject: [PATCH 5/5] #1497 - minor CS fixes --- docs/en/cookbook/working-with-datetime.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/en/cookbook/working-with-datetime.rst b/docs/en/cookbook/working-with-datetime.rst index 2b5317c69..3b1e2e59f 100644 --- a/docs/en/cookbook/working-with-datetime.rst +++ b/docs/en/cookbook/working-with-datetime.rst @@ -89,7 +89,7 @@ the UTC time at the time of the booking and the timezone the event happened in. class UTCDateTimeType extends DateTimeType { - static private $utc = null; + static private $utc; public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -122,7 +122,6 @@ the UTC time at the time of the booking and the timezone the event happened in. return $converted; } - } This database type makes sure that every DateTime instance is always saved in UTC, relative