From df129635cf2de5f137145af478335bcda78ed019 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 11 Dec 2015 18:52:26 +0100 Subject: [PATCH] #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; } }