Date/Format/Generic.pm

filter: Statementstmt 91.2% 135 / 148 Branchbran 73.3% 22 / 30 Conditioncond 57.1% 4 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.6% 59 / 63 Podpod 0.0% 0 / 57 totaltotal 71.2% 220 / 309 CC 18 SCAR 5.9
CC
18
Cov
87%
format_P
6.0
strftime
4.0
time2str
4.0
CRAP 1.8
« Date/Language/Austrian.pm ↑ Summary Date/Format.pm »
1 ##
2 ##
3 ##
4  
5 package Date::Format::Generic;
6  
7 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 14 our ($epoch, $tzname);
11 14 ▶use Time::Zone;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 our $VERSION = '2.35'; # VERSION: generated
15 # ABSTRACT: Date formatting subroutines
16  
17 sub ctime
18 18 ▶{
Subroutine ctime: called CC=1 SCAR=0.0
Pod undocumented
19 18 my($me,$t,$tz) = @_;
20 18 $me->time2str("%a %b %e %T %Y\n", $t, $tz);
21 }
22  
23 sub asctime
24 0 ▶{
Subroutine asctime: not called CC=1 SCAR=6.9
Pod undocumented
25 0 my($me,$t,$tz) = @_;
26 0 $me->strftime("%a %b %e %T %Y\n", $t, $tz);
27 }
28  
29 sub _subs
30 786 ▶{
Subroutine _subs: called CC=2 SCAR=7.0
31 786 my $fn;
32 786 $_[1] =~ s/
33 %(O?[%a-zA-Z])
34 /
35 1685 ▶ ($_[0]->can("format_$1") || sub { $1 })->($_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$_[0]->can("format_$1") || sub { $1; }
l!l&&r!l&&!r
167960
MC/DC0%0/2
$_[0]->can("format_$1") || sub { $1; }
$_[0]->can("format_$1") sub { $1; }
Truth table66%2/3
A || B
A= $_[0]->can("format_$1")B= sub { $1; }
ABresult
1X1
011
000
36 /sgeox;
37  
38 786 $_[1];
39 }
40  
41 sub strftime
42 5 ▶{
Subroutine strftime: called CC=4 SCAR=13.9
Pod undocumented
43 5 my($pkg,$fmt,$time);
44  
45 5 ($pkg,$fmt,$time,$tzname) = @_;
46  
47 5 ▶ my $me = ref($pkg) ? $pkg : bless [];
Branch: 1 branch
BranchTrueFalseTotal
ref $pkg ? : 0 5 5
48  
49 5 ▶ if(defined $tzname)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Date::Format::Generic::tzname) { } 3 2 5
50 {
51 3 $tzname = uc $tzname;
52  
53 3 ▶ $tzname = sprintf("%+05d",$tzname)
Branch: 1 branch
BranchTrueFalseTotal
unless $Date::Format::Generic::tzname =~ /\D/ 0 3 3
54 unless($tzname =~ /\D/);
55  
56 3 $epoch = timelocal(@{$time}[0..5]);
57  
58 3 @$me = gmtime($epoch + tz_offset($tzname));
59 }
60 else
61 {
62 2 @$me = @$time;
63 2 undef $epoch;
64 }
65  
66 5 _subs($me,$fmt);
67 }
68  
69 sub time2str
70 751 ▶{
Subroutine time2str: called CC=4 SCAR=13.9
Pod undocumented
71 751 my($pkg,$fmt,$time);
72  
73 751 ($pkg,$fmt,$time,$tzname) = @_;
74  
75 751 ▶ my $me = ref($pkg) ? $pkg : bless [], $pkg;
Branch: 1 branch
BranchTrueFalseTotal
ref $pkg ? : 543 208 751
76  
77 751 $epoch = $time;
78  
79 751 ▶ if(defined $tzname)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Date::Format::Generic::tzname) { } 731 20 751
80 {
81 731 $tzname = uc $tzname;
82  
83 731 ▶ $tzname = sprintf("%+05d",$tzname)
Branch: 1 branch
BranchTrueFalseTotal
unless $Date::Format::Generic::tzname =~ /\D/ 0 731 731
84 unless($tzname =~ /\D/);
85  
86 731 $time += tz_offset($tzname);
87 731 @$me = gmtime($time);
88 }
89 else
90 {
91 20 @$me = localtime($time);
92 }
93 751 $me->[9] = $time;
94 751 _subs($me,$fmt);
95 }
96  
97 14 my(@DoW,@MoY,@DoWs,@MoYs,@AMPM,%format,@Dsuf);
98  
99 14 @DoW = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
100  
101 14 @MoY = qw(January February March April May June
102 July August September October November December);
103  
104 14 @DoWs = map { substr($_,0,3) } @DoW;
105 14 @MoYs = map { substr($_,0,3) } @MoY;
106  
107 14 @AMPM = qw(AM PM);
108  
109 14 @Dsuf = (qw(th st nd rd th th th th th th)) x 3;
110 14 @Dsuf[11,12,13] = qw(th th th);
111 14 @Dsuf[30,31] = qw(th st);
112  
113 14 %format = ('x' => "%m/%d/%y",
114 'C' => "%a %b %e %T %Z %Y",
115 'X' => "%H:%M:%S",
116 );
117  
118 14 my @locale;
119 14 my $locale = "/usr/share/lib/locale/LC_TIME/default";
120 14 local *LOCALE;
121  
122 14 ▶if(open(LOCALE,"$locale"))
Branch: 1 branch
BranchTrueFalseTotal
if (open LOCALE, "$locale") 0 14 14
123 {
124 0 chop(@locale = <LOCALE>);
125 0 close(LOCALE);
126  
127 0 @MoYs = @locale[0 .. 11];
128 0 @MoY = @locale[12 .. 23];
129 0 @DoWs = @locale[24 .. 30];
130 0 @DoW = @locale[31 .. 37];
131 0 @format{"X","x","C"} = @locale[38 .. 40];
132 0 @AMPM = @locale[41 .. 42];
133 }
134  
135 12 ▶sub wkyr {
Subroutine wkyr: called CC=1 SCAR=0.0
Pod undocumented
136 12 my($wstart, $wday, $yday) = @_;
137 12 $wday = ($wday + 7 - $wstart) % 7;
138 12 return int(($yday - $wday + 13) / 7 - 1);
139 }
140  
141 ##
142 ## these 6 formatting routines need to be *copied* into the language
143 ## specific packages
144 ##
145  
146 14 my @roman = ('',qw(I II III IV V VI VII VIII IX));
147 60 ▶sub roman {
Subroutine roman: called CC=4 SCAR=13.9
Pod undocumented
148 60 my $n = shift;
149  
150 60 $n =~ s/(\d)$//;
151 60 my $r = $roman[ $1 ];
152  
153 60 ▶ if($n =~ s/(\d)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/(\d)$//) 44 16 60
154 44 (my $t = $roman[$1]) =~ tr/IVX/XLC/;
155 44 $r = $t . $r;
156 }
157 60 ▶ if($n =~ s/(\d)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/(\d)$//) 10 50 60
158 10 (my $t = $roman[$1]) =~ tr/IVX/CDM/;
159 10 $r = $t . $r;
160 }
161 60 ▶ if($n =~ s/(\d)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/(\d)$//) 5 55 60
162 5 (my $t = $roman[$1]) =~ tr/IVX/M../;
163 5 $r = $t . $r;
164 }
165 60 $r;
166 }
167  
168 151 ▶sub format_a { $DoWs[$_[0]->[6]] }
Subroutine format_a: called CC=1 SCAR=0.0
Pod undocumented
169 2 ▶sub format_A { $DoW[$_[0]->[6]] }
Subroutine format_A: called CC=1 SCAR=0.0
Pod undocumented
170 151 ▶sub format_b { $MoYs[$_[0]->[4]] }
Subroutine format_b: called CC=1 SCAR=0.0
Pod undocumented
171 2 ▶sub format_B { $MoY[$_[0]->[4]] }
Subroutine format_B: called CC=1 SCAR=0.0
Pod undocumented
172 1 ▶sub format_h { $MoYs[$_[0]->[4]] }
Subroutine format_h: called CC=1 SCAR=0.0
Pod undocumented
173 7 ▶sub format_p { $_[0]->[2] >= 12 ? $AMPM[1] : $AMPM[0] }
Subroutine format_p: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0][2] >= 12 ? : 6 1 7
174 0 ▶sub format_P { lc($_[0]->[2] >= 12 ? $AMPM[1] : $AMPM[0]) }
Subroutine format_P: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0][2] >= 12 ? : 0 0 0
175  
176 29 ▶sub format_d { sprintf("%02d",$_[0]->[3]) }
Subroutine format_d: called CC=1 SCAR=0.0
Pod undocumented
177 184 ▶sub format_e { sprintf("%2d",$_[0]->[3]) }
Subroutine format_e: called CC=1 SCAR=0.0
Pod undocumented
178 208 ▶sub format_H { sprintf("%02d",$_[0]->[2]) }
Subroutine format_H: called CC=1 SCAR=0.0
Pod undocumented
179 17 ▶sub format_I { sprintf("%02d",$_[0]->[2] % 12 || 12)}
Subroutine format_I: called CC=2 SCAR=7.6
Pod undocumented
Condition50%1/2
$_[0][2] % 12 || 12
l!l
170
MC/DC0%0/1
$_[0][2] % 12 || 12
$_[0][2] % 12
Truth table50%1/2
A
A= $_[0][2] % 12
Aresult
11
00
180 11 ▶sub format_j { sprintf("%03d",$_[0]->[7] + 1) }
Subroutine format_j: called CC=1 SCAR=0.0
Pod undocumented
181 11 ▶sub format_k { sprintf("%2d",$_[0]->[2]) }
Subroutine format_k: called CC=1 SCAR=0.0
Pod undocumented
182 11 ▶sub format_l { sprintf("%2d",$_[0]->[2] % 12 || 12)}
Subroutine format_l: called CC=2 SCAR=7.6
Pod undocumented
Condition50%1/2
$_[0][2] % 12 || 12
l!l
110
MC/DC0%0/1
$_[0][2] % 12 || 12
$_[0][2] % 12
Truth table50%1/2
A
A= $_[0][2] % 12
Aresult
11
00
183 6 ▶sub format_L { $_[0]->[4] + 1 }
Subroutine format_L: called CC=1 SCAR=0.0
Pod undocumented
184 29 ▶sub format_m { sprintf("%02d",$_[0]->[4] + 1) }
Subroutine format_m: called CC=1 SCAR=0.0
Pod undocumented
185 214 ▶sub format_M { sprintf("%02d",$_[0]->[1]) }
Subroutine format_M: called CC=1 SCAR=0.0
Pod undocumented
186 11 ▶sub format_q { sprintf("%01d",int($_[0]->[4] / 3) + 1) }
Subroutine format_q: called CC=1 SCAR=0.0
Pod undocumented
187 13 ▶sub format_s {
Subroutine format_s: called CC=2 SCAR=6.9
Pod undocumented
188 13 ▶ $epoch = timelocal(@{$_[0]}[0..5])
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Date::Format::Generic::epoch 2 11 13
189 unless defined $epoch;
190 13 sprintf("%d",$epoch)
191 }
192 203 ▶sub format_S { sprintf("%02d",$_[0]->[0]) }
Subroutine format_S: called CC=1 SCAR=0.0
Pod undocumented
193 6 ▶sub format_U { wkyr(0, $_[0]->[6], $_[0]->[7]) }
Subroutine format_U: called CC=1 SCAR=0.0
Pod undocumented
194 6 ▶sub format_w { $_[0]->[6] }
Subroutine format_w: called CC=1 SCAR=0.0
Pod undocumented
195 6 ▶sub format_W { wkyr(1, $_[0]->[6], $_[0]->[7]) }
Subroutine format_W: called CC=1 SCAR=0.0
Pod undocumented
196 34 ▶sub format_y { sprintf("%02d",$_[0]->[5] % 100) }
Subroutine format_y: called CC=1 SCAR=0.0
Pod undocumented
197 189 ▶sub format_Y { sprintf("%04d",$_[0]->[5] + 1900) }
Subroutine format_Y: called CC=1 SCAR=0.0
Pod undocumented
198  
199 175 ▶sub format_Z {
Subroutine format_Z: called CC=2 SCAR=7.2
Pod undocumented
200 175 my $o = tz_local_offset($_[0]->[9]);
201 175 ▶ defined $tzname ? $tzname : uc tz_name($o, $_[0]->[8]);
Branch: 1 branch
BranchTrueFalseTotal
defined $Date::Format::Generic::tzname ? : 175 0 175
202 }
203  
204 11 ▶sub format_z {
Subroutine format_z: called CC=2 SCAR=7.1
Pod undocumented
205 11 my $t = $_[0]->[9];
206 11 ▶ my $o = defined $tzname ? tz_offset($tzname, $t) : tz_offset(undef,$t);
Branch: 1 branch
BranchTrueFalseTotal
defined $Date::Format::Generic::tzname ? : 11 0 11
207 11 sprintf("%+03d%02d", int($o / 3600), int(abs($o) % 3600) / 60);
208 }
209  
210 6 ▶sub format_c { &format_x . " " . &format_X }
Subroutine format_c: called CC=1 SCAR=0.0
Pod undocumented
211 6 ▶sub format_D { &format_m . "/" . &format_d . "/" . &format_y }
Subroutine format_D: called CC=1 SCAR=0.0
Pod undocumented
212 6 ▶sub format_r { &format_I . ":" . &format_M . ":" . &format_S . " " . &format_p }
Subroutine format_r: called CC=1 SCAR=0.0
Pod undocumented
213 6 ▶sub format_R { &format_H . ":" . &format_M }
Subroutine format_R: called CC=1 SCAR=0.0
Pod undocumented
214 179 ▶sub format_T { &format_H . ":" . &format_M . ":" . &format_S }
Subroutine format_T: called CC=1 SCAR=0.0
Pod undocumented
215 0 ▶sub format_t { "\t" }
Subroutine format_t: not called CC=1 SCAR=6.9
Pod undocumented
216 0 ▶sub format_n { "\n" }
Subroutine format_n: not called CC=1 SCAR=6.9
Pod undocumented
217 2 ▶sub format_o { sprintf("%2d%s",$_[0]->[3],$Dsuf[$_[0]->[3]]) }
Subroutine format_o: called CC=1 SCAR=0.0
Pod undocumented
218 12 ▶sub format_x { my $f = $format{'x'}; _subs($_[0],$f); }
Subroutine format_x: called CC=1 SCAR=0.0
Pod undocumented
219 12 ▶sub format_X { my $f = $format{'X'}; _subs($_[0],$f); }
Subroutine format_X: called CC=1 SCAR=0.0
Pod undocumented
220 6 ▶sub format_C { my $f = $format{'C'}; _subs($_[0],$f); }
Subroutine format_C: called CC=1 SCAR=0.0
Pod undocumented
221  
222 5 ▶sub format_Od { roman(format_d(@_)) }
Subroutine format_Od: called CC=1 SCAR=0.0
Pod undocumented
223 5 ▶sub format_Oe { roman(format_e(@_)) }
Subroutine format_Oe: called CC=1 SCAR=0.0
Pod undocumented
224 5 ▶sub format_OH { roman(format_H(@_)) }
Subroutine format_OH: called CC=1 SCAR=0.0
Pod undocumented
225 5 ▶sub format_OI { roman(format_I(@_)) }
Subroutine format_OI: called CC=1 SCAR=0.0
Pod undocumented
226 5 ▶sub format_Oj { roman(format_j(@_)) }
Subroutine format_Oj: called CC=1 SCAR=0.0
Pod undocumented
227 5 ▶sub format_Ok { roman(format_k(@_)) }
Subroutine format_Ok: called CC=1 SCAR=0.0
Pod undocumented
228 5 ▶sub format_Ol { roman(format_l(@_)) }
Subroutine format_Ol: called CC=1 SCAR=0.0
Pod undocumented
229 5 ▶sub format_Om { roman(format_m(@_)) }
Subroutine format_Om: called CC=1 SCAR=0.0
Pod undocumented
230 5 ▶sub format_OM { roman(format_M(@_)) }
Subroutine format_OM: called CC=1 SCAR=0.0
Pod undocumented
231 5 ▶sub format_Oq { roman(format_q(@_)) }
Subroutine format_Oq: called CC=1 SCAR=0.0
Pod undocumented
232 5 ▶sub format_Oy { roman(format_y(@_)) }
Subroutine format_Oy: called CC=1 SCAR=0.0
Pod undocumented
233 5 ▶sub format_OY { roman(format_Y(@_)) }
Subroutine format_OY: called CC=1 SCAR=0.0
Pod undocumented
234  
235 3 ▶sub format_G { int(($_[0]->[9] - 315993600) / 604800) }
Subroutine format_G: called CC=1 SCAR=0.0
Pod undocumented
236  
237 14 1;
238  
239 __END__
« Date/Language/Austrian.pm ↑ Summary Date/Format.pm »