ソースを参照

fix date converting issues with timezones

euromark 12 年 前
コミット
1e36c61406
2 ファイル変更30 行追加3 行削除
  1. 6 1
      Lib/Utility/TimeLib.php
  2. 24 2
      Test/Case/Lib/Utility/TimeLibTest.php

+ 6 - 1
Lib/Utility/TimeLib.php

@@ -450,6 +450,9 @@ class TimeLib extends CakeTime {
 		$defaults = array('default' => '-----', 'timezone' => null);
 		$options = array_merge($defaults, $options);
 
+		if ($options['timezone'] === null && strlen($dateString) === 10) {
+			$options['timezone'] = date_default_timezone_get();
+		}
 		if ($dateString === null) {
 			$dateString = time();
 		}
@@ -481,10 +484,12 @@ class TimeLib extends CakeTime {
 		$defaults = array('default' => '-----', 'timezone' => null);
 		$options = array_merge($defaults, $options);
 
+		if ($options['timezone'] === null && strlen($dateString) === 10) {
+			$options['timezone'] = date_default_timezone_get();
+		}
 		if ($dateString === null) {
 			$dateString = time();
 		}
-		$date = null;
 		$date = self::fromString($dateString, $options['timezone']);
 
 		if ($date === null || $date === false || $date <= 0) {

+ 24 - 2
Test/Case/Lib/Utility/TimeLibTest.php

@@ -78,7 +78,7 @@ class TimeLibTest extends MyCakeTestCase {
 
 	public function testNiceDate() {
 		$res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
-		//$this->assertTrue(!empty($res));
+		$this->assertTrue(!empty($res));
 
 		$values = array(
 			array('2009-12-01 00:00:00', FORMAT_NICE_YMD, '01.12.2009'),
@@ -87,10 +87,32 @@ class TimeLibTest extends MyCakeTestCase {
 		foreach ($values as $v) {
 			$ret = TimeLib::niceDate($v[0], $v[1]);
 			//$this->debug($ret);
-			$this->assertEquals($ret, $v[2]);
+			$this->assertEquals($v[2], $ret);
 		}
 	}
 
+	/**
+	 * Test that input as date only (YYYY-MM-DD) does not suddendly return a
+	 * different date on output due to timezone differences.
+	 * Here the timezone should not apply since we only input date and only output
+	 * date (time itself is irrelevant).
+	 *
+	 * @return void
+	 */
+	public function testDateWithTimezone() {
+		$res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
+		$this->assertTrue(!empty($res));
+		Configure::write('Config.timezone', 'America/Anchorage');
+
+		$ret = TimeLib::niceDate('2009-12-01');
+		//debug($ret);
+		$this->assertEquals('01.12.2009', $ret);
+
+		$ret = TimeLib::localDate('2009-12-01');
+		//debug($ret);
+		$this->assertEquals('01.12.2009', $ret);
+	}
+
 	public function testLocalDate() {
 		$this->skipIf(php_sapi_name() === 'cli', 'for now');
 		$res = setlocale(LC_TIME, array('de_DE.UTF-8', 'deu_deu'));