class Time

Same name in this branch
  1. 9 core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
Same name and namespace in other branches
  1. 8.9.x core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
  2. 8.9.x core/lib/Drupal/Component/Datetime/Time.php \Drupal\Component\Datetime\Time
  3. 10 core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
  4. 10 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.

Hierarchy

Expanded class hierarchy of Time

2 files declare their use of Time
TestTime.php in core/modules/update/tests/modules/update_test/src/Datetime/TestTime.php
TimeTest.php in core/tests/Drupal/Tests/Component/Datetime/TimeTest.php
23 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 10

Namespace

Drupal\Component\Datetime
View source
class Time implements TimeInterface {
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * Constructs a Time object.
     *
     * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
     *   The request stack.
     */
    public function __construct(RequestStack $request_stack) {
        $this->requestStack = $request_stack;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRequestTime() {
        $request = $this->requestStack
            ->getCurrentRequest();
        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->getCurrentTime();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRequestMicroTime() {
        $request = $this->requestStack
            ->getCurrentRequest();
        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->getCurrentMicroTime();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCurrentTime() {
        return time();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCurrentMicroTime() {
        return microtime(TRUE);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Time::$requestStack protected property The request stack.
Time::getCurrentMicroTime public function Returns the current system time with microsecond precision. Overrides TimeInterface::getCurrentMicroTime
Time::getCurrentTime public function Returns the current system time as an integer. Overrides TimeInterface::getCurrentTime
Time::getRequestMicroTime public function Returns the timestamp for the current request with microsecond precision. Overrides TimeInterface::getRequestMicroTime
Time::getRequestTime public function Returns the timestamp for the current request. Overrides TimeInterface::getRequestTime 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.