Skip to content

EspoCRM – Open Source CRM Application

Use clock in DateTIme

Yurii6d0a9094856a
diff --git a/application/Espo/Core/Loaders/DateTime.php b/application/Espo/Core/Loaders/DateTime.php
index 2902779619..bcceb517d8 100644
--- a/application/Espo/Core/Loaders/DateTime.php
+++ b/application/Espo/Core/Loaders/DateTime.php
@@ -32,19 +32,23 @@ namespace Espo\Core\Loaders;
use Espo\Core\Container\Loader;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\DateTime as DateTimeService;
+use Espo\Core\Utils\DateTime\Clock;
class DateTime implements Loader
{
- public function __construct(private Config $config)
- {}
+ public function __construct(
+ private Config $config,
+ private Clock $clock,
+ ) {}
public function load(): DateTimeService
{
return new DateTimeService(
- $this->config->get('dateFormat'),
- $this->config->get('timeFormat'),
- $this->config->get('timeZone'),
- $this->config->get('language')
+ dateFormat: $this->config->get('dateFormat'),
+ timeFormat: $this->config->get('timeFormat'),
+ timeZone: $this->config->get('timeZone'),
+ language: $this->config->get('language'),
+ clock: $this->clock,
);
}
}
diff --git a/application/Espo/Core/Utils/DateTime.php b/application/Espo/Core/Utils/DateTime.php
index cae75fc701..ca45e2f42d 100644
--- a/application/Espo/Core/Utils/DateTime.php
+++ b/application/Espo/Core/Utils/DateTime.php
@@ -37,6 +37,7 @@ use Espo\Core\Field\DateTime as DateTimeField;
use DateTime as DateTimeStd;
use DateTimeImmutable;
use DateTimeZone;
+use Espo\Core\Utils\DateTime\Clock;
use Exception;
use RuntimeException;
@@ -46,8 +47,8 @@ use RuntimeException;
*/
class DateTime
{
- public const SYSTEM_DATE_TIME_FORMAT = 'Y-m-d H:i:s';
- public const SYSTEM_DATE_FORMAT = 'Y-m-d';
+ public const string SYSTEM_DATE_TIME_FORMAT = 'Y-m-d H:i:s';
+ public const string SYSTEM_DATE_FORMAT = 'Y-m-d';
private string $dateFormat;
private string $timeFormat;
@@ -58,7 +59,8 @@ class DateTime
?string $dateFormat = 'YYYY-MM-DD',
?string $timeFormat = 'HH:mm',
?string $timeZone = 'UTC',
- ?string $language = 'en_US'
+ ?string $language = 'en_US',
+ private ?Clock $clock = null,
) {
$this->dateFormat = $dateFormat ?? 'YYYY-MM-DD';
$this->timeFormat = $timeFormat ?? 'HH:mm';
@@ -168,8 +170,8 @@ class DateTime
throw new RuntimeException($e->getMessage());
}
- $dateTime = new DateTimeStd();
- $dateTime->setTimezone($tz);
+ $dateTime = $this->getNowInternal()
+ ->setTimezone($tz);
$carbon = Carbon::instance($dateTime);
$carbon->locale($this->language);
@@ -191,9 +193,8 @@ class DateTime
throw new RuntimeException($e->getMessage());
}
- $dateTime = new DateTimeStd();
-
- $dateTime->setTimezone($tz);
+ $dateTime = $this->getNowInternal()
+ ->setTimezone($tz);
$carbon = Carbon::instance($dateTime);
@@ -257,7 +258,7 @@ class DateTime
*/
public function getToday(): Date
{
- $string = (new DateTimeImmutable)
+ $string = $this->getNowInternal()
->setTimezone($this->timezone)
->format(self::SYSTEM_DATE_FORMAT);
@@ -271,7 +272,7 @@ class DateTime
*/
public function getNow(): DateTimeField
{
- return DateTimeField::createNow()
+ return DateTimeField::fromDateTime($this->getNowInternal())
->withTimezone($this->timezone);
}
@@ -307,4 +308,9 @@ class DateTime
{
return $this->convertSystemDateTime($string);
}
+
+ private function getNowInternal(): DateTimeImmutable
+ {
+ return $this->clock?->now() ?? new DateTimeImmutable();
+ }
}