Fixing issue where nice and niceShort did not use the timezone if no dateString is passed

This commit is contained in:
Jelle Henkens 2012-05-13 11:22:29 +01:00
parent 6ede36f8e0
commit 0d9506fa2e
2 changed files with 42 additions and 11 deletions

View file

@ -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();
}
/**

View file

@ -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';