From 7911bb9052b0132150742fa5e7255b5186a3c789 Mon Sep 17 00:00:00 2001 From: bancer Date: Thu, 24 Jan 2019 15:43:11 +0100 Subject: [PATCH] Remove unnecessary timezone offset manipulation --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 49 ++++++++++++++------- lib/Cake/Utility/CakeTime.php | 2 +- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index a18e38070..91fb244f5 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -966,23 +966,32 @@ class CakeTimeTest extends CakeTestCase { */ public function testFromStringWithDateTime() { date_default_timezone_set('UTC'); - $date = new DateTime('+1 hour', new DateTimeZone('America/New_York')); $result = $this->Time->fromString($date, 'UTC'); $date->setTimezone(new DateTimeZone('UTC')); $expected = $date->format('U') + $date->getOffset(); - $this->assertWithinMargin($expected, $result, 1); + $this->_restoreSystemTimezone(); + } + public function testFromStringWithDateTimeAsia() { date_default_timezone_set('Australia/Melbourne'); - $date = new DateTime('+1 hour', new DateTimeZone('America/New_York')); $result = $this->Time->fromString($date, 'Asia/Kuwait'); - $date->setTimezone(new DateTimeZone('Asia/Kuwait')); $expected = $date->format('U') + $date->getOffset(); $this->assertWithinMargin($expected, $result, 1); + $this->_restoreSystemTimezone(); + } + public function testFromStringTimezoneConversionToUTC() { + date_default_timezone_set('Europe/Copenhagen'); // server timezone + $clientTimeZone = new DateTimeZone('Asia/Bangkok'); + $clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone); + // Convert to UTC. + $actual = CakeTime::fromString($clientDateTime, 'UTC'); + $expected = 1548900000; // '2019-01-31 03:00:00' timestamp + $this->assertEquals($expected, $actual); $this->_restoreSystemTimezone(); } @@ -1087,6 +1096,15 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($expected, $result); } + public function testConvertTimezoneConversionToUTC() { + date_default_timezone_set('Europe/Copenhagen'); // server timezone + $serverTime = 1548903600; // '2019-01-31 04:00:00' timestamp + $actual = CakeTime::convert($serverTime, 'UTC'); + $expected = 1548900000; // '2019-01-31 03:00:00' timestamp + $this->assertEquals($expected, $actual); + $this->_restoreSystemTimezone(); + } + /** * test convert %e on Windows. * @@ -1149,6 +1167,16 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($expected, $result); } + public function testI18nFormatTimezoneConversionToUTC() { + date_default_timezone_set('Europe/Copenhagen'); // server timezone + $clientTimeZone = new DateTimeZone('Asia/Bangkok'); + $clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone); + // Convert to UTC. + $actual = CakeTime::i18nFormat($clientDateTime, '%Y-%m-%d %H:%M:%S', false, 'UTC'); + $this->assertEquals('2019-01-31 03:00:00', $actual); + $this->_restoreSystemTimezone(); + } + /** * test new format() syntax which inverts first and second parameters * @@ -1226,23 +1254,14 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($expected->format('Y-m-d H:i'), $converted); } - public function testTimezoneConversionToUTC() { + public function testFormatTimezoneConversionToUTC() { date_default_timezone_set('Europe/Copenhagen'); // server timezone $clientTimeZone = new DateTimeZone('Asia/Bangkok'); $clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone); // Convert to UTC. $actual = CakeTime::format($clientDateTime, '%Y-%m-%d %H:%M:%S', false, 'UTC'); $this->assertEquals('2019-01-31 03:00:00', $actual); - } - - public function testTimezoneConversionToUTCPlainPHP() { - date_default_timezone_set('Europe/Copenhagen'); // server timezone - $clientTimeZone = new DateTimeZone('Asia/Bangkok'); - $clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone); - // Convert to UTC. - $clientDateTime->setTimezone(new DateTimeZone('UTC')); - $actual = $clientDateTime->format('Y-m-d H:i:s'); - $this->assertEquals('2019-01-31 03:00:00', $actual); + $this->_restoreSystemTimezone(); } } diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 2a1de55f3..7383f34cb 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -327,7 +327,7 @@ class CakeTime { ) { $clone = clone $dateString; $clone->setTimezone(new DateTimeZone(date_default_timezone_get())); - $date = (int)$clone->format('U') + $clone->getOffset(); + $date = (int)$clone->format('U')/* + $clone->getOffset()*/; } elseif ($dateString instanceof DateTime) { $date = (int)$dateString->format('U'); } else {