Skip to content

Commit c7ec447

Browse files
lukaspijakdg
authored andcommitted
DateTime: modify() and modifyClone() throw exception on error (#293)
1 parent 9124157 commit c7ec447

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Utils/DateTime.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ public function __toString(): string
129129
}
130130

131131

132+
public function modify(string $modifier): static
133+
{
134+
$datetime = @parent::modify($modifier); // @ is escalated to exception
135+
if ($datetime === false) {
136+
throw new Nette\InvalidArgumentException(Helpers::getLastError());
137+
}
138+
return $datetime;
139+
}
140+
141+
132142
/**
133143
* Creates a copy with a modified time.
134144
*/

tests/Utils/DateTime.modifyClone.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ $dolly2 = $date->modifyClone('+1 hour');
2525
Assert::type(DateTime::class, $dolly2);
2626
Assert::notSame($date, $dolly2);
2727
Assert::notSame((string) $date, (string) $dolly2);
28+
29+
Assert::exception(
30+
fn() => $date->modifyClone('xx'),
31+
PHP_VERSION_ID >= 80300 ? DateMalformedStringException::class : Nette\InvalidArgumentException::class,
32+
'DateTime::modify(): Failed to parse %a%',
33+
);

0 commit comments

Comments
 (0)