blib/lib/Time/Simple.pm

filter: Statementstmt 82.0% 128 / 156 Branchbran 56.7% 42 / 74 Conditioncond 66.6% 18 / 27 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 86.6% 26 / 30 Podpod 50.0% 6 / 12 totaltotal 71.8% 225 / 313 CC 49 SCAR 32.5
CC
49
Cov
73%
new
66.3
validate
8.1
_increment
6.0
CRAP 25.9
↑ Summary
1 package Time::Simple;
2  
3 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 our $VERSION = '0.06';
5 1 our $FATALS = 1;
6  
7 =head1 NAME
8  
9 Time::Simple - A simple, light-weight ISO 8601 time object.
10  
11 =head1 SYNOPSIS
12  
13 use Time::Simple;
14 my $time = Time::Simple->new('23:24:59');
15 my $hour = $time->hours;
16 my $minute = $time->minutes;
17 my $second = $time->seconds;
18  
19 my $time2 = Time::Simple->new($hour, $minute, $second);
20  
21 my $now = Time::Simple->new;
22 my $nexthour = $now + (60*60);
23 print "An hour from now is $nexthour.\n";
24  
25 if ($nexthour->hour > 23) {
26 print "It'll be tomorrow within the next hour!\n";
27 }
28  
29 # You can also do this:
30 ($time cmp "23:24:25")
31 # ...and this:
32 ($time <=> [23, 24, 25])
33  
34 $time++; # Add a second
35 $time--; # Subtract a second
36  
37 # Seconds of difference:
38 $seconds = Time::Simple->new("00:00:02")
39 - Time::Simple->new("00:00:01");
40  
41 my $now = Time::Simple->new;
42 # A minute from now:
43 my $then = Time::Simple->new( $now + 60 );
44 # Or:
45 my $soon = Time::Simple->new( '00:01:00' );
46  
47 =head1 DESCRIPTION
48  
49 A simple, light-weight time object.
50  
51 B<This version should be considered an alpha developer release>.
52  
53 How do you think this moudle should handle return values of multiplacation, where the
54 return value would be greater than 23:59:59?
55  
56 =head2 FATAL ERRORS
57  
58 Attempting to create an invalid time with this module will return C<undef> rather than an object.
59  
60 Some operations can produce fatal errors: these can be replaced by warnings and the
61 return of C<undef> by switching the value of C<$FATALS>:
62  
63 $Time::Simple::FATALS = undef;
64  
65 You will then only get warnings to C<STDERR>, and even then only if you asked perl for
66 warnings with C<use warnings> or by setting C<$^W> either directly or with the C<-w>
67 command-line switch.
68  
69 =head2 EXPORT
70  
71 None by default.
72  
73 =cut
74  
75 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use POSIX qw(strftime mktime);
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 use overload
81 1 ▶ '=' => '_copy',
Subroutine BEGIN: called CC=1 SCAR=0.0
82 '+=' => '_increment',
83 '++' => '_increment_mod',
84 '-=' => '_decrement',
85 '--' => '_decrement_mod',
86 '+' => '_add',
87 '-' => '_subtract',
88 '<=>' => '_compare',
89 'cmp' => '_compare',
90 '""' => '_stringify',
91 '*' => '_multiply',
92 '/' => '_divide',
93 # fallback=>1
94 1 ;
95  
96 =head1 METHODS
97  
98 =cut
99  
100 # http://rt.cpan.org/Public/Bug/Display.html?id=34710 :-
101 # Log: Make the isdst argument to asctime and mktime default to -1
102 # instead of 0, as suggested by Mike Schilli.
103 # Branch: perl
104 # ! ext/POSIX/POSIX.pm ext/POSIX/POSIX.pod ext/POSIX/POSIX.xs
105 1 ▶my $DST = $^V lt v5.10.0? 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^V lt v5.10.0 ? : 0 1 1
106  
107 =head2 CONSTRUCTOR (new)
108  
109 $_ = Time::Simple->new('21:10:09');
110 $_ = Time::Simple->new( 11,10, 9 );
111 Time::Simple->new() == Time::Simple->new( time() );
112  
113 The constructor C<new> returns a C<Time::Simple> object if the supplied
114 values specify a valid time, otherwise returns C<undef>.
115  
116 Valid times are either as supplied by the L<time|perlfunc/time>, or in ISO 8601
117 format. In the latter case, the values may be supplied as a colon-delimited scalar,
118 as a list, or as an anonymous array.
119  
120 If nothing is supplied to the constructor, the current local time will be used.
121  
122 =cut
123  
124 75 ▶sub new {
Subroutine new: called CC=28 SCAR=41.9
Pod undocumented
125 75 my ($that, @hms) = (@_);
126 75 my $time;
127  
128 75 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
0750
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
129  
130 # From time()
131 75 ▶ if (scalar(@hms) and
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @hms and $hms[0] =~ /^\d{10}$/g || scalar @hms == 10 || scalar @hms == 9) { } 2 73 75
elsif (@hms == 1) { } 62 11 73
Condition77%7/9
scalar @hms and $hms[0] =~ /^\d{10}$/g || scalar @hms == 10 || scalar @hms == 9
!ll&&!rl&&r
5682
$hms[0] =~ /^\d{10}$/g or scalar @hms == 10 or scalar @hms == 9
l!l&&r!l&&!r
2068
$hms[0] =~ /^\d{10}$/g or scalar @hms == 10
l!l&&r!l&&!r
2068
MC/DC50%2/4
scalar @hms and $hms[0] =~ /^\d{10}$/g || scalar @hms == 10 || scalar @hms == 9
scalar @hms $hms[0] =~ /^\d{10}$/g scalar @hms == 10 scalar @hms == 9
Truth table60%3/5
A and B or C or D
A= scalar @hmsB= $hms[0] =~ /^\d{10}$/gC= scalar @hms == 10D= scalar @hms == 9
ABCDresult
0XXX0
10000
11XX1
101X1
10011
132 ($hms[0] =~ /^\d{10}$/g or scalar(@hms) == 10 or scalar(@hms) == 9)
133 ) {
134 2 $time = join'',@hms;
135 2 ▶ if ($time =~ /\D/g){
Branch: 1 branch
BranchTrueFalseTotal
if ($time =~ /\D/g) 0 2 2
136 0 ▶ if ($FATALS){
Branch: 1 branch
BranchTrueFalseTotal
if ($Time::Simple::FATALS) { } 0 0 0
137 0 croak "Could not make a time from $time - please read the documentation";
138 } else {
139 0 ▶ Carp::cluck("Could not make a time from $time - please read the documentation") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
140 0 return undef;
141 }
142 }
143 }
144  
145 elsif (@hms == 1) {
146 62 ▶ if(ref $hms[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hms[0] eq "ARRAY") 8 54 62
147 8 @hms = join':',@{$hms[0]};
148 }
149 62 @hms = $hms[0] =~ /^(\d{1,2})(:\d{1,2})?(:\d{1,2})?$/;
150 62 ▶ $hms[1] ||= '00';
Condition100%2/2
$hms[1] ||= "00"
l!l
593
MC/DC100%1/1
$hms[1] ||= "00"
$hms[1]
Truth table100%2/2
A
A= $hms[1]
Aresult
11
00
151 62 ▶ $hms[2] ||= '00';
Condition100%2/2
$hms[2] ||= "00"
l!l
575
MC/DC100%1/1
$hms[2] ||= "00"
$hms[2]
Truth table100%2/2
A
A= $hms[2]
Aresult
11
00
152 62 s/^:// foreach @hms[1..2];
153 62 ▶ if (not defined $hms[0]){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hms[0]) 1 61 62
154 1 ▶ if ($FATALS){
Branch: 1 branch
BranchTrueFalseTotal
if ($Time::Simple::FATALS) { } 1 0 1
155 1 croak"'$_[1]' is not a valid ISO 8601 formated time" ;
156 } else {
157 0 ▶ Carp::cluck("'$_[1]' is not a valid ISO 8601 formated time") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
158 0 return undef;
159 }
160 }
161 }
162  
163 74 ▶ if (not defined $time){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 72 2 74
164 72 ▶ if (@hms == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if (@hms == 3) { } 67 5 72
elsif (@hms == 0) { } 5 0 5
elsif ($Time::Simple::FATALS) { } 0 0 0
165 67 ▶ unless (validate(@hms)){
Branch: 1 branch
BranchTrueFalseTotal
unless (&validate(@hms)) 1 66 67
166 1 ▶ if ($FATALS){
Branch: 1 branch
BranchTrueFalseTotal
if ($Time::Simple::FATALS) { } 1 0 1
167 1 croak "Could not make a time - please read the documentation";
168 } else {
169 0 ▶ Carp::cluck("Could not make a time - please read the documentation") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
170 0 return undef;
171 }
172 }
173  
174 # mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0/-1)
175 66 my @localtime = localtime;
176 66 ▶ $time = mktime (
Branch: 2 branches
BranchTrueFalseTotal
$localtime[8] ? : 0 66 66
$localtime[8] ? : 0 66 66
177 $hms[2],
178 $hms[1],
179 $hms[0] - ($localtime[8]? 1 : 0), # Daylight saving time xxx
180 $localtime[3],
181 $localtime[4],
182 $localtime[5],
183 0,0,
184 $localtime[8]? $DST : 0
185 );
186 66 ▶ confess 'Can not mktime' if not $time;
Branch: 1 branch
BranchTrueFalseTotal
unless $time 0 66 66
187 }
188  
189 elsif (@hms == 0) {
190 5 $time = time;
191 }
192  
193 elsif ($FATALS){
194 0 croak "Could not make a time - please read the documentation";
195 }
196  
197 else {
198 0 ▶ Carp::cluck("Could not make a time - please read the documentation") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
199 0 return undef;
200 }
201 }
202 73 return bless \$time, $class;
203 }
204  
205 1 ▶sub next { return $_[0] + 1 }
Subroutine next: called CC=1 SCAR=0.0
Pod documented
206 1 ▶sub prev { return $_[0] - 1 }
Subroutine prev: called CC=1 SCAR=0.0
Pod documented
207  
208 5 ▶sub _mktime_seconds($) {
Subroutine _mktime_seconds: called CC=1 SCAR=0.0
209 5 my $t = shift;
210 5 my $h = int( $t / (60*60));
211 5 my $m = int(($t % (60*60)) / 60);
212 5 my $s = int( $t % (60));
213 5 return $h, $m, $s;
214 }
215  
216 # Return the number of seoncds in time
217 5 ▶sub total_seconds($){
Subroutine total_seconds: called CC=1 SCAR=0.0
Pod undocumented
218 5 my $self = shift;
219 5 my ($sh, $sm, $ss) = $self =~ /^0?(\d+?).0?(\d+?).0?(\d+?)$/;
220 5 $ss += ($sm * 60) + ($sh * 60 * 60);
221 }
222  
223 8 ▶sub hour { return (localtime ${$_[0]})[2] }
Subroutine hour: called CC=1 SCAR=0.0
Pod documented
224 0 ▶sub hours { return (localtime ${$_[0]})[2] }
Subroutine hours: not called CC=1 SCAR=6.9
Pod undocumented
225  
226 8 ▶sub minute { return (localtime ${$_[0]})[1] }
Subroutine minute: called CC=1 SCAR=0.0
Pod documented
227 0 ▶sub minutes { return (localtime ${$_[0]})[1] }
Subroutine minutes: not called CC=1 SCAR=6.9
Pod undocumented
228  
229 6 ▶sub second { return (localtime ${$_[0]})[0] }
Subroutine second: called CC=1 SCAR=0.0
Pod documented
230 2 ▶sub seconds { return (localtime ${$_[0]})[0] }
Subroutine seconds: called CC=1 SCAR=0.0
Pod undocumented
231  
232 31 ▶sub format {
Subroutine format: called CC=4 SCAR=14.1
Pod documented
233 31 my $self = shift;
234 31 ▶ my $format = shift || '%H:%M:%S';
Condition50%1/2
shift() || '%H:%M:%S'
l!l
031
MC/DC0%0/1
shift() || '%H:%M:%S'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
235 # strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
236 31 my $test;
237 31 eval { $test = scalar localtime $$self };
238 31 ▶ Carp::confess("Invalid time ($$self)") unless defined $test;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test 0 31 31
239 31 my $return = eval {strftime($format, localtime($$self));};
240 31 ▶ Carp::confess "You supplied $$self: ".$@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 31 31
241 31 return $return;
242 }
243  
244 67 ▶sub validate ($$$) {
Subroutine validate: called CC=7 SCAR=20.9
Pod undocumented
245 67 my ($h, $m, $s)= @_;
246 67 foreach my $i (@_){
247 201 ▶ return 0 if $i != abs int $i or $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i != abs int $i or $i < 0 0 201 201
Condition33%1/3
$i != abs int $i or $i < 0
l!l&&r!l&&!r
00201
MC/DC0%0/2
$i != abs int $i or $i < 0
$i != abs int $i $i < 0
Truth table33%1/3
A or B
A= $i != abs int $iB= $i < 0
ABresult
1X1
011
000
248 }
249 67 ▶ return 0 if $h > 23
Branch: 1 branch
BranchTrueFalseTotal
if $h > 23 or $m > 59 or $s > 59 1 66 67
Condition66%4/6
$h > 23 or $m > 59 or $s > 59
l!l&&r!l&&!r
1066
$h > 23 or $m > 59
l!l&&r!l&&!r
1066
MC/DC33%1/3
$h > 23 or $m > 59 or $s > 59
$h > 23 $m > 59 $s > 59
Truth table50%2/4
A or B or C
A= $h > 23B= $m > 59C= $s > 59
ABCresult
1XX1
01X1
0011
0000
250 or $m > 59
251 or $s > 59;
252 66 return 1;
253 }
254  
255  
256 #------------------------------------------------------------------------------
257 # the following methods are called by the overloaded operators, so they should
258 # not normally be called directly.
259 #------------------------------------------------------------------------------
260 31 ▶sub _stringify { $_[0]->format }
Subroutine _stringify: called CC=1 SCAR=0.0
261  
262 14 ▶sub _copy {
Subroutine _copy: called CC=1 SCAR=0.0
263 14 my $self = shift;
264 14 my $v = $$self;
265 14 my $copy = \$v;
266 14 bless $copy, ref $self;
267 14 return $copy;
268 }
269  
270 0 ▶sub _increment {
Subroutine _increment: not called CC=2 SCAR=17.9
271 0 my ($self, $n) = @_;
272 0 ▶ if (UNIVERSAL::isa($n, ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($n, ref $self)) 0 0 0
273 0 $n = $$n;
274 }
275 0 my $copy = $self->_copy;
276 0 $$copy+= $n;
277 0 return $copy;
278 }
279  
280 3 ▶sub _increment_mod {
Subroutine _increment_mod: called CC=2 SCAR=7.0
281 3 my ($self, $n) = @_;
282 3 ▶ $n = $$n if UNIVERSAL::isa($n, ref($self));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($n, ref $self) 0 3 3
283 3 $$self ++;
284 3 return $self;
285 }
286  
287 0 ▶sub _decrement {
Subroutine _decrement: not called CC=2 SCAR=17.9
288 0 my ($self, $n, $reverse) = @_;
289 0 ▶ $n = $$n if UNIVERSAL::isa($n, ref($self));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($n, ref $self) 0 0 0
290 0 my $copy = $self->_copy;
291 0 $$copy -= $n;
292 0 return $copy;
293 }
294  
295 3 ▶sub _decrement_mod {
Subroutine _decrement_mod: called CC=2 SCAR=7.0
296 3 my ($self, $n, $reverse) = @_;
297 3 ▶ $n = $$n if UNIVERSAL::isa($n, ref($self));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($n, ref $self) 0 3 3
298 3 $$self --;
299 3 return $self;
300 }
301  
302 6 ▶sub _add {
Subroutine _add: called CC=2 SCAR=6.9
303 6 my ($self, $n, $reverse) = @_;
304 6 ▶ if (UNIVERSAL::isa($n, ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($n, ref $self)) 2 4 6
305 2 my $s = ($n->hour * 60 * 60)
306 + ($n->minute * 60)
307 + $n->seconds;
308 2 $n = $s;
309 }
310 6 my $copy = $self->_copy;
311 6 $$copy += $n;
312 6 return $copy;
313 }
314  
315 8 ▶sub _subtract {
Subroutine _subtract: called CC=4 SCAR=13.9
316 8 my ($self, $n, $reverse) = @_;
317 8 ▶ if (UNIVERSAL::isa($n, ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($n, ref $self)) { } 5 3 8
318 5 my $copy = $self->_copy;
319 5 my $diff = $$copy - $$n;
320 # $diff /= 86400;
321 # $reverse should probably always be false here, but...
322 5 ▶ $diff = -$diff if $diff < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $diff < 0 1 4 5
323 5 ▶ return $reverse ? -$diff : $diff;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 5 5
324 } else {
325 3 my $copy = $self->_copy;
326 3 $$copy -= $n;
327 3 return $copy;
328 }
329 }
330  
331 36 ▶sub _compare {
Subroutine _compare: called CC=3 SCAR=11.0
332 36 my ($self, $x, $reverse) = @_;
333 36 ▶ $x = ref($self)->new($x) unless UNIVERSAL::isa($x, ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($x, ref $self) 33 3 36
334 36 my $c = (int(${$self}) <=> int(${$x}));
335 36 ▶ return $reverse ? -$c : $c;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 1 35 36
336 }
337  
338  
339  
340 3 ▶sub _multiply {
Subroutine _multiply: called CC=2 SCAR=7.1
341 3 my ($self, $n, $reverse) = @_;
342  
343 3 ▶ if (UNIVERSAL::isa($n, ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($n, ref $self)) 0 3 3
344 0 Carp::cluck "Cannot multiply a time by a time, only a time by a number.";
345 }
346  
347 # Convert time to seconds
348 3 my $ss = $self->total_seconds;
349 3 $ss *= $n;
350 3 my @hms = _mktime_seconds($ss);
351 3 return ref($self)->new( @hms );
352 }
353  
354 2 ▶sub _divide {
Subroutine _divide: called CC=2 SCAR=7.1
355 2 my ($self, $n, $reverse) = @_;
356  
357 2 ▶ if (UNIVERSAL::isa($n, ref($self) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($n, ref $self)) 0 2 2
358 0 Carp::cluck "Cannot multiply a time by a time, only a time by a number.";
359 }
360  
361 # Convert time to seconds
362 2 my $ss = $self->total_seconds;
363 2 my $return = $ss /= $n;
364  
365 # Convert return value to time
366 2 my @hms = _mktime_seconds($return);
367 2 return ref($self)->new( @hms );
368 }
369  
370 1 1;
371  
372 __END__
↑ Summary