diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 2294d47..0c1115c 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -82,6 +82,9 @@ * * @see http://php.net/manual/reserved.variables.server.php * @see http://php.net/manual/function.time.php + * + * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. + * Use \Drupal::time()->getRequestTime(); */ define('REQUEST_TIME', (int) $_SERVER['REQUEST_TIME']); diff --git a/core/lib/Drupal/Component/Datetime/Time.php b/core/lib/Drupal/Component/Datetime/Time.php index 8697bb1..969843c 100644 --- a/core/lib/Drupal/Component/Datetime/Time.php +++ b/core/lib/Drupal/Component/Datetime/Time.php @@ -33,6 +33,13 @@ public function getRequestTime() { /** * @inheritdoc */ + public function getRequestMicroTime() { + return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME_FLOAT'); + } + + /** + * @inheritdoc + */ public function getCurrentTime() { return time(); } diff --git a/core/lib/Drupal/Component/Datetime/TimeInterface.php b/core/lib/Drupal/Component/Datetime/TimeInterface.php index dce752e..18a4b7b 100644 --- a/core/lib/Drupal/Component/Datetime/TimeInterface.php +++ b/core/lib/Drupal/Component/Datetime/TimeInterface.php @@ -13,6 +13,14 @@ public function getRequestTime(); /** + * Returns the timestamp for the current request with microsecond precision. + * + * @return float + * A Unix timestamp with a factional portion. + */ + public function getRequestMicroTime(); + + /** * Returns the current system time as an integer. * * @return int @@ -21,7 +29,7 @@ public function getRequestTime(); public function getCurrentTime(); /** - * Returns the current system time as a float. + * Returns the current system time with microsecond precision. * * @return float * A Unix timestamp with a factional portion. diff --git a/core/tests/Drupal/Tests/Component/Datetime/TimeTest.php b/core/tests/Drupal/Tests/Component/Datetime/TimeTest.php index 22ffed7..c856e4f 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/TimeTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/TimeTest.php @@ -28,7 +28,7 @@ function microtime() { /** * Set up the real namespace for the test. */ -namespace Drupal\Tests\Core\Datetime { +namespace Drupal\Tests\Component\Datetime { use Drupal\Component\Datetime\Time; use Drupal\Tests\UnitTestCase; @@ -83,6 +83,25 @@ public function testGetRequestTime() { } /** + * Tests the getRequestMicroTime method. + * + * @covers ::getRequestMicroTime + */ + public function testGetRequestMicroTime() { + $expected = 1234567.89; + + $request = Request::createFromGlobals(); + $request->server->set('REQUEST_TIME_FLOAT', $expected); + + // Mocks a the request stack getting the current request. + $this->requestStack->expects($this->any()) + ->method('getCurrentRequest') + ->willReturn($request); + + $this->assertEquals($expected, $this->time->getRequestMicroTime()); + } + + /** * Tests the getCurrentTime method. * * @covers ::getCurrentTime