-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sleep.php
47 lines (38 loc) · 790 Bytes
/
Sleep.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php declare(strict_types=1);
namespace h4kuna\DataType\Date;
use Nette\StaticClass;
final class Sleep
{
use StaticClass;
/**
* @param int<0, max> $duration like 500 as half second
*/
public static function milliseconds(int $duration): void
{
if ($duration > 0) {
if ($duration > 999) {
self::seconds($duration / 1_000.0);
} else {
self::sleep($duration);
}
}
}
/**
* @param float $duration like 0.5 as half second
*/
public static function seconds(float $duration): void
{
$seconds = (int) $duration;
if ($seconds > 0) {
sleep($seconds);
}
$milli = $duration - $seconds;
if ($milli > 0) {
self::sleep($milli * 1_000.0);
}
}
private static function sleep(int|float $milli): void
{
usleep((int) ($milli * 1_000.0));
}
}