From 177d23e3aec1ab9c04ec36f5f097d5d1157c730a Mon Sep 17 00:00:00 2001 From: Mateusz Bieniek Date: Thu, 29 Nov 2018 11:01:53 +0100 Subject: [PATCH 1/3] EZP-29279: ezdate timestamps are now stored in UTC --- autoload/ezp_kernel.php | 1 + .../classes/datatypes/ezdate/ezdatetype.php | 20 ++++++--- lib/ezutils/classes/eztimestamp.php | 43 +++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 lib/ezutils/classes/eztimestamp.php diff --git a/autoload/ezp_kernel.php b/autoload/ezp_kernel.php index ed8ea90e524..b8ef66449c6 100755 --- a/autoload/ezp_kernel.php +++ b/autoload/ezp_kernel.php @@ -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', diff --git a/kernel/classes/datatypes/ezdate/ezdatetype.php b/kernel/classes/datatypes/ezdate/ezdatetype.php index c256446600e..6b80dc59426 100644 --- a/kernel/classes/datatypes/ezdate/ezdatetype.php +++ b/kernel/classes/datatypes/ezdate/ezdatetype.php @@ -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 ); @@ -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 ); @@ -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; } @@ -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; @@ -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 ); } } diff --git a/lib/ezutils/classes/eztimestamp.php b/lib/ezutils/classes/eztimestamp.php new file mode 100644 index 00000000000..006b69bb477 --- /dev/null +++ b/lib/ezutils/classes/eztimestamp.php @@ -0,0 +1,43 @@ +setTimestamp($localTimestamp); + + $utcDate = new \DateTime($localDate->format('Y-m-d'), $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'), $localTimezone); + $localTimestamp = $localDate->getTimestamp(); + + return $localTimestamp; + } +} +?> From 6b98fbe6e7b1c9e93bbf6b42a1d00205f42705bd Mon Sep 17 00:00:00 2001 From: Mateusz Bieniek Date: Thu, 29 Nov 2018 14:50:48 +0100 Subject: [PATCH 2/3] Added missing hours, minutes and seconds to Timestamp conversion and add more s p a c e s to the same class code --- lib/ezutils/classes/eztimestamp.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/ezutils/classes/eztimestamp.php b/lib/ezutils/classes/eztimestamp.php index 006b69bb477..af0f828ccab 100644 --- a/lib/ezutils/classes/eztimestamp.php +++ b/lib/ezutils/classes/eztimestamp.php @@ -13,28 +13,28 @@ class eZTimestamp /*! \return a timestamp in UTC */ - public static function getUtcTimestampFromLocalTimestamp($localTimestamp) { - $utcTimezone = new \DateTimeZone('UTC'); - $localTimezone = new \DateTimeZone(date_default_timezone_get()); + public static function getUtcTimestampFromLocalTimestamp( $localTimestamp ) { + $utcTimezone = new \DateTimeZone( 'UTC' ); + $localTimezone = new \DateTimeZone( date_default_timezone_get() ); - $localDate = new \DateTime(null, $localTimezone); - $localDate->setTimestamp($localTimestamp); + $localDate = new \DateTime( null, $localTimezone ); + $localDate->setTimestamp( $localTimestamp ); - $utcDate = new \DateTime($localDate->format('Y-m-d'), $utcTimezone); + $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()); + public static function getLocalTimestampFromUtcTimestamp( $utcTimestamp ) { + $utcTimezone = new \DateTimeZone( 'UTC' ); + $localTimezone = new \DateTimeZone( date_default_timezone_get() ); - $utcDate = new \DateTime(null, $utcTimezone); - $utcDate->setTimestamp($utcTimestamp); + $utcDate = new \DateTime( null, $utcTimezone ); + $utcDate->setTimestamp( $utcTimestamp ); - $localDate = new \DateTime($utcDate->format('Y-m-d'), $localTimezone); + $localDate = new \DateTime( $utcDate->format( 'Y-m-d H:i:s' ), $localTimezone ); $localTimestamp = $localDate->getTimestamp(); return $localTimestamp; From 0d6e1f7d402c0f754e0f932d1909c994453ac386 Mon Sep 17 00:00:00 2001 From: Mateusz Bieniek Date: Fri, 30 Nov 2018 10:03:44 +0100 Subject: [PATCH 3/3] Fixup after CR --- lib/ezutils/classes/eztimestamp.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/ezutils/classes/eztimestamp.php b/lib/ezutils/classes/eztimestamp.php index af0f828ccab..fcbecd85b23 100644 --- a/lib/ezutils/classes/eztimestamp.php +++ b/lib/ezutils/classes/eztimestamp.php @@ -19,8 +19,8 @@ public static function getUtcTimestampFromLocalTimestamp( $localTimestamp ) { $localDate = new \DateTime( null, $localTimezone ); $localDate->setTimestamp( $localTimestamp ); - $utcDate = new \DateTime( $localDate->format( 'Y-m-d H:i:s' ), $utcTimezone ); + return $utcDate->getTimestamp(); } @@ -33,11 +33,9 @@ public static function getLocalTimestampFromUtcTimestamp( $utcTimestamp ) { $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; + return $localDate->getTimestamp(); } } ?>