Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-300: Added timezone offset to timestamp #181

Merged
merged 9 commits into from
May 12, 2021
46 changes: 33 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,17 @@ 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');
$dateTime->setTime(0, 0, 0);

return $dateTime->getTimestamp();
}

protected function getSearchTargetValueOne()
Expand All @@ -387,4 +399,12 @@ protected function getSearchTargetValueTwo()

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

protected function getValueOneDate(): DateTime
{
$dateTime = new DateTime('1970-01-02');
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
$dateTime->setTime(0, 0, 0);

return $dateTime;
}
}
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