From 4eb42e0b05ce49171094fcf59931c3769cbcbaa4 Mon Sep 17 00:00:00 2001 From: bancer Date: Fri, 18 Jan 2019 17:07:05 +0100 Subject: [PATCH 01/10] Add unit test for timezone conversion --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index b358b1b48..3e597154e 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -1226,4 +1226,13 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($expected->format('Y-m-d H:i'), $converted); } + public function testTimezoneConversionToUTC() { + 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); + } + } From c269ae29e6074911d03473e50bcfe33319f622f1 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Sun, 20 Jan 2019 21:54:41 +0100 Subject: [PATCH 02/10] Add timezone conversion unit test --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 3e597154e..a18e38070 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -1235,4 +1235,14 @@ class CakeTimeTest extends CakeTestCase { $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); + } + } From 7911bb9052b0132150742fa5e7255b5186a3c789 Mon Sep 17 00:00:00 2001 From: bancer Date: Thu, 24 Jan 2019 15:43:11 +0100 Subject: [PATCH 03/10] 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 { From 044e8f24af58fb4418ce052b3104624b34f4ffa3 Mon Sep 17 00:00:00 2001 From: bancer Date: Fri, 25 Jan 2019 12:05:44 +0100 Subject: [PATCH 04/10] Fix timezone conversion --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 73 +++++++++++++++++---- lib/Cake/Utility/CakeTime.php | 32 ++++++--- 2 files changed, 82 insertions(+), 23 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 91fb244f5..2d3c6b6e0 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -990,7 +990,19 @@ class CakeTimeTest extends CakeTestCase { $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 + $clientDateTime->setTimezone(new DateTimeZone('UTC')); + $expected = $clientDateTime->getTimestamp() + $clientDateTime->getOffset(); // 1548903600 + $this->assertEquals($expected, $actual); + $this->_restoreSystemTimezone(); + } + + public function testFromStringUTCtoCopenhagen() { + date_default_timezone_set('UTC'); // server timezone + $clientTimeZone = new DateTimeZone('UTC'); + $clientDateTime = new DateTime('2012-01-01 10:00:00', $clientTimeZone); + $actual = CakeTime::fromString($clientDateTime, 'Europe/Copenhagen'); + $clientDateTime->setTimezone(new DateTimeZone('Europe/Copenhagen')); + $expected = $clientDateTime->getTimestamp() + $clientDateTime->getOffset(); // 1325415600 $this->assertEquals($expected, $actual); $this->_restoreSystemTimezone(); } @@ -1007,6 +1019,25 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($result, $date->format('U')); } + public function testConvertToBangkok() { + $serverTimeZoneName = 'Europe/Copenhagen'; + date_default_timezone_set($serverTimeZoneName); + + $serverTimeZone = new DateTimeZone($serverTimeZoneName); + $DateTime = new DateTime('2019-01-31 04:00:00', $serverTimeZone); + $serverTimestamp = $DateTime->getTimestamp() + $DateTime->getOffset(); // 1548907200 + + $clientTimeZoneName = 'Asia/Bangkok'; + $clientTimeZone = new DateTimeZone($clientTimeZoneName); + $DateTime->setTimezone($clientTimeZone); + $expected = $DateTime->getTimestamp() + $DateTime->getOffset(); // 1548928800 + + $actual = CakeTime::convert($serverTimestamp, $clientTimeZoneName); + $this->assertEquals($expected, $actual); + $this->_restoreSystemTimezone(); + } + + /** * test converting time specifiers using a time definition localfe file * @@ -1096,15 +1127,6 @@ 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. * @@ -1173,10 +1195,22 @@ class CakeTimeTest extends CakeTestCase { $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); + $clientDateTime->setTimezone(new DateTimeZone('UTC')); + $expected = $clientDateTime->format('Y-m-d H:i:s'); + $this->assertEquals($expected, $actual); $this->_restoreSystemTimezone(); } + public function testI18nFormatUTCtoCopenhagen() { + date_default_timezone_set('UTC'); + $clientTimeZone = new DateTimeZone('UTC'); + $clientDateTime = new DateTime('2012-01-01 10:00:00', $clientTimeZone); + $actual = CakeTime::i18nFormat($clientDateTime, '%Y-%m-%d %H:%M', false, 'Europe/Copenhagen'); + $clientDateTime->setTimezone(new DateTimeZone('Europe/Copenhagen')); + $expected = $clientDateTime->format('Y-m-d H:i'); + $this->assertEquals($expected, $actual); + } + /** * test new format() syntax which inverts first and second parameters * @@ -1245,7 +1279,7 @@ class CakeTimeTest extends CakeTestCase { * * @return void */ - public function testCorrectTimezoneConversion() { + public function testCorrectTimezoneConversionAsString() { date_default_timezone_set('UTC'); $date = '2012-01-01 10:00:00'; $converted = CakeTime::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen'); @@ -1254,13 +1288,26 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($expected->format('Y-m-d H:i'), $converted); } + public function testCorrectTimezoneConversionAsObject() { + date_default_timezone_set('UTC'); + $clientTimeZone = new DateTimeZone('UTC'); + $date = '2012-01-01 10:00:00'; + $clientDateTime = new DateTime($date, $clientTimeZone); + $converted = CakeTime::format($clientDateTime, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen'); + $clientDateTime->setTimezone(new DateTimeZone('Europe/Copenhagen')); + $expected = $clientDateTime->format('Y-m-d H:i'); + $this->assertEquals($expected, $converted); + } + 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); + $clientDateTime->setTimezone(new DateTimeZone('UTC')); + $expected = $clientDateTime->format('Y-m-d H:i:s'); + $this->assertEquals($expected, $actual); $this->_restoreSystemTimezone(); } diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 7383f34cb..14c78fce2 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -239,9 +239,9 @@ class CakeTime { /** * Converts given time (in server's time zone) to user's local time, given his/her timezone. * - * @param string $serverTime UNIX timestamp - * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object - * @return int UNIX timestamp + * @param integer $serverTime Server's timestamp. + * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object. + * @return int User's timezone timestamp. * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::convert */ public static function convert($serverTime, $timezone) { @@ -303,11 +303,11 @@ class CakeTime { } /** - * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. + * Returns a timestamp, given either a UNIX timestamp or a valid strtotime() date string. * * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object - * @return string Parsed timestamp + * @return int|false Parsed given timezone timestamp. * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::fromString */ public static function fromString($dateString, $timezone = null) { @@ -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 { @@ -1055,17 +1055,29 @@ class CakeTime { * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::i18nFormat */ public static function i18nFormat($date, $format = null, $default = false, $timezone = null) { - $date = static::fromString($date, $timezone); - if ($date === false && $default !== false) { + $timestamp = static::fromString($date, $timezone); + if ($timestamp === false && $default !== false) { return $default; } - if ($date === false) { + if ($timestamp === false) { return ''; } if (empty($format)) { $format = '%x'; } - return static::_strftime(static::convertSpecifiers($format, $date), $date); + $serverTimeZone = date_default_timezone_get(); + if ( + !empty($timezone) && + $date instanceof DateTime && + $date->getTimezone()->getName() != $serverTimeZone + ) { + date_default_timezone_set($timezone); + } + $result = static::_strftime(static::convertSpecifiers($format, $timestamp), $timestamp); + if (!empty($serverTimeZone)) { + date_default_timezone_set($serverTimeZone); + } + return $result; } /** From 009d69b3f76a3887546b9a054bfe0e50a1b3f29d Mon Sep 17 00:00:00 2001 From: bancer Date: Fri, 25 Jan 2019 12:21:34 +0100 Subject: [PATCH 05/10] Fix code style errors --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 1 - lib/Cake/Utility/CakeTime.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 2d3c6b6e0..4c89398b5 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -1037,7 +1037,6 @@ class CakeTimeTest extends CakeTestCase { $this->_restoreSystemTimezone(); } - /** * test converting time specifiers using a time definition localfe file * diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 14c78fce2..c12767529 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -239,7 +239,7 @@ class CakeTime { /** * Converts given time (in server's time zone) to user's local time, given his/her timezone. * - * @param integer $serverTime Server's timestamp. + * @param int $serverTime Server's timestamp. * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object. * @return int User's timezone timestamp. * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::convert From c855ef874b064649ec9447b60c3f979f2ac08325 Mon Sep 17 00:00:00 2001 From: bancer Date: Fri, 25 Jan 2019 12:25:26 +0100 Subject: [PATCH 06/10] Simplify i18nFormat --- lib/Cake/Utility/CakeTime.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index c12767529..1c2b1dfea 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -1074,9 +1074,7 @@ class CakeTime { date_default_timezone_set($timezone); } $result = static::_strftime(static::convertSpecifiers($format, $timestamp), $timestamp); - if (!empty($serverTimeZone)) { - date_default_timezone_set($serverTimeZone); - } + date_default_timezone_set($serverTimeZone); return $result; } From e845e8876ddd9f1042a93d12f13b4f34404e086f Mon Sep 17 00:00:00 2001 From: bancer Date: Fri, 25 Jan 2019 13:04:41 +0100 Subject: [PATCH 07/10] Add unit test --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 4c89398b5..4bc9f8a62 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -421,6 +421,18 @@ class CakeTimeTest extends CakeTestCase { $this->_restoreSystemTimezone(); } + public function testNiceTimezoneConversion() { + 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::nice($clientDateTime, 'UTC', '%Y-%m-%d %H:%M:%S'); + $clientDateTime->setTimezone(new DateTimeZone('UTC')); + $expected = $clientDateTime->format('Y-m-d H:i:s'); + $this->assertEquals($expected, $actual); + $this->_restoreSystemTimezone(); + } + /** * testNiceShort method * From ec5bac5e520dc3d50ffcb8aa688ded185bb645fd Mon Sep 17 00:00:00 2001 From: bancer Date: Fri, 25 Jan 2019 13:56:50 +0100 Subject: [PATCH 08/10] Fix timezone adjustment in CakeTime::nice and CakeTime::niceShort --- lib/Cake/Utility/CakeTime.php | 101 ++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 1c2b1dfea..6b79511b3 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -354,22 +354,22 @@ class CakeTime { * See http://php.net/manual/en/function.strftime.php for information on formatting * using locale strings. * - * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object + * @param int|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object * @param string $format The format to use. If null, `CakeTime::$niceFormat` is used * @return string Formatted date string * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::nice */ - public static function nice($dateString = null, $timezone = null, $format = null) { - if (!$dateString) { - $dateString = time(); + public static function nice($date = null, $timezone = null, $format = null) { + if (!$date) { + $date = time(); } - $date = static::fromString($dateString, $timezone); - + $timestamp = static::fromString($date, $timezone); if (!$format) { $format = static::$niceFormat; } - return static::_strftime(static::convertSpecifiers($format, $date), $date); + $convertedFormat = static::convertSpecifiers($format, $timestamp); + return static::__strftime($convertedFormat, $timestamp, $date, $timezone); } /** @@ -382,28 +382,31 @@ class CakeTime { * If $dateString's year is the current year, the returned string does not * include mention of the year. * - * @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object + * @param int|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object * @return string Described, relative date string * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::niceShort */ - public static function niceShort($dateString = null, $timezone = null) { - if (!$dateString) { - $dateString = time(); + public static function niceShort($date = null, $timezone = null) { + if (!$date) { + $date = time(); } - $date = static::fromString($dateString, $timezone); + $timestamp = static::fromString($date, $timezone); - if (static::isToday($dateString, $timezone)) { - return __d('cake', 'Today, %s', static::_strftime("%H:%M", $date)); + if (static::isToday($date, $timezone)) { + $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone); + return __d('cake', 'Today, %s', $formattedDate); } - if (static::wasYesterday($dateString, $timezone)) { - return __d('cake', 'Yesterday, %s', static::_strftime("%H:%M", $date)); + if (static::wasYesterday($date, $timezone)) { + $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone); + return __d('cake', 'Yesterday, %s', $formattedDate); } - if (static::isTomorrow($dateString, $timezone)) { - return __d('cake', 'Tomorrow, %s', static::_strftime("%H:%M", $date)); + if (static::isTomorrow($date, $timezone)) { + $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone); + return __d('cake', 'Tomorrow, %s', $formattedDate); } - $d = static::_strftime("%w", $date); + $d = static::__strftime("%w", $timestamp, $date, $timezone); $day = array( __d('cake', 'Sunday'), __d('cake', 'Monday'), @@ -413,18 +416,21 @@ class CakeTime { __d('cake', 'Friday'), __d('cake', 'Saturday') ); - if (static::wasWithinLast('7 days', $dateString, $timezone)) { - return sprintf('%s %s', $day[$d], static::_strftime(static::$niceShortFormat, $date)); + if (static::wasWithinLast('7 days', $date, $timezone)) { + $formattedDate = static::__strftime(static::$niceShortFormat, $timestamp, $date, $timezone); + return sprintf('%s %s', $day[$d], $formattedDate); } - if (static::isWithinNext('7 days', $dateString, $timezone)) { - return __d('cake', 'On %s %s', $day[$d], static::_strftime(static::$niceShortFormat, $date)); + if (static::isWithinNext('7 days', $date, $timezone)) { + $formattedDate = static::__strftime(static::$niceShortFormat, $timestamp, $date, $timezone); + return __d('cake', 'On %s %s', $day[$d], $formattedDate); } $y = ''; - if (!static::isThisYear($date)) { + if (!static::isThisYear($timestamp)) { $y = ' %Y'; } - return static::_strftime(static::convertSpecifiers("%b %eS{$y}, %H:%M", $date), $date); + $format = static::convertSpecifiers("%b %eS{$y}, %H:%M", $timestamp); + return static::__strftime($format, $timestamp, $date, $timezone); } /** @@ -1065,17 +1071,8 @@ class CakeTime { if (empty($format)) { $format = '%x'; } - $serverTimeZone = date_default_timezone_get(); - if ( - !empty($timezone) && - $date instanceof DateTime && - $date->getTimezone()->getName() != $serverTimeZone - ) { - date_default_timezone_set($timezone); - } - $result = static::_strftime(static::convertSpecifiers($format, $timestamp), $timestamp); - date_default_timezone_set($serverTimeZone); - return $result; + $convertedFormat = static::convertSpecifiers($format, $timestamp); + return static::__strftime($convertedFormat, $timestamp, $date, $timezone); } /** @@ -1166,13 +1163,12 @@ class CakeTime { * Handles utf8_encoding the result of strftime when necessary. * * @param string $format Format string. - * @param int $date Timestamp to format. + * @param int $timestamp Timestamp to format. * @return string formatted string with correct encoding. */ - protected static function _strftime($format, $date) { - $format = strftime($format, $date); + protected static function _strftime($format, $timestamp) { + $format = strftime($format, $timestamp); $encoding = Configure::read('App.encoding'); - if (!empty($encoding) && $encoding === 'UTF-8') { if (function_exists('mb_check_encoding')) { $valid = mb_check_encoding($format, $encoding); @@ -1186,4 +1182,29 @@ class CakeTime { return $format; } +/** + * Multibyte wrapper for strftime. + * + * Adjusts the timezone when necessary before formatting the time. + * + * @param string $format Format string. + * @param int $timestamp Timestamp to format. + * @param int|string|DateTime $date Timestamp, strtotime() valid string or DateTime object. + * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object. + * @return string Formatted date string with correct encoding. + */ + private static function __strftime($format, $timestamp, $date, $timezone) { + $serverTimeZone = date_default_timezone_get(); + if ( + !empty($timezone) && + $date instanceof DateTime && + $date->getTimezone()->getName() != $serverTimeZone + ) { + date_default_timezone_set($timezone); + } + $result = static::_strftime($format, $timestamp); + date_default_timezone_set($serverTimeZone); + return $result; + } + } From e94183c6d8eef8c2657b3fe26dec5c9f259ab32e Mon Sep 17 00:00:00 2001 From: bancer Date: Tue, 29 Jan 2019 09:16:48 +0100 Subject: [PATCH 09/10] Give a better name to a new function --- lib/Cake/Utility/CakeTime.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 6b79511b3..f1c7ee158 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -369,7 +369,7 @@ class CakeTime { $format = static::$niceFormat; } $convertedFormat = static::convertSpecifiers($format, $timestamp); - return static::__strftime($convertedFormat, $timestamp, $date, $timezone); + return static::__strftimeWithTimezone($convertedFormat, $timestamp, $date, $timezone); } /** @@ -394,19 +394,19 @@ class CakeTime { $timestamp = static::fromString($date, $timezone); if (static::isToday($date, $timezone)) { - $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone); + $formattedDate = static::__strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); return __d('cake', 'Today, %s', $formattedDate); } if (static::wasYesterday($date, $timezone)) { - $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone); + $formattedDate = static::__strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); return __d('cake', 'Yesterday, %s', $formattedDate); } if (static::isTomorrow($date, $timezone)) { - $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone); + $formattedDate = static::__strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); return __d('cake', 'Tomorrow, %s', $formattedDate); } - $d = static::__strftime("%w", $timestamp, $date, $timezone); + $d = static::__strftimeWithTimezone("%w", $timestamp, $date, $timezone); $day = array( __d('cake', 'Sunday'), __d('cake', 'Monday'), @@ -417,11 +417,11 @@ class CakeTime { __d('cake', 'Saturday') ); if (static::wasWithinLast('7 days', $date, $timezone)) { - $formattedDate = static::__strftime(static::$niceShortFormat, $timestamp, $date, $timezone); + $formattedDate = static::__strftimeWithTimezone(static::$niceShortFormat, $timestamp, $date, $timezone); return sprintf('%s %s', $day[$d], $formattedDate); } if (static::isWithinNext('7 days', $date, $timezone)) { - $formattedDate = static::__strftime(static::$niceShortFormat, $timestamp, $date, $timezone); + $formattedDate = static::__strftimeWithTimezone(static::$niceShortFormat, $timestamp, $date, $timezone); return __d('cake', 'On %s %s', $day[$d], $formattedDate); } @@ -430,7 +430,7 @@ class CakeTime { $y = ' %Y'; } $format = static::convertSpecifiers("%b %eS{$y}, %H:%M", $timestamp); - return static::__strftime($format, $timestamp, $date, $timezone); + return static::__strftimeWithTimezone($format, $timestamp, $date, $timezone); } /** @@ -1072,7 +1072,7 @@ class CakeTime { $format = '%x'; } $convertedFormat = static::convertSpecifiers($format, $timestamp); - return static::__strftime($convertedFormat, $timestamp, $date, $timezone); + return static::__strftimeWithTimezone($convertedFormat, $timestamp, $date, $timezone); } /** @@ -1193,7 +1193,7 @@ class CakeTime { * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object. * @return string Formatted date string with correct encoding. */ - private static function __strftime($format, $timestamp, $date, $timezone) { + private static function __strftimeWithTimezone($format, $timestamp, $date, $timezone) { $serverTimeZone = date_default_timezone_get(); if ( !empty($timezone) && From bb20b4f419a94ad913f6f565acbe10c58a7174a8 Mon Sep 17 00:00:00 2001 From: bancer Date: Thu, 31 Jan 2019 09:49:41 +0100 Subject: [PATCH 10/10] Change private method to protected --- lib/Cake/Utility/CakeTime.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index f1c7ee158..63911bfde 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -369,7 +369,7 @@ class CakeTime { $format = static::$niceFormat; } $convertedFormat = static::convertSpecifiers($format, $timestamp); - return static::__strftimeWithTimezone($convertedFormat, $timestamp, $date, $timezone); + return static::_strftimeWithTimezone($convertedFormat, $timestamp, $date, $timezone); } /** @@ -394,19 +394,19 @@ class CakeTime { $timestamp = static::fromString($date, $timezone); if (static::isToday($date, $timezone)) { - $formattedDate = static::__strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); + $formattedDate = static::_strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); return __d('cake', 'Today, %s', $formattedDate); } if (static::wasYesterday($date, $timezone)) { - $formattedDate = static::__strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); + $formattedDate = static::_strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); return __d('cake', 'Yesterday, %s', $formattedDate); } if (static::isTomorrow($date, $timezone)) { - $formattedDate = static::__strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); + $formattedDate = static::_strftimeWithTimezone("%H:%M", $timestamp, $date, $timezone); return __d('cake', 'Tomorrow, %s', $formattedDate); } - $d = static::__strftimeWithTimezone("%w", $timestamp, $date, $timezone); + $d = static::_strftimeWithTimezone("%w", $timestamp, $date, $timezone); $day = array( __d('cake', 'Sunday'), __d('cake', 'Monday'), @@ -417,11 +417,11 @@ class CakeTime { __d('cake', 'Saturday') ); if (static::wasWithinLast('7 days', $date, $timezone)) { - $formattedDate = static::__strftimeWithTimezone(static::$niceShortFormat, $timestamp, $date, $timezone); + $formattedDate = static::_strftimeWithTimezone(static::$niceShortFormat, $timestamp, $date, $timezone); return sprintf('%s %s', $day[$d], $formattedDate); } if (static::isWithinNext('7 days', $date, $timezone)) { - $formattedDate = static::__strftimeWithTimezone(static::$niceShortFormat, $timestamp, $date, $timezone); + $formattedDate = static::_strftimeWithTimezone(static::$niceShortFormat, $timestamp, $date, $timezone); return __d('cake', 'On %s %s', $day[$d], $formattedDate); } @@ -430,7 +430,7 @@ class CakeTime { $y = ' %Y'; } $format = static::convertSpecifiers("%b %eS{$y}, %H:%M", $timestamp); - return static::__strftimeWithTimezone($format, $timestamp, $date, $timezone); + return static::_strftimeWithTimezone($format, $timestamp, $date, $timezone); } /** @@ -1072,7 +1072,7 @@ class CakeTime { $format = '%x'; } $convertedFormat = static::convertSpecifiers($format, $timestamp); - return static::__strftimeWithTimezone($convertedFormat, $timestamp, $date, $timezone); + return static::_strftimeWithTimezone($convertedFormat, $timestamp, $date, $timezone); } /** @@ -1193,7 +1193,7 @@ class CakeTime { * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object. * @return string Formatted date string with correct encoding. */ - private static function __strftimeWithTimezone($format, $timestamp, $date, $timezone) { + protected static function _strftimeWithTimezone($format, $timestamp, $date, $timezone) { $serverTimeZone = date_default_timezone_get(); if ( !empty($timezone) &&