Skip to content

Commit

Permalink
IBX-300: Added timezone offset to timestamp (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszdebinski authored May 12, 2021
1 parent 5161006 commit da07b78
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
42 changes: 29 additions & 13 deletions eZ/Publish/API/Repository/Tests/FieldType/DateIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public function getInvalidValidatorConfiguration()
*/
public function getValidCreationFieldData()
{
// We may only create times from timestamps here, since storing will
// loose information about the timezone.
return DateValue::fromTimestamp(86400);
$dateTime = $this->getValueOneDate();

return DateValue::fromTimestamp($dateTime->getTimestamp());
}

/**
Expand Down Expand Up @@ -147,8 +147,10 @@ public function assertFieldDataLoadedCorrect(Field $field)
$field->value
);

$dateTime = $this->getValueOneDate();

$expectedData = [
'date' => new DateTime('@86400'),
'date' => $dateTime,
];

$this->assertPropertiesCorrect(
Expand Down Expand Up @@ -195,7 +197,9 @@ public function provideInvalidCreationFieldData()
*/
public function getValidUpdateFieldData()
{
return DateValue::fromTimestamp(86400);
$dateTime = $this->getValueOneDate();

return DateValue::fromTimestamp($dateTime->getTimestamp());
}

/**
Expand All @@ -213,8 +217,10 @@ public function assertUpdatedFieldDataLoadedCorrect(Field $field)
$field->value
);

$dateTime = $this->getValueOneDate();

$expectedData = [
'date' => new DateTime('@86400'),
'date' => $dateTime,
];
$this->assertPropertiesCorrect(
$expectedData,
Expand Down Expand Up @@ -283,8 +289,9 @@ public function assertCopiedFieldDataLoadedCorrectly(Field $field)
*/
public function provideToHashData()
{
$timestamp = 186401;
$dateTime = new DateTime("@{$timestamp}");
$timestamp = 186400;
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);

return [
[
Expand Down Expand Up @@ -327,17 +334,17 @@ public function provideFromHashData()
return [
[
[
'timestamp' => $timestamp,
'timestamp' => $dateTime->getTimestamp(),
'rfc850' => ($rfc850 = $dateTime->format(DateTime::RFC850)),
],
DateValue::fromString($rfc850),
],
[
[
'timestamp' => $timestamp,
'timestamp' => $dateTime->getTimestamp(),
'rfc850' => null,
],
DateValue::fromTimestamp($timestamp),
DateValue::fromTimestamp($dateTime->getTimestamp()),
],
];
}
Expand All @@ -360,12 +367,16 @@ public function providerForTestIsNotEmptyValue()

protected function getValidSearchValueOne()
{
return 86400;
$dateTime = $this->getValueOneDate();

return $dateTime->getTimestamp();
}

protected function getValidSearchValueTwo()
{
return 172800;
$dateTime = new DateTime('1970-01-03');

return $dateTime->getTimestamp();
}

protected function getSearchTargetValueOne()
Expand All @@ -387,4 +398,9 @@ protected function getSearchTargetValueTwo()

return '1970-01-03T00:00:00Z';
}

protected function getValueOneDate(): DateTime
{
return new DateTime('1970-01-02');
}
}
5 changes: 4 additions & 1 deletion eZ/Publish/Core/FieldType/Date/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public static function fromString($dateString)
public static function fromTimestamp($timestamp)
{
try {
return new static(new DateTime("@{$timestamp}"));
$datetime = new DateTime();
$datetime->setTimestamp($timestamp);

return new static($datetime);
} catch (Exception $e) {
throw new InvalidArgumentValue('$timestamp', $timestamp, __CLASS__, $e);
}
Expand Down
8 changes: 3 additions & 5 deletions eZ/Publish/Core/FieldType/Tests/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,11 @@ public function provideValidInputForAcceptValue()
],
[
($timestamp = 1346149200),
new DateValue(
new DateTime("@{$timestamp}")
),
new DateValue((new DateTime())->setTimestamp($timestamp)),
],
[
DateValue::fromTimestamp($timestamp = 1372895999),
new DateValue(new DateTime("@{$timestamp}")),
new DateValue((new DateTime())->setTimestamp($timestamp)),
],
[
($dateTime = new DateTime()),
Expand Down Expand Up @@ -260,7 +258,7 @@ public function provideInputForFromHash()
[
'timestamp' => ($timestamp = 1362614400),
],
new DateValue(new DateTime("@{$timestamp}")),
new DateValue($dateTime->setTimestamp($timestamp)),
],
[
[
Expand Down

0 comments on commit da07b78

Please sign in to comment.