Commit 6a8f4b0e by Carsten Brandt

fixed date formatter to display date-only values correctly

fixes #5448
parent c2d814cc
...@@ -8,6 +8,7 @@ Yii Framework 2 Change Log ...@@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #4823: `yii message` accuracy and error handling were improved (samdark) - Bug #4823: `yii message` accuracy and error handling were improved (samdark)
- Bug #4889: Application was getting into redirect loop when user wasn't allowed accessing login page. Now shows 403 (samdark) - Bug #4889: Application was getting into redirect loop when user wasn't allowed accessing login page. Now shows 403 (samdark)
- Bug #5402: Debugger was not loading when there were closures in asset classes (samdark) - Bug #5402: Debugger was not loading when there were closures in asset classes (samdark)
- Bug #5448: Date formatter was doing timezone conversion on date only values resulting in different date displayed than provided (cebe)
- Bug #5452: Errors occurring after the response is sent are not displayed (qiangxue) - Bug #5452: Errors occurring after the response is sent are not displayed (qiangxue)
- Bug #5521: Fixed `yii\console\controllers\AssetController` breaks CSS URLs, which start from '/' (klimov-paul) - Bug #5521: Fixed `yii\console\controllers\AssetController` breaks CSS URLs, which start from '/' (klimov-paul)
- Bug #5570: `yii\bootstrap\Tabs` would throw an exception if `content` is not set for one of its `items` (RomeroMsk) - Bug #5570: `yii\bootstrap\Tabs` would throw an exception if `content` is not set for one of its `items` (RomeroMsk)
......
...@@ -34,9 +34,14 @@ class DateTimeExtended extends \DateTime ...@@ -34,9 +34,14 @@ class DateTimeExtended extends \DateTime
*/ */
public function __construct ($time = 'now', \DateTimeZone $timezone = null) public function __construct ($time = 'now', \DateTimeZone $timezone = null)
{ {
// TODO get date info
$this->_isDateOnly = false;
parent::__construct($time, $timezone); parent::__construct($time, $timezone);
$info = date_parse($time);
if ($info['hour'] === false && $info['minute'] === false && $info['second'] === false) {
$this->_isDateOnly = true;
} else {
$this->_isDateOnly = false;
}
} }
/** /**
...@@ -47,12 +52,23 @@ class DateTimeExtended extends \DateTime ...@@ -47,12 +52,23 @@ class DateTimeExtended extends \DateTime
* @return DateTimeExtended * @return DateTimeExtended
* @link http://php.net/manual/en/datetime.createfromformat.php * @link http://php.net/manual/en/datetime.createfromformat.php
*/ */
public static function createFromFormat ($format, $time, \DateTimeZone $timezone=null) public static function createFromFormat ($format, $time, $timezone = null)
{ {
$dateTime = parent::createFromFormat($format, $time, $timezone); if (($originalDateTime = parent::createFromFormat($format, $time, $timezone)) === false) {
// TODO turn object into instance of $this return false;
// TODO get date info }
// $dateTime->_isDateOnly = false; $info = date_parse_from_format($format, $time);
/** @var $dateTime \DateTime */
$dateTime = new static;
if ($info['hour'] === false && $info['minute'] === false && $info['second'] === false) {
$dateTime->_isDateOnly = true;
} else {
$dateTime->_isDateOnly = false;
}
$dateTime->setTimezone($originalDateTime->getTimezone());
$dateTime->setTimestamp($originalDateTime->getTimestamp());
return $dateTime; return $dateTime;
} }
...@@ -60,4 +76,22 @@ class DateTimeExtended extends \DateTime ...@@ -60,4 +76,22 @@ class DateTimeExtended extends \DateTime
{ {
return $this->_isDateOnly; return $this->_isDateOnly;
} }
public function getTimezone()
{
if ($this->_isDateOnly) {
return false;
} else {
return parent::getTimezone();
}
}
public function getOffset()
{
if ($this->_isDateOnly) {
return false;
} else {
return parent::getOffset();
}
}
} }
...@@ -534,7 +534,7 @@ class Formatter extends Component ...@@ -534,7 +534,7 @@ class Formatter extends Component
// avoid time zone conversion for date-only values // avoid time zone conversion for date-only values
if ($type === 'date' && $timestamp->isDateOnly()) { if ($type === 'date' && $timestamp->isDateOnly()) {
$timeZone = $this->defaultTimeZone; // TODO maybe just NULL? $timeZone = $this->defaultTimeZone;
} else { } else {
$timeZone = $this->timeZone; $timeZone = $this->timeZone;
} }
......
...@@ -629,6 +629,18 @@ class FormatterTest extends TestCase ...@@ -629,6 +629,18 @@ class FormatterTest extends TestCase
} }
public function testDateOnlyValues()
{
date_default_timezone_set('Pacific/Kiritimati');
// timzones with exactly 24h difference, ensure this test does not fail on a certain time
$this->formatter->defaultTimeZone = 'Pacific/Kiritimati'; // always UTC+14
$this->formatter->timeZone = 'Pacific/Honolulu'; // always UTC-10
// when timezone conversion is made on this date, it will result in 2014-07-31 to be returned.
// ensure this does not happen on date only values
$this->assertSame('2014-08-01', $this->formatter->asDate('2014-08-01', 'yyyy-MM-dd'));
}
// number format // number format
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment