From 84bfc3fc886104d24ef468fddfa8e6c7a7bdd658 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sat, 25 Jan 2014 23:47:24 +0400 Subject: [PATCH 1/4] add timeZone into base formatter --- framework/CHANGELOG.md | 1 + framework/base/Formatter.php | 38 ++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index f7af0c307f..432134feff 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -103,6 +103,7 @@ Yii Framework 2 Change Log - Enh: Added support to parse json request data to Request::getRestParams() (cebe) - Enh: Added ability to get incoming headers (dizews) - Enh: Added `beforeRun()` and `afterRun()` to `yii\base\Action` (qiangxue) +- Enh: Added support formatter timeZone by default (dizews) - Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue) - Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue) - Chg #1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected (qiangxue) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index bfc1e36113..2fd1976e35 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -27,6 +27,15 @@ use yii\helpers\Html; */ class Formatter extends Component { + /** + * @var string|\IntlTimeZone|\DateTimeZone the timezone to use for formatting time and date values. + * This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php) + * e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. + * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones. + * This can also be an IntlTimeZone or a DateTimeZone object. + * If not set, [[\yii\base\Application::timezone]] will be used. + */ + public $timeZone; /** * @var string the default format string to be used to format a date using PHP date() function. */ @@ -59,12 +68,20 @@ class Formatter extends Component */ public $thousandSeparator; - /** * Initializes the component. */ public function init() { + if ($this->timeZone === null) { + $this->timeZone = Yii::$app->timeZone; + } + if (is_string($this->timeZone)) { + $this->timeZone = new \DateTimeZone($this->timeZone); + } elseif ($this->timeZone instanceof IntlTimeZone) { + $this->timeZone = $this->timeZone->toDateTimeZone(); + } + if (empty($this->booleanFormat)) { $this->booleanFormat = [Yii::t('yii', 'No'), Yii::t('yii', 'Yes')]; } @@ -258,7 +275,7 @@ class Formatter extends Component return $this->nullDisplay; } $value = $this->normalizeDatetimeValue($value); - return date($format === null ? $this->dateFormat : $format, $value); + return $this->formatTimestamp($value, $format); } /** @@ -282,7 +299,7 @@ class Formatter extends Component return $this->nullDisplay; } $value = $this->normalizeDatetimeValue($value); - return date($format === null ? $this->timeFormat : $format, $value); + return $this->formatTimestamp($value, $format); } /** @@ -306,7 +323,7 @@ class Formatter extends Component return $this->nullDisplay; } $value = $this->normalizeDatetimeValue($value); - return date($format === null ? $this->datetimeFormat : $format, $value); + return $this->formatTimestamp($value, $format); } /** @@ -325,6 +342,19 @@ class Formatter extends Component } } + /** + * @param integer $value normalized datetime value + * @param string $format the format used to convert the value into a date string. + * @return string the formatted result + */ + protected function formatTimestamp($value, $format = null) + { + $date = new DateTime(); + $date->setTimestamp($value); + $date->setTimezone($this->timeZone); + return $date->format($format === null ? $this->datetimeFormat : $format); + } + /** * Formats the value as an integer. * @param mixed $value the value to be formatted From 230f40c866f27f37adbd2f2265b4088166c9f7d3 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sun, 26 Jan 2014 00:32:21 +0400 Subject: [PATCH 2/4] fix formatter --- framework/base/Formatter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index 2fd1976e35..61afb6e10a 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -275,7 +275,7 @@ class Formatter extends Component return $this->nullDisplay; } $value = $this->normalizeDatetimeValue($value); - return $this->formatTimestamp($value, $format); + return $this->formatTimestamp($value, $format === null ? $this->dateFormat : $format, $value); } /** @@ -299,7 +299,7 @@ class Formatter extends Component return $this->nullDisplay; } $value = $this->normalizeDatetimeValue($value); - return $this->formatTimestamp($value, $format); + return $this->formatTimestamp($value, $format === null ? $this->timeFormat : $format, $value); } /** @@ -323,7 +323,7 @@ class Formatter extends Component return $this->nullDisplay; } $value = $this->normalizeDatetimeValue($value); - return $this->formatTimestamp($value, $format); + return $this->formatTimestamp($value, $format === null ? $this->datetimeFormat : $format, $value); } /** @@ -347,12 +347,12 @@ class Formatter extends Component * @param string $format the format used to convert the value into a date string. * @return string the formatted result */ - protected function formatTimestamp($value, $format = null) + protected function formatTimestamp($value, $format) { $date = new DateTime(); $date->setTimestamp($value); $date->setTimezone($this->timeZone); - return $date->format($format === null ? $this->datetimeFormat : $format); + return $date->format($format); } /** From c8a9ed80c843bc5c0755bbb634385de8f3685ce8 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sun, 26 Jan 2014 00:51:17 +0400 Subject: [PATCH 3/4] prevent changing public timezone --- framework/base/Formatter.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index 61afb6e10a..346e5420e1 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -68,6 +68,11 @@ class Formatter extends Component */ public $thousandSeparator; + /** + * @var \DateTimeZone + */ + private $_timeZone; + /** * Initializes the component. */ @@ -77,9 +82,9 @@ class Formatter extends Component $this->timeZone = Yii::$app->timeZone; } if (is_string($this->timeZone)) { - $this->timeZone = new \DateTimeZone($this->timeZone); - } elseif ($this->timeZone instanceof IntlTimeZone) { - $this->timeZone = $this->timeZone->toDateTimeZone(); + $this->_timeZone = new \DateTimeZone($this->timeZone); + } elseif ($this->_timeZone instanceof IntlTimeZone) { + $this->_timeZone = $this->timeZone->toDateTimeZone(); } if (empty($this->booleanFormat)) { @@ -351,7 +356,7 @@ class Formatter extends Component { $date = new DateTime(); $date->setTimestamp($value); - $date->setTimezone($this->timeZone); + $date->setTimezone($this->_timeZone); return $date->format($format); } From 330ae053e6bfe107c636fb688839abd52d392e94 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sun, 26 Jan 2014 01:42:36 +0400 Subject: [PATCH 4/4] remove timezone from i18n formatter --- framework/base/Formatter.php | 13 ++++--------- framework/i18n/Formatter.php | 12 ------------ 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index 346e5420e1..61afb6e10a 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -68,11 +68,6 @@ class Formatter extends Component */ public $thousandSeparator; - /** - * @var \DateTimeZone - */ - private $_timeZone; - /** * Initializes the component. */ @@ -82,9 +77,9 @@ class Formatter extends Component $this->timeZone = Yii::$app->timeZone; } if (is_string($this->timeZone)) { - $this->_timeZone = new \DateTimeZone($this->timeZone); - } elseif ($this->_timeZone instanceof IntlTimeZone) { - $this->_timeZone = $this->timeZone->toDateTimeZone(); + $this->timeZone = new \DateTimeZone($this->timeZone); + } elseif ($this->timeZone instanceof IntlTimeZone) { + $this->timeZone = $this->timeZone->toDateTimeZone(); } if (empty($this->booleanFormat)) { @@ -356,7 +351,7 @@ class Formatter extends Component { $date = new DateTime(); $date->setTimestamp($value); - $date->setTimezone($this->_timeZone); + $date->setTimezone($this->timeZone); return $date->format($format); } diff --git a/framework/i18n/Formatter.php b/framework/i18n/Formatter.php index 0c832909f9..4619facfef 100644 --- a/framework/i18n/Formatter.php +++ b/framework/i18n/Formatter.php @@ -38,15 +38,6 @@ class Formatter extends \yii\base\Formatter * If not set, [[\yii\base\Application::language]] will be used. */ public $locale; - /** - * @var string|\IntlTimeZone|\DateTimeZone the timezone to use for formatting time and date values. - * This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php) - * e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. - * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones. - * This can also be an IntlTimeZone or a DateTimeZone object. - * If not set, [[\yii\base\Application::timezone]] will be used. - */ - public $timeZone; /** * @var string the default format string to be used to format a date. * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. @@ -98,9 +89,6 @@ class Formatter extends \yii\base\Formatter if ($this->locale === null) { $this->locale = Yii::$app->language; } - if ($this->timeZone === null) { - $this->timeZone = Yii::$app->timeZone; - } if ($this->decimalSeparator === null || $this->thousandSeparator === null) { $formatter = new NumberFormatter($this->locale, NumberFormatter::DECIMAL); if ($this->decimalSeparator === null) {