Skip to content
This repository was archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
Fix microtime multiply micro instead of nano (fixed #1)
Browse files Browse the repository at this point in the history
Signed-off-by: Charlotte Dunois <[email protected]>
  • Loading branch information
leoloso authored and Charlotte Dunois committed Apr 1, 2020
1 parent adaed6a commit 9536311
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ function hrtime_fallback(bool $get_as_number = false) {
static $timer;

if(!$timer) {
$timer = (int) (\microtime(true) * 1e6);
$timer = (int) (\microtime(true) * 1e9);
}

$now = (int) (\microtime(true) * 1e6);
$now = (int) (\microtime(true) * 1e9);
$nanoseconds = (int) ($now - $timer);

if($get_as_number) {
return $nanoseconds;
}

return internal_conversion_nanoseconds_to_seconds($nanoseconds);
}
}
20 changes: 5 additions & 15 deletions tests/HrtimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,20 @@
class HrtimeTest extends TestCase {
function providerTestFunctions() {
return array(
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv', false),
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime', false),
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback', true)
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv'),
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime'),
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback')
);
}

/**
* @dataProvider providerTestFunctions
* @param callable $function
* @param bool $sleep
*/
function testHrtimeAsNumber(callable $function, bool $sleep) {
function testHrtimeAsNumber(callable $function) {
$number = $function(true);
$this->assertIsInt($number);

if($sleep) {
\sleep(1);
}

$number2 = $function(true);
$this->assertIsInt($number2);

Expand All @@ -45,17 +40,12 @@ function testHrtimeAsNumber(callable $function, bool $sleep) {
/**
* @dataProvider providerTestFunctions
* @param callable $function
* @param bool $sleep
*/
function testHrtimeAsArray(callable $function, bool $sleep) {
function testHrtimeAsArray(callable $function) {
$arr = $function(false);
$this->assertIsArray($arr);
$this->assertCount(2, $arr);

if($sleep) {
\sleep(1);
}

$arr2 = $function(false);
$this->assertIsArray($arr2);
$this->assertCount(2, $arr2);
Expand Down

0 comments on commit 9536311

Please sign in to comment.