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
3b0f5614
Commit
3b0f5614
authored
Sep 23, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
format timestamps with invalid date input
fixes #4989
parent
18b57af5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
6 deletions
+11
-6
Formatter.php
framework/i18n/Formatter.php
+6
-2
FormatterTest.php
tests/unit/framework/i18n/FormatterTest.php
+5
-4
No files found.
framework/i18n/Formatter.php
View file @
3b0f5614
...
@@ -548,13 +548,17 @@ class Formatter extends Component
...
@@ -548,13 +548,17 @@ class Formatter extends Component
$value
=
0
;
$value
=
0
;
}
}
try
{
try
{
if
(
is_numeric
(
$value
))
{
if
(
is_numeric
(
$value
))
{
// process as unix timestamp
// process as unix timestamp
if
((
$timestamp
=
DateTime
::
createFromFormat
(
'U'
,
$value
))
===
false
)
{
if
((
$timestamp
=
DateTime
::
createFromFormat
(
'U'
,
$value
))
===
false
)
{
throw
new
InvalidParamException
(
"Failed to parse '
$value
' as a UNIX timestamp."
);
throw
new
InvalidParamException
(
"Failed to parse '
$value
' as a UNIX timestamp."
);
}
}
return
$timestamp
;
return
$timestamp
;
}
elseif
((
$timestamp
=
DateTime
::
createFromFormat
(
'Y-m-d'
,
$value
))
!==
false
)
{
// try Y-m-d format
return
$timestamp
;
}
elseif
((
$timestamp
=
DateTime
::
createFromFormat
(
'Y-m-d H:i:s'
,
$value
))
!==
false
)
{
// try Y-m-d H:i:s format
return
$timestamp
;
}
}
// finally try to create a DateTime object with the value
$timestamp
=
new
DateTime
(
$value
);
$timestamp
=
new
DateTime
(
$value
);
return
$timestamp
;
return
$timestamp
;
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
...
...
tests/unit/framework/i18n/FormatterTest.php
View file @
3b0f5614
...
@@ -454,7 +454,8 @@ class FormatterTest extends TestCase
...
@@ -454,7 +454,8 @@ class FormatterTest extends TestCase
public
function
dateInputs
()
public
function
dateInputs
()
{
{
return
[
return
[
[
false
,
'2014-13-01'
,
'yii\base\InvalidParamException'
],
// ['2015-01-01 00:00:00', '2014-13-01'], // TODO evals to current time on that date
[
'2015-01-01 00:00:00'
,
'2014-13-01 00:00:00'
],
[
false
,
'asdfg'
,
'yii\base\InvalidParamException'
],
[
false
,
'asdfg'
,
'yii\base\InvalidParamException'
],
// [(string)strtotime('now'), 'now'], // fails randomly
// [(string)strtotime('now'), 'now'], // fails randomly
];
];
...
@@ -476,9 +477,9 @@ class FormatterTest extends TestCase
...
@@ -476,9 +477,9 @@ class FormatterTest extends TestCase
if
(
$expectedException
!==
null
)
{
if
(
$expectedException
!==
null
)
{
$this
->
setExpectedException
(
$expectedException
);
$this
->
setExpectedException
(
$expectedException
);
}
}
$this
->
assertSame
(
$expected
,
$this
->
formatter
->
asDate
(
$value
,
'
php:U
'
));
$this
->
assertSame
(
$expected
,
$this
->
formatter
->
asDate
(
$value
,
'
yyyy-MM-dd HH:mm:ss
'
));
$this
->
assertSame
(
$expected
,
$this
->
formatter
->
asTime
(
$value
,
'
php:U
'
));
$this
->
assertSame
(
$expected
,
$this
->
formatter
->
asTime
(
$value
,
'
yyyy-MM-dd HH:mm:ss
'
));
$this
->
assertSame
(
$expected
,
$this
->
formatter
->
asDatetime
(
$value
,
'
php:U
'
));
$this
->
assertSame
(
$expected
,
$this
->
formatter
->
asDatetime
(
$value
,
'
yyyy-MM-dd HH:mm:ss
'
));
}
}
...
...
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