Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
68.42% |
26 / 38 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
DateIntervalMS | |
68.42% |
26 / 38 |
|
40.00% |
2 / 5 |
22.09 | |
0.00% |
0 / 1 |
format | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
microsecondsToSeconds | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
secondsToMicroseconds | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getLegacySpec | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
9.05 | |||
__construct | |
52.94% |
9 / 17 |
|
0.00% |
0 / 1 |
3.94 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | * |
21 | */ |
22 | |
23 | namespace oat\tao\helpers; |
24 | |
25 | /** |
26 | * Helps manipulate intervals of dates with microseconds, extended from built in DateInterval |
27 | * |
28 | * Class DateIntervalMS |
29 | * @author Ivan Klimchuk <klimchuk@1pt.com> |
30 | * @package oat\tao\helpers |
31 | */ |
32 | class DateIntervalMS extends \DateInterval |
33 | { |
34 | public $u = 0; |
35 | |
36 | protected static $interval_spec_regex = "/ |
37 | ^ ## start of the string |
38 | P ## first character must be a P |
39 | (?:(?P<y>\d+)Y)? ## year |
40 | (?:(?P<m>\d+)M)? ## month |
41 | (?:(?P<d>\d+)D)? ## day |
42 | (?:T ## T delineates between day and time information |
43 | (?:(?P<h>\d+)H)? ## hour |
44 | (?:(?P<i>\d+)M)? ## minute |
45 | (?:(?P<s>\d+(?:\.\d+)?)S)? ## seconds as float. |
46 | )? ## closes 'T' subexpression |
47 | $ ## end of the string |
48 | /x"; |
49 | |
50 | /** |
51 | * Formatting for current date interval |
52 | * @param $format |
53 | * @return string |
54 | */ |
55 | public function format($format) |
56 | { |
57 | $format = str_replace('%U', sprintf("%06d", $this->u), $format); |
58 | $format = str_replace('%u', sprintf("%d", intval($this->u)), $format); |
59 | |
60 | return parent::format($format); |
61 | } |
62 | |
63 | /** |
64 | * Convert microseconds to seconds in float type |
65 | * @param $microseconds |
66 | * @return float |
67 | */ |
68 | public static function microsecondsToSeconds($microseconds) |
69 | { |
70 | $microseconds = intval($microseconds); |
71 | $seconds = round($microseconds / 1000000, 6); |
72 | |
73 | return $seconds; |
74 | } |
75 | |
76 | /** |
77 | * Convert seconds with microseconds as fraction to amount of microseconds |
78 | * @param $seconds |
79 | * @return int |
80 | */ |
81 | public static function secondsToMicroseconds($seconds) |
82 | { |
83 | $seconds = round($seconds, 6); |
84 | $microseconds = intval($seconds * 1000000); |
85 | |
86 | return $microseconds; |
87 | } |
88 | |
89 | /** |
90 | * Build valid DateInterval format |
91 | * @param array $parts |
92 | * @return string |
93 | */ |
94 | private static function getLegacySpec(array $parts) |
95 | { |
96 | $spec = "P"; |
97 | $spec .= $parts['y'] !== "" ? "{$parts['y']}Y" : ""; |
98 | $spec .= $parts['m'] !== "" ? "{$parts['m']}M" : ""; |
99 | $spec .= $parts['d'] !== "" ? "{$parts['d']}D" : ""; |
100 | if ($parts['h'] . $parts['i'] . $parts['s'] !== "") { |
101 | $spec .= "T"; |
102 | $spec .= $parts['h'] !== "" ? "{$parts['h']}H" : ""; |
103 | $spec .= $parts['i'] !== "" ? "{$parts['i']}M" : ""; |
104 | $spec .= $parts['s'] !== "" ? "{$parts['s']}S" : ""; |
105 | } |
106 | if ($spec === "P") { |
107 | $spec = ""; |
108 | } |
109 | |
110 | return $spec; |
111 | } |
112 | |
113 | /** |
114 | * Custom construct with support microseconds |
115 | * @param string $interval_spec |
116 | */ |
117 | public function __construct($interval_spec) |
118 | { |
119 | if (!preg_match(static::$interval_spec_regex, $interval_spec, $parts)) { |
120 | throw new \UnexpectedValueException( |
121 | sprintf( |
122 | "%s::%s: Unknown or bad format (%s)", |
123 | get_called_class(), |
124 | '__construct', |
125 | $interval_spec |
126 | ) |
127 | ); |
128 | } |
129 | |
130 | if (isset($parts['s'])) { |
131 | $preciseSeconds = floatval($parts['s']); |
132 | $microseconds = static::secondsToMicroseconds(fmod($preciseSeconds, 1.0)); |
133 | $seconds = floor($preciseSeconds); |
134 | $this->u = $microseconds; |
135 | $parts['s'] = $seconds; |
136 | } |
137 | |
138 | $legacy_spec = static::getLegacySpec($parts); |
139 | |
140 | parent::__construct($legacy_spec); |
141 | } |
142 | } |