You are here

class Time in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Component/Datetime/Time.php \Drupal\Component\Datetime\Time
  2. 10 core/modules/views/src/Plugin/views/cache/Time.php \Drupal\views\Plugin\views\cache\Time
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Datetime/Time.php \Drupal\Component\Datetime\Time
  2. 9 core/lib/Drupal/Component/Datetime/Time.php \Drupal\Component\Datetime\Time

Provides a class for obtaining system time.

Hierarchy

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

Expanded class hierarchy of Time

1 file declares its use of Time
TimeTest.php in core/tests/Drupal/Tests/Component/Datetime/TimeTest.php
2 string references to 'Time'
core.services.yml in core/core.services.yml
core/core.services.yml
Datetime::processDatetime in core/lib/Drupal/Core/Datetime/Element/Datetime.php
Expands a datetime element type into date and/or time elements.
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