Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch DateTime failure #4097

Merged
merged 2 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"codeigniter4/codeigniter4-standard": "^1.0",
"fakerphp/faker": "^1.9",
"mikey179/vfsstream": "^1.6",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan": "0.12.65",
"phpunit/phpunit": "^8.5 || ^9.1",
"predis/predis": "^1.1",
"rector/rector": "^0.8",
Expand Down
13 changes: 13 additions & 0 deletions system/I18n/Exceptions/I18nException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
*/
class I18nException extends FrameworkException
{
/**
* Thrown when createFromFormat fails to receive a valid
* DateTime back from DateTime::createFromFormat.
*
* @param string $format
*
* @return static
*/
public static function forInvalidFormat(string $format)
{
return new static(lang('Time.invalidFormat', [$format]));
}

/**
* Thrown when the numeric representation of the month falls
* outside the range of allowed months.
Expand Down
5 changes: 4 additions & 1 deletion system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ public static function create(int $year = null, int $month = null, int $day = nu
*/
public static function createFromFormat($format, $datetime, $timeZone = null)
{
$date = parent::createFromFormat($format, $datetime);
if (! $date = parent::createFromFormat($format, $datetime))
{
throw I18nException::forInvalidFormat($format);
}

return new Time($date->format('Y-m-d H:i:s'), $timeZone);
}
Expand Down
1 change: 1 addition & 0 deletions system/Language/en/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// Time language settings
return [
'invalidFormat' => '"{0}" is not a valid datetime format',
'invalidMonth' => 'Months must be between 1 and 12. Given: {0}',
'invalidDay' => 'Days must be between 1 and 31. Given: {0}',
'invalidOverDay' => 'Days must be between 1 and {0}. Given: {1}',
Expand Down
11 changes: 11 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace CodeIgniter\I18n;

use CodeIgniter\I18n\Exceptions\I18nException;
use DateTime;
use DateTimeZone;
use IntlDateFormatter;
Expand Down Expand Up @@ -197,6 +198,16 @@ public function testCreateFromFormatWithTimezoneObject()
$this->assertCloseEnoughString(date('2017-01-15 H:i:s'), $time->toDateTimeString());
}

public function testCreateFromFormatWithInvalidFormat()
{
$format = 'foobar';

$this->expectException(I18nException::class);
$this->expectExceptionMessage(lang('Time.invalidFormat', [$format]));

$time = Time::createFromFormat($format, 'America/Chicago');
}

public function testCreateFromTimestamp()
{
$time = Time::createFromTimestamp(strtotime('2017-03-18 midnight'));
Expand Down