Skip to content

Commit bfb1140

Browse files
IBX-801: Fixed timezone for DateTime fields in content name pattern #3
1 parent 51bed6f commit bfb1140

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

eZ/Publish/Core/FieldType/DateAndTime/Type.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use eZ\Publish\SPI\FieldType\Value as SPIValue;
1515
use DateInterval;
1616
use DateTime;
17-
use DateTimeZone;
1817

1918
class Type extends FieldType
2019
{
@@ -64,10 +63,7 @@ public function getName(SPIValue $value, FieldDefinition $fieldDefinition, strin
6463
return '';
6564
}
6665

67-
$dt = clone $value->value;
68-
$dt->setTimezone(new DateTimeZone(date_default_timezone_get()));
69-
70-
return $dt->format('D Y-d-m H:i:s');
66+
return $value->value->format('D Y-d-m H:i:s');
7167
}
7268

7369
/**

eZ/Publish/Core/FieldType/DateAndTime/Value.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
1111
use Exception;
1212
use DateTime;
13+
use DateTimeZone;
1314

1415
/**
1516
* Value for DateAndTime field type.
@@ -50,7 +51,7 @@ public function __construct(DateTime $dateTime = null)
5051
public static function fromString($dateString)
5152
{
5253
try {
53-
return new static(new DateTime($dateString));
54+
return new static(static::getDateTime($dateString));
5455
} catch (Exception $e) {
5556
throw new InvalidArgumentValue('$dateString', $dateString, __CLASS__, $e);
5657
}
@@ -66,12 +67,27 @@ public static function fromString($dateString)
6667
public static function fromTimestamp($timestamp)
6768
{
6869
try {
69-
return new static(new DateTime("@{$timestamp}"));
70+
return new static(static::getDateTime("@{$timestamp}"));
7071
} catch (Exception $e) {
7172
throw new InvalidArgumentValue('$timestamp', $timestamp, __CLASS__, $e);
7273
}
7374
}
7475

76+
/**
77+
* Creates a DateTime object from the string with respecting server timezone.
78+
*
79+
* @param string $datetime
80+
*
81+
* @return \DateTime
82+
*/
83+
public static function getDateTime($datetime)
84+
{
85+
$dt = new DateTime($datetime);
86+
$dt->setTimezone(new DateTimeZone(date_default_timezone_get()));
87+
88+
return $dt;
89+
}
90+
7591
/**
7692
* @see \eZ\Publish\Core\FieldType\Value
7793
*/

0 commit comments

Comments
 (0)