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

EZP-29279: Values of Date Field Type should be handled in UTC only #1401

Merged
merged 3 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions autoload/ezp_kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
'eZTextTool' => 'lib/ezutils/classes/eztexttool.php',
'eZTextType' => 'kernel/classes/datatypes/eztext/eztexttype.php',
'eZTime' => 'lib/ezlocale/classes/eztime.php',
'eZTimestamp' => 'lib/ezutils/classes/eztimestamp.php',
'eZTimeType' => 'kernel/classes/datatypes/eztime/eztimetype.php',
'eZTipafriendCounter' => 'kernel/classes/eztipafriendcounter.php',
'eZTipafriendRequest' => 'kernel/classes/eztipafriendrequest.php',
Expand Down
20 changes: 15 additions & 5 deletions kernel/classes/datatypes/ezdate/ezdatetype.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
$date = new eZDate();
$date->setMDY( $month, $day, $year );
$stamp = $date->timeStamp();
$stamp = eZTimestamp::getUtcTimestampFromLocalTimestamp( $date->timeStamp() );
}

$contentObjectAttribute->setAttribute( 'data_int', $stamp );
Expand Down Expand Up @@ -175,7 +175,7 @@ function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $
{
$date = new eZDate();
$date->setMDY( $month, $day, $year );
$stamp = $date->timeStamp();
$stamp = eZTimestamp::getUtcTimestampFromLocalTimestamp( $date->timeStamp() );
}

$collectionAttribute->setAttribute( 'data_int', $stamp );
Expand All @@ -191,7 +191,9 @@ function objectAttributeContent( $contentObjectAttribute )
{
$date = new eZDate( );
$stamp = $contentObjectAttribute->attribute( 'data_int' );
$date->setTimeStamp( $stamp );
$date->setTimeStamp(
eZTimestamp::getLocalTimestampFromUtcTimestamp( $stamp )
);
return $date;
}

Expand Down Expand Up @@ -344,7 +346,13 @@ function serializeContentObjectAttribute( $package, $objectAttribute )
{
$dom = $node->ownerDocument;
$dateNode = $dom->createElement( 'date' );
$dateNode->appendChild( $dom->createTextNode( eZDateUtils::rfc1123Date( $stamp ) ) );
$dateNode->appendChild(
$dom->createTextNode(
eZDateUtils::rfc1123Date(
eZTimestamp::getLocalTimestampFromUtcTimestamp( $stamp )
)
)
);
$node->appendChild( $dateNode );
}
return $node;
Expand All @@ -355,7 +363,9 @@ function unserializeContentObjectAttribute( $package, $objectAttribute, $attribu
$dateNode = $attributeNode->getElementsByTagName( 'date' )->item( 0 );
if ( is_object( $dateNode ) )
{
$timestamp = eZDateUtils::textToDate( $dateNode->textContent );
$timestamp = eZTimestamp::getUtcTimestampFromLocalTimestamp(
eZDateUtils::textToDate( $dateNode->textContent )
);
$objectAttribute->setAttribute( 'data_int', $timestamp );
}
}
Expand Down
43 changes: 43 additions & 0 deletions lib/ezutils/classes/eztimestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* File containing the eZTimestamp class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package lib
*/

class eZTimestamp
{
/*!
\return a timestamp in UTC
*/
public static function getUtcTimestampFromLocalTimestamp( $localTimestamp ) {
$utcTimezone = new \DateTimeZone( 'UTC' );
$localTimezone = new \DateTimeZone( date_default_timezone_get() );

$localDate = new \DateTime( null, $localTimezone );
$localDate->setTimestamp( $localTimestamp );

$utcDate = new \DateTime( $localDate->format( 'Y-m-d H:i:s' ), $utcTimezone );
return $utcDate->getTimestamp();
}

/*!
\return a timestamp in timezone defined in php.ini
*/
public static function getLocalTimestampFromUtcTimestamp( $utcTimestamp ) {
$utcTimezone = new \DateTimeZone( 'UTC' );
$localTimezone = new \DateTimeZone( date_default_timezone_get() );

$utcDate = new \DateTime( null, $utcTimezone );
$utcDate->setTimestamp( $utcTimestamp );

$localDate = new \DateTime( $utcDate->format( 'Y-m-d H:i:s' ), $localTimezone );
$localTimestamp = $localDate->getTimestamp();

return $localTimestamp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should do it like:

$localDate = new \DateTime( $utcDate->format( 'Y-m-d H:i:s' ), $localTimezone );

return $localDate->getTimestamp();

or even:

return new \DateTime( $utcDate->format( 'Y-m-d H:i:s' ), $localTimezone )->getTimestamp();

but I prefer the first way, it's more readable.

The same applies for https://github.com/ezsystems/ezpublish-legacy/pull/1401/files/6b98fbe6e7b1c9e93bbf6b42a1d00205f42705bd#diff-9ae7112c632bad6c06997d57aefd6f82R24.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I prefer the first approach as well. Fixed.

}
}
?>