class Time

Same name in this branch
  1. 10 core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
  2. 9 core/lib/Drupal/Component/Datetime/Time.php \Drupal\Component\Datetime\Time
  3. 8.9.x core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
  4. 8.9.x core/lib/Drupal/Component/Datetime/Time.php \Drupal\Component\Datetime\Time
  5. 11.x core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
  6. 11.x core/lib/Drupal/Component/Datetime/Time.php \Drupal\Component\Datetime\Time

Provides a class for obtaining system time.

While the normal use case of this class expects that a Request object is available from the RequestStack, it is still possible to use it without, for example for early bootstrap containers or for unit tests. In those cases, the class will access global variables or set a proxy request time in order to return the request time.

Hierarchy

  • class \Drupal\Component\Datetime\Time implements \Drupal\Component\Datetime\TimeInterface

Expanded class hierarchy of Time

16 files declare their use of Time
AliasManagerTest.php in core/modules/path_alias/tests/src/Unit/AliasManagerTest.php
AssetResolverTest.php in core/tests/Drupal/Tests/Core/Asset/AssetResolverTest.php
BackendChainImplementationUnitTest.php in core/tests/Drupal/Tests/Core/Cache/BackendChainImplementationUnitTest.php
ChainedFastBackendTest.php in core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendTest.php
CheckpointStorageTest.php in core/tests/Drupal/Tests/Core/Config/Checkpoint/CheckpointStorageTest.php

... See full list

25 string references to 'Time'
CacheTest::testCacheData in core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
Tests the data contained in cached items.
CacheTest::testHeaderStorage in core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
Tests css/js storage and restoring mechanism.
CacheTest::testTimeResultCaching in core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
Tests time based caching.
CacheTest::testTimeResultCachingWithFilter in core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
Tests result caching with filters.
CacheTest::testTimeResultCachingWithPager in core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
Tests result caching with a pager.

... See full list

1 service uses Time
datetime.time in core/core.services.yml
Drupal\Component\Datetime\Time

File

core/lib/Drupal/Component/Datetime/Time.php, line 16

Namespace

Drupal\Component\Datetime
View source
class Time implements TimeInterface {
    
    /**
     * The request stack.
     */
    protected ?RequestStack $requestStack;
    
    /**
     * A proxied request time if the request time is not available.
     */
    protected float $proxyRequestTime;
    
    /**
     * Constructs a Time object.
     *
     * @param \Symfony\Component\HttpFoundation\RequestStack|null $request_stack
     *   (Optional) The request stack.
     */
    public function __construct(?RequestStack $request_stack = NULL) {
        $this->requestStack = $request_stack;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRequestTime() {
        $request = $this->requestStack ? $this->requestStack
            ->getCurrentRequest() : NULL;
        if ($request) {
            return $request->server
                ->get('REQUEST_TIME');
        }
        // If this is called prior to the request being pushed to the stack fallback
        // to built-in globals (if available) or the system time.
        return $_SERVER['REQUEST_TIME'] ?? $this->getProxyRequestTime();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRequestMicroTime() {
        $request = $this->requestStack ? $this->requestStack
            ->getCurrentRequest() : NULL;
        if ($request) {
            return $request->server
                ->get('REQUEST_TIME_FLOAT');
        }
        // If this is called prior to the request being pushed to the stack fallback
        // to built-in globals (if available) or the system time.
        return $_SERVER['REQUEST_TIME_FLOAT'] ?? $this->getProxyRequestMicroTime();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCurrentTime() {
        return time();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCurrentMicroTime() {
        return microtime(TRUE);
    }
    
    /**
     * Returns a mimic of the timestamp of the current request.
     *
     * @return int
     *   A value returned by time().
     */
    protected function getProxyRequestTime() : int {
        if (!isset($this->proxyRequestTime)) {
            $this->proxyRequestTime = $this->getCurrentMicroTime();
        }
        return (int) $this->proxyRequestTime;
    }
    
    /**
     * Returns a mimic of the timestamp of the current request.
     *
     * @return float
     *   A value returned by microtime().
     */
    protected function getProxyRequestMicroTime() : float {
        if (!isset($this->proxyRequestTime)) {
            $this->proxyRequestTime = $this->getCurrentMicroTime();
        }
        return $this->proxyRequestTime;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
Time::$proxyRequestTime protected property A proxied request time if the request time is not available.
Time::$requestStack protected property The request stack.
Time::getCurrentMicroTime public function
Time::getCurrentTime public function
Time::getProxyRequestMicroTime protected function Returns a mimic of the timestamp of the current request.
Time::getProxyRequestTime protected function Returns a mimic of the timestamp of the current request.
Time::getRequestMicroTime public function
Time::getRequestTime public function 1
Time::__construct public function Constructs a Time object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.