Skip to content

Commit 453af97

Browse files
authored
Merge pull request #4097 from MGatner/datetime-format
Catch DateTime failure
2 parents 3119fc8 + fab0a66 commit 453af97

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"codeigniter4/codeigniter4-standard": "^1.0",
1919
"fakerphp/faker": "^1.9",
2020
"mikey179/vfsstream": "^1.6",
21-
"phpstan/phpstan": "^0.12",
21+
"phpstan/phpstan": "0.12.65",
2222
"phpunit/phpunit": "^8.5 || ^9.1",
2323
"predis/predis": "^1.1",
2424
"rector/rector": "^0.8",

system/I18n/Exceptions/I18nException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
*/
1919
class I18nException extends FrameworkException
2020
{
21+
/**
22+
* Thrown when createFromFormat fails to receive a valid
23+
* DateTime back from DateTime::createFromFormat.
24+
*
25+
* @param string $format
26+
*
27+
* @return static
28+
*/
29+
public static function forInvalidFormat(string $format)
30+
{
31+
return new static(lang('Time.invalidFormat', [$format]));
32+
}
33+
2134
/**
2235
* Thrown when the numeric representation of the month falls
2336
* outside the range of allowed months.

system/I18n/Time.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,10 @@ public static function create(int $year = null, int $month = null, int $day = nu
277277
*/
278278
public static function createFromFormat($format, $datetime, $timeZone = null)
279279
{
280-
$date = parent::createFromFormat($format, $datetime);
280+
if (! $date = parent::createFromFormat($format, $datetime))
281+
{
282+
throw I18nException::forInvalidFormat($format);
283+
}
281284

282285
return new Time($date->format('Y-m-d H:i:s'), $timeZone);
283286
}

system/Language/en/Time.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// Time language settings
1313
return [
14+
'invalidFormat' => '"{0}" is not a valid datetime format',
1415
'invalidMonth' => 'Months must be between 1 and 12. Given: {0}',
1516
'invalidDay' => 'Days must be between 1 and 31. Given: {0}',
1617
'invalidOverDay' => 'Days must be between 1 and {0}. Given: {1}',

tests/system/I18n/TimeTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace CodeIgniter\I18n;
33

4+
use CodeIgniter\I18n\Exceptions\I18nException;
45
use DateTime;
56
use DateTimeZone;
67
use IntlDateFormatter;
@@ -197,6 +198,16 @@ public function testCreateFromFormatWithTimezoneObject()
197198
$this->assertCloseEnoughString(date('2017-01-15 H:i:s'), $time->toDateTimeString());
198199
}
199200

201+
public function testCreateFromFormatWithInvalidFormat()
202+
{
203+
$format = 'foobar';
204+
205+
$this->expectException(I18nException::class);
206+
$this->expectExceptionMessage(lang('Time.invalidFormat', [$format]));
207+
208+
$time = Time::createFromFormat($format, 'America/Chicago');
209+
}
210+
200211
public function testCreateFromTimestamp()
201212
{
202213
$time = Time::createFromTimestamp(strtotime('2017-03-18 midnight'));

0 commit comments

Comments
 (0)