From 0d9506fa2e4764812d99f1d03e4937177efd1b65 Mon Sep 17 00:00:00 2001 From: Jelle Henkens Date: Sun, 13 May 2012 11:22:29 +0100 Subject: [PATCH] Fixing issue where nice and niceShort did not use the timezone if no dateString is passed --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 40 +++++++++++++++++---- lib/Cake/Utility/CakeTime.php | 13 ++++--- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 266b49ff1..5c546f43d 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -25,6 +25,13 @@ App::uses('CakeTime', 'Utility'); */ class CakeTimeTest extends CakeTestCase { +/** + * Default system timezone identifier + * + * @var string + */ + protected $_systemTimezoneIdentifier = null; + /** * setUp method * @@ -32,6 +39,7 @@ class CakeTimeTest extends CakeTestCase { */ public function setUp() { $this->Time = new CakeTime(); + $this->_systemTimezoneIdentifier = date_default_timezone_get(); } /** @@ -41,8 +49,18 @@ class CakeTimeTest extends CakeTestCase { */ public function tearDown() { unset($this->Time); + $this->_restoreSystemTimezone(); } +/** + * Restored the original system timezone + * + * @param string $timezoneIdentifier Timezone string + * @return void + */ + protected function _restoreSystemTimezone() { + date_default_timezone_set($this->_systemTimezoneIdentifier); + } /** * testToQuarter method * @@ -323,6 +341,13 @@ class CakeTimeTest extends CakeTestCase { CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S'; $this->assertEquals(date('Y-d-m H:i:s', $time), $this->Time->nice($time)); $this->assertEquals('%Y-%d-%m %H:%M:%S', $this->Time->niceFormat); + + date_default_timezone_set('UTC'); + $result = $this->Time->nice(null, 'America/New_York'); + $expected = $this->Time->nice(time(), 'America/New_York'); + $this->assertEqual($expected, $result); + + $this->_restoreSystemTimezone(); } /** @@ -340,12 +365,16 @@ class CakeTimeTest extends CakeTestCase { $time = time() + DAY; $this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time)); - $oldTimezone = date_default_timezone_get(); date_default_timezone_set('Europe/London'); - $result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels')); $this->assertEqual('Jan 15th 2005, 11:00', $result); - date_default_timezone_set($oldTimezone); + + date_default_timezone_set('UTC'); + $result = $this->Time->niceShort(null, 'America/New_York'); + $expected = $this->Time->niceShort(time(), 'America/New_York'); + $this->assertEqual($expected, $result); + + $this->_restoreSystemTimezone(); } /** @@ -393,9 +422,8 @@ class CakeTimeTest extends CakeTestCase { * @return void */ public function testToServer() { - $tzBackup = date_default_timezone_get(); - date_default_timezone_set('UTC'); + $serverTime = new DateTime('now'); $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu'); @@ -405,7 +433,7 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime)); } - date_default_timezone_set($tzBackup); + $this->_restoreSystemTimezone(); } /** diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index e6109e602..8f00a367b 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -344,11 +344,11 @@ class CakeTime { * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting */ public static function nice($dateString = null, $timezone = null, $format = null) { - if ($dateString != null) { - $date = self::fromString($dateString, $timezone); - } else { - $date = time(); + if (!$dateString) { + $dateString = time(); } + $date = self::fromString($dateString, $timezone); + if (!$format) { $format = self::$niceFormat; } @@ -372,7 +372,10 @@ class CakeTime { * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting */ public static function niceShort($dateString = null, $timezone = null) { - $date = $dateString ? self::fromString($dateString, $timezone) : time(); + if (!$dateString) { + $dateString = time(); + } + $date = self::fromString($dateString, $timezone); $y = self::isThisYear($date) ? '' : ' %Y';