From 227fe7be7672f18c8e94bc54e66b686d71dc1c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Wed, 20 Mar 2024 14:00:30 +0100 Subject: [PATCH] rename period --- src/DateTime/{DateTimePeriod.php => Period.php} | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) rename src/DateTime/{DateTimePeriod.php => Period.php} (71%) diff --git a/src/DateTime/DateTimePeriod.php b/src/DateTime/Period.php similarity index 71% rename from src/DateTime/DateTimePeriod.php rename to src/DateTime/Period.php index 3826014..edc9e0b 100644 --- a/src/DateTime/DateTimePeriod.php +++ b/src/DateTime/Period.php @@ -4,34 +4,31 @@ namespace Fykosak\Utils\DateTime; -class DateTimePeriod +class Period { public function __construct( public readonly \DateTimeImmutable $begin, public readonly \DateTimeImmutable $end, ) { - if ($this->begin->getTimestamp() > $this->end->getTimestamp()) { + if ($this->begin > $this->end) { throw new \LogicException(); } } public function isBefore(?\DateTimeInterface $dateTime = null): bool { - $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); - return $this->begin->getTimestamp() > $timeStamp; + return $this->begin > ($dateTime ?? new \DateTimeImmutable()); } public function isAfter(?\DateTimeInterface $dateTime = null): bool { - $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); - return $this->end->getTimestamp() < $timeStamp; + return $this->end < ($dateTime ?? new \DateTimeImmutable()); } public function isOnGoing(?\DateTimeInterface $dateTime = null): bool { - $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); - return $this->begin->getTimestamp() <= $timeStamp - && $this->end->getTimestamp() >= $timeStamp; + return $this->begin <= ($dateTime ?? new \DateTimeImmutable()) + && $this->end >= ($dateTime ?? new \DateTimeImmutable()); } public function is(Phase $period, ?\DateTimeInterface $dateTime = null): bool