mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 15:06:06 +00:00
datetime fields impr
This commit is contained in:
@@ -29,6 +29,8 @@
|
||||
|
||||
namespace Espo\Core\Field;
|
||||
|
||||
use Espo\Core\Field\DateTime\DateTimeable;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use DateInterval;
|
||||
@@ -39,7 +41,7 @@ use Throwable;
|
||||
/**
|
||||
* A date value object. Immutable.
|
||||
*/
|
||||
class Date
|
||||
class Date implements DateTimeable
|
||||
{
|
||||
private $value;
|
||||
|
||||
@@ -165,6 +167,25 @@ class Date
|
||||
return self::fromDateTime($dateTime);
|
||||
}
|
||||
|
||||
public function diff(DateTimeable $other): DateInterval
|
||||
{
|
||||
return $this->getDateTime()->diff($other->getDateTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a today.
|
||||
*/
|
||||
public static function createToday(?DateTimeZone $timezone = null): self
|
||||
{
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
if ($timezone) {
|
||||
$now = $now->setTimezone($timezone);
|
||||
}
|
||||
|
||||
return self::fromDateTime($now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create from a string with a date in `Y-m-d` format.
|
||||
*/
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
|
||||
namespace Espo\Core\Field;
|
||||
|
||||
use Espo\Core\Field\DateTime\DateTimeable;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use DateInterval;
|
||||
@@ -39,7 +41,7 @@ use Throwable;
|
||||
/**
|
||||
* A date-time value object. Immutable.
|
||||
*/
|
||||
class DateTime
|
||||
class DateTime implements DateTimeable
|
||||
{
|
||||
private $value;
|
||||
|
||||
@@ -197,6 +199,11 @@ class DateTime
|
||||
return self::fromDateTime($dateTime);
|
||||
}
|
||||
|
||||
public function diff(DateTimeable $other): DateInterval
|
||||
{
|
||||
return $this->getDateTime()->diff($other->getDateTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones and apply a timezone.
|
||||
*/
|
||||
@@ -207,6 +214,14 @@ class DateTime
|
||||
return self::fromDateTime($dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a current time.
|
||||
*/
|
||||
public static function createNow(): self
|
||||
{
|
||||
return self::fromDateTime(new DateTimeImmutable());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create from a string with a date-time in `Y-m-d H:i:s` format.
|
||||
*/
|
||||
|
||||
37
application/Espo/Core/Field/DateTime/DateTimeable.php
Normal file
37
application/Espo/Core/Field/DateTime/DateTimeable.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Field\DateTime;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface DateTimeable
|
||||
{
|
||||
public function getDateTime(): DateTimeImmutable;
|
||||
}
|
||||
@@ -29,10 +29,9 @@
|
||||
|
||||
namespace Espo\Core\Field;
|
||||
|
||||
use Espo\Core\Field\{
|
||||
DateTime,
|
||||
Date,
|
||||
};
|
||||
use Espo\Core\Field\DateTime;
|
||||
use Espo\Core\Field\Date;
|
||||
use Espo\Core\Field\DateTime\DateTimeable;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
@@ -43,7 +42,7 @@ use RuntimeException;
|
||||
/**
|
||||
* A date-time or date. Immutable.
|
||||
*/
|
||||
class DateTimeOptional
|
||||
class DateTimeOptional implements DateTimeable
|
||||
{
|
||||
private $dateTimeValue = null;
|
||||
|
||||
@@ -263,6 +262,33 @@ class DateTimeOptional
|
||||
);
|
||||
}
|
||||
|
||||
public function diff(DateTimeable $other): DateInterval
|
||||
{
|
||||
return $this->getDateTime()->diff($other->getDateTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a current time.
|
||||
*/
|
||||
public static function createNow(): self
|
||||
{
|
||||
return self::fromDateTime(new DateTimeImmutable());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a today.
|
||||
*/
|
||||
public static function createToday(?DateTimeZone $timezone = null): self
|
||||
{
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
if ($timezone) {
|
||||
$now = $now->setTimezone($timezone);
|
||||
}
|
||||
|
||||
return self::fromDateTimeAllDay($now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create from a string with a date in `Y-m-d` format.
|
||||
*/
|
||||
|
||||
@@ -130,4 +130,21 @@ class DateTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertNotSame($modifiedValue, $value);
|
||||
}
|
||||
|
||||
public function testDiff(): void
|
||||
{
|
||||
$value1 = Date::fromString('2021-05-01');
|
||||
$value2 = Date::fromString('2021-05-02');
|
||||
|
||||
$this->assertEquals(1, $value1->diff($value2)->d);
|
||||
$this->assertEquals(0, $value1->diff($value2)->invert);
|
||||
}
|
||||
|
||||
public function testToday(): void
|
||||
{
|
||||
$value1 = Date::createToday();
|
||||
$value2 = Date::createToday(new DateTimeZone('Europe/Kiev'));
|
||||
|
||||
$this->assertEquals(0, $value1->diff($value2)->invert);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,4 +162,20 @@ class DateTimeTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertEquals(new DateTimeZone('Europe/Kiev'), $value->getTimezone());
|
||||
}
|
||||
|
||||
public function testDiff(): void
|
||||
{
|
||||
$value1 = DateTime::fromString('2021-05-01 10:10:30');
|
||||
$value2 = DateTime::fromString('2021-05-01 10:20:30');
|
||||
|
||||
$this->assertEquals(10, $value1->diff($value2)->i);
|
||||
$this->assertEquals(0, $value1->diff($value2)->invert);
|
||||
}
|
||||
|
||||
public function testNow(): void
|
||||
{
|
||||
$value = DateTime::createNow();
|
||||
|
||||
$this->assertNotNull($value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,4 +152,21 @@ class DateTimeOptionalDateTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertFalse($value->isAllDay());
|
||||
}
|
||||
|
||||
public function testDiff(): void
|
||||
{
|
||||
$value1 = DateTimeOptional::fromString('2021-05-01');
|
||||
$value2 = DateTimeOptional::fromString('2021-05-02');
|
||||
|
||||
$this->assertEquals(1, $value1->diff($value2)->d);
|
||||
$this->assertEquals(0, $value1->diff($value2)->invert);
|
||||
}
|
||||
|
||||
public function testToday(): void
|
||||
{
|
||||
$value1 = DateTimeOptional::createToday();
|
||||
$value2 = DateTimeOptional::createToday(new DateTimeZone('Europe/Kiev'));
|
||||
|
||||
$this->assertEquals(0, $value1->diff($value2)->invert);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,4 +164,20 @@ class DateTimeOptionalTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertEquals(new DateTimeZone('Europe/Kiev'), $value->getTimezone());
|
||||
}
|
||||
|
||||
public function testDiff(): void
|
||||
{
|
||||
$value1 = DateTimeOptional::fromString('2021-05-01 10:10:30');
|
||||
$value2 = DateTimeOptional::fromString('2021-05-01 10:20:30');
|
||||
|
||||
$this->assertEquals(10, $value1->diff($value2)->i);
|
||||
$this->assertEquals(0, $value1->diff($value2)->invert);
|
||||
}
|
||||
|
||||
public function testNow(): void
|
||||
{
|
||||
$value = DateTimeOptional::createNow();
|
||||
|
||||
$this->assertNotNull($value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user