@@ -20,19 +20,19 @@ Michele Orselli mo@ideato.it
20
20
*
21
21
* @return bool True if the absolute difference is less than or equal to the tolerance, false otherwise.
22
22
*/
23
- function isWithinTolerance (float $ val , float $ target , float $ tolerance ): bool {
23
+ function isWithinTolerance (float $ val , float $ target , float $ tolerance ): bool
24
+ {
24
25
return abs ($ val - $ target ) <= $ tolerance ;
25
26
}
26
27
28
+ $ startTime = microtime (true );
27
29
// Calculate the target sleep time (2 seconds from now)
28
- $ targetTime = microtime (true ) + 2 ;
29
- $ sleepUntil = $ targetTime ;
30
-
31
- // Sleep until the target time and output the result of time_sleep_until()
32
- var_dump (time_sleep_until ($ sleepUntil ));
33
-
30
+ $ targetTime = $ startTime + 2 ;
31
+ // Sleep until the target time
32
+ $ dump = time_sleep_until ($ targetTime );
34
33
// Capture the current time immediately after sleep
35
- $ currentTime = microtime (true );
34
+ $ timeAfterSleep = microtime (true );
35
+ var_dump ($ dump );
36
36
if (stripos (PHP_OS , 'WIN ' ) === 0 ) {
37
37
// on windows, time_sleep_until has millisecond accuracy while microtime() is accurate
38
38
// to 10th of a second. this means there can be up to a .9 millisecond difference
@@ -45,18 +45,24 @@ if (stripos(PHP_OS, 'WIN') === 0) {
45
45
$ tolerance = 0.05 ;
46
46
} elseif (stripos (PHP_OS , 'DARWIN ' ) === 0 ) {
47
47
// macOS: time_sleep_until() may wake up slightly early for unknown reasons. Allow a larger tolerance.
48
- $ tolerance = 0.005 ;
48
+ $ tolerance = 0.02 ;
49
49
} else {
50
50
// Default tolerance
51
- $ tolerance = 0.004 ;
51
+ $ tolerance = 0.01 ;
52
52
}
53
53
54
- if (isWithinTolerance ($ currentTime , $ sleepUntil , $ tolerance )) {
54
+ if (1 && isWithinTolerance ($ timeAfterSleep , $ targetTime , $ tolerance )) {
55
55
echo "Success " . PHP_EOL ;
56
56
} else {
57
- echo "Sleep until (before truncation): {$ targetTime }" . PHP_EOL ;
58
- echo "Sleep until: {$ sleepUntil }" . PHP_EOL ;
59
- echo "Now: {$ currentTime }" . PHP_EOL ;
57
+ echo "Failure " . PHP_EOL ;
58
+ var_dump ([
59
+ "startTime " => $ startTime ,
60
+ "targetTime " => $ targetTime ,
61
+ "timeAfterSleep " => $ timeAfterSleep ,
62
+ "diff " => $ timeAfterSleep - $ targetTime ,
63
+ "tolerance " => $ tolerance ,
64
+ "distanceFromTarget " => abs ($ timeAfterSleep - $ targetTime ),
65
+ ]);
60
66
}
61
67
?>
62
68
--EXPECT--
0 commit comments