Overview
Namespaces
Classes
Exceptions
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: list($usec, $sec) = explode(' ', microtime());
46: 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: list($sec, $usec) = explode('.', $microtime);
60:
61: $days = floor($sec / 86400);
62:
63: $sec -= $days * 86400;
64: $hours = floor($sec / 3600);
65:
66: $sec -= $hours * 3600;
67: $minutes = floor($sec / 60);
68:
69: $sec -= $minutes * 60;
70:
71: if (strlen($hours) == 1)
72: {
73: $hours = '0' . $hours;
74: }
75: if (strlen($minutes) == 1)
76: {
77: $minutes = '0' . $minutes;
78: }
79: if (strlen($sec) == 1)
80: {
81: $sec = '0' . $sec;
82: }
83:
84: $result = $hours . ':' . $minutes . ':' . $sec . '.' . $usec;
85:
86: if ($days)
87: {
88: $result = $days . 'days ' . $result;
89: }
90:
91: 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: 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: if (count($operands) <= 0)
121: {
122: return '0.' . str_repeat('0', $scale);
123: }
124:
125: $result = array_shift($operands);
126:
127: foreach ($operands as $operand)
128: {
129: $result = bcadd($result, $operand, $scale);
130: }
131:
132: 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: return bcdiv($leftOperand, $rightOperand, $scale);
148: }
149:
150:
151: }
152: