1
0
mirror of synced 2025-03-06 12:56:10 +03:00

#1497 - simplifying UTC datetime handling logic in the examples

Highly performance sensitive code should avoid method calls, sadly
This commit is contained in:
Marco Pivetta 2015-12-11 18:52:26 +01:00
parent a130ff96ba
commit df129635cf

View File

@ -91,18 +91,9 @@ the UTC time at the time of the booking and the timezone the event happened in.
{ {
static private $utc = null; 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) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value) { if ($value instanceof \DateTime) {
$value->setTimezone(self::getUtc()); $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) public function convertToPHPValue($value, AbstractPlatform $platform)
{ {
if ($value === null || $value instanceof \DateTime) { if (null === $value || $value instanceof \DateTime) {
return $value; 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) { if (! $converted) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); throw ConversionException::conversionFailedFormat(
$value,
$this->getName(),
$platform->getDateTimeFormatString()
);
} }
return $val; return $converted;
} }
} }