You are here

public function TimeChanger::getCurrentTime in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 modules/content_lock_timeout/tests/modules/content_lock_timeout_test/src/TimeChanger.php \Drupal\content_lock_timeout_test\TimeChanger::getCurrentTime()

Returns the current system time as an integer.

This method should be used to obtain the current system time, at the time the method was called.

This method can replace many instances of

$time = time();

with

$request_time = \Drupal::time()
  ->getCurrentTime();

or the equivalent using the injected service.

This method should only be used when the current system time is actually needed, such as with timers or time interval calculations. If only the time at the start of the request is needed, use TimeInterface::getRequestTime().

Using the time service, rather than other methods, is especially important when creating tests, which require predictable timestamps.

Return value

int A Unix timestamp.

Overrides Time::getCurrentTime

See also

\Drupal\Component\Datetime\TimeInterface::getRequestTime()

\Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()

\Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()

File

modules/content_lock_timeout/tests/modules/content_lock_timeout_test/src/TimeChanger.php, line 19

Class

TimeChanger
This is a datetime.time service for testing only.

Namespace

Drupal\content_lock_timeout_test

Code

public function getCurrentTime() {
  $time = \Drupal::keyValue('time')
    ->get('time', NULL);
  if (!empty($time)) {
    return $time;
  }
  return parent::getCurrentTime();
}