AzimuthAero/Utils.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« AzimuthAero/RouteMap.pm ↑ Summary AzimuthAero.pm »
1 package WWW::AzimuthAero::Utils;
2 $WWW::AzimuthAero::Utils::VERSION = '0.3';
3  
4 # ABSTRACT: functions that can be used outside WWW::AzimuthAero::* packages
5  
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use JavaScript::V8;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use DateTime;
16 use DateTime::Format::Strptime;
17  
18 require Exporter;
19 our @ISA = qw(Exporter);
20  
21 our @EXPORT_OK = qw(
22 get_next_dow_date
23 get_next_dow_date_dmy
24 get_dates_from_dows
25 get_dates_from_range
26 filter_dates
27 sort_dates
28 extract_js_glob_var
29 iata_pairwise
30 fix_html_string
31 );
32  
33 # TO-DO: to_dt and from_dt methods
34  
35 our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] );
36  
37  
38 sub get_next_dow_date {
39 my ( $date, $dow, $pattern ) = @_;
40  
41 $pattern = '%d.%m.%Y' unless defined $pattern;
42  
43 unless ( ref($date) eq 'DateTime' ) {
44 $date = DateTime::Format::Strptime->new( pattern => $pattern )
45 ->parse_datetime($date);
46 }
47  
48 my $days_diff = $dow - $date->day_of_week;
49 my $dur = DateTime::Duration->new( days => $days_diff );
50 return $date if ( $dur->is_zero );
51 return $date->add_duration($dur) if ( $dur->is_positive );
52 return $date->add( days => 7 - $dur->days ) if ( $dur->is_negative );
53 }
54  
55 sub get_next_dow_date_dmy {
56 my ( $date, $dow, $pattern ) = @_;
57 get_next_dow_date( $date, $dow, $pattern )->dmy('.');
58 }
59  
60  
61 sub get_dates_from_dows {
62 my (%params) = @_;
63  
64 #warn "get_dates() params : ".Dumper \%params;
65 confess "max date is not defined" unless defined $params{max};
66 confess "days of week (days variable) are not defined"
67 unless defined $params{days};
68  
69 my @wdays = split( '', $params{days} );
70  
71 #say "wdays: ".Dumper \@wdays;
72  
73 # TO-DO: $dt_max: 4 month further
74 my $dt_max = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' )
75 ->parse_datetime( $params{max} );
76  
77 my $dt_min =
78 ( defined $params{min} )
79 ? ( DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' )
80 ->parse_datetime( $params{min} ) )
81 : ( DateTime->now->truncate( to => 'day' ) );
82  
83 # $dt_min = DateTime->now if ( $dt_min <= DateTime->now );
84  
85 my @res = ();
86 for my $dow (@wdays) {
87 my $next_target_dt = get_next_dow_date( $dt_min, $dow );
88 while ( $next_target_dt <= $dt_max ) {
89  
90 # push @res, $next_target_dt; # strange behaviour if push datetime
91 push @res, $next_target_dt->dmy('.');
92 $next_target_dt =
93 $next_target_dt + DateTime::Duration->new( weeks => 1 );
94 }
95 }
96  
97 return sort_dates(@res);
98 }
99  
100  
101 # This method used for filtering dates when no available_to property
102  
103 sub get_dates_from_range {
104 my (%params) = @_;
105  
106 # confess "min date is not defined" unless defined $params{min};
107 # confess "max date is not defined" unless defined $params{max};
108  
109 my $dt_max =
110 ( defined $params{max} )
111 ? ( DateTime::Format::Strptime->new( pattern => '%d.%m.%Y' )
112 ->parse_datetime( $params{max} ) )
113 : ( DateTime->now->truncate( to => 'day' )->add( months => 2 ) );
114  
115 # my $dt_max = DateTime::Format::Strptime->new( pattern => '%d.%m.%Y' )
116 #->parse_datetime( $params{max} );
117  
118 # DateTime->now->truncate include max_day
119 my $dt_min =
120 ( defined $params{min} )
121 ? ( DateTime::Format::Strptime->new( pattern => '%d.%m.%Y' )
122 ->parse_datetime( $params{min} ) )
123 : ( DateTime->now->truncate( to => 'day' ) );
124  
125 my $next_dt = $dt_min; ### TO-DO: round to midnight, '%Y-%m-%d 00:00:00'
126 my @res;
127  
128 while ( DateTime->compare( $dt_max, $next_dt ) >= 0 ) {
129 push @res, $next_dt->dmy('.');
130 $next_dt = $next_dt->add( days => 1 );
131 }
132  
133 return @res;
134  
135 }
136  
137  
138 sub sort_dates {
139 my @dates = @_;
140 my $pattern = '%d.%m.%Y';
141  
142 return map { $_->dmy('.') }
143 sort { DateTime->compare( $a, $b ) }
144 grep { DateTime->compare( $_, DateTime->now ) >= 0 }
145 map {
146 DateTime::Format::Strptime->new( pattern => $pattern )
147 ->parse_datetime($_)
148 } @dates;
149 }
150  
151  
152 sub filter_dates {
153 my ( $dates, %params ) = @_;
154 my $pattern = '%d.%m.%Y';
155  
156 confess "max date is not defined" unless defined $params{max};
157  
158 $params{max} = DateTime::Format::Strptime->new( pattern => $pattern )
159 ->parse_datetime( $params{max} );
160  
161 carp "max date in past" if ( $params{max} < DateTime->now() );
162  
163 $params{min} =
164 ( defined $params{min} )
165 ? ( DateTime::Format::Strptime->new( pattern => $pattern )
166 ->parse_datetime( $params{min} ) )
167 : ( DateTime->now() );
168  
169 return map { $_->dmy('.') }
170 grep { $_ >= $params{min} && $_ <= $params{max} }
171 map {
172 DateTime::Format::Strptime->new( pattern => $pattern )
173 ->parse_datetime($_)
174 } @$dates;
175 }
176  
177  
178 sub extract_js_glob_var {
179 my ( $code_str, $var_name ) = @_;
180 my $res;
181 my $context = JavaScript::V8::Context->new();
182 $context->eval($code_str);
183 $context->bind( console_log => sub { $res = $_[0] } );
184 $context->eval( 'console_log(' . $var_name . ')' );
185 undef $context;
186 return $res;
187 }
188  
189  
190 sub fix_html_string {
191 my ($html_str) = @_;
192 my $str = $html_str;
193 $str =~ s/[\r\n\t]//g;
194 $str =~ s/^\s+//;
195 $str =~ s/\s+$//;
196  
197 # hack for deleting &nbsp; at flight_num
198 $str =~ s/([A-Z1-9]).(\d{3})/$1 $2/;
199  
200 return $str;
201 }
202  
203  
204 sub iata_pairwise {
205 my ($AoA) = @_;
206 my @res;
207 for my $x (@$AoA) {
208 push @res, { from => $x->[0], via => $x->[1], to => $x->[2] };
209 }
210 return @res;
211 }
212  
213 1;
214  
215 __END__
216  
217 =pod
218  
219 =encoding UTF-8
220  
221 =head1 NAME
222  
223 WWW::AzimuthAero::Utils - functions that can be used outside WWW::AzimuthAero::* packages
224  
225 =head1 VERSION
226  
227 version 0.3
228  
229 =head1 DESCRIPTION
230  
231 Some useful date manipulation and filtering functions
232  
233 =head1 FUNCTIONS
234  
235 =head2 get_next_dow_date
236  
237 Get next dow-date following by specified date
238  
239 By default all input and output dates in '%d.%m.%Y' format, but you can easily specified needed one or deturn DateTime object
240  
241 get_next_dow_date( '7.06.2019', 7 )->dmy('.'); # '9.06.2019'
242 get_next_dow_date( '7.06.2019', 3 )->dmy('.'); # '12.06.2019'
243  
244 =head2 get_dates_from_dows
245  
246 Get particular dates, based on min_date, max_date and days_of_week
247  
248 min_date and max_date are in '%Y-%m-%d' format by default
249  
250 get_dates_from_dows( min => '2019-06-01', max => '2019-10-26', 'days' => '16' );
251  
252 Return sorted array
253  
254 TO-DO: carefully check for max day without min day (result may not include it)
255  
256 =head2 get_dates_from_range
257  
258 =head2 sort_dates
259  
260 =head2 filter_dates
261  
262 Filter dates by max and min dates
263  
264 filter_dates( \@dates, max => '15.06.2019' );
265 filter_dates( \@dates, max => '15.06.2019', min => '07.06.2019' );
266  
267 =head2 extract_js_glob_var
268  
269 Extract global variable value from JavaScript code
270  
271 =head2 fix_html_string
272  
273 remove newline symbols, leading and trailing whitespaces
274  
275 =head2 pairwise
276  
277 Transform
278  
279 [ [ 'ROV', 'MOW', 'LED' ], [ 'ROV', 'KRR', 'LED' ] ]
280  
281 to
282  
283 [
284 { from => 'ROV', via => 'MOW', to => 'LED' },
285 { from => 'ROV', via => 'KRR', to => 'LED' }
286 ]
287  
288 =head1 AUTHOR
289  
290 Pavel Serikov <pavelsr@cpan.org>
291  
292 =head1 COPYRIGHT AND LICENSE
293  
294 This software is copyright (c) 2019 by Pavel Serikov.
295  
296 This is free software; you can redistribute it and/or modify it under
297 the same terms as the Perl 5 programming language system itself.
298  
299 =cut
« AzimuthAero/RouteMap.pm ↑ Summary AzimuthAero.pm »