Skip to content

Commit

Permalink
IBX-801: Fixed timezone for DateTime fields in content name pattern #3
Browse files Browse the repository at this point in the history
  • Loading branch information
SerheyDolgushev committed Jul 21, 2021
1 parent 51bed6f commit bfb1140
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 1 addition & 5 deletions eZ/Publish/Core/FieldType/DateAndTime/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use DateInterval;
use DateTime;
use DateTimeZone;

class Type extends FieldType
{
Expand Down Expand Up @@ -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');
}

/**
Expand Down
20 changes: 18 additions & 2 deletions eZ/Publish/Core/FieldType/DateAndTime/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use Exception;
use DateTime;
use DateTimeZone;

/**
* Value for DateAndTime field type.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
*/
Expand Down

0 comments on commit bfb1140

Please sign in to comment.