Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of phpunit/php-timer.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianBergmann\Timer;
11
 
12
use function floor;
13
use function sprintf;
14
 
15
/**
16
 * @psalm-immutable
17
 */
18
final class Duration
19
{
20
    /**
21
     * @var float
22
     */
23
    private $nanoseconds;
24
 
25
    /**
26
     * @var int
27
     */
28
    private $hours;
29
 
30
    /**
31
     * @var int
32
     */
33
    private $minutes;
34
 
35
    /**
36
     * @var int
37
     */
38
    private $seconds;
39
 
40
    /**
41
     * @var int
42
     */
43
    private $milliseconds;
44
 
45
    public static function fromMicroseconds(float $microseconds): self
46
    {
47
        return new self($microseconds * 1000);
48
    }
49
 
50
    public static function fromNanoseconds(float $nanoseconds): self
51
    {
52
        return new self($nanoseconds);
53
    }
54
 
55
    private function __construct(float $nanoseconds)
56
    {
57
        $this->nanoseconds     = $nanoseconds;
58
        $timeInMilliseconds    = $nanoseconds / 1000000;
59
        $hours                 = floor($timeInMilliseconds / 60 / 60 / 1000);
60
        $hoursInMilliseconds   = $hours * 60 * 60 * 1000;
61
        $minutes               = floor($timeInMilliseconds / 60 / 1000) % 60;
62
        $minutesInMilliseconds = $minutes * 60 * 1000;
63
        $seconds               = floor(($timeInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds) / 1000);
64
        $secondsInMilliseconds = $seconds * 1000;
65
        $milliseconds          = $timeInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds - $secondsInMilliseconds;
66
        $this->hours           = (int) $hours;
67
        $this->minutes         = $minutes;
68
        $this->seconds         = (int) $seconds;
69
        $this->milliseconds    = (int) $milliseconds;
70
    }
71
 
72
    public function asNanoseconds(): float
73
    {
74
        return $this->nanoseconds;
75
    }
76
 
77
    public function asMicroseconds(): float
78
    {
79
        return $this->nanoseconds / 1000;
80
    }
81
 
82
    public function asMilliseconds(): float
83
    {
84
        return $this->nanoseconds / 1000000;
85
    }
86
 
87
    public function asSeconds(): float
88
    {
89
        return $this->nanoseconds / 1000000000;
90
    }
91
 
92
    public function asString(): string
93
    {
94
        $result = '';
95
 
96
        if ($this->hours > 0) {
97
            $result = sprintf('%02d', $this->hours) . ':';
98
        }
99
 
100
        $result .= sprintf('%02d', $this->minutes) . ':';
101
        $result .= sprintf('%02d', $this->seconds);
102
 
103
        if ($this->milliseconds > 0) {
104
            $result .= '.' . sprintf('%03d', $this->milliseconds);
105
        }
106
 
107
        return $result;
108
    }
109
}