mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
datetime comparison methods
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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')
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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')
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user