From bfb11405aa7edcd92a4ad2c6cf23c72375c56779 Mon Sep 17 00:00:00 2001 From: Serhey Dolgushev Date: Wed, 21 Jul 2021 14:00:24 +0100 Subject: [PATCH] IBX-801: Fixed timezone for DateTime fields in content name pattern #3 --- .../Core/FieldType/DateAndTime/Type.php | 6 +----- .../Core/FieldType/DateAndTime/Value.php | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/eZ/Publish/Core/FieldType/DateAndTime/Type.php b/eZ/Publish/Core/FieldType/DateAndTime/Type.php index cbd3569034..7e8bc72c09 100644 --- a/eZ/Publish/Core/FieldType/DateAndTime/Type.php +++ b/eZ/Publish/Core/FieldType/DateAndTime/Type.php @@ -14,7 +14,6 @@ use eZ\Publish\SPI\FieldType\Value as SPIValue; use DateInterval; use DateTime; -use DateTimeZone; class Type extends FieldType { @@ -64,10 +63,7 @@ public function getName(SPIValue $value, FieldDefinition $fieldDefinition, strin return ''; } - $dt = clone $value->value; - $dt->setTimezone(new DateTimeZone(date_default_timezone_get())); - - return $dt->format('D Y-d-m H:i:s'); + return $value->value->format('D Y-d-m H:i:s'); } /** diff --git a/eZ/Publish/Core/FieldType/DateAndTime/Value.php b/eZ/Publish/Core/FieldType/DateAndTime/Value.php index 0174570408..57b88bd0b9 100644 --- a/eZ/Publish/Core/FieldType/DateAndTime/Value.php +++ b/eZ/Publish/Core/FieldType/DateAndTime/Value.php @@ -10,6 +10,7 @@ use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue; use Exception; use DateTime; +use DateTimeZone; /** * Value for DateAndTime field type. @@ -50,7 +51,7 @@ public function __construct(DateTime $dateTime = null) public static function fromString($dateString) { try { - return new static(new DateTime($dateString)); + return new static(static::getDateTime($dateString)); } catch (Exception $e) { throw new InvalidArgumentValue('$dateString', $dateString, __CLASS__, $e); } @@ -66,12 +67,27 @@ public static function fromString($dateString) public static function fromTimestamp($timestamp) { try { - return new static(new DateTime("@{$timestamp}")); + return new static(static::getDateTime("@{$timestamp}")); } catch (Exception $e) { throw new InvalidArgumentValue('$timestamp', $timestamp, __CLASS__, $e); } } + /** + * Creates a DateTime object from the string with respecting server timezone. + * + * @param string $datetime + * + * @return \DateTime + */ + public static function getDateTime($datetime) + { + $dt = new DateTime($datetime); + $dt->setTimezone(new DateTimeZone(date_default_timezone_get())); + + return $dt; + } + /** * @see \eZ\Publish\Core\FieldType\Value */