Skip to content

Commit

Permalink
IBX-801: Fixed timezone for DateTime fields in content name pattern #4
Browse files Browse the repository at this point in the history
  • Loading branch information
SerheyDolgushev committed Jul 21, 2021
1 parent bfb1140 commit 2bd2545
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions eZ/Publish/Core/FieldType/DateAndTime/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use Exception;
use DateTime;
use DateTimeZone;

/**
* Value for DateAndTime field type.
Expand Down Expand Up @@ -51,7 +50,7 @@ public function __construct(DateTime $dateTime = null)
public static function fromString($dateString)
{
try {
return new static(static::getDateTime($dateString));
return new static(new DateTime($dateString));
} catch (Exception $e) {
throw new InvalidArgumentValue('$dateString', $dateString, __CLASS__, $e);
}
Expand All @@ -67,27 +66,15 @@ public static function fromString($dateString)
public static function fromTimestamp($timestamp)
{
try {
return new static(static::getDateTime("@{$timestamp}"));
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);

return new static($dateTime);
} 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 2bd2545

Please sign in to comment.