Skip to content

Commit

Permalink
DateTime: strict constructor, setDate(), setTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 1, 2023
1 parent 0134347 commit eaa63f7
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Utils/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@ public static function check(
}


public function __construct(string $datetime = 'now', ?\DateTimeZone $timezone = null)
{
parent::__construct($datetime, $timezone);
$errors = self::getLastErrors();
if ($errors && $errors['warnings']) {
throw new Nette\InvalidArgumentException(Arrays::first($errors['warnings']) . " '$datetime'");
}
}


public function setDate(int $year, int $month, int $day): static
{
self::check($year, $month, $day);
return parent::setDate($year, $month, $day);
}


public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static
{
self::check(hour: $hour, minute: $minute, second: $second, microsecond: $microsecond);
return parent::setTime($hour, $minute, $second, $microsecond);
}


/**
* Returns JSON representation in ISO 8601 (used by JavaScript).
*/
Expand Down

0 comments on commit eaa63f7

Please sign in to comment.