datetime_diff.pm

filter: Statementstmt 88.0% 110 / 125 Branchbran 50.0% 4 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 97.2% 35 / 36 Podpod 0.0% 0 / 1 totaltotal 86.2% 151 / 175 CC 6 SCAR 4.7
CC
6
Cov
85%
__ANON__
5.7
emit
2.1
_get_locale
2.0
CRAP 1.6
↑ Summary datetime_format.pm »
1 package Catmandu::Fix::datetime_diff;
2 2 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Catmandu::Util qw(:is :check :array);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DateTime::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = "0.0132";
12  
13 2 with 'Catmandu::Fix::Base';
14  
15 2 has path => (
16 fix_arg => 1
17 );
18 2 has start => (
19 fix_arg => 1
20 );
21 2 has end => (
22 fix_arg => 1
23 );
24 has start_time_zone => (
25 fix_opt => 1,
26 is => 'ro',
27 required => 1,
28 4 ▶ isa => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 4 check_string($_[0]);
30 },
31 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 3 "UTC"
33 }
34 2 );
35 has end_time_zone => (
36 fix_opt => 1,
37 is => 'ro',
38 required => 1,
39 4 ▶ isa => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 4 check_string($_[0]);
41 },
42 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 3 "UTC"
44 }
45 2 );
46  
47 has validate => (
48 fix_opt => 1,
49 is => 'ro',
50 required => 0,
51 lazy => 1,
52 4 ▶ default => sub { 1; }
Subroutine __ANON__: called CC=1 SCAR=0.0
53 2 );
54  
55 2 has delete => (
56 fix_opt => 1,
57 is => 'ro'
58 );
59 has start_pattern => (
60 fix_opt => 1,
61 is => 'ro',
62 required => 1,
63 4 ▶ isa => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 4 check_string($_[0]);
65 },
66 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 1 "%FT%T.%NZ"
68 }
69 2 );
70 has end_pattern => (
71 fix_opt => 1,
72 is => 'ro',
73 required => 1,
74 4 ▶ isa => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 4 check_string($_[0]);
76 },
77 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 "%FT%T.%NZ"
79 }
80 2 );
81 has start_locale => (
82 is => 'ro',
83 required => 1,
84 4 ▶ isa => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 4 check_string($_[0]);
86 },
87 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 4 "en_US"
89 }
90 2 );
91 2 has _start_locale => (
92 is => 'ro',
93 required => 0,
94 lazy => 1,
95 builder => '_build_start_locale'
96 );
97 has end_locale => (
98 is => 'ro',
99 required => 1,
100 4 ▶ isa => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 4 check_string($_[0]);
102 },
103 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 4 "en_US"
105 }
106 2 );
107 2 has _end_locale => (
108 is => 'ro',
109 required => 0,
110 lazy => 1,
111 builder => '_build_end_locale'
112 );
113  
114 2 has _start_time_zone => (
115 is => 'ro',
116 required => 0,
117 lazy => 1,
118 builder => '_build_start_time_zone'
119 );
120 2 has _end_time_zone => (
121 is => 'ro',
122 required => 0,
123 lazy => 1,
124 builder => '_build_end_time_zone'
125 );
126  
127 has _start_datetime_parser => (
128 is => 'ro',
129 lazy => 1,
130 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
131 4 my $self = $_[0];
132 4 DateTime::Format::Strptime->new(
133 pattern => $self->start_pattern,
134 locale => $self->_start_locale,
135 time_zone => $self->_start_time_zone,
136 on_error => 'undef'
137 );
138 }
139 2 );
140 has _end_datetime_parser => (
141 is => 'ro',
142 lazy => 1,
143 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 4 my $self = $_[0];
145 4 DateTime::Format::Strptime->new(
146 pattern => $self->end_pattern,
147 locale => $self->_end_locale,
148 time_zone => $self->_end_time_zone,
149 on_error => 'undef'
150 );
151 }
152 2 );
153  
154 8 ▶sub _get_locale {
Subroutine _get_locale: called CC=2 SCAR=7.0
155 8 state $l = {};
156 8 my $name = $_[0];
157 8 ▶ $l->{$name} ||= DateTime::Locale->load($name);
Condition66%2/3
$l->{$name} ||= "DateTime::Locale"->load($name)
l!l&&r!l&&!r
710
MC/DC0%0/2
$l->{$name} ||= "DateTime::Locale"->load($name)
$l->{$name} "DateTime::Locale"->load($name)
Truth table66%2/3
A ||= B
A= $l->{$name}B= "DateTime::Locale"->load($name)
ABresult
1X1
011
000
158 }
159 8 ▶sub _get_time_zone {
Subroutine _get_time_zone: called CC=1 SCAR=0.0
160 8 state $t = {};
161 8 my $name = $_[0];
162 8 $t->{$name} = DateTime::TimeZone->new( name => $name );
163 }
164 4 ▶sub _build_start_locale {
Subroutine _build_start_locale: called CC=1 SCAR=0.0
165 4 _get_locale($_[0]->start_locale);
166 }
167 4 ▶sub _build_end_locale {
Subroutine _build_end_locale: called CC=1 SCAR=0.0
168 4 _get_locale($_[0]->end_locale);
169 }
170 4 ▶sub _build_start_time_zone {
Subroutine _build_start_time_zone: called CC=1 SCAR=0.0
171 4 _get_time_zone( $_[0]->start_time_zone );
172 }
173 4 ▶sub _build_end_time_zone {
Subroutine _build_end_time_zone: called CC=1 SCAR=0.0
174 4 _get_time_zone( $_[0]->end_time_zone );
175 }
176  
177 4 ▶sub emit {
Subroutine emit: called CC=2 SCAR=7.5
Pod undocumented
178 4 my($self,$fixer) = @_;
179  
180 4 my @perl;
181  
182 4 my $path = $fixer->split_path($self->path());
183 4 my $start = $fixer->split_path($self->start());
184 4 my $start_key = pop @$start;
185 4 my $end = $fixer->split_path($self->end());
186 4 my $end_key = pop @$end;
187  
188 4 my $start_time_zone = $fixer->capture($self->_start_time_zone());
189 4 my $start_locale = $fixer->capture($self->_start_locale());
190 4 my $end_time_zone = $fixer->capture($self->_end_time_zone());
191 4 my $end_locale = $fixer->capture($self->_end_locale());
192  
193 4 my $start_parser = $fixer->capture($self->_start_datetime_parser());
194 4 my $end_parser = $fixer->capture($self->_end_datetime_parser());
195  
196 #cf. http://www.nntp.perl.org/group/perl.datetime/2012/05/msg7838.html
197 4 ▶ push @perl, "local \$Params::Validate::NO_VALIDATION = ".($self->validate() ? 0 : 1).";";
Branch: 1 branch
BranchTrueFalseTotal
$self->validate ? : 4 0 4
198  
199 4 ▶ push @perl, $fixer->emit_walk_path( $fixer->var, $start, sub {
Subroutine __ANON__: called CC=1 SCAR=0.7
200  
201 4 my $p_start_var = shift;
202  
203 4 ▶ $fixer->emit_get_key( $p_start_var, $start_key, sub {
Subroutine __ANON__: called CC=1 SCAR=0.8
204  
205 4 my $start_var = shift;
206  
207 4 ▶ $fixer->emit_walk_path( $fixer->var, $end, sub {
Subroutine __ANON__: called CC=1 SCAR=0.9
208  
209 4 my $p_end_var = shift;
210  
211 4 ▶ $fixer->emit_get_key( $p_end_var, $end_key, sub {
Subroutine __ANON__: called CC=4 SCAR=17.4
212  
213 4 my $end_var = shift;
214  
215 4 my $dt_start = $fixer->generate_var();
216 4 my $dt_end = $fixer->generate_var();
217  
218 4 my @p;
219  
220 4 push @p, $fixer->emit_declare_vars($dt_start);
221 4 push @p, $fixer->emit_declare_vars($dt_end);
222  
223 #start: no parsing needed (fast)
224 4 ▶ if($self->start_pattern() =~ /\s*%s\s*/o){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->start_pattern =~ /\s*%s\s*/o) { } 0 4 4
225 0 push @p, "if( is_string(${start_var}) ) {";
226 0 push @p, " ${start_var} =~ s\/^\\s+|\\s+\$\/\/go;";
227 0 push @p, " $dt_start = DateTime->from_epoch(epoch => ${start_var},time_zone => ${start_time_zone},locale => ${start_locale});";
228 0 push @p, "}"
229 }
230 #start: parsing needed (slow)
231 else{
232 4 push @p, " $dt_start = ".${start_parser}."->parse_datetime(${start_var}) if is_string(${start_var});";
233 }
234 #end: no parsing needed (fast)
235 4 ▶ if($self->end_pattern() =~ /\s*%s\s*/o){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->end_pattern =~ /\s*%s\s*/o) { } 0 4 4
236 0 push @p, "if( is_string(${end_var}) ) {";
237 0 push @p, " ${end_var} =~ s\/^\\s+|\\s+\$\/\/go;";
238 0 push @p, " $dt_end = DateTime->from_epoch(epoch => ${end_var},time_zone => ${end_time_zone},locale => ${end_locale});";
239 0 push @p, "}"
240 }
241 #end: parsing needed (slow)
242 else{
243 4 push @p, " $dt_end = ".${end_parser}."->parse_datetime(${end_var}) if is_string(${end_var});";
244 }
245  
246 4 push @p, " if($dt_start && $dt_end){";
247  
248 4 ▶ push @p, $fixer->emit_create_path( $fixer->var, $path, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
249  
250 4 my $var = shift;
251 4 " ${var} = $dt_end->subtract_datetime_absolute( $dt_start )->seconds();";
252  
253 4 });
254  
255 4 push @p, " }";
256  
257 4 ▶ if($self->delete){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->delete) 0 4 4
258 0 my $dest_path = [@$path];
259 0 my $dest_key = pop @$dest_path;
260 0 push @p, " else { ";
261 0 ▶ push @p, $fixer->emit_walk_path( $fixer->var, $dest_path, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 0 my $p_dest = shift;
263 0 $fixer->emit_delete_key( $p_dest, $dest_key );
264 0 });
265 0 push @p, " }";
266 }
267  
268 4 join('', @p);
269  
270  
271 4 });
272  
273 4 });
274  
275 4 });
276  
277 4 });
278  
279  
280 4 join('',@perl);
281 }
282  
283 2 1;
284 __END__
↑ Summary datetime_format.pm »