Skip to content

Commit

Permalink
DateTime: createFromFormat returns FALSE on failure [Closes #33][Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
stekycz authored and dg committed Oct 26, 2014
1 parent c86ddec commit 09161a4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Utils/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getTimestamp()
* @param string The format the $time parameter should be in
* @param string String representing the time
* @param string|\DateTimeZone desired timezone (default timezone is used if NULL is passed)
* @return DateTime
* @return DateTime|FALSE
*/
public static function createFromFormat($format, $time, $timezone = NULL)
{
Expand All @@ -106,7 +106,8 @@ public static function createFromFormat($format, $time, $timezone = NULL)
throw new Nette\InvalidArgumentException('Invalid timezone given');
}

return static::from(parent::createFromFormat($format, $time, $timezone));
$date = parent::createFromFormat($format, $time, $timezone);
return $date ? static::from($date) : FALSE;
}

}
2 changes: 2 additions & 0 deletions tests/Utils/DateTime.createFromFormat.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ Assert::same( 'Europe/Bratislava', DateTime::createFromFormat('Y', '2050', 'Euro
Assert::error(function(){
DateTime::createFromFormat('Y-m-d H:i:s', '2050-08-13 11:40:00', 5);
}, 'Nette\InvalidArgumentException', 'Invalid timezone given' );

Assert::false(DateTime::createFromFormat('Y-m-d', '2014-10'));

0 comments on commit 09161a4

Please sign in to comment.