Skip to content

Commit eaa63f7

Browse files
committed
DateTime: strict constructor, setDate(), setTime()
1 parent 0134347 commit eaa63f7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Utils/DateTime.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ public static function check(
127127
}
128128

129129

130+
public function __construct(string $datetime = 'now', ?\DateTimeZone $timezone = null)
131+
{
132+
parent::__construct($datetime, $timezone);
133+
$errors = self::getLastErrors();
134+
if ($errors && $errors['warnings']) {
135+
throw new Nette\InvalidArgumentException(Arrays::first($errors['warnings']) . " '$datetime'");
136+
}
137+
}
138+
139+
140+
public function setDate(int $year, int $month, int $day): static
141+
{
142+
self::check($year, $month, $day);
143+
return parent::setDate($year, $month, $day);
144+
}
145+
146+
147+
public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static
148+
{
149+
self::check(hour: $hour, minute: $minute, second: $second, microsecond: $microsecond);
150+
return parent::setTime($hour, $minute, $second, $microsecond);
151+
}
152+
153+
130154
/**
131155
* Returns JSON representation in ISO 8601 (used by JavaScript).
132156
*/

0 commit comments

Comments
 (0)