Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b59b77cd
Commit
b59b77cd
authored
Dec 17, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added timeZone property to i18n formatter
fixed #1021
parent
2febbebb
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
8 deletions
+26
-8
Application.php
framework/yii/base/Application.php
+1
-0
Formatter.php
framework/yii/i18n/Formatter.php
+22
-7
FormatterTest.php
tests/unit/framework/i18n/FormatterTest.php
+3
-1
No files found.
framework/yii/base/Application.php
View file @
b59b77cd
...
@@ -370,6 +370,7 @@ abstract class Application extends Module
...
@@ -370,6 +370,7 @@ abstract class Application extends Module
/**
/**
* Sets the time zone used by this application.
* Sets the time zone used by this application.
* This is a simple wrapper of PHP function date_default_timezone_set().
* This is a simple wrapper of PHP function date_default_timezone_set().
* Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
* @param string $value the time zone used by this application.
* @param string $value the time zone used by this application.
* @see http://php.net/manual/en/function.date-default-timezone-set.php
* @see http://php.net/manual/en/function.date-default-timezone-set.php
*/
*/
...
...
framework/yii/i18n/Formatter.php
View file @
b59b77cd
...
@@ -39,6 +39,15 @@ class Formatter extends \yii\base\Formatter
...
@@ -39,6 +39,15 @@ class Formatter extends \yii\base\Formatter
*/
*/
public
$locale
;
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.
* @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.
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
...
@@ -84,11 +93,14 @@ class Formatter extends \yii\base\Formatter
...
@@ -84,11 +93,14 @@ class Formatter extends \yii\base\Formatter
public
function
init
()
public
function
init
()
{
{
if
(
!
extension_loaded
(
'intl'
))
{
if
(
!
extension_loaded
(
'intl'
))
{
throw
new
InvalidConfigException
(
'The "intl" PHP extension is not install. It is required to format data values in localized formats.'
);
throw
new
InvalidConfigException
(
'The "intl" PHP extension is not install
ed
. It is required to format data values in localized formats.'
);
}
}
if
(
$this
->
locale
===
null
)
{
if
(
$this
->
locale
===
null
)
{
$this
->
locale
=
Yii
::
$app
->
language
;
$this
->
locale
=
Yii
::
$app
->
language
;
}
}
if
(
$this
->
timeZone
===
null
)
{
$this
->
timeZone
=
Yii
::
$app
->
timeZone
;
}
if
(
$this
->
decimalSeparator
===
null
||
$this
->
thousandSeparator
===
null
)
{
if
(
$this
->
decimalSeparator
===
null
||
$this
->
thousandSeparator
===
null
)
{
$formatter
=
new
NumberFormatter
(
$this
->
locale
,
NumberFormatter
::
DECIMAL
);
$formatter
=
new
NumberFormatter
(
$this
->
locale
,
NumberFormatter
::
DECIMAL
);
if
(
$this
->
decimalSeparator
===
null
)
{
if
(
$this
->
decimalSeparator
===
null
)
{
...
@@ -125,6 +137,7 @@ class Formatter extends \yii\base\Formatter
...
@@ -125,6 +137,7 @@ class Formatter extends \yii\base\Formatter
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
*
*
* @return string the formatted result
* @return string the formatted result
* @throws InvalidConfigException when formatting fails due to invalid parameters.
* @see dateFormat
* @see dateFormat
*/
*/
public
function
asDate
(
$value
,
$format
=
null
)
public
function
asDate
(
$value
,
$format
=
null
)
...
@@ -137,9 +150,9 @@ class Formatter extends \yii\base\Formatter
...
@@ -137,9 +150,9 @@ class Formatter extends \yii\base\Formatter
$format
=
$this
->
dateFormat
;
$format
=
$this
->
dateFormat
;
}
}
if
(
isset
(
$this
->
_dateFormats
[
$format
]))
{
if
(
isset
(
$this
->
_dateFormats
[
$format
]))
{
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
$this
->
_dateFormats
[
$format
],
IntlDateFormatter
::
NONE
);
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
$this
->
_dateFormats
[
$format
],
IntlDateFormatter
::
NONE
,
$this
->
timeZone
);
}
else
{
}
else
{
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
IntlDateFormatter
::
NONE
);
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
IntlDateFormatter
::
NONE
,
$this
->
timeZone
);
if
(
$formatter
!==
null
)
{
if
(
$formatter
!==
null
)
{
$formatter
->
setPattern
(
$format
);
$formatter
->
setPattern
(
$format
);
}
}
...
@@ -166,6 +179,7 @@ class Formatter extends \yii\base\Formatter
...
@@ -166,6 +179,7 @@ class Formatter extends \yii\base\Formatter
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
*
*
* @return string the formatted result
* @return string the formatted result
* @throws InvalidConfigException when formatting fails due to invalid parameters.
* @see timeFormat
* @see timeFormat
*/
*/
public
function
asTime
(
$value
,
$format
=
null
)
public
function
asTime
(
$value
,
$format
=
null
)
...
@@ -178,9 +192,9 @@ class Formatter extends \yii\base\Formatter
...
@@ -178,9 +192,9 @@ class Formatter extends \yii\base\Formatter
$format
=
$this
->
timeFormat
;
$format
=
$this
->
timeFormat
;
}
}
if
(
isset
(
$this
->
_dateFormats
[
$format
]))
{
if
(
isset
(
$this
->
_dateFormats
[
$format
]))
{
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
$this
->
_dateFormats
[
$format
]);
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
$this
->
_dateFormats
[
$format
]
,
$this
->
timeZone
);
}
else
{
}
else
{
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
IntlDateFormatter
::
NONE
);
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
IntlDateFormatter
::
NONE
,
$this
->
timeZone
);
if
(
$formatter
!==
null
)
{
if
(
$formatter
!==
null
)
{
$formatter
->
setPattern
(
$format
);
$formatter
->
setPattern
(
$format
);
}
}
...
@@ -207,6 +221,7 @@ class Formatter extends \yii\base\Formatter
...
@@ -207,6 +221,7 @@ class Formatter extends \yii\base\Formatter
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
* It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
*
*
* @return string the formatted result
* @return string the formatted result
* @throws InvalidConfigException when formatting fails due to invalid parameters.
* @see datetimeFormat
* @see datetimeFormat
*/
*/
public
function
asDatetime
(
$value
,
$format
=
null
)
public
function
asDatetime
(
$value
,
$format
=
null
)
...
@@ -219,9 +234,9 @@ class Formatter extends \yii\base\Formatter
...
@@ -219,9 +234,9 @@ class Formatter extends \yii\base\Formatter
$format
=
$this
->
datetimeFormat
;
$format
=
$this
->
datetimeFormat
;
}
}
if
(
isset
(
$this
->
_dateFormats
[
$format
]))
{
if
(
isset
(
$this
->
_dateFormats
[
$format
]))
{
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
$this
->
_dateFormats
[
$format
],
$this
->
_dateFormats
[
$format
]);
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
$this
->
_dateFormats
[
$format
],
$this
->
_dateFormats
[
$format
]
,
$this
->
timeZone
);
}
else
{
}
else
{
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
IntlDateFormatter
::
NONE
);
$formatter
=
new
IntlDateFormatter
(
$this
->
locale
,
IntlDateFormatter
::
NONE
,
IntlDateFormatter
::
NONE
,
$this
->
timeZone
);
if
(
$formatter
!==
null
)
{
if
(
$formatter
!==
null
)
{
$formatter
->
setPattern
(
$format
);
$formatter
->
setPattern
(
$format
);
}
}
...
...
tests/unit/framework/i18n/FormatterTest.php
View file @
b59b77cd
...
@@ -28,7 +28,9 @@ class FormatterTest extends TestCase
...
@@ -28,7 +28,9 @@ class FormatterTest extends TestCase
if
(
!
extension_loaded
(
'intl'
))
{
if
(
!
extension_loaded
(
'intl'
))
{
$this
->
markTestSkipped
(
'intl extension is required.'
);
$this
->
markTestSkipped
(
'intl extension is required.'
);
}
}
$this
->
mockApplication
();
$this
->
mockApplication
([
'timeZone'
=>
'UTC'
,
]);
$this
->
formatter
=
new
Formatter
([
'locale'
=>
'en-US'
]);
$this
->
formatter
=
new
Formatter
([
'locale'
=>
'en-US'
]);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment