blib/lib/DateTime/Format/Mail.pm

filter: Statementstmt 100% 123 / 123 Branchbran 73.2% 41 / 56 Conditioncond 73.9% 17 / 23 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 22 / 22 Podpod 100% 10 / 10 totaltotal 88.6% 219 / 247 CC 38 SCAR 13.7
CC
38
Cov
90%
_determine_timezone
8.0
parse_datetime
6.0
new
5.0
CRAP 3.9
↑ Summary
1 package DateTime::Format::Mail;
2 # $Id$
3 8 $DateTime::Format::Mail::VERSION = '0.403';
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use DateTime 1.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Params::Validate qw( validate validate_pos SCALAR );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 my %validations = (
12 year_cutoff => {
13 type => SCALAR,
14 callbacks => {
15 18 ▶ 'greater than or equal to zero, less than 100' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
16 18 ▶ defined $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /^ \d+ $/x and $_[0] >= 0 and $_[0] < 100 17 1 18
Condition66%4/6
defined $_[0] and $_[0] =~ /^ \d+ $/x and $_[0] >= 0
!ll&&!rl&&r
1017
defined $_[0] and $_[0] =~ /^ \d+ $/x
!ll&&!rl&&r
0117
MC/DC33%1/3
defined $_[0] and $_[0] =~ /^ \d+ $/x and $_[0] >= 0
defined $_[0] $_[0] =~ /^ \d+ $/x $_[0] >= 0
Truth table50%2/4
A and B and C
A= defined $_[0]B= $_[0] =~ /^ \d+ $/xC= $_[0] >= 0
ABCresult
0XX0
10X0
1100
1111
17 and $_[0] =~ /^ \d+ $/x
18 and $_[0] >= 0
19 and $_[0] < 100
20 },
21 },
22 }
23 8 );
24  
25 # Timezones for strict parser.
26 8 my %timezones = qw(
27 EDT -0400 EST -0500 CDT -0500 CST -0600
28 MDT -0600 MST -0700 PDT -0700 PST -0800
29 GMT +0000 UT +0000
30 );
31 8 my $tz_RE = join( '|', sort keys %timezones );
32 8 $tz_RE= qr/(?:$tz_RE)/;
33 8 $timezones{UTC} = $timezones{UT};
34  
35 # Strict parser regex
36  
37 # Lovely regex. Mostly a translation of the BNF in 2822.
38 # XXX - need more thorough tests to ensure it's *strict*.
39  
40 8 my $strict_RE = qr{
41 ^ \s* # optional
42 # [day-of-week "," ]
43 (?:
44 (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) ,
45 \s+
46 )?
47 # date => day month year
48 (\d{1,2}) # day => 1*2DIGIT
49 \s+
50 (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) # month-name
51 \s*
52 ((?:\d\d)?\d\d) # year
53 # FWS
54 \s+
55 # time
56 (\d\d):(\d\d):(\d\d) # time
57 (?:
58 \s+ (
59 [+-] \d{4} # standard form
60 | $tz_RE # obsolete form (mostly ignored)
61 | [A-IK-Za-ik-z] # including military (no 'J')
62 ) # time zone (optional)
63 )?
64 \s* $
65 }ox;
66  
67 # Loose parser regex
68 8 my $loose_RE = qr{
69 ^ \s* # optional
70 (?i:
71 (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun|[A-Z][a-z][a-z]) ,? # Day name + comma
72 )?
73 # (empirically optional)
74 \s*
75 (\d{1,2}) # day of month
76 [-\s]*
77 (?i: (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ) # month
78 [-\s]*
79 ((?:\d\d)?\d\d) # year
80 \s+
81 (\d?\d):(\d?\d) (?: :(\d?\d) )? # time
82 (?:
83 \s+ "? (
84 [+-] \d{4} # standard form
85 | [A-Z]+ # obsolete form (mostly ignored)
86 | GMT [+-] \d+ # empirical (converted)
87 | [A-Z]+\d+ # bizarre empirical (ignored)
88 | [a-zA-Z/]+ # linux style (ignored)
89 | [+-]{0,2} \d{3,5} # corrupted standard form
90 ) "? # time zone (optional)
91 )?
92 (?: \s+ \([^\)]+\) )? # (friendly tz name; empirical)
93 \s* \.? $
94 }x;
95  
96 sub _set_parse_method
97 15 ▶{
Subroutine _set_parse_method: called CC=2 SCAR=7.0
98 15 my $self = shift;
99 15 ▶ croak "Calling object method as class method!" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 15 15
100 15 $self->{parser_method} = shift;
101 15 return $self;
102 }
103  
104 sub _get_parse_method
105 29557 ▶{
Subroutine _get_parse_method: called CC=3 SCAR=11.7
106 29557 my $self = shift;
107 29557 ▶ my $method = ref($self) ? $self->{parser_method} : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 29557 0 29557
108 29557 ▶ $method ||= '_parse_strict';
Condition50%1/2
$method ||= "_parse_strict"
l!l
295570
MC/DC0%0/1
$method ||= "_parse_strict"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
109 }
110  
111 sub new
112 13 ▶{
Subroutine new: called CC=5 SCAR=16.1
Pod documented
113 13 my $class = shift;
114 my %args = validate( @_, {
115 loose => {
116 type => SCALAR,
117 default => 0,
118 },
119 year_cutoff => {
120 13 %{ $validations{year_cutoff} },
121 default => $class->default_cutoff,
122 },
123 }
124 );
125  
126 12 ▶ my $self = bless {}, ref($class)||$class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
390
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
127 12 ▶ if (ref $class)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 3 9 12
128 {
129 # If called on an object, clone
130 3 $self->_set_parse_method( $class->_get_parse_method );
131 3 $self->set_year_cutoff( $class->year_cutoff );
132 # and that's it. we don't store much info per object
133 }
134 else
135 {
136 9 ▶ my $parser = $args{loose} ? "loose" : "strict";
Branch: 1 branch
BranchTrueFalseTotal
$args{'loose'} ? : 1 8 9
137 9 $self->$parser();
138 9 ▶ $self->set_year_cutoff( $args{year_cutoff} ) if $args{year_cutoff};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"year_cutoff"} 9 0 9
139 }
140  
141 12 $self;
142 }
143  
144 sub clone
145 1 ▶{
Subroutine clone: called CC=2 SCAR=7.1
Pod documented
146 1 my $self = shift;
147 1 ▶ croak "Calling object method as class method!" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
148 1 return $self->new();
149 }
150  
151 sub loose
152 4 ▶{
Subroutine loose: called CC=2 SCAR=7.1
Pod documented
153 4 my $self = shift;
154 4 ▶ croak "loose() takes no arguments!" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
155 4 return $self->_set_parse_method( '_parse_loose' );
156 }
157  
158 sub strict
159 8 ▶{
Subroutine strict: called CC=2 SCAR=7.1
Pod documented
160 8 my $self = shift;
161 8 ▶ croak "strict() takes no arguments!" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 8 8
162 8 return $self->_set_parse_method( '_parse_strict' );
163 }
164  
165 sub _parse_strict
166 4 ▶{
Subroutine _parse_strict: called CC=2 SCAR=7.0
167 4 my $self = shift;
168 4 my $date = shift;
169  
170 # Wed, 12 Mar 2003 13:05:00 +1100
171 4 my @parsed = $date =~ $strict_RE;
172 4 ▶ croak "Invalid format for date!" unless @parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless @parsed 0 4 4
173 4 my %when;
174 4 @when{qw( day month year hour minute second time_zone)} = @parsed;
175 4 return \%when;
176 }
177  
178 sub _parse_loose
179 29550 ▶{
Subroutine _parse_loose: called CC=3 SCAR=11.0
180 29550 my $self = shift;
181 29550 my $date = shift;
182  
183 # Wed, 12 Mar 2003 13:05:00 +1100
184 29550 my @parsed = $date =~ $loose_RE;
185 29550 ▶ croak "Invalid format for date!" unless @parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless @parsed 22 29528 29550
186 29528 my %when;
187 29528 @when{qw( day month year hour minute second time_zone)} = @parsed;
188 29528 $when{month} = "\L\u$when{month}";
189 29528 ▶ $when{second} ||= 0;
Condition100%2/2
$when{"second"} ||= 0
l!l
266372891
MC/DC100%1/1
$when{"second"} ||= 0
$when{"second"}
Truth table100%2/2
A
A= $when{"second"}
Aresult
11
00
190 29528 return \%when;
191 }
192  
193 sub parse_datetime
194 29554 ▶{
Subroutine parse_datetime: called CC=6 SCAR=18.0
Pod documented
195 29554 my $self = shift;
196 29554 ▶ croak "No date specified for parse_datetime." unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 29554 29554
197 29554 ▶ croak "Too many arguments to parse_datetime." if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 29554 29554
198 29554 my $date = shift;
199  
200 # Wed, 12 Mar 2003 13:05:00 +1100
201 29554 my $method = $self->_get_parse_method();
202 29554 my %when = %{ $self->$method($date) };
203 29532 ▶ $when{time_zone} ||= '-0000';
Condition100%2/2
$when{"time_zone"} ||= "-0000"
l!l
295311
MC/DC100%1/1
$when{"time_zone"} ||= "-0000"
$when{"time_zone"}
Truth table100%2/2
A
A= $when{"time_zone"}
Aresult
11
00
204  
205 29532 my %months = do { my $i = 1;
206 29532 map { $_, $i++ } qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
207 };
208 $when{month} = $months{$when{month}}
209 29532 ▶ or croak "Invalid month `$when{month}'.";
Branch: 1 branch
BranchTrueFalseTotal
$when{"month"} = $months{$when{"month"}} or croak(qq[Invalid month `$when{"month"}'.]) 29532 0 29532
210  
211 29532 $when{year} = $self->fix_year( $when{year} );
212 29532 $when{time_zone} = _determine_timezone( $when{time_zone} );
213 29532 ▶ $when{time_zone} = 'floating' if $when{time_zone} eq '-0000';
Branch: 1 branch
BranchTrueFalseTotal
if $when{"time_zone"} eq "-0000" 5596 23936 29532
214  
215 29532 my $date_time = DateTime->new( %when );
216  
217 29532 return $date_time;
218 }
219  
220 sub _determine_timezone
221 29544 ▶{
Subroutine _determine_timezone: called CC=8 SCAR=20.8
222 29544 my $tz = shift;
223 29544 ▶ return '-0000' unless defined $tz; # return quickly if nothing needed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tz 0 29544 29544
224 29544 ▶ return $tz if $tz =~ /^[+-]\d{4}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $tz =~ /^[+-]\d{4}$/ 29470 74 29544
225  
226 74 $tz =~ s/ ^ [+-] (?=[+-]) //x; # for when there are two signs
227  
228 74 ▶ if (exists $timezones{$tz}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $timezones{$tz}) { } 19 55 74
elsif (substr($tz, 0, 3) eq 'GMT' and length $tz > 4) { } 9 46 55
elsif ($tz =~ /^ ([+-]?) (\d+) $/x) { } 20 26 46
229 19 $tz = $timezones{$tz};
230 ▶ } elsif (substr($tz, 0, 3) eq 'GMT' and length($tz) > 4) {
Condition66%2/3
substr($tz, 0, 3) eq "GMT" and length $tz > 4
!ll&&!rl&&r
4609
MC/DC50%1/2
substr($tz, 0, 3) eq "GMT" and length $tz > 4
substr($tz, 0, 3) eq "GMT" length $tz > 4
Truth table66%2/3
A and B
A= substr($tz, 0, 3) eq "GMT"B= length $tz > 4
ABresult
0X0
100
111
231 9 $tz = sprintf "%5.5s", substr($tz,3)."0000";
232 } elsif ( $tz =~ /^ ([+-]?) (\d+) $/x) {
233 20 ▶ my $p = $1||'+';
Condition100%2/2
$1 || '+'
l!l
137
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
234 20 $tz = sprintf "%s%04d", $p, $2;
235 } else {
236 26 $tz = "-0000";
237 }
238  
239 74 return $tz;
240 }
241  
242 sub set_year_cutoff
243 18 ▶{
Subroutine set_year_cutoff: called CC=3 SCAR=11.2
Pod documented
244 18 my $self = shift;
245 18 ▶ croak "Calling object method as class method!" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 18 18
246 18 validate_pos( @_, $validations{year_cutoff} );
247 13 ▶ croak "Wrong number of arguments (should be 1) to set_year_cutoff"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 13 13
248 unless @_ == 1;
249 13 my $cutoff = shift;
250 13 $self->{year_cutoff} = $cutoff;
251 13 return $self;
252 }
253  
254 # rfc2822, 4.3. Obsolete Date and Time
255 # Where a two or three digit year occurs in a date, the year is to be
256 # interpreted as follows: If a two digit year is encountered whose
257 # value is between 00 and 49, the year is interpreted by adding 2000,
258 # ending up with a value between 2000 and 2049. If a two digit year is
259 # encountered with a value between 50 and 99, or any three digit year
260 # is encountered, the year is interpreted by adding 1900.
261 sub default_cutoff
262 19 ▶{
Subroutine default_cutoff: called CC=1 SCAR=0.0
Pod documented
263 19 49;
264 }
265  
266 sub year_cutoff
267 21 ▶{
Subroutine year_cutoff: called CC=4 SCAR=14.2
Pod documented
268 21 my $self = shift;
269 21 ▶ croak "Too many arguments (should be 0) to year_cutoff" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 21 21
270 21 ▶ (ref $self and $self->{year_cutoff}) or $self->default_cutoff;
Branch: 1 branch
BranchTrueFalseTotal
ref $self and $self->{"year_cutoff"} or $self->default_cutoff 15 6 21
Condition66%2/3
ref $self and $self->{"year_cutoff"}
!ll&&!rl&&r
6015
MC/DC50%1/2
ref $self and $self->{"year_cutoff"}
ref $self $self->{"year_cutoff"}
Truth table66%2/3
A and B
A= ref $selfB= $self->{"year_cutoff"}
ABresult
0X0
100
111
271 }
272  
273 sub fix_year
274 29548 ▶{
Subroutine fix_year: called CC=3 SCAR=11.0
Pod documented
275 29548 my $self = shift;
276 29548 my $year = shift;
277 29548 ▶ return $year if length $year >= 4; # Return quickly if we can
Branch: 1 branch
BranchTrueFalseTotal
if length $year >= 4 29533 15 29548
278  
279 15 my $cutoff = $self->year_cutoff;
280 15 ▶ $year += $year > $cutoff ? 1900 : 2000;
Branch: 1 branch
BranchTrueFalseTotal
$year > $cutoff ? : 6 9 15
281 15 return $year;
282 }
283  
284 sub format_datetime
285 32 ▶{
Subroutine format_datetime: called CC=3 SCAR=11.0
Pod documented
286 32 my $self = shift;
287 32 ▶ croak "No DateTime object specified." unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 32 32
288 32 my $dt = $_[0]->clone;
289 32 $dt->set_locale('en_US');
290  
291 32 my $rv = $dt->strftime( "%a, %e %b %Y %H:%M:%S %z" );
292 32 ▶ $rv =~ s/\+0000$/-0000/ if $dt->time_zone->is_floating;
Branch: 1 branch
BranchTrueFalseTotal
if $dt->time_zone->is_floating 5 27 32
293 32 $rv;
294 }
295  
296 8 1;
297  
298 __END__
↑ Summary