blib/lib/DateTime/Format/GnuAt.pm

filter: Statementstmt 71.0% 103 / 145 Branchbran 61.8% 47 / 76 Conditioncond 60.0% 12 / 20 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 67.7% 181 / 267 CC 47 SCAR 37.3
CC
47
Cov
67%
_parse_date
94.0
_parse_time
38.0
_parse_spec_base
8.0
CRAP 41.5
↑ Summary
1 package DateTime::Format::GnuAt;
2  
3 1 our $VERSION = '0.03';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 my @periods = qw(minute min hour day week month year);
11 1 my $period_re = join '|', @periods;
12 1 $period_re = qr/(?:$period_re)/i;
13  
14 16 ▶sub _period {
Subroutine _period: called CC=2 SCAR=6.9
15 16 my $period = lc shift;
16 16 ▶ return ($period eq 'min' ? 'minutes' : $period.'s');
Branch: 1 branch
BranchTrueFalseTotal
$period eq 'min' ? : 4 12 16
17 }
18  
19 1 my (%month, %wday);
20 1 my @months = qw(january february march april may june july august
21 september october november december);
22 1 @month{map substr($_, 0, 3), @months} = (1..12);
23  
24 1 my @wdays = qw(monday tuesday wednesday thursday friday saturday sunday);
25 1 @wday{map substr($_, 0, 3), @wdays} = (1..7);
26  
27 2 ▶sub _make_alternation_re {
Subroutine _make_alternation_re: called CC=2 SCAR=6.9
28 my $re = join '|',
29 map {
30 2 ▶ substr($_, 0, 3) . (length > 3 ? '(?:' . substr($_, 3) . ')?' : '')
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 3 ? : 18 1 19
31 } @_;
32 2 return qr/(?:$re)\b/i;
33 }
34  
35 1 my $month_re = _make_alternation_re(@months);
36 1 my $wday_re = _make_alternation_re(@wdays );
37  
38 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 1 my $class = shift;
40 1 my $self = {};
41 1 bless $self, $class;
42 }
43  
44 29 ▶sub _reset {
Subroutine _reset: called CC=2 SCAR=7.0
45 29 my ($self, $opts) = @_;
46 29 %{$self} = ();
47 29 my $now = delete $opts->{now};
48 29 ▶ $self->{now} = (defined $now ? $now->clone : DateTime->now(time_zone => 'local'));
Branch: 1 branch
BranchTrueFalseTotal
defined $now ? : 29 0 29
49 29 $self->{now}->set(second => 0);
50 }
51  
52 29 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=5 SCAR=16.1
Pod documented
53 29 my ($self, $spec, %opts) = @_;
54  
55 29 $self->_reset(\%opts);
56  
57 29 for ($spec) {
58 29 local ($@, $SIG{__DIE__});
59 29 eval {
60 29 /^\s*/gc;
61  
62 29 ▶ if ($self->_parse_spec_base()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parse_spec_base) 24 1 25
63 24 /\G\s*/gc;
64 24 $self->_parse_inc_or_dec;
65 }
66 25 /\G\s*/gc;
67 25 ▶ /\G\S/gc and die "unparsed rubbish";
Branch: 1 branch
BranchTrueFalseTotal
/\G\S/cg and die "unparsed rubbish" 1 24 25
68  
69 24 $self->{date}->set_time_zone('UTC');
70 };
71 29 $self->{error} = $@;
72 29 ▶ return $self->{date} unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 24 5 29
73 }
74  
75 5 croak "unable to parse datetime specification '$spec'";
76 }
77  
78 29 ▶sub _parse_spec_base {
Subroutine _parse_spec_base: called CC=8 SCAR=20.8
79 29 my $self = shift;
80 29 ▶ if ($self->_parse_date) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_parse_date) { } 9 16 25
elsif ($self->_parse_time) { } 15 1 16
81 9 return 1;
82 }
83 elsif ($self->_parse_time) {
84 15 my $pos = pos;
85 15 ▶ unless (/\G\s+/gc and $self->_parse_date) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G\s+/cg and $self->_parse_date) 6 9 15
Condition100%3/3
/\G\s+/cg and $self->_parse_date
!ll&&!rl&&r
339
MC/DC100%2/2
/\G\s+/cg and $self->_parse_date
/\G\s+/cg $self->_parse_date
Truth table100%3/3
A and B
A= /\G\s+/cgB= $self->_parse_date
ABresult
0X0
100
111
86 6 pos = $pos;
87 6 my $base = $self->{now};
88 6 my $base_hour = $base->hour;
89 6 my $base_min = $base->min;
90 6 ▶ if ( ( $base_hour > $self->{hour} ) or
Branch: 1 branch
BranchTrueFalseTotal
if ($base_hour > $self->{"hour"} or $base_hour == $self->{"hour"} and $base_min >= $self->{"min"}) 3 3 6
Condition66%4/6
$base_hour > $self->{"hour"} or $base_hour == $self->{"hour"} and $base_min >= $self->{"min"}
l!l&&r!l&&!r
033
$base_hour == $self->{"hour"} and $base_min >= $self->{"min"}
!ll&&!rl&&r
303
MC/DC33%1/3
$base_hour > $self->{"hour"} or $base_hour == $self->{"hour"} and $base_min >= $self->{"min"}
$base_hour > $self->{"hour"} $base_hour == $self->{"hour"} $base_min >= $self->{"min"}
Truth table50%2/4
A or B and C
A= $base_hour > $self->{"hour"}B= $base_hour == $self->{"hour"}C= $base_min >= $self->{"min"}
ABCresult
1XX1
0111
00X0
0100
91 ( ( $base_hour == $self->{hour} ) and
92 ( $base_min >= $self->{min} ) ) ) {
93 3 $base = $base->add(days => 1);
94 }
95 6 $self->{date} = $base;
96 }
97  
98 $self->{date}->set(hour => $self->{hour},
99 minute => $self->{min},
100 15 second => 0);
101  
102  
103 15 return 1;
104 }
105 return
106 1 }
107  
108 41 ▶sub _parse_date {
Subroutine _parse_date: called CC=20 SCAR=45.4
109 41 my $self = shift;
110  
111 41 my $now = $self->{now};
112  
113 41 ▶ if (/\G($month_re)\s+(\d\d?)(?:(?:\s+|\s*,\s*)(\d\d(?:\d\d)?))?/gco) {
Branch: 11 branches
BranchTrueFalseTotal
if (/\G($month_re)\s+(\d\d?)(?:(?:\s+|\s*,\s*)(\d\d(?:\d\d)?))?/cog) { } 13 28 41
elsif (/\G(?:next\s+)?($wday_re)/cgio) { } 0 28 28
elsif (/\Gtoday\b/cig) { } 0 28 28
elsif (/\Gtomorrow\b/cig) { } 0 28 28
elsif (/\G(\d\d?)\.(\d\d?)\.(\d\d(?:\d\d)?)\b/cg) { } 0 28 28
elsif (/\G(\d\d(?:\d\d)?)-(\d\d?)-(\d\d?)\b/cg) { } 0 28 28
elsif (/\Gnow\b/cig) { } 9 19 28
elsif (/\G(\d\d?)\s+($month_re)(?:\s+(\d\d(?:\d\d)?))?/cog) { } 0 19 19
elsif (m[\G(\d\d?)/(\d\d?)/(\d\d(?:\d\d)?)\b]cg) { } 0 19 19
elsif (/\G(\d\d?)(\d\d)(\d\d(?:\d\d)?)\b/cg) { } 0 19 19
elsif (/\Gnext\s+($period_re)\b/cgio) { } 0 19 19
114 # month_name day_number
115 # month_name day_number year_number
116 # month_name day_number ',' year_number
117 13 @{$self}{qw(month_name day year)} = ($1, $2, $3);
118 }
119 elsif (/\G(?:next\s+)?($wday_re)/gcio) {
120 # day_of_week
121 0 $self->{wday_name} = $1;
122 0 my $wday = $self->{wday} = $wday{lc substr $1, 0, 3};
123 0 my $delta = $wday - $now->day_of_week;
124 0 ▶ $delta += 7 if $delta <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delta <= 0 0 0 0
125 0 $self->{date} = $now->add(days => $delta);
126 0 return 1;
127 }
128 elsif (/\Gtoday\b/gci) {
129 # TODAY
130 0 $self->{today} = 1;
131 0 $self->{date} = $now;
132 0 return 1;
133 }
134 elsif (/\Gtomorrow\b/gci) {
135 # TOMORROW
136 0 $self->{tomorrow} = 1;
137 0 $self->{date} = $now->add(days => 1);
138 0 return 1;
139 }
140 elsif (/\G(\d\d?)\.(\d\d?)\.(\d\d(?:\d\d)?)\b/gc) {
141 # DOTTEDDATE (dd.mm.[cc]yy)
142 0 @{$self}{qw(day month year)} = ($1, $2, $3);
143 }
144 elsif (/\G(\d\d(?:\d\d)?)-(\d\d?)-(\d\d?)\b/gc) {
145 # HYPHENDATE ([cc]yy-mm-dd)
146 0 @{$self}{qw(year month day)} = ($1, $2, $3);
147 }
148 elsif (/\Gnow\b/gci) {
149 # NOW
150 9 $self->{is_now} = 1;
151 9 $self->{date} = $now;
152 9 return 1;
153 }
154 elsif (/\G(\d\d?)\s+($month_re)(?:\s+(\d\d(?:\d\d)?))?/gco) {
155 # day_number month_name
156 # day_number month_name year_number
157 0 @{$self}{qw(day month_name year)} = ($1, $2, $3);
158 }
159 elsif (/\G(\d\d?)\/(\d\d?)\/(\d\d(?:\d\d)?)\b/gc) {
160 # month_number '/' day_number '/' year_number
161 0 @{$self}{qw(month day year)} = ($1, $2, $3);
162 }
163 elsif (/\G(\d\d?)(\d\d)(\d\d(?:\d\d)?)\b/gc) {
164 # concatenated_date (m[m]dd[cc]yy)
165 0 @{$self}{qw(month day year)} = ($1, $2, $3);
166 }
167 elsif (/\Gnext\s+($period_re)\b/gcio) {
168 # NEXT inc_dec_period
169 0 $self->{next_period} = $1;
170 0 $self->{date} = $now->add(_period($1) => 1);
171 0 return 1;
172 }
173 else {
174 19 return;
175 }
176  
177 13 ▶ $self->{month} //= $month{lc substr $self->{month_name}, 0, 3};
Condition33%1/3
$self->{"month"} //= $month{lc substr($self->{"month_name"}, 0, 3)}
l!l&&r!l&&!r
0130
MC/DC0%0/2
$self->{"month"} //= $month{lc substr($self->{"month_name"}, 0, 3)}
$self->{"month"} $month{lc substr($self->{"month_name"}, 0, 3)}
Truth table33%1/3
A //= B
A= $self->{"month"}B= $month{lc substr($self->{"month_name"}, 0, 3)}
ABresult
1X1
011
000
178  
179 13 ▶ if (defined (my $year = $self->{year})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $year = $self->{'year'})) { } 0 13 13
180 0 ▶ if (length $year <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $year <= 2) { } 0 0 0
181 0 ▶ $self->{year4} = $year + ($year < 70 ? 2000 : 1900);
Branch: 1 branch
BranchTrueFalseTotal
$year < 70 ? : 0 0 0
182 }
183 else {
184 0 $self->{year4} = $year;
185 }
186 }
187 else {
188 13 my $now_day = $now->day;
189 13 my $now_month = $now->month;
190 13 $self->{year4} = $now->year;
191 $self->{year4}++ if ( ($now_month > $self->{month}) or
192 ( ($now_month == $self->{month}) and
193 13 ▶ ($now_day > $self->{day}) ) );
Branch: 1 branch
BranchTrueFalseTotal
if $now_month > $self->{"month"} or $now_month == $self->{"month"} and $now_day > $self->{"day"} 7 6 13
Condition50%3/6
$now_month > $self->{"month"} or $now_month == $self->{"month"} and $now_day > $self->{"day"}
l!l&&r!l&&!r
706
$now_month == $self->{"month"} and $now_day > $self->{"day"}
!ll&&!rl&&r
600
MC/DC33%1/3
$now_month > $self->{"month"} or $now_month == $self->{"month"} and $now_day > $self->{"day"}
$now_month > $self->{"month"} $now_month == $self->{"month"} $now_day > $self->{"day"}
Truth table50%2/4
A or B and C
A= $now_month > $self->{"month"}B= $now_month == $self->{"month"}C= $now_day > $self->{"day"}
ABCresult
1XX1
0111
00X0
0100
194 }
195  
196 $self->{date} = DateTime->new(year => $self->{year4},
197 month => $self->{month},
198 day => $self->{day},
199 13 hour => $now->hour,
200 minute => $now->minute,
201 time_zone => $now->time_zone);
202  
203 9 return 1;
204  
205  
206 }
207  
208 16 ▶sub _parse_time {
Subroutine _parse_time: called CC=12 SCAR=36.4
209 16 my $self = shift;
210  
211 16 ▶ if (/\G(\d\d)(\d\d)\b/gc) {
Branch: 5 branches
BranchTrueFalseTotal
if (/\G(\d\d)(\d\d)\b/cg) { } 0 16 16
elsif (/\G(([012]?[0-9])(?:[:'h,.](\d\d))?(?:\s*([ap]m))?\b)/cig) { } 15 1 16
elsif (/\Gnoon\b/cg) { } 0 1 1
elsif (/\Gmidnight\b/cg) { } 0 1 1
elsif (/\Gteatime\b/cg) { } 0 1 1
212 # hr24clock_hr_min (hhmm)
213 0 @{$self}{qw(hour min)} = ($1, $2);
214 }
215 elsif (/\G(([012]?[0-9])(?:[:'h,.](\d\d))?(?:\s*([ap]m))?\b)/gci) {
216 # time_hour am_pm
217 # time_hour_min
218 # time_hour_min am_pm
219 15 ▶ @{$self}{qw(hour min am_pm)} = ($2, ($3 // 0), $4);
Condition50%1/2
$3 // 0
l!l
150
MC/DC0%0/1
$3 // 0
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
220  
221 15 ▶ if (defined $4) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $4) 0 15 15
222 0 my $hour = $2;
223 0 ▶ if ($hour > 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hour > 11) 0 0 0
224 0 ▶ $hour > 12 and return;
Branch: 1 branch
BranchTrueFalseTotal
$hour > 12 and return 0 0 0
225 0 $hour = 0;
226 }
227 0 ▶ $hour += 12 if lc($4) eq 'pm';
Branch: 1 branch
BranchTrueFalseTotal
if lc $4 eq "pm" 0 0 0
228 0 $self->{hour} = $hour;
229 }
230 }
231 elsif (/\Gnoon\b/gc) {
232 0 @{$self}{qw(hour min noon)} = (12, 0, 1);
233 }
234 elsif (/\Gmidnight\b/gc) {
235 0 @{$self}{qw(hour min midnight)} = (0, 0, 1);
236 }
237 elsif (/\Gteatime\b/gc) {
238 0 @{$self}{qw(hour min teatime)} = (16, 0, 1);
239 }
240 else {
241 return
242 1 }
243  
244 15 ▶ if (/\G\s*(utc)\b/gci) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\s*(utc)\b/cig) 1 14 15
245 1 $self->{tz} = uc $1;
246 1 $self->{now}->set_time_zone($self->{tz});
247 }
248  
249 15 return 1;
250 }
251  
252 24 ▶sub _parse_inc_or_dec {
Subroutine _parse_inc_or_dec: called CC=3 SCAR=11.0
253 24 my $self = shift;
254  
255 24 ▶ if (/\G([+-])\s*(\d+)\s*($period_re)s?\b/gci) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G([+-])\s*(\d+)\s*($period_re)s?\b/cig) 16 8 24
256 16 @{$self}{qw(increment increment_period)} = ("$1$2", $3);
257 16 ▶ my $method = ($1 eq '+' ? 'add' : 'subtract');
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '+' ? : 16 0 16
258 16 $self->{date} = $self->{date}->$method(_period($3) => $2);
259  
260 16 return 1;
261 }
262 8 return;
263 }
264  
265 1 1;
266 __END__
↑ Summary