blib/lib/DTL/Fast/Utils.pm

filter: Statementstmt 44.6% 63 / 141 Branchbran 42.0% 53 / 126 Conditioncond 3.4% 2 / 58 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 34.4% 125 / 363 SCAR 75.5
CC
84
Cov
36%
time2str_php
2821.9
as_bool
6.3
BEGIN
1.0
CRAP 1907.1
↑ Summary blib/lib/DTL/Fast/Expression/Operator/Binary/Plus.pm »
1 package DTL::Fast::Utils;
2 98 ▶use strict; use utf8; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 98 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 98 require Date::Format;
6 98 require URI::Escape::XS;
7  
8 98 our $VERSION = '1.00';
9  
10 98 our @EXPORT_OK;
11  
12 # @todo what with timezones?
13 98 push @EXPORT_OK, 'time2str';
14 sub time2str
15 {
16 8 ▶ my $format = shift;
Subroutine time2str: called CC=1 SCAR=0.0
Pod undocumented
17 8 my $time = shift;
18  
19 # TIME_FORMAT, DATE_FORMAT, DATETIME_FORMAT, SHORT_DATE_FORMAT or SHORT_DATETIME_FORMAT
20  
21 8 return Date::Format::time2str($format, $time );
22 }
23  
24 98 push @EXPORT_OK, 'time2str_php';
25 # the code below has been taken from Dotiac::DTL and should be re-written on C
26 # would be nice to implement module with this functionality (take from PHP source)
27 #locale stuff
28 98 our @datemonths=qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
29 98 our @datemonthl=qw( January February March April May Juni Juli August September October November December );
30 98 our @datemontha=qw( Jan. Feb. March April May Juni Juli Aug. Sep. Oct. Nov. Dec. );
31 98 our @weekdays=qw/Sun Mon Tue Wed Thu Fri Sat/;
32 98 our @weekdayl=qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday/;
33 98 our @timeampm=qw/a.m. p.m. AM PM/;
34 98 our @timespotnames=qw/midnight noon/;
35 98 our @datesuffixes=qw/th st nd rd/; #qw/Default day1 day2 day3 day4 day5...
36 sub time2str_php
37 {
38 6 ▶ my $format = shift // "";
Subroutine time2str_php: called CC=79 SCAR=79.5
Pod undocumented
Condition50%1/2
shift() // ""
l!l
60
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
39 6 ▶ my $time = shift // 0;
Condition50%1/2
shift() // 0
l!l
60
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
40  
41 6 my @t = localtime($time);
42 6 my @s = split //, $format;
43  
44 6 my $res;
45  
46 6 ▶ while(my $s = shift @s )
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = shift @s) 54 6 60
47 {
48 54 ▶ if ($s eq '\\') {
Branch: 38 branches
BranchTrueFalseTotal
if ($s eq '\\') { } 3 51 54
elsif ($s eq 'a') { } 0 51 51
elsif ($s eq 'A') { } 0 51 51
elsif ($s eq 'b') { } 0 51 51
elsif ($s eq 'd') { } 0 51 51
elsif ($s eq 'D') { } 0 51 51
elsif ($s eq 'f') { } 0 51 51
elsif ($s eq 'F') { } 0 51 51
elsif ($s eq 'g') { } 0 51 51
elsif ($s eq 'G') { } 0 51 51
elsif ($s eq 'h') { } 0 51 51
elsif ($s eq 'H') { } 0 51 51
elsif ($s eq 'i') { } 0 51 51
elsif ($s eq 'j') { } 0 51 51
elsif ($s eq 'l') { } 0 51 51
elsif ($s eq 'L') { } 0 51 51
elsif ($s eq 'm') { } 0 51 51
elsif ($s eq 'M') { } 0 51 51
elsif ($s eq 'n') { } 0 51 51
elsif ($s eq 'N') { } 0 51 51
elsif ($s eq 'O') { } 0 51 51
elsif ($s eq 'P') { } 0 51 51
elsif ($s eq 'r') { } 0 51 51
elsif ($s eq 's') { } 0 51 51
elsif ($s eq 'S') { } 0 51 51
elsif ($s eq 't') { } 0 51 51
elsif ($s eq 'T') { } 0 51 51
elsif ($s eq 't') { } 0 51 51
elsif ($s eq 'W') { } 0 51 51
elsif ($s eq 'y') { } 6 45 51
elsif ($s eq 'Y') { } 6 39 45
elsif ($s eq 'z') { } 6 33 39
elsif ($s eq 'Z') { } 6 27 33
elsif ($s eq "\n") { } 0 27 27
elsif ($s eq "\t") { } 0 27 27
elsif ($s eq "\f") { } 0 27 27
elsif ($s eq "\cH") { } 0 27 27
elsif ($s eq "\r") { } 0 27 27
49 3 $res.=shift(@s);
50 }
51 elsif ($s eq "a") {
52 0 ▶ if ($t[2] > 12 or ($t[2] == 12 and $t[1] > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[2] > 12 or $t[2] == 12 and $t[1] > 0) { } 0 0 0
Condition0%0/6
$t[2] > 12 or $t[2] == 12 and $t[1] > 0
l!l&&r!l&&!r
000
$t[2] == 12 and $t[1] > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$t[2] > 12 or $t[2] == 12 and $t[1] > 0
$t[2] > 12 $t[2] == 12 $t[1] > 0
Truth table0%0/4
A or B and C
A= $t[2] > 12B= $t[2] == 12C= $t[1] > 0
ABCresult
1XX1
0111
00X0
0100
53 0 $res.=$timeampm[0];
54 }
55 else {
56 0 $res.=$timeampm[1];
57 }
58 }
59 elsif ($s eq "A") {
60 0 ▶ if ($t[2] > 12 or ($t[2] == 12 and $t[1] > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[2] > 12 or $t[2] == 12 and $t[1] > 0) { } 0 0 0
Condition0%0/6
$t[2] > 12 or $t[2] == 12 and $t[1] > 0
l!l&&r!l&&!r
000
$t[2] == 12 and $t[1] > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$t[2] > 12 or $t[2] == 12 and $t[1] > 0
$t[2] > 12 $t[2] == 12 $t[1] > 0
Truth table0%0/4
A or B and C
A= $t[2] > 12B= $t[2] == 12C= $t[1] > 0
ABCresult
1XX1
0111
00X0
0100
61 0 $res.=$timeampm[2];
62 }
63 else {
64 0 $res.=$timeampm[3];
65 }
66 }
67 elsif ($s eq "b") {
68 0 $res.=lc($datemonths[$t[4]]);
69 }
70 elsif ($s eq "d") {
71 0 $res.=sprintf("%02d",$t[3]);
72 }
73 elsif ($s eq "D") {
74 0 $res.=$weekdays[$t[6]];
75 }
76 elsif ($s eq "f") {
77 0 my $h=$t[2];
78 0 $h=$h%12;
79 0 ▶ $h=12 unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
80 0 $res.=$h;
81 0 ▶ $res.=sprintf(":%02d",$t[1]) if ($t[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $t[1] 0 0 0
82 }
83 elsif ($s eq "F") {
84 0 $res.=$datemonthl[$t[4]];
85 }
86 elsif ($s eq "g") {
87 0 my $h=$t[2];
88 0 $h=$h%12;
89 0 ▶ $h=12 unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
90 0 $res.=$h;
91 }
92 elsif ($s eq "G") {
93 0 $res.=$t[2];
94 }
95 elsif ($s eq "h") {
96 0 my $h=$t[2];
97 0 $h=$h%12;
98 0 ▶ $h=12 unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
99 0 $res.=sprintf("%02d",$h);
100 }
101 elsif ($s eq "H") {
102 0 $res.=sprintf("%02d",$t[2]);
103 }
104 elsif ($s eq "i") {
105 0 $res.=sprintf("%02d",$t[1]);
106 }
107 elsif ($s eq "j") {
108 0 $res.=$t[3];
109 }
110 elsif ($s eq "l") {
111 0 $res.=$weekdayl[$t[6]];
112 }
113 elsif ($s eq "L") {
114 0 my $d=$t[5]+1900;
115 0 ▶ $res.=(((not $d%4 and $d%100) or not $d%400)?"1":"0");
Branch: 1 branch
BranchTrueFalseTotal
!($d % 4) && $d % 100 || !($d % 400) ? : 0 0 0
Condition0%0/6
not $d % 4 and $d % 100 or not $d % 400
l!l&&r!l&&!r
000
not $d % 4 and $d % 100
!ll&&!rl&&r
000
MC/DC0%0/3
not $d % 4 and $d % 100 or not $d % 400
not $d % 4 $d % 100 not $d % 400
Truth table0%0/5
not(A) and B or not(C)
A= not $d % 4B= $d % 100C= not $d % 400
ABCresult
11X1
0X11
1011
0X00
1000
116 }
117 elsif ($s eq "m") {
118 0 $res.=sprintf("%02d",$t[4]+1);
119 }
120 elsif ($s eq "M") {
121 0 $res.=$datemonths[$t[4]];
122 }
123 elsif ($s eq "n") {
124 0 $res.=$t[4]+1;
125 }
126 elsif ($s eq "N") {
127 0 $res.=$datemontha[$t[4]];
128 }
129 elsif ($s eq "O") {
130 0 my @tt=localtime(0);
131 0 ▶ $tt[2]+=1 if $t[8];
Branch: 1 branch
BranchTrueFalseTotal
if $t[8] 0 0 0
132 0 $res.=sprintf("%+05d",$tt[2]*100+$tt[1]);
133 }
134 elsif ($s eq "P") {
135 0 ▶ if ($t[2] == 12 and $t[1] == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t[2] == 12 and $t[1] == 0) { } 0 0 0
elsif ($t[2] == 0 and $t[1] == 0) { } 0 0 0
Condition0%0/3
$t[2] == 12 and $t[1] == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$t[2] == 12 and $t[1] == 0
$t[2] == 12 $t[1] == 0
Truth table0%0/3
A and B
A= $t[2] == 12B= $t[1] == 0
ABresult
0X0
100
111
136 0 $res.=$timespotnames[1];
137 }
138 ▶ elsif ($t[2] == 0 and $t[1] == 0) {
Condition0%0/3
$t[2] == 0 and $t[1] == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$t[2] == 0 and $t[1] == 0
$t[2] == 0 $t[1] == 0
Truth table0%0/3
A and B
A= $t[2] == 0B= $t[1] == 0
ABresult
0X0
100
111
139 0 $res.=$timespotnames[0];
140 }
141 else {
142 0 my $h=$t[2];
143 0 $h=$h%12;
144 0 ▶ $h=12 unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
145 0 $res.=$h;
146 0 ▶ $res.=sprintf(":%02d",$t[1]) if ($t[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $t[1] 0 0 0
147 0 ▶ if ($t[2] > 12 or ($t[2] == 12 and $t[1] > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[2] > 12 or $t[2] == 12 and $t[1] > 0) { } 0 0 0
Condition0%0/6
$t[2] > 12 or $t[2] == 12 and $t[1] > 0
l!l&&r!l&&!r
000
$t[2] == 12 and $t[1] > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$t[2] > 12 or $t[2] == 12 and $t[1] > 0
$t[2] > 12 $t[2] == 12 $t[1] > 0
Truth table0%0/4
A or B and C
A= $t[2] > 12B= $t[2] == 12C= $t[1] > 0
ABCresult
1XX1
0111
00X0
0100
148 0 $res.=" ".$timeampm[0];
149 }
150 else {
151 0 $res.=" ".$timeampm[1];
152 }
153 }
154  
155 }
156 elsif ($s eq "r") {
157 0 $res.=$weekdays[$t[6]];
158 0 $res.=", ";
159 0 $res.=$t[4]+1;
160 0 $res.=" ".$datemonths[$t[4]]." ".($t[5]+1900);
161 0 $res.=sprintf(" %02d:%02d:%02d",$t[2],$t[1],$t[0]);
162 0 my @tt=localtime(0);
163 0 ▶ $tt[2]+=1 if $t[8];
Branch: 1 branch
BranchTrueFalseTotal
if $t[8] 0 0 0
164 0 $res.=sprintf(" %+05d",$tt[2]*100+$tt[1]);
165 }
166 elsif ($s eq "s") {
167 0 $res.=sprintf("%02d",$t[0]);
168 }
169 elsif ($s eq "S") {
170 0 ▶ if ($datesuffixes[$t[3]]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DTL::Fast::Utils::datesuffixes[$t[3]]) { } 0 0 0
171 0 $res.=$datesuffixes[$t[3]];
172 }
173 else {
174 0 $res.=$datesuffixes[0]
175 }
176 }
177 elsif ($s eq "t") {
178 0 ▶ if ($t[4] == 1 or $t[4]==3 or $t[4] == 5 or $t[4] == 7 or $t[4] == 8 or $t[4] == 10 or $t[4] == 12) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t[4] == 1 or $t[4] == 3 or $t[4] == 5 or $t[4] == 7 or $t[4] == 8 or $t[4] == 10 or $t[4] == 12) { } 0 0 0
elsif ($t[4] == 2) { } 0 0 0
Condition0%0/18
$t[4] == 1 or $t[4] == 3 or $t[4] == 5 or $t[4] == 7 or $t[4] == 8 or $t[4] == 10 or $t[4] == 12
l!l&&r!l&&!r
000
$t[4] == 1 or $t[4] == 3 or $t[4] == 5 or $t[4] == 7 or $t[4] == 8 or $t[4] == 10
l!l&&r!l&&!r
000
$t[4] == 1 or $t[4] == 3 or $t[4] == 5 or $t[4] == 7 or $t[4] == 8
l!l&&r!l&&!r
000
$t[4] == 1 or $t[4] == 3 or $t[4] == 5 or $t[4] == 7
l!l&&r!l&&!r
000
$t[4] == 1 or $t[4] == 3 or $t[4] == 5
l!l&&r!l&&!r
000
$t[4] == 1 or $t[4] == 3
l!l&&r!l&&!r
000
MC/DC0%0/7
$t[4] == 1 or $t[4] == 3 or $t[4] == 5 or $t[4] == 7 or $t[4] == 8 or $t[4] == 10 or $t[4] == 12
$t[4] == 1 $t[4] == 3 $t[4] == 5 $t[4] == 7 $t[4] == 8 $t[4] == 10 $t[4] == 12
Truth table0%0/8
A or B or C or D or E or F or G
A= $t[4] == 1B= $t[4] == 3C= $t[4] == 5D= $t[4] == 7E= $t[4] == 8F= $t[4] == 10G= $t[4] == 12
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
179 0 $res.="31";
180 }
181 elsif ($t[4] == 2) {
182 0 my $d=$t[5]+1900;
183 0 ▶ if ((not $d%4 and $d%100) or not $d%400) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $d % 4 and $d % 100 or not $d % 400) { } 0 0 0
Condition0%0/6
not $d % 4 and $d % 100 or not $d % 400
l!l&&r!l&&!r
000
not $d % 4 and $d % 100
!ll&&!rl&&r
000
MC/DC0%0/3
not $d % 4 and $d % 100 or not $d % 400
not $d % 4 $d % 100 not $d % 400
Truth table0%0/5
not(A) and B or not(C)
A= not $d % 4B= $d % 100C= not $d % 400
ABCresult
11X1
0X11
1011
0X00
1000
184 0 $res.="29";
185 }
186 else {
187 0 $res.="28";
188 }
189 }
190 else {
191 0 $res.="30";
192 }
193 }
194 elsif ($s eq "T") {
195 0 require POSIX;
196 0 $res.=POSIX::strftime("%Z", @t);
197 }
198 elsif ($s eq "t") {
199 0 $res.=$t[6];
200 }
201 elsif ($s eq "W") {
202 0 require POSIX;
203 0 $res.=POSIX::strftime("%W", @t);
204 }
205 elsif ($s eq "y") {
206 6 $res.=sprintf("%02d",($t[5]%100));
207 }
208 elsif ($s eq "Y") {
209 6 $res.=sprintf("%04d",$t[5]+1900);
210 }
211 elsif ($s eq "z") {
212 6 $res.=$t[7];
213 }
214 elsif ($s eq "Z") {
215 6 my @tt=localtime(0);
216 6 ▶ $tt[2]+=1 if $t[8];
Branch: 1 branch
BranchTrueFalseTotal
if $t[8] 0 6 6
217 6 $res.=$tt[2]*3600+$t[1]*60+$t[0];
218 }
219 elsif ($s eq "\n") {
220 0 $res.="n";
221 }
222 elsif ($s eq "\t") {
223 0 $res.="t";
224 }
225 elsif ($s eq "\f") {
226 0 $res.="f";
227 }
228 elsif ($s eq "\b") {
229 0 $res.="b";
230 }
231 elsif ($s eq "\r") {
232 0 $res.="r";
233 }
234 else {
235 27 $res.=$s;
236 }
237 }
238 6 return $res;
239 }
240 ### end of Dotiac::DTL code
241  
242 #Shortcuts below not working on mswin and perl =< 18.2
243 98 push @EXPORT_OK, 'uri_escape';
244 98 *DTL::Fast::Utils::uri_escape = \&URI::Escape::XS::uri_escape;
245  
246 98 push @EXPORT_OK, 'uri_unescape';
247 98 *DTL::Fast::Utils::uri_unescape = \&URI::Escape::XS::uri_unescape;
248  
249 98 push @EXPORT_OK, 'escape';
250 98 *DTL::Fast::Utils::escape = \&URI::Escape::XS::encodeURIComponent;
251  
252 98 push @EXPORT_OK, 'unescape';
253 98 *DTL::Fast::Utils::unescape = \&URI::Escape::XS::decodeURIComponent;
254  
255 98 push @EXPORT_OK, 'as_bool';
256 sub as_bool
257 {
258 5887 ▶ my $value = shift;
Subroutine as_bool: called CC=6 SCAR=18.5
Pod undocumented
259 5887 my $value_type = ref $value;
260  
261 5887 ▶ if( $value_type )
Branch: 1 branch
BranchTrueFalseTotal
if ($value_type) 984 4903 5887
262 {
263 984 ▶ if ( $value_type eq 'SCALAR' )
Branch: 4 branches
BranchTrueFalseTotal
if ($value_type eq 'SCALAR') { } 328 656 984
elsif ($value_type eq 'HASH') { } 328 328 656
elsif ($value_type eq 'ARRAY') { } 328 0 328
elsif (UNIVERSAL::can($value, 'as_bool')) { } 0 0 0
264 {
265 328 $value = $$value;
266 }
267 elsif( $value_type eq 'HASH' )
268 {
269 328 $value = scalar keys(%$value);
270 }
271 elsif( $value_type eq 'ARRAY' )
272 {
273 328 $value = scalar @$value;
274 }
275 elsif( UNIVERSAL::can( $value, 'as_bool' ) )
276 {
277 0 $value = $value->as_bool();
278 }
279 }
280  
281 5887 return $value;
282 }
283  
284  
285 98 1;
↑ Summary blib/lib/DTL/Fast/Expression/Operator/Binary/Plus.pm »