Log4perl/DateFormat.pm

filter: Statementstmt 94.7% 127 / 134 Branchbran 90.3% 56 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 4 totaltotal 91.9% 205 / 223 CC 34 SCAR 22.0
CC
34
Cov
93%
rep
20.2
new
6.0
format
5.0
CRAP 9.1
« Log4perl/Config/PropertyConfigurator.pm ↑ Summary Log4perl/JavaMap/NTEventLogAppender.pm »
1 ###########################################
2 package Log::Log4perl::DateFormat;
3 ###########################################
4 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 54 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 54 our $GMTIME = 0;
10  
11 54 my @MONTH_NAMES = qw(
12 January February March April May June July
13 August September October November December);
14  
15 54 my @WEEK_DAYS = qw(
16 Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
17  
18 ###########################################
19 59 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod undocumented
20 ###########################################
21 59 my($class, $format) = @_;
22  
23 59 my $self = {
24 stack => [],
25 fmt => undef,
26 };
27  
28 59 bless $self, $class;
29  
30 # Predefined formats
31 59 ▶ if($format eq "ABSOLUTE") {
Branch: 4 branches
BranchTrueFalseTotal
if ($format eq 'ABSOLUTE') { } 1 58 59
elsif ($format eq 'DATE') { } 1 57 58
elsif ($format eq 'ISO8601') { } 1 56 57
elsif ($format eq 'APACHE') { } 1 55 56
32 1 $format = "HH:mm:ss,SSS";
33 } elsif($format eq "DATE") {
34 1 $format = "dd MMM yyyy HH:mm:ss,SSS";
35 } elsif($format eq "ISO8601") {
36 1 $format = "yyyy-MM-dd HH:mm:ss,SSS";
37 } elsif($format eq "APACHE") {
38 1 $format = "[EEE MMM dd HH:mm:ss yyyy]";
39 }
40  
41 59 ▶ if($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 59 0 59
42 59 $self->prepare($format);
43 }
44  
45 58 return $self;
46 }
47  
48 ###########################################
49 59 ▶sub prepare {
Subroutine prepare: called CC=4 SCAR=13.9
Pod undocumented
50 ###########################################
51 59 my($self, $format) = @_;
52  
53 # the actual DateTime spec allows for literal text delimited by
54 # single quotes; a single quote can be embedded in the literal
55 # text by using two single quotes.
56 #
57 # my strategy here is to split the format into active and literal
58 # "chunks"; active chunks are prepared using $self->rep() as
59 # before, while literal chunks get transformed to accommodate
60 # single quotes and to protect percent signs.
61 #
62 # motivation: the "recommended" ISO-8601 date spec for a time in
63 # UTC is actually:
64 #
65 # YYYY-mm-dd'T'hh:mm:ss.SSS'Z'
66  
67 59 my $fmt = "";
68  
69 59 foreach my $chunk ( split /('(?:''|[^'])*')/, $format ) {
70 75 ▶ if ( $chunk =~ /\A'(.*)'\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk =~ /\A'(.*)'\z/) { } 9 66 75
elsif ($chunk =~ /'/) { } 1 65 66
71 # literal text
72 9 my $literal = $1;
73 9 $literal =~ s/''/'/g;
74 9 $literal =~ s/\%/\%\%/g;
75 9 $fmt .= $literal;
76 } elsif ( $chunk =~ /'/ ) {
77 # single quotes should always be in a literal
78 1 croak "bad date format \"$format\": " .
79 "unmatched single quote in chunk \"$chunk\"";
80 } else {
81 # handle active chunks just like before
82 65 $chunk =~ s/(([GyMdhHmsSEeDFwWakKzZ])\2*)/$self->rep($1)/ge;
83 65 $fmt .= $chunk;
84 }
85 }
86  
87 58 return $self->{fmt} = $fmt;
88 }
89  
90 ###########################################
91 291 ▶sub rep {
Subroutine rep: called CC=20 SCAR=30.1
Pod undocumented
92 ###########################################
93 291 my ($self, $string) = @_;
94  
95 291 my $first = substr $string, 0, 1;
96 291 my $len = length $string;
97  
98 291 my $time=time();
99 291 my @g = gmtime($time);
100 291 my @t = localtime($time);
101 291 my $z = $t[1]-$g[1]+($t[2]-$g[2])*60+($t[7]-$g[7])*1440+
102 ($t[5]-$g[5])*(525600+(abs($t[7]-$g[7])>364)*1440);
103 291 my $offset = sprintf("%+.2d%.2d", $z/60, "00");
104  
105 #my ($s,$mi,$h,$d,$mo,$y,$wd,$yd,$dst) = localtime($time);
106  
107 # Here's how this works:
108 # Detect what kind of parameter we're dealing with and determine
109 # what type of sprintf-placeholder to return (%d, %02d, %s or whatever).
110 # Then, we're setting up an array, specific to the current format,
111 # that can be used later on to compute the components of the placeholders
112 # one by one when we get the components of the current time later on
113 # via localtime.
114
115 # So, we're parsing the "yyyy/MM" format once, replace it by, say
116 # "%04d:%02d" and store an array that says "for the first placeholder,
117 # get the localtime-parameter on index #5 (which is years since the
118 # epoch), add 1900 to it and pass it on to sprintf(). For the 2nd
119 # placeholder, get the localtime component at index #2 (which is hours)
120 # and pass it on unmodified to sprintf.
121
122 # So, the array to compute the time format at logtime contains
123 # as many elements as the original SimpleDateFormat contained. Each
124 # entry is a array ref, holding an array with 2 elements: The index
125 # into the localtime to obtain the value and a reference to a subroutine
126 # to do computations eventually. The subroutine expects the original
127 # localtime() time component (like year since the epoch) and returns
128 # the desired value for sprintf (like y+1900).
129  
130 # This way, we're parsing the original format only once (during system
131 # startup) and during runtime all we do is call localtime *once* and
132 # run a number of blazingly fast computations, according to the number
133 # of placeholders in the format.
134  
135 ###########
136 #G - epoch#
137 ###########
138 291 ▶ if($first eq "G") {
Branch: 14 branches
BranchTrueFalseTotal
if ($first eq 'G') { } 0 291 291
elsif ($first eq 'e') { } 1 290 291
elsif ($first eq 'y') { } 42 248 290
elsif ($first eq 'M') { } 40 208 248
elsif ($first eq 'd') { } 39 169 208
elsif ($first eq 'h') { } 6 163 169
elsif ($first eq 'H') { } 44 119 163
elsif ($first eq 'm') { } 44 75 119
elsif ($first eq 's') { } 44 31 75
elsif ($first eq 'E') { } 5 26 31
elsif ($first eq 'D') { } 6 20 26
elsif ($first eq 'a') { } 3 17 20
elsif ($first eq 'S') { } 16 1 17
elsif ($first eq 'Z') { } 0 1 1
139 # Always constant
140 0 return "AD";
141  
142 ###################
143 #e - epoch seconds#
144 ###################
145 } elsif($first eq "e") {
146 # index (0) irrelevant, but we return time() which
147 # comes in as 2nd parameter
148 1 ▶ push @{$self->{stack}}, [0, sub { return $_[1] }];
Subroutine __ANON__: called CC=1 SCAR=0.0
149 1 return "%d";
150  
151 ##########
152 #y - year#
153 ##########
154 } elsif($first eq "y") {
155 42 ▶ if($len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len >= 4) { } 41 1 42
156 # 4-digit year
157 41 ▶ push @{$self->{stack}}, [5, sub { return $_[0] + 1900 }];
Subroutine __ANON__: called CC=1 SCAR=0.0
158 41 return "%04d";
159 } else {
160 # 2-digit year
161 1 ▶ push @{$self->{stack}}, [5, sub { $_[0] % 100 }];
Subroutine __ANON__: called CC=1 SCAR=0.0
162 1 return "%02d";
163 }
164  
165 ###########
166 #M - month#
167 ###########
168 } elsif($first eq "M") {
169 40 ▶ if($len >= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len >= 3) { } 5 35 40
elsif ($len == 2) { } 34 1 35
170 # Use month name
171 5 ▶ push @{$self->{stack}}, [4, sub { return $MONTH_NAMES[$_[0]] }];
Subroutine __ANON__: called CC=1 SCAR=0.0
172 5 ▶ if($len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len >= 4) { } 1 4 5
173 1 return "%s";
174 } else {
175 4 return "%.3s";
176 }
177 } elsif($len == 2) {
178 # Use zero-padded month number
179 34 ▶ push @{$self->{stack}}, [4, sub { $_[0]+1 }];
Subroutine __ANON__: called CC=1 SCAR=0.0
180 34 return "%02d";
181 } else {
182 # Use zero-padded month number
183 1 ▶ push @{$self->{stack}}, [4, sub { $_[0]+1 }];
Subroutine __ANON__: called CC=1 SCAR=0.0
184 1 return "%d";
185 }
186  
187 ##################
188 #d - day of month#
189 ##################
190 } elsif($first eq "d") {
191 39 ▶ push @{$self->{stack}}, [3, sub { return $_[0] }];
Subroutine __ANON__: called CC=1 SCAR=0.0
192 39 return "%0" . $len . "d";
193  
194 ##################
195 #h - am/pm hour#
196 ##################
197 } elsif($first eq "h") {
198 6 ▶ push @{$self->{stack}}, [2, sub { ($_[0] % 12) || 12 }];
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_[0] % 12 or 12 4 0 4
199 6 return "%0" . $len . "d";
200  
201 ##################
202 #H - 24 hour#
203 ##################
204 } elsif($first eq "H") {
205 44 ▶ push @{$self->{stack}}, [2, sub { return $_[0] }];
Subroutine __ANON__: called CC=1 SCAR=0.0
206 44 return "%0" . $len . "d";
207  
208 ##################
209 #m - minute#
210 ##################
211 } elsif($first eq "m") {
212 44 ▶ push @{$self->{stack}}, [1, sub { return $_[0] }];
Subroutine __ANON__: called CC=1 SCAR=0.0
213 44 return "%0" . $len . "d";
214  
215 ##################
216 #s - second#
217 ##################
218 } elsif($first eq "s") {
219 44 ▶ push @{$self->{stack}}, [0, sub { return $_[0] }];
Subroutine __ANON__: called CC=1 SCAR=0.0
220 44 return "%0" . $len . "d";
221  
222 ##################
223 #E - day of week #
224 ##################
225 } elsif($first eq "E") {
226 5 ▶ push @{$self->{stack}}, [6, sub { $WEEK_DAYS[$_[0]] }];
Subroutine __ANON__: called CC=1 SCAR=0.0
227 5 ▶ if($len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len >= 4) { } 1 4 5
228 1 return "%${len}s";
229 } else {
230 4 return "%.3s";
231 }
232  
233 ######################
234 #D - day of the year #
235 ######################
236 } elsif($first eq "D") {
237 6 ▶ push @{$self->{stack}}, [7, sub { $_[0] + 1}];
Subroutine __ANON__: called CC=1 SCAR=0.0
238 6 return "%0" . $len . "d";
239  
240 ######################
241 #a - am/pm marker #
242 ######################
243 } elsif($first eq "a") {
244 3 ▶ push @{$self->{stack}}, [2, sub { $_[0] < 12 ? "AM" : "PM" }];
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 12 ? : 0 2 2
245 3 return "%${len}s";
246  
247 ######################
248 #S - milliseconds #
249 ######################
250 } elsif($first eq "S") {
251 16 push @{$self->{stack}},
252 16 ▶ [9, sub { substr sprintf("%06d", $_[0]), 0, $len }];
Subroutine __ANON__: called CC=1 SCAR=0.0
253 16 return "%s";
254  
255 ###############################
256 #Z - RFC 822 time zone -0800 #
257 ###############################
258 } elsif($first eq "Z") {
259 0 ▶ push @{$self->{stack}}, [10, sub { $offset }];
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 0 return "%s";
261  
262 #############################
263 #Something that's not defined
264 #(F=day of week in month
265 # w=week in year W=week in month
266 # k=hour in day K=hour in am/pm
267 # z=timezone
268 #############################
269 } else {
270 1 return "-- '$first' not (yet) implemented --";
271 }
272  
273 0 return $string;
274 }
275  
276 ###########################################
277 89 ▶sub format {
Subroutine format: called CC=5 SCAR=16.1
Pod undocumented
278 ###########################################
279 89 my($self, $secs, $msecs) = @_;
280  
281 89 ▶ $msecs = 0 unless defined $msecs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msecs 20 69 89
282  
283 89 my @time;
284  
285 89 ▶ if($GMTIME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Log4perl::DateFormat::GMTIME) { } 26 63 89
286 26 @time = gmtime($secs);
287 } else {
288 63 @time = localtime($secs);
289 }
290  
291 # add milliseconds
292 89 push @time, $msecs;
293  
294 89 my @values = ();
295  
296 89 for(@{$self->{stack}}) {
297 469 my($val, $code) = @$_;
298 469 ▶ if($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 469 0 469
299 469 push @values, $code->($time[$val], $secs);
300 } else {
301 0 push @values, $time[$val];
302 }
303 }
304  
305 89 return sprintf($self->{fmt}, @values);
306 }
307  
308 54 1;
309  
310 __END__
« Log4perl/Config/PropertyConfigurator.pm ↑ Summary Log4perl/JavaMap/NTEventLogAppender.pm »