Skip to content

Commit

Permalink
[7.0] EZP-28269: Fixed inconsistent creating of eztime fromTimestamp (#…
Browse files Browse the repository at this point in the history
…2159)

* EZP-28269: Aligned Time::fromTimestamp with DateAndTime::fromTimestamp

* EZP-28269: Fixed Unit Tests

* EZP-28269: Fixed eztime integration tests

* [Doc] EZP-28269: Updated 7.0 BC doc
  • Loading branch information
alongosz authored and Łukasz Serwatka committed Nov 23, 2017
1 parent 644b847 commit 9ab2e6e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
3 changes: 3 additions & 0 deletions doc/bc/changes-7.0.md
Expand Up @@ -24,6 +24,9 @@ Changes affecting version compatibility with former or future versions.
* "cache_pool_name" siteaccess setting has been removed & replaced by "cache_service_name" as the semantic is different.
The new setting should contain the full service name of a symfony cache service, by default app_cache.app is used.

* The method `eZ\Publish\Core\FieldType\Time\Value::fromTimestamp` returns `Time\Value` without
taking into account the timezone. The reason for this change is consistency with the behavior of
`eZ\Publish\Core\FieldType\DateAndTime\Value::fromTimestamp`.

## Deprecations

Expand Down
Expand Up @@ -185,7 +185,7 @@ public function provideInvalidCreationFieldData()
/**
* Get update field externals data.
*
* @return array
* @return TimeValue
*/
public function getValidUpdateFieldData()
{
Expand All @@ -206,9 +206,9 @@ public function assertUpdatedFieldDataLoadedCorrect(Field $field)
$field->value
);

$dateTime = new DateTime();
$dateTime = new DateTime('@12345678');
$expectedData = array(
'time' => $dateTime->setTimestamp(12345678)->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(),
'time' => $dateTime->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(),
);
$this->assertPropertiesCorrect(
$expectedData,
Expand Down Expand Up @@ -305,12 +305,13 @@ public function assertCopiedFieldDataLoadedCorrectly(Field $field)
*/
public function provideToHashData()
{
$dateTime = new DateTime();
$timestamp = 123456;
$dateTime = new DateTime("@{$timestamp}");

return array(
array(
TimeValue::fromTimestamp(123456),
$dateTime->setTimestamp(123456)->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(),
TimeValue::fromTimestamp($timestamp),
$dateTime->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(),
),
);
}
Expand Down
46 changes: 23 additions & 23 deletions eZ/Publish/Core/FieldType/Tests/TimeTest.php
Expand Up @@ -140,44 +140,44 @@ public function provideInvalidInputForAcceptValue()
public function provideValidInputForAcceptValue()
{
$dateTime = new DateTime();
$secondsInDay = 24 * 60 * 60;
// change timezone to UTC (+00:00) to be able to calculate proper TimeValue
$timestamp = $dateTime->setTimezone(new \DateTimeZone('UTC'))->getTimestamp();

return array(
array(
return [
[
null,
new TimeValue(),
),
array(
],
[
'2012-08-28 12:20',
new TimeValue(44400),
),
array(
],
[
'2012-08-28 12:20 Europe/Berlin',
new TimeValue(44400),
),
array(
],
[
'2012-08-28 12:20 Asia/Sakhalin',
new TimeValue(44400),
),
array(
// Set $dateTime to proper time for correct offset
$dateTime->setTimestamp(1372896001)->getTimestamp(),
// Correct for negative offset
new TimeValue(($secondsInDay + $dateTime->getOffset() + 1) % $secondsInDay),
),
array(
TimeValue::fromTimestamp($timestamp = 1346149200),
],
[
// create new DateTime object from timestamp w/o taking into account server timezone
(new DateTime('@1372896001'))->getTimestamp(),
new TimeValue(1),
],
[
TimeValue::fromTimestamp($timestamp),
new TimeValue(
$dateTime->setTimestamp($timestamp)->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp()
$timestamp - $dateTime->setTime(0, 0, 0)->getTimestamp()
),
),
array(
],
[
clone $dateTime,
new TimeValue(
$dateTime->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp()
),
),
);
],
];
}

/**
Expand Down
3 changes: 1 addition & 2 deletions eZ/Publish/Core/FieldType/Time/Value.php
Expand Up @@ -86,8 +86,7 @@ public static function fromString($timeString)
public static function fromTimestamp($timestamp)
{
try {
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);
$dateTime = new DateTime("@{$timestamp}");

return static::fromDateTime($dateTime);
} catch (Exception $e) {
Expand Down

0 comments on commit 9ab2e6e

Please sign in to comment.