blib/lib/Time/HiRes/Value.pm

filter: Statementstmt 98.9% 95 / 96 Branchbran 94.7% 36 / 38 Conditioncond 75.0% 18 / 24 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 17 / 17 Podpod 88.8% 8 / 9 totaltotal 92.0% 184 / 200 CC 28 SCAR 13.5
CC
28
Cov
94%
_split_sec_usec
6.0
new
5.1
STRING
5.0
CRAP 3.9
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2006-2015 -- leonerd@leonerd.org.uk
5  
6 package Time::HiRes::Value;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Time::HiRes qw( gettimeofday );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use POSIX qw( floor );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '0.08';
17  
18 # Since we use this number quite a lot, make a constant out of it to avoid
19 # typoes
20 2 ▶use constant USEC => 1_000_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 C<Time::HiRes::Value> - represent a time value or interval in exact
25 microseconds
26  
27 =head1 DESCRIPTION
28  
29 The L<Time::HiRes> module allows perl to access the system's clock to
30 microsecond accuracy. However, floating point numbers are not suitable for
31 manipulating such time values, as rounding errors creep in to calculations
32 performed on floating-point representations of UNIX time. This class provides
33 a solution to this problem, by storing the seconds and miliseconds in separate
34 integer values, in an array. In this way, the value can remain exact, and no
35 rounding errors result.
36  
37 =cut
38  
39 # Internal helpers
40 sub _split_sec_usec($)
41 43 ▶{
Subroutine _split_sec_usec: called CC=6 SCAR=17.9
42 43 my ( $t ) = @_;
43  
44 43 my $negative = 0;
45 43 ▶ if( $t =~ s/^-// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ s/^-//) 7 36 43
46 7 $negative = 1;
47 }
48  
49 43 my ( $sec, $usec );
50  
51 # Try not to use floating point maths because that loses too much precision
52 43 ▶ if( $t =~ m/^(\d+)\.(\d+)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t =~ /^(\d+)\.(\d+)$/) { } 22 21 43
elsif ($t =~ /^(\d+)$/) { } 19 2 21
53 22 $sec = $1;
54 22 $usec = $2;
55  
56 # Pad out to 6 digits
57 22 $usec .= "0" x ( 6 - length( $usec ) );
58 }
59 elsif( $t =~ m/^(\d+)$/ ) {
60 # Plain integer
61 19 $sec = $1;
62 19 $usec = 0;
63 }
64 else {
65 2 croak "Cannot convert string '$t' into a " . __PACKAGE__;
66 }
67  
68 41 ▶ if( $negative ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 7 34 41
69 7 ▶ if( $usec != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usec != 0) { } 5 2 7
70 5 $sec = -$sec - 1;
71 5 $usec = USEC - $usec;
72 }
73 else {
74 2 $sec = -$sec;
75 }
76 }
77  
78 41 return [ $sec, $usec ];
79 }
80  
81 =head1 FUNCTIONS
82  
83 =cut
84  
85 =head2 $time = Time::HiRes::Value->new( $sec, $usec )
86  
87 This function returns a new instance of a C<Time::HiRes::Value> object. This
88 object is immutable, and represents the time passed in to the C<I<$sec>> and
89 C<I<$usec>> parameters.
90  
91 If the C<I<$usec>> value is provided then the new C<Time::HiRes::Value> object
92 will store the values passed directly, which must both be integers. Negative
93 values are represented in "additive" form; that is, a value of C<-1.5> seconds
94 would be represented by
95  
96 Time::HiRes::Value->new( -2, 500000 );
97  
98 If the C<I<$usec>> value is not provided, then the C<I<$sec>> value will be
99 parsed as a decimal string, attempting to match out a decimal point to split
100 seconds and microseconds. This method avoids rounding errors introduced by
101 floating-point maths.
102  
103 =cut
104  
105 sub new
106 39 ▶{
Subroutine new: called CC=5 SCAR=16.2
Pod documented
107 39 my $class = shift;
108  
109 39 my ( $sec, $usec );
110  
111 39 ▶ if( @_ == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 32 7 39
elsif (@_ == 1) { } 7 0 7
112 32 ▶ croak "Cannot accept '$_[0]' for seconds for a " . __PACKAGE__ unless $_[0] =~ m/^[+-]?\d+(?:\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^[+-]?\d+(?:\.\d+)?$/ 1 31 32
113 31 ▶ croak "Cannot accept '$_[1]' for microseconds for a " . __PACKAGE__ unless $_[1] =~ m/^[+-]?\d+(?:\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ /^[+-]?\d+(?:\.\d+)?$/ 0 31 31
114  
115 31 ( $sec, $usec ) = @_;
116 }
117 elsif( @_ == 1 ) {
118 7 ( $sec, $usec ) = @{ _split_sec_usec( $_[0] ) };
119 }
120 else {
121 0 carp "Bad number of elements in \@_";
122 }
123  
124 # Handle case where $sec is non-integer
125 36 $usec += USEC * ( $sec - int( $sec ) );
126 36 $sec = int( $sec );
127  
128 # Move overflow from $usec into $sec
129 36 $sec += floor( $usec / USEC );
130 36 $usec %= USEC;
131  
132 36 my $self = [ $sec, $usec ];
133  
134 36 return bless $self, $class;
135 }
136  
137 =head2 $time = Time::HiRes::Value->now()
138  
139 This function returns a new instance of C<Time::HiRes::Value> containing the
140 current system time, as returned by the system's C<gettimeofday()> call.
141  
142 =cut
143  
144 sub now
145 2 ▶{
Subroutine now: called CC=1 SCAR=0.0
Pod documented
146 2 my $class = shift;
147 2 my @now = gettimeofday();
148 2 return $class->new( @now );
149 }
150  
151 2 ▶use overload '""' => \&STRING,
Subroutine BEGIN: called CC=1 SCAR=0.0
152 '0+' => \&NUMBER,
153 '+' => \&add,
154 '-' => \&sub,
155 '*' => \&mult,
156 '/' => \&div,
157 2 '<=>' => \&cmp;
158  
159 =head1 OPERATORS
160  
161 Each of the methods here overloads an operator
162  
163 =cut
164  
165 =head2 $self->STRING()
166  
167 =head2 "$self"
168  
169 This method returns a string representation of the time, in the form of a
170 decimal string with 6 decimal places. For example
171  
172 15.000000
173 -3.000000
174 4.235996
175  
176 A leading C<-> sign will be printed if the stored time is negative, and the
177 C<I<$usec>> part will always contain 6 digits.
178  
179 =cut
180  
181 sub STRING
182 6 ▶{
Subroutine STRING: called CC=5 SCAR=16.1
Pod documented
183 6 my $self = shift;
184 6 ▶ if( $self->[0] < -1 && $self->[1] != 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[0] < -1 and $self->[1] != 0) { } 1 5 6
elsif ($self->[0] == -1 and $self->[1] != 0) { } 1 4 5
Condition100%3/3
$self->[0] < -1 and $self->[1] != 0
!ll&&!rl&&r
411
MC/DC100%2/2
$self->[0] < -1 and $self->[1] != 0
$self->[0] < -1 $self->[1] != 0
Truth table100%3/3
A and B
A= $self->[0] < -1B= $self->[1] != 0
ABresult
0X0
100
111
185 # Fractional below -1.000000
186 1 return sprintf( '%d.%06d', $self->[0] + 1, USEC - $self->[1] );
187 }
188 ▶ elsif( $self->[0] == -1 && $self->[1] != 0 ) {
Condition66%2/3
$self->[0] == -1 and $self->[1] != 0
!ll&&!rl&&r
401
MC/DC50%1/2
$self->[0] == -1 and $self->[1] != 0
$self->[0] == -1 $self->[1] != 0
Truth table66%2/3
A and B
A= $self->[0] == -1B= $self->[1] != 0
ABresult
0X0
100
111
189 # Special case - between -1 and 0 need to handle the sign carefully
190 1 return sprintf( '-0.%06d', USEC - $self->[1] );
191 }
192 else {
193 4 return sprintf( '%d.%06d', $self->[0], $self->[1] );
194 }
195 }
196  
197 sub NUMBER
198 1 ▶{
Subroutine NUMBER: called CC=1 SCAR=0.0
Pod undocumented
199 1 my $self = shift;
200 1 return $self->[0] + ($self->[1] / USEC);
201 }
202  
203 =head2 $self->add( $other )
204  
205 =head2 $self->sum( $other )
206  
207 =head2 $self + $other
208  
209 This method returns a new C<Time::HiRes::Value> value, containing the sum of the
210 passed values. If a string is passed, it will be parsed according to the same
211 rules as for the C<new()> constructor.
212  
213 Note that C<sum> is provided as an alias to C<add>.
214  
215 =cut
216  
217 sub add
218 8 ▶{
Subroutine add: called CC=3 SCAR=11.0
Pod documented
219 8 my $self = shift;
220 8 my ( $other ) = @_;
221  
222 8 ▶ if( !ref( $other ) || !$other->isa( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $other and $other->isa(__PACKAGE__)) 7 1 8
Condition66%2/3
ref $other and $other->isa(__PACKAGE__)
!ll&&!rl&&r
701
MC/DC50%1/2
ref $other and $other->isa(__PACKAGE__)
ref $other $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $otherB= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
223 7 $other = _split_sec_usec( $other );
224 }
225  
226 8 return Time::HiRes::Value->new( $self->[0] + $other->[0], $self->[1] + $other->[1] );
227 }
228  
229 2 *sum = \&add;
230  
231 =head2 $self->sub( $other )
232  
233 =head2 $self->diff( $other )
234  
235 =head2 $self - $other
236  
237 This method returns a new C<Time::HiRes::Value> value, containing the difference
238 of the passed values. If a string is passed, it will be parsed according to
239 the same rules as for the C<new()> constructor.
240  
241 Note that C<diff> is provided as an alias to C<sub>.
242  
243 =cut
244  
245 sub sub
246 5 ▶{
Subroutine sub: called CC=4 SCAR=13.9
Pod documented
247 5 my $self = shift;
248 5 my ( $other, $swap ) = @_;
249  
250 5 ▶ if( !ref( $other ) || !$other->isa( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $other and $other->isa(__PACKAGE__)) 4 1 5
Condition66%2/3
ref $other and $other->isa(__PACKAGE__)
!ll&&!rl&&r
401
MC/DC50%1/2
ref $other and $other->isa(__PACKAGE__)
ref $other $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $otherB= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
251 4 $other = _split_sec_usec( $other );
252 }
253  
254 5 ▶ ( $self, $other ) = ( $other, $self ) if( $swap );
Branch: 1 branch
BranchTrueFalseTotal
if $swap 1 4 5
255  
256 5 return Time::HiRes::Value->new( $self->[0] - $other->[0], $self->[1] - $other->[1] );
257 }
258  
259 2 *diff = \&sub;
260  
261 =head2 $self->mult( $other )
262  
263 =head2 $self * $other
264  
265 This method returns a new C<Time::HiRes::Value> value, containing the product
266 of the passed values. C<$other> must not itself be a C<Time::HiRes::Value>
267 object; it is an error to attempt to multiply two times together.
268  
269 =cut
270  
271 sub mult
272 10 ▶{
Subroutine mult: called CC=3 SCAR=11.0
Pod documented
273 10 my $self = shift;
274 10 my ( $other ) = @_;
275  
276 10 ▶ if( ref( $other ) and $other->isa( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other and $other->isa(__PACKAGE__)) 1 9 10
Condition66%2/3
ref $other and $other->isa(__PACKAGE__)
!ll&&!rl&&r
901
MC/DC50%1/2
ref $other and $other->isa(__PACKAGE__)
ref $other $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $otherB= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
277 1 croak "Cannot multiply a ".__PACKAGE__." with another";
278 }
279  
280 9 return Time::HiRes::Value->new( $self->[0] * $other, $self->[1] * $other );
281 }
282  
283 =head2 $self->div( $other )
284  
285 =head2 $self / $other
286  
287 This method returns a new C<Time::HiRes::Value> value, containing the quotient
288 of the passed values. C<$other> must not itself be a C<Time::HiRes::Value>
289 object; it is an error for a time to be used as a divisor.
290  
291 =cut
292  
293 sub div
294 9 ▶{
Subroutine div: called CC=5 SCAR=16.1
Pod documented
295 9 my $self = shift;
296 9 my ( $other, $swap ) = @_;
297  
298 9 ▶ croak "Cannot divide a quantity by a ".__PACKAGE__ if $swap;
Branch: 1 branch
BranchTrueFalseTotal
if $swap 1 8 9
299  
300 8 ▶ if( ref( $other ) and $other->isa( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other and $other->isa(__PACKAGE__)) 1 7 8
Condition66%2/3
ref $other and $other->isa(__PACKAGE__)
!ll&&!rl&&r
701
MC/DC50%1/2
ref $other and $other->isa(__PACKAGE__)
ref $other $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $otherB= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
301 1 croak "Cannot divide a ".__PACKAGE__." by another";
302 }
303  
304 7 ▶ croak "Illegal division by zero" if $other == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $other == 0 1 6 7
305  
306 6 return Time::HiRes::Value->new( $self->[0] / $other, $self->[1] / $other );
307 }
308  
309 =head2 $self->cmp( $other )
310  
311 =head2 $self <=> $other
312  
313 This method compares the two passed values, and returns a number that is
314 positive, negative or zero, as per the usual rules for the C<< <=> >>
315 operator. If a string is passed, it will be parsed according to the same
316 rules as for the C<new()> constructor.
317  
318 =cut
319  
320 sub cmp
321 33 ▶{
Subroutine cmp: called CC=4 SCAR=13.9
Pod documented
322 33 my $self = shift;
323 33 my ( $other ) = @_;
324  
325 33 ▶ if( !ref( $other ) || !$other->isa( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $other and $other->isa(__PACKAGE__)) 25 8 33
Condition66%2/3
ref $other and $other->isa(__PACKAGE__)
!ll&&!rl&&r
2508
MC/DC50%1/2
ref $other and $other->isa(__PACKAGE__)
ref $other $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $otherB= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
326 25 $other = _split_sec_usec( $other );
327 }
328  
329 33 ▶ return $self->[0] <=> $other->[0] ||
Condition100%3/3
$self->[0] <=> $other->[0] || $self->[1] <=> $other->[1]
l!l&&r!l&&!r
4227
MC/DC100%2/2
$self->[0] <=> $other->[0] || $self->[1] <=> $other->[1]
$self->[0] <=> $other->[0] $self->[1] <=> $other->[1]
Truth table100%3/3
A || B
A= $self->[0] <=> $other->[0]B= $self->[1] <=> $other->[1]
ABresult
1X1
011
000
330 $self->[1] <=> $other->[1];
331 }
332  
333 =head1 SEE ALSO
334  
335 =over 4
336  
337 =item *
338  
339 L<Time::HiRes> - Obtain system timers in resolution greater than 1 second
340  
341 =back
342  
343 =head1 AUTHOR
344  
345 Paul Evans <leonerd@leonerd.org.uk>
346  
347 =cut
348  
349 2 0x55AA;
↑ Summary