diff --git a/application/Espo/Core/Field/Date.php b/application/Espo/Core/Field/Date.php index 3868b830da..d5922f39b9 100644 --- a/application/Espo/Core/Field/Date.php +++ b/application/Espo/Core/Field/Date.php @@ -172,6 +172,30 @@ class Date implements DateTimeable return $this->getDateTime()->diff($other->getDateTime()); } + /** + * Whether greater than a given value. + */ + public function isGreaterThan(DateTimeable $other): bool + { + return $this->getDateTime() > $other->getDateTime(); + } + + /** + * Whether less than a given value. + */ + public function isLessThan(DateTimeable $other): bool + { + return $this->getDateTime() < $other->getDateTime(); + } + + /** + * Whether equals to a given value. + */ + public function isEqualTo(DateTimeable $other): bool + { + return $this->getDateTime() == $other->getDateTime(); + } + /** * Create a today. */ diff --git a/application/Espo/Core/Field/DateTime.php b/application/Espo/Core/Field/DateTime.php index 74f29881c7..1688e40c70 100644 --- a/application/Espo/Core/Field/DateTime.php +++ b/application/Espo/Core/Field/DateTime.php @@ -230,6 +230,30 @@ class DateTime implements DateTimeable return self::fromDateTime($dateTime); } + /** + * Whether greater than a given value. + */ + public function isGreaterThan(DateTimeable $other): bool + { + return $this->getDateTime() > $other->getDateTime(); + } + + /** + * Whether less than a given value. + */ + public function isLessThan(DateTimeable $other): bool + { + return $this->getDateTime() < $other->getDateTime(); + } + + /** + * Whether equals to a given value. + */ + public function isEqualTo(DateTimeable $other): bool + { + return $this->getDateTime() == $other->getDateTime(); + } + /** * Create a current time. */ diff --git a/application/Espo/Core/Field/DateTimeOptional.php b/application/Espo/Core/Field/DateTimeOptional.php index b5670e6c9d..72d790b083 100644 --- a/application/Espo/Core/Field/DateTimeOptional.php +++ b/application/Espo/Core/Field/DateTimeOptional.php @@ -315,6 +315,30 @@ class DateTimeOptional implements DateTimeable return $this->getDateTime()->diff($other->getDateTime()); } + /** + * Whether greater than a given value. + */ + public function isGreaterThan(DateTimeable $other): bool + { + return $this->getDateTime() > $other->getDateTime(); + } + + /** + * Whether less than a given value. + */ + public function isLessThan(DateTimeable $other): bool + { + return $this->getDateTime() < $other->getDateTime(); + } + + /** + * Whether equals to a given value. + */ + public function isEqualTo(DateTimeable $other): bool + { + return $this->getDateTime() == $other->getDateTime(); + } + /** * Create a current time. */ diff --git a/tests/unit/Espo/Core/Field/Date/DateTest.php b/tests/unit/Espo/Core/Field/Date/DateTest.php index 96901c4ee5..0678ac33c9 100644 --- a/tests/unit/Espo/Core/Field/Date/DateTest.php +++ b/tests/unit/Espo/Core/Field/Date/DateTest.php @@ -147,4 +147,45 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertEquals(0, $value1->diff($value2)->invert); } + + public function testComparison(): void + { + $value = Date::fromString('2021-05-01'); + + $this->assertTrue( + $value->isEqualTo( + $value->modify('+1 day')->modify('-1 day') + ) + ); + + $this->assertFalse( + $value->isEqualTo( + $value->modify('+1 day') + ) + ); + + $this->assertFalse( + $value->isGreaterThan( + $value->modify('+1 day') + ) + ); + + $this->assertFalse( + $value->isLessThan( + $value->modify('-1 day') + ) + ); + + $this->assertTrue( + $value->isGreaterThan( + $value->modify('-1 day') + ) + ); + + $this->assertTrue( + $value->isLessThan( + $value->modify('+1 day') + ) + ); + } } diff --git a/tests/unit/Espo/Core/Field/DateTime/DateTimeTest.php b/tests/unit/Espo/Core/Field/DateTime/DateTimeTest.php index 6cabe608b8..a89ad17b0a 100644 --- a/tests/unit/Espo/Core/Field/DateTime/DateTimeTest.php +++ b/tests/unit/Espo/Core/Field/DateTime/DateTimeTest.php @@ -29,9 +29,7 @@ namespace tests\unit\Espo\Core\Field\DateTime; -use Espo\Core\{ - Field\DateTime, -}; +use Espo\Core\Field\DateTime; use DateTimeImmutable; use DateTimeZone; @@ -203,4 +201,47 @@ class DateTimeTest extends \PHPUnit\Framework\TestCase $value->withTime(null, 0, 10)->getString() ); } + + public function testComparison(): void + { + $value = DateTime::fromString('2021-05-01 10:10:30') + ->withTimezone(new DateTimeZone('Europe/Kiev')); + + $this->assertTrue( + $value->isEqualTo( + $value->withTimezone(new DateTimeZone('UTC')) + ) + ); + + $this->assertFalse( + $value->isEqualTo( + $value->modify('+1 minute') + ) + ); + + $this->assertFalse( + $value->isGreaterThan( + $value->modify('+1 minute') + ) + ); + + $this->assertFalse( + $value->isLessThan( + $value->modify('-1 minute') + ) + ); + + $this->assertTrue( + $value->isGreaterThan( + $value->modify('-1 minute') + ) + ); + + $this->assertTrue( + $value->isLessThan( + $value->modify('+1 minute') + ) + ); + } } + diff --git a/tests/unit/Espo/Core/Field/DateTimeOptional/DateTimeOptionalTest.php b/tests/unit/Espo/Core/Field/DateTimeOptional/DateTimeOptionalTest.php index 2757999282..9b062d0c7d 100644 --- a/tests/unit/Espo/Core/Field/DateTimeOptional/DateTimeOptionalTest.php +++ b/tests/unit/Espo/Core/Field/DateTimeOptional/DateTimeOptionalTest.php @@ -231,4 +231,46 @@ class DateTimeOptionalTest extends \PHPUnit\Framework\TestCase $value->withTime(null, 0, 10)->getString() ); } + + public function testComparison(): void + { + $value = DateTimeOptional::fromString('2021-05-01 10:10:30') + ->withTimezone(new DateTimeZone('Europe/Kiev')); + + $this->assertTrue( + $value->isEqualTo( + $value->withTimezone(new DateTimeZone('UTC')) + ) + ); + + $this->assertFalse( + $value->isEqualTo( + $value->modify('+1 minute') + ) + ); + + $this->assertFalse( + $value->isGreaterThan( + $value->modify('+1 minute') + ) + ); + + $this->assertFalse( + $value->isLessThan( + $value->modify('-1 minute') + ) + ); + + $this->assertTrue( + $value->isGreaterThan( + $value->modify('-1 minute') + ) + ); + + $this->assertTrue( + $value->isLessThan( + $value->modify('+1 minute') + ) + ); + } }