Elapsed.pm

filter: Statementstmt 85.8% 151 / 176 Branchbran 51.6% 32 / 62 Conditioncond 40.0% 10 / 25 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 96.8% 31 / 32 Podpod 100% 1 / 1 totaltotal 73.2% 227 / 310 CC 46 SCAR 27.1
CC
46
Cov
73%
_compile_all
72.0
_fixer
21.6
_examine
21.0
CRAP 15.0
↑ Summary Elapsed/Lang/DA.pm »
1 package Time::Elapsed;
2 2 $Time::Elapsed::VERSION = '0.33';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # time constants
8 2 ▶use constant SECOND => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use constant MINUTE => 60 * SECOND;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use constant HOUR => 60 * MINUTE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use constant DAY => 24 * HOUR;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use constant WEEK => 7 * DAY;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use constant MONTH => 30 * DAY;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use constant YEAR => 365 * DAY;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # elapsed data fields
16 2 ▶use constant INDEX => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use constant MULTIPLIER => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use constant FIXER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use constant T_SECOND => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use constant T_MINUTE => T_SECOND;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use constant T_HOUR => T_SECOND;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use constant T_DAY => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use constant T_WEEK => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use constant T_MONTH => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use constant T_MONTHW => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use constant T_YEAR => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 our @EXPORT = qw( elapsed );
32 2 our %EXPORT_TAGS = ( all => [ @EXPORT ] );
33  
34 # elapsed time formatter keys
35 2 my $EC = 0;
36 2 my $ELAPSED = {
37 # name index multiplier fixer
38 second => [ $EC++, T_SECOND, T_MINUTE ],
39 minute => [ $EC++, T_MINUTE, T_HOUR ],
40 hour => [ $EC++, T_HOUR, T_DAY ],
41 day => [ $EC++, T_DAY, T_MONTH ],
42 month => [ $EC++, T_MONTH, T_YEAR ],
43 year => [ $EC++, T_YEAR, 1 ],
44 };
45  
46 2 my $EW = 0;
47 2 my $ELAPSED_W = {
48 # name index multiplier fixer
49 second => [ $EW++, T_SECOND, T_MINUTE ],
50 minute => [ $EW++, T_MINUTE, T_HOUR ],
51 hour => [ $EW++, T_HOUR, T_DAY ],
52 day => [ $EW++, T_DAY, T_WEEK ],
53 week => [ $EW++, T_WEEK, T_MONTHW ],
54 month => [ $EW++, T_MONTHW, T_YEAR ],
55 year => [ $EW++, T_YEAR, 1 ],
56 };
57  
58 # formatters for _fixer()
59 2 my $FIXER = { map { $_ => $ELAPSED->{$_}[FIXER] } keys %{ $ELAPSED } };
60 2 my $FIXER_W = { map { $_ => $ELAPSED_W->{$_}[FIXER] } keys %{ $ELAPSED_W } };
61  
62 21 my $NAMES = [ sort { $ELAPSED->{ $a }[INDEX] <=> $ELAPSED->{ $b }[INDEX] }
63 2 keys %{ $ELAPSED } ];
64  
65 23 my $NAMES_W = [ sort { $ELAPSED_W->{ $a }[INDEX] <=> $ELAPSED_W->{ $b }[INDEX] }
66 2 keys %{ $ELAPSED_W } ];
67  
68 2 my $LCACHE; # language cache
69  
70 sub import {
71 1 ▶ my($class, @raw) = @_;
Subroutine import: called CC=4 SCAR=15.9
72 1 my @exports;
73 1 foreach my $e ( @raw ) {
74 1 ▶ _compile_all() && next if $e eq '-compile';
Branch: 1 branch
BranchTrueFalseTotal
if $e eq "-compile" 0 1 1
Condition20%1/5
$e eq '-compile' && (_compile_all() && (next))
!ll&&!rl&&r
100
_compile_all() and next
!ll
00
MC/DC0%0/2
$e eq '-compile' && (_compile_all() && (next))
$e eq '-compile' _compile_all()
Truth table33%1/3
A && B
A= $e eq '-compile'B= _compile_all()
ABresult
0X0
100
111
75 1 push @exports, $e;
76 }
77 1 return $class->export_to_level( 1, $class, @exports );
78 }
79  
80 sub elapsed {
81 36 ▶ my $sec = shift;
Subroutine elapsed: called CC=9 SCAR=22.0
Pod documented
82 36 ▶ return if ! defined $sec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sec 2 34 36
83 34 ▶ my $opt = shift || {};
Condition100%2/2
shift() || {}
l!l
313
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
84 34 ▶ $opt = { lang => $opt } if ! ref $opt;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt 31 3 34
85 34 ▶ $sec = 0 if !$sec; # can be empty string
Branch: 1 branch
BranchTrueFalseTotal
unless $sec 20 14 34
86 34 $sec += 0; # force number
87  
88 34 ▶ my $l = _get_lang( $opt->{lang} || 'EN' ); # get language keys
Condition100%2/2
$opt->{'lang'} || 'EN'
l!l
313
MC/DC100%1/1
$opt->{'lang'} || 'EN'
$opt->{'lang'}
Truth table100%2/2
A
A= $opt->{'lang'}
Aresult
11
00
89 34 ▶ return $l->{other}{zero} if ! $sec;
Branch: 1 branch
BranchTrueFalseTotal
unless $sec 20 14 34
90  
91 14 ▶ my $w = $opt->{weeks} || 0;
Condition50%1/2
$opt->{'weeks'} || 0
l!l
014
MC/DC0%0/1
$opt->{'weeks'} || 0
$opt->{'weeks'}
Truth table50%1/2
A
A= $opt->{'weeks'}
Aresult
11
00
92 14 my @rv = _populate(
93 $l,
94 _fixer(
95 $w,
96 _parser(
97 $w,
98 _examine( abs($sec), $w )
99 )
100 )
101 );
102  
103 14 my $last_value = pop @rv;
104  
105 14 ▶ return @rv ? join(', ', @rv) . " $l->{other}{and} $last_value"
Branch: 1 branch
BranchTrueFalseTotal
@rv ? : 14 0 14
106 : $last_value; # only a single value, no need for template/etc.
107 }
108  
109 sub _populate {
110 14 ▶ my($l, @parsed) = @_;
Subroutine _populate: called CC=4 SCAR=13.9
111 14 my @buf;
112 14 foreach my $e ( @parsed ) {
113 56 ▶ next if ! $e->[MULTIPLIER]; # disable zero values
Branch: 1 branch
BranchTrueFalseTotal
unless $e->[1] 14 42 56
114 42 ▶ my $type = $e->[MULTIPLIER] > 1 ? 'plural' : 'singular';
Branch: 1 branch
BranchTrueFalseTotal
$e->[1] > 1 ? : 36 6 42
115 42 push @buf, join q{ }, $e->[MULTIPLIER], $l->{ $type }{ $e->[INDEX] };
116 }
117 14 return @buf;
118 }
119  
120 sub _fixer {
121 # There can be values like "60 seconds". _fixer() corrects this kind of error
122 14 ▶ my($weeks, @raw) = @_;
Subroutine _fixer: called CC=10 SCAR=30.7
123 14 my(@fixed,$default,$add);
124  
125 14 ▶ my $f = $weeks ? $FIXER_W : $FIXER;
Branch: 1 branch
BranchTrueFalseTotal
$weeks ? : 0 14 14
126 14 ▶ my $e = $weeks ? $ELAPSED_W : $ELAPSED;
Branch: 1 branch
BranchTrueFalseTotal
$weeks ? : 0 14 14
127 14 ▶ my $n = $weeks ? $NAMES_W : $NAMES;
Branch: 1 branch
BranchTrueFalseTotal
$weeks ? : 0 14 14
128  
129 14 my @top;
130 14 foreach my $i ( reverse 0..$#raw ) {
131 56 my $r = $raw[$i];
132 56 $default = $f->{ $r->[INDEX] };
133 56 ▶ if ( $add ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add) 0 56 56
134 0 $r->[MULTIPLIER] += $add; # we need a fix
135 0 $add = 0; # reset
136 }
137  
138 # year is the top-most element currently does not have any limits (def=1)
139 56 ▶ if ( $r->[MULTIPLIER] >= $default && $r->[INDEX] ne 'year' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->[1] >= $default and $r->[0] ne "year") 0 56 56
Condition33%1/3
$r->[1] >= $default and $r->[0] ne "year"
!ll&&!rl&&r
5600
MC/DC0%0/2
$r->[1] >= $default and $r->[0] ne "year"
$r->[1] >= $default $r->[0] ne "year"
Truth table33%1/3
A and B
A= $r->[1] >= $defaultB= $r->[0] ne "year"
ABresult
0X0
100
111
140 0 $add = int $r->[MULTIPLIER] / $default;
141 0 $r->[MULTIPLIER] -= $default * $add;
142 0 ▶ if ( $i == 0 ) { # we need to add to a non-existent upper level
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) 0 0 0
143 0 my $id = $e->{ $r->[INDEX] }[INDEX];
144 0 ▶ my $up = $n->[ $id + 1 ]
Condition0%0/2
$$n[$id + 1] || die("Can not happen: unable to locate top-level\n")
l!l
00
MC/DC0%0/1
$$n[$id + 1] || die("Can not happen: unable to locate top-level\n")
$$n[$id + 1]
Truth table0%0/2
A
A= $$n[$id + 1]
Aresult
11
00
145 || die "Can not happen: unable to locate top-level\n";
146 0 unshift @top, [ $up, $add ];
147 }
148 }
149  
150 56 unshift @fixed, [ $r->[INDEX], $r->[MULTIPLIER] ];
151 }
152  
153 14 unshift @fixed, @top;
154 14 return @fixed;
155 }
156  
157 sub _parser { # recursive formatter/parser
158 56 ▶ my($weeks, $id, $mul) = @_;
Subroutine _parser: called CC=5 SCAR=16.2
159 56 ▶ my $e = $weeks ? $ELAPSED_W : $ELAPSED;
Branch: 1 branch
BranchTrueFalseTotal
$weeks ? : 0 56 56
160 56 ▶ my $n = $weeks ? $NAMES_W : $NAMES;
Branch: 1 branch
BranchTrueFalseTotal
$weeks ? : 0 56 56
161 56 my $xmid = $e->{ $id }[INDEX];
162 56 ▶ my @parsed = [ $id, $xmid ? int $mul : sprintf '%.0f', $mul ];
Branch: 1 branch
BranchTrueFalseTotal
$xmid ? : 42 14 56
163  
164 56 ▶ if ( $xmid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmid) 42 14 56
165 push @parsed, _parser(
166 $weeks,
167 $n->[ $xmid - 1 ],
168 42 ($mul - int $mul) * $e->{$id}[MULTIPLIER]
169 );
170 }
171  
172 56 return @parsed;
173 }
174  
175 sub _examine {
176 14 ▶ my($sec, $weeks) = @_;
Subroutine _examine: called CC=8 SCAR=30.5
177 return
178 14 ▶ $sec >= YEAR ? ( year => $sec / YEAR )
Branch: 6 branches
BranchTrueFalseTotal
$sec >= 31536000 ? : 0 14 14
$sec >= 2592000 ? : 0 14 14
$sec >= 604800 && $weeks ? : 0 14 14
$sec >= 86400 ? : 14 0 14
$sec >= 3600 ? : 0 0 0
$sec >= 60 ? : 0 0 0
Condition33%1/3
$sec >= 604800 and $weeks
!ll&&!rl&&r
0140
MC/DC0%0/2
$sec >= 604800 and $weeks
$sec >= 604800 $weeks
Truth table33%1/3
A and B
A= $sec >= 604800B= $weeks
ABresult
0X0
100
111
179 : $sec >= MONTH ? ( month => $sec / MONTH )
180 : $sec >= WEEK && $weeks ? ( week => $sec / WEEK )
181 : $sec >= DAY ? ( day => $sec / DAY )
182 : $sec >= HOUR ? ( hour => $sec / HOUR )
183 : $sec >= MINUTE ? ( minute => $sec / MINUTE )
184 : ( second => $sec )
185 ;
186 }
187  
188 sub _get_lang {
189 34 ▶ my $lang = shift || croak '_get_lang(): Language ID is missing';
Subroutine _get_lang: called CC=5 SCAR=18.1
Condition33%1/3
shift() || croak('_get_lang(): Language ID is missing')
l!l&&r!l&&!r
3400
MC/DC0%0/2
shift() || croak('_get_lang(): Language ID is missing')
shift() croak('_get_lang(): Language ID is missing')
Truth table33%1/3
A || B
A= shift()B= croak('_get_lang(): Language ID is missing')
ABresult
1X1
011
000
190 34 $lang = uc $lang;
191 34 ▶ if ( ! exists $LCACHE->{ $lang } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $LCACHE->{$lang}) 5 29 34
192 5 ▶ if ( $lang =~ m{[^a-z_A-Z_0-9]}xms || $lang =~ m{ \A [0-9] }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang =~ /[^a-z_A-Z_0-9]/msx or $lang =~ / \A [0-9] /msx) 0 5 5
Condition33%1/3
$lang =~ /[^a-z_A-Z_0-9]/msx or $lang =~ / \A [0-9] /msx
l!l&&r!l&&!r
005
MC/DC0%0/2
$lang =~ /[^a-z_A-Z_0-9]/msx or $lang =~ / \A [0-9] /msx
$lang =~ /[^a-z_A-Z_0-9]/msx $lang =~ / \A [0-9] /msx
Truth table33%1/3
A or B
A= $lang =~ /[^a-z_A-Z_0-9]/msxB= $lang =~ / \A [0-9] /msx
ABresult
1X1
011
000
193 0 croak "Bad language identifier: $lang";
194 }
195 5 _set_lang_cache( $lang );
196 }
197 34 return $LCACHE->{ $lang };
198 }
199  
200 sub _set_lang_cache {
201 5 ▶ my($lang) = @_;
Subroutine _set_lang_cache: called CC=1 SCAR=0.0
202 5 my $class = join q{::}, __PACKAGE__, 'Lang', $lang;
203 5 my $file = join(q{/} , split m{::}xms, $class ) . '.pm';
204 5 require $file;
205 5 $LCACHE->{ $lang } = {
206 singular => { $class->singular },
207 plural => { $class->plural },
208 other => { $class->other },
209 };
210 5 return;
211 }
212  
213 sub _compile_all {
214 0 ▶ require File::Spec;
Subroutine _compile_all: not called CC=8 SCAR=42.8
215 0 require Symbol;
216 0 my($test, %lang);
217  
218 # search lib paths
219 0 foreach my $lib ( @INC ) {
220 0 $test = File::Spec->catfile( $lib, qw/ Time Elapsed Lang /);
221 0 ▶ next if not -d $test;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $test 0 0 0
222 0 my $LDIR = Symbol::gensym();
223 0 ▶ opendir $LDIR, $test or croak "opendir($test): $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $LDIR, $test or croak("opendir($test): $!") 0 0 0
224  
225 0 ▶ while ( my $file = readdir $LDIR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $LDIR)) 0 0 0
226 0 ▶ next if -d $file;
Branch: 1 branch
BranchTrueFalseTotal
if -d $file 0 0 0
227 0 ▶ if ( $file =~ m{ \A (.+?) \. pm \z }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ / \A (.+?) \. pm \z /msx) 0 0 0
228 0 $lang{ uc $1 }++;
229 }
230 }
231  
232 0 closedir $LDIR;
233 }
234  
235 # compile language data
236 0 foreach my $id ( keys %lang ) {
237 0 _get_lang( $id );
238 }
239  
240 0 return 1;
241 }
242  
243 2 1;
244  
245 __END__
↑ Summary Elapsed/Lang/DA.pm »