Skip to content

Commit b2001c9

Browse files
authored
DateTime: modify() and modifyClone throw exception on error (#293)
1 parent 072c303 commit b2001c9

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Utils/DateTime.php

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ public function __toString(): string
129129
}
130130

131131

132+
public function modify(string $modifier = ''): static
133+
{
134+
$datetime = @parent::modify($modifier);
135+
136+
if ($datetime === false) {
137+
throw new Nette\InvalidArgumentException("Failed to parse time string '$modifier'");
138+
}
139+
return $datetime;
140+
}
141+
142+
132143
/**
133144
* Creates a copy with a modified time.
134145
*/

tests/Utils/DateTime.modifyClone.phpt

+5
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ $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+
Nette\InvalidArgumentException::class,
32+
);

0 commit comments

Comments
 (0)