datetime comparison methods

This commit is contained in:
Yuri Kuznetsov
2022-05-26 14:06:35 +03:00
parent e3319a3bd4
commit bedb66fdd5
6 changed files with 199 additions and 3 deletions

View File

@@ -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.
*/

View File

@@ -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.
*/

View File

@@ -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.
*/

View File

@@ -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')
)
);
}
}

View File

@@ -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')
)
);
}
}

View File

@@ -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')
)
);
}
}