mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 17:16:18 +00:00
Merge pull request #651 from jellehenkens/2.2-cake-time-fixes
CakeTime::nice() and CakeTime::niceShort() do not respect the timezone if no date is passed
This commit is contained in:
commit
07c901dfd0
2 changed files with 42 additions and 11 deletions
|
@ -25,6 +25,13 @@ App::uses('CakeTime', 'Utility');
|
||||||
*/
|
*/
|
||||||
class CakeTimeTest extends CakeTestCase {
|
class CakeTimeTest extends CakeTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default system timezone identifier
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $_systemTimezoneIdentifier = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* setUp method
|
* setUp method
|
||||||
*
|
*
|
||||||
|
@ -32,6 +39,7 @@ class CakeTimeTest extends CakeTestCase {
|
||||||
*/
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
$this->Time = new CakeTime();
|
$this->Time = new CakeTime();
|
||||||
|
$this->_systemTimezoneIdentifier = date_default_timezone_get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,8 +49,18 @@ class CakeTimeTest extends CakeTestCase {
|
||||||
*/
|
*/
|
||||||
public function tearDown() {
|
public function tearDown() {
|
||||||
unset($this->Time);
|
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
|
* testToQuarter method
|
||||||
*
|
*
|
||||||
|
@ -323,6 +341,13 @@ class CakeTimeTest extends CakeTestCase {
|
||||||
CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S';
|
CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S';
|
||||||
$this->assertEquals(date('Y-d-m H:i:s', $time), $this->Time->nice($time));
|
$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);
|
$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;
|
$time = time() + DAY;
|
||||||
$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
|
$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
|
||||||
|
|
||||||
$oldTimezone = date_default_timezone_get();
|
|
||||||
date_default_timezone_set('Europe/London');
|
date_default_timezone_set('Europe/London');
|
||||||
|
|
||||||
$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
|
$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
|
||||||
$this->assertEqual('Jan 15th 2005, 11:00', $result);
|
$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
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testToServer() {
|
public function testToServer() {
|
||||||
$tzBackup = date_default_timezone_get();
|
|
||||||
|
|
||||||
date_default_timezone_set('UTC');
|
date_default_timezone_set('UTC');
|
||||||
|
|
||||||
$serverTime = new DateTime('now');
|
$serverTime = new DateTime('now');
|
||||||
|
|
||||||
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
|
$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));
|
$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
|
||||||
}
|
}
|
||||||
|
|
||||||
date_default_timezone_set($tzBackup);
|
$this->_restoreSystemTimezone();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -344,11 +344,11 @@ class CakeTime {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
||||||
*/
|
*/
|
||||||
public static function nice($dateString = null, $timezone = null, $format = null) {
|
public static function nice($dateString = null, $timezone = null, $format = null) {
|
||||||
if ($dateString != null) {
|
if (!$dateString) {
|
||||||
$date = self::fromString($dateString, $timezone);
|
$dateString = time();
|
||||||
} else {
|
|
||||||
$date = time();
|
|
||||||
}
|
}
|
||||||
|
$date = self::fromString($dateString, $timezone);
|
||||||
|
|
||||||
if (!$format) {
|
if (!$format) {
|
||||||
$format = self::$niceFormat;
|
$format = self::$niceFormat;
|
||||||
}
|
}
|
||||||
|
@ -372,7 +372,10 @@ class CakeTime {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
||||||
*/
|
*/
|
||||||
public static function niceShort($dateString = null, $timezone = null) {
|
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';
|
$y = self::isThisYear($date) ? '' : ' %Y';
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue