1 : <?php
2 :
3 : /**
4 : * XSLT Benchmarking
5 : * @link https://github.com/masicek/XSLT-Benchmarking
6 : * @author Viktor Mašíček <viktor@masicek.net>
7 : * @license "New" BSD License
8 : */
9 :
10 : namespace XSLTBenchmarking;
11 :
12 : /**
13 : * Static class for working with microtime with sufficient precision
14 : *
15 : * @author Viktor Mašíček <viktor@masicek.net>
16 : */
17 : class Microtime
18 : {
19 :
20 : /**
21 : * Default scale of time decimal precision
22 : */
23 : const SCALE = 6;
24 :
25 :
26 : /**
27 : * Make this class static
28 : *
29 : * @codeCoverageIgnore
30 : */
31 : private function __construct()
32 : {
33 : }
34 :
35 :
36 : /**
37 : * Get current time stamp with sufficient precision
38 : *
39 : * @param int $scale Scale of time decimal precision
40 : *
41 : * @return string
42 : */
43 : public static function now($scale = self::SCALE)
44 : {
45 1 : list($usec, $sec) = explode(' ', microtime());
46 1 : return bcadd($sec, $usec, $scale);
47 : }
48 :
49 :
50 : /**
51 : * Return microtime in humanreadable form
52 : *
53 : * @param string $microtime Timestamp returned by another function in this class
54 : *
55 : * @return string
56 : */
57 : public static function humanReadable($microtime)
58 : {
59 1 : list($sec, $usec) = explode('.', $microtime);
60 :
61 1 : $days = floor($sec / 86400);
62 :
63 1 : $sec -= $days * 86400;
64 1 : $hours = floor($sec / 3600);
65 :
66 1 : $sec -= $hours * 3600;
67 1 : $minutes = floor($sec / 60);
68 :
69 1 : $sec -= $minutes * 60;
70 :
71 1 : if (strlen($hours) == 1)
72 1 : {
73 1 : $hours = '0' . $hours;
74 1 : }
75 1 : if (strlen($minutes) == 1)
76 1 : {
77 1 : $minutes = '0' . $minutes;
78 1 : }
79 1 : if (strlen($sec) == 1)
80 1 : {
81 1 : $sec = '0' . $sec;
82 1 : }
83 :
84 1 : $result = $hours . ':' . $minutes . ':' . $sec . '.' . $usec;
85 :
86 : if ($days)
87 1 : {
88 1 : $result = $days . 'days ' . $result;
89 1 : }
90 :
91 1 : return $result;
92 : }
93 :
94 :
95 : /**
96 : * Get substrast of time stamps with sufficient precision
97 : *
98 : * @param string $leftOperand Left operand
99 : * @param string $rightOperand Right operand
100 : * @param int $scale Scale of time decimal precision
101 : *
102 : * @return string = $leftOperand - $rightOperand
103 : */
104 : public static function substract($leftOperand, $rightOperand, $scale = self::SCALE)
105 : {
106 1 : return bcsub($leftOperand, $rightOperand, $scale);
107 : }
108 :
109 :
110 : /**
111 : * Sum all arguments
112 : *
113 : * @param array $operands List of operands for sum
114 : * @param int $scale Scale of time decimal precision
115 : *
116 : * @return string
117 : */
118 : public static function sum($operands, $scale = self::SCALE)
119 : {
120 1 : if (count($operands) <= 0)
121 1 : {
122 1 : return '0.' . str_repeat('0', $scale);
123 : }
124 :
125 1 : $result = array_shift($operands);
126 :
127 1 : foreach ($operands as $operand)
128 : {
129 1 : $result = bcadd($result, $operand, $scale);
130 1 : }
131 :
132 1 : return $result;
133 : }
134 :
135 :
136 : /**
137 : * Get division of time stamps with sufficient precision
138 : *
139 : * @param string $leftOperand Left operand
140 : * @param string $rightOperand Right operand
141 : * @param int $scale Scale of time decimal precision
142 : *
143 : * @return string = $leftOperand / $rightOperand
144 : */
145 : public static function divide($leftOperand, $rightOperand, $scale = self::SCALE)
146 : {
147 1 : return bcdiv($leftOperand, $rightOperand, $scale);
148 : }
149 :
150 :
151 : }
|