Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing issue where nice and niceShort did not use the timezone if no …

…dateString is passed
  • Loading branch information...
commit 0d9506fa2e4764812d99f1d03e4937177efd1b65 1 parent 6ede36f
Jelle Henkens jellehenkens authored
40 lib/Cake/Test/Case/Utility/CakeTimeTest.php
View
@@ -26,12 +26,20 @@
class CakeTimeTest extends CakeTestCase {
/**
+ * Default system timezone identifier
+ *
+ * @var string
+ */
+ protected $_systemTimezoneIdentifier = null;
+
+/**
* setUp method
*
* @return void
*/
public function setUp() {
$this->Time = new CakeTime();
+ $this->_systemTimezoneIdentifier = date_default_timezone_get();
}
/**
@@ -41,9 +49,19 @@ public function setUp() {
*/
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
*
* @return void
@@ -323,6 +341,13 @@ public function testNice() {
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 @@ public function testNiceShort() {
$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 @@ public function testToUnix() {
* @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 @@ public function testToServer() {
$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
}
- date_default_timezone_set($tzBackup);
+ $this->_restoreSystemTimezone();
}
/**
13 lib/Cake/Utility/CakeTime.php
View
@@ -344,11 +344,11 @@ public static function fromString($dateString, $timezone = null) {
* @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 @@ public static function nice($dateString = null, $timezone = null, $format = null
* @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';
Please sign in to comment.
Something went wrong with that request. Please try again.