blib/lib/Mo/utils/Date.pm

filter: Statementstmt 100% 100 / 100 Branchbran 100% 50 / 50 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 100% 178 / 178 CC 28 SCAR 15.9
CC
28
Cov
100%
check_date_order
8.0
check_date
5.0
check_date_dmy
5.0
CRAP 4.9
↑ Summary
1 package Mo::utils::Date;
2  
3 7 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 Readonly::Array our @EXPORT_OK => qw(check_date check_date_dmy check_date_ddmmyy
13 check_date_yymmdd check_date_order);
14  
15 7 our $VERSION = 0.05;
16  
17 20 ▶sub check_date {
Subroutine check_date: called CC=5 SCAR=16.1
Pod documented
18 20 my ($self, $key) = @_;
19  
20 20 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 19 20
21 1 return;
22 }
23  
24 19 ▶ if (! defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$key}) 1 18 19
25 1 return;
26 }
27  
28 # Check year format.
29 18 ▶ if ($self->{$key} !~ m/^\-?(\d{1,4})\-?\d{0,2}\-?\d{0,2}$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key} =~ /^\-?(\d{1,4})\-?\d{0,2}\-?\d{0,2}$/ms) 1 17 18
30 err "Parameter '$key' is in bad format.",
31 1 'Value' => $self->{$key},
32 ;
33 }
34 17 my $year = $1;
35  
36 # Check year greater than actual.
37 17 ▶ if ($year > DateTime->now->year) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year > "DateTime"->now->year) 1 16 17
38 1 err "Parameter '$key' has year greater than actual year.";
39 }
40  
41 16 return;
42 }
43  
44 9 ▶sub check_date_dmy {
Subroutine check_date_dmy: called CC=5 SCAR=16.1
Pod documented
45 9 my ($self, $key) = @_;
46  
47 9 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 8 9
48 1 return;
49 }
50  
51 8 ▶ if (! defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$key}) 1 7 8
52 1 return;
53 }
54  
55 7 ▶ if ($self->{$key} !~ m/^(\d{1,2}).(\d{1,2}).(\d{4})$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key} =~ /^(\d{1,2}).(\d{1,2}).(\d{4})$/ms) 1 6 7
56 err "Parameter '$key' is in bad format.",
57 1 'Value' => $self->{$key},
58 ;
59 }
60 6 my ($day, $month, $year) = ($1, $2, $3);
61 6 eval {
62 6 DateTime->new(
63 'day' => $1,
64 'month' => $2,
65 'year' => $3,
66 );
67 };
68 6 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mo::utils::Date::EVAL_ERROR) 1 5 6
69 err "Parameter '$key' is bad date.",
70 1 'Value' => $self->{$key},
71 'DateTime error' => $EVAL_ERROR,
72 ;
73 }
74  
75 5 return;
76 }
77  
78 6 ▶sub check_date_ddmmyy {
Subroutine check_date_ddmmyy: called CC=5 SCAR=16.1
Pod documented
79 6 my ($self, $key) = @_;
80  
81 6 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 5 6
82 1 return;
83 }
84  
85 5 ▶ if (! defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$key}) 1 4 5
86 1 return;
87 }
88  
89 4 ▶ if ($self->{$key} !~ m/^(\d{2})(\d{2})(\d{2})$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key} =~ /^(\d{2})(\d{2})(\d{2})$/ms) 2 2 4
90 err "Parameter '$key' is in bad date format.",
91 2 'Value', $self->{$key},
92 ;
93 }
94 2 my ($day, $month, $year) = ($1, $2, $3);
95 2 eval {
96 2 DateTime->new(
97 'day' => $day,
98 'month' => $month,
99 'year' => 2000 + $year,
100 );
101 };
102 2 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mo::utils::Date::EVAL_ERROR) 1 1 2
103 err "Parameter '$key' is bad date.",
104 1 'Value' => $self->{$key},
105 'DateTime error' => $EVAL_ERROR,
106 ;
107 }
108  
109 1 return;
110 }
111  
112 6 ▶sub check_date_yymmdd {
Subroutine check_date_yymmdd: called CC=5 SCAR=16.1
Pod documented
113 6 my ($self, $key) = @_;
114  
115 6 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 1 5 6
116 1 return;
117 }
118  
119 5 ▶ if (! defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$key}) 1 4 5
120 1 return;
121 }
122  
123 4 ▶ if ($self->{$key} !~ m/^(\d{2})(\d{2})(\d{2})$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$key} =~ /^(\d{2})(\d{2})(\d{2})$/ms) 2 2 4
124 err "Parameter '$key' is in bad date format.",
125 2 'Value', $self->{$key},
126 ;
127 }
128 2 my ($year, $month, $day) = ($1, $2, $3);
129 2 eval {
130 2 DateTime->new(
131 'day' => $day,
132 'month' => $month,
133 'year' => 2000 + $year,
134 );
135 };
136 2 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mo::utils::Date::EVAL_ERROR) 1 1 2
137 err "Parameter '$key' is bad date.",
138 1 'Value' => $self->{$key},
139 'DateTime error' => $EVAL_ERROR,
140 ;
141 }
142  
143 1 return;
144 }
145  
146 13 ▶sub check_date_order {
Subroutine check_date_order: called CC=8 SCAR=20.8
Pod documented
147 13 my ($self, $key1, $key2) = @_;
148  
149 13 ▶ if (! exists $self->{$key1} || ! exists $self->{$key2}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key1} and exists $self->{$key2}) 2 11 13
Condition100%3/3
exists $self->{$key1} and exists $self->{$key2}
!ll&&!rl&&r
1111
MC/DC100%2/2
exists $self->{$key1} and exists $self->{$key2}
exists $self->{$key1} exists $self->{$key2}
Truth table100%3/3
A and B
A= exists $self->{$key1}B= exists $self->{$key2}
ABresult
0X0
100
111
150 2 return;
151 }
152  
153 11 ▶ if (! defined $self->{$key1} || ! defined $self->{$key2}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$key1} and defined $self->{$key2}) 2 9 11
Condition100%3/3
defined $self->{$key1} and defined $self->{$key2}
!ll&&!rl&&r
119
MC/DC100%2/2
defined $self->{$key1} and defined $self->{$key2}
defined $self->{$key1} defined $self->{$key2}
Truth table100%3/3
A and B
A= defined $self->{$key1}B= defined $self->{$key2}
ABresult
0X0
100
111
154 2 return;
155 }
156  
157 9 my ($dt1, $dt2);
158 9 ▶ if (ref $self->{$key1} eq 'DateTime') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key1} eq 'DateTime') { } 2 7 9
159 2 $dt1 = $self->{$key1};
160 } else {
161 7 $dt1 = _construct_dt($self->{$key1});
162 }
163 7 ▶ if (ref $self->{$key2} eq 'DateTime') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key2} eq 'DateTime') { } 2 5 7
164 2 $dt2 = $self->{$key2};
165 } else {
166 5 $dt2 = _construct_dt($self->{$key2});
167 }
168  
169 7 my $cmp = DateTime->compare($dt1, $dt2);
170  
171 # dt1 >= dt2
172 7 ▶ if ($cmp != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp != -1) 3 4 7
173 3 err "Parameter '$key1' has date greater or same as parameter '$key2' date.";
174 }
175  
176 4 return;
177 }
178  
179 12 ▶sub _construct_dt {
Subroutine _construct_dt: called CC=5 SCAR=16.1
180 12 my $date = shift;
181  
182 12 my ($year, $month, $day);
183 12 ▶ if ($date =~ m/^(\-?\d{1,4})\-?(\d{0,2})\-?(\d{0,2})$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^(\-?\d{1,4})\-?(\d{0,2})\-?(\d{0,2})$/ms) { } 11 1 12
184 11 ($year, $month, $day) = ($1, $2, $3);
185 } else {
186 1 err 'Cannot parse date/time string.',
187 'Value' => $date,
188 ;
189 }
190 11 my $dt = eval {
191 11 ▶ DateTime->new(
Branch: 2 branches
BranchTrueFalseTotal
$month ? : 7 4 11
$day ? : 7 4 11
192 'year' => $year,
193 $month ? ('month' => $month) : (),
194 $day ? ('day' => $day) : (),
195 );
196 };
197 11 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mo::utils::Date::EVAL_ERROR) 1 10 11
198 1 err "Cannot construct DateTime object from date.",
199 'Value' => $date,
200 'DateTime error' => $EVAL_ERROR,
201 ;
202 }
203  
204 10 return $dt;
205 }
206  
207 7 1;
208  
209 __END__
↑ Summary