Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Time.php

class Time

Same name in this branch
  1. 11.1.x vendor/ramsey/uuid/src/Type/Time.php \Ramsey\Uuid\Type\Time
  2. 11.1.x vendor/symfony/validator/Constraints/Time.php \Symfony\Component\Validator\Constraints\Time
  3. 11.1.x core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\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

2 files declare their use of Time
NormalInstallerServiceProvider.php in core/lib/Drupal/Core/Installer/NormalInstallerServiceProvider.php
StorageComparer.php in core/lib/Drupal/Core/Config/StorageComparer.php
27 string references to 'Time'
BuildInformation::setBuildTime in vendor/phpunit/php-code-coverage/src/Report/Xml/BuildInformation.php
claro_preprocess_input in core/themes/claro/claro.theme
Implements template_preprocess_HOOK() for input.
DateRangeDefaultWidget::formElement in core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php
Returns the form for a single field widget.
Datetime::getHtml5TimeFormat in core/lib/Drupal/Core/Datetime/Element/Datetime.php
Retrieves the right format for an HTML5 time element.
Datetime::getInfo in core/lib/Drupal/Core/Datetime/Element/Datetime.php
Returns the element properties for this element.

... See full list

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 Overriden Title
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 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::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 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
Time::__construct public function Constructs a Time object.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal