Skip to content

Commit

Permalink
EZP-29279: Values of Date Field Type should be handled in UTC only (#…
Browse files Browse the repository at this point in the history
…1401)

* EZP-29279: ezdate timestamps are now stored in UTC

* Added missing hours, minutes and seconds to Timestamp conversion and add more s p a c e s to the same class code

* Fixup after CR
  • Loading branch information
mateuszbieniek authored and andrerom committed Mar 11, 2019
1 parent 215ce2c commit 3ce2994
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
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
41 changes: 41 additions & 0 deletions lib/ezutils/classes/eztimestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 );

return $localDate->getTimestamp();
}
}
?>

0 comments on commit 3ce2994

Please sign in to comment.