Skip to content

Commit

Permalink
DateTime: modify() and modifyClone() throw exception on error (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspijak authored and dg committed Jul 30, 2023
1 parent 688acce commit 8c12f6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Utils/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public function __toString(): string
}


public function modify(string $modifier): static
{
$datetime = @parent::modify($modifier); // @ is escalated to exception
if ($datetime === false) {
throw new Nette\InvalidArgumentException(Helpers::getLastError());
}
return $datetime;
}


/**
* Creates a copy with a modified time.
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/Utils/DateTime.modifyClone.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ $dolly2 = $date->modifyClone('+1 hour');
Assert::type(DateTime::class, $dolly2);
Assert::notSame($date, $dolly2);
Assert::notSame((string) $date, (string) $dolly2);

Assert::exception(
fn() => $date->modifyClone('xx'),
PHP_VERSION_ID >= 80300 ? DateMalformedStringException::class : Nette\InvalidArgumentException::class,
'DateTime::modify(): Failed to parse %a%',
);

0 comments on commit 8c12f6e

Please sign in to comment.