File Coverage

blib/lib/App/MonM/Notifier/Util.pm
Criterion Covered Total %
statement 78 97 80.4
branch 22 40 55.0
condition 15 41 36.5
subroutine 12 13 92.3
pod 2 2 100.0
total 129 193 66.8


line stmt bran cond sub pod time code
1             package App::MonM::Notifier::Util; # $Id: Util.pm 59 2019-07-14 09:14:38Z abalama $
2 2     2   54246 use strict;
  2         11  
  2         48  
3 2     2   502 use utf8;
  2         14  
  2         9  
4              
5             =encoding utf-8
6              
7             =head1 NAME
8              
9             App::MonM::Notifier::Util - Utility tools
10              
11             =head1 VERSION
12              
13             Version 1.01
14              
15             =head1 SYNOPSIS
16              
17             use App::MonM::Notifier::Util;
18              
19             =head1 DESCRIPTION
20              
21             Utility tools
22              
23             =head2 checkPubDate
24              
25             my $status = checkPubDate( $user_config_struct, $channel_name );
26              
27             Returns the sign (BOOL) of the permission to send a message (allowed or not allowed) by public date
28              
29             =head2 getPeriods
30              
31             my %periods = getPeriods( $user_config_struct );
32             my %periods = getPeriods( $user_config_struct, $channel_name );
33              
34             This function returns periods on everyday of week for all channels or only for specified
35              
36             Format of the returned hash-structure:
37              
38             monday => [start_time, finish_time],
39              
40             =head1 HISTORY
41              
42             See C file
43              
44             =head1 DEPENDENCIES
45              
46             L
47              
48             =head1 TO DO
49              
50             See C file
51              
52             =head1 BUGS
53              
54             * none noted
55              
56             =head1 SEE ALSO
57              
58             L
59              
60             =head1 AUTHOR
61              
62             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
63              
64             =head1 COPYRIGHT
65              
66             Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
67              
68             =head1 LICENSE
69              
70             This program is free software; you can redistribute it and/or
71             modify it under the same terms as Perl itself.
72              
73             See C file and L
74              
75             =cut
76              
77 2     2   76 use vars qw/$VERSION @EXPORT @EXPORT_OK/;
  2         3  
  2         171  
78             $VERSION = '1.01';
79              
80             use constant {
81 2         182 DAYS_OF_WEEK => [qw/sunday monday tuesday wednesday thursday friday saturday/],
82             DAYS_OF_WEEK_S => [qw/sun mon tue wed thu fri sat/],
83             OFFSET_START => 0, # 00:00
84             OFFSET_FINISH => 60*60*24-1, # 23:59
85 2     2   12 };
  2         3  
86              
87 2     2   12 use base qw/Exporter/;
  2         2  
  2         241  
88              
89 2     2   342 use CTK::ConfGenUtil;
  2         840  
  2         140  
90 2     2   359 use CTK::TFVals qw/ :ALL /;
  2         1612  
  2         422  
91 2     2   16 use Carp; # carp - warn; croak - die;
  2         3  
  2         85  
92 2     2   384 use Time::Local;
  2         1791  
  2         85  
93              
94 2     2   339 use App::MonM::Notifier::Const;
  2         8  
  2         1688  
95              
96             # Items to export into callers namespace by default
97             # (move infrequently used names to @EXPORT_OK below)
98             @EXPORT = (qw/
99             checkPubDate
100             getPeriods
101             /);
102              
103             # Other items we are prepared to export if requested
104             @EXPORT_OK = (qw/
105             DAYS_OF_WEEK
106             DAYS_OF_WEEK_S
107             /, @EXPORT);
108              
109             sub checkPubDate {
110 0     0 1 0 my %periods = getPeriods(@_);
111 0 0 0     0 return 0 unless %periods && keys %periods;
112              
113 0         0 my @dow = @{DAYS_OF_WEEK()};
  0         0  
114              
115 0         0 my $curtime = time();
116 0         0 my $wday = (localtime($curtime))[6];
117 0         0 my ($start, $finish) = ($periods{$dow[$wday]}[0], $periods{$dow[$wday]}[1]);
118 0 0       0 $finish += 59 if defined $finish;
119 0 0 0     0 if ($start && ($curtime >= $start) && $finish && ($curtime <= $finish)) {
      0        
      0        
120             #printf(">>> %s -> %s\n", scalar(localtime($start)), scalar(localtime($finish)));
121 0         0 return 1;
122             }
123 0         0 return 0;
124             }
125             sub getPeriods { # Get periods as hash
126 3     3 1 1222 my $us = shift;
127 3         5 my $channel = shift;
128              
129 3 50 33     8 return () unless is_hash($us) && keys %$us;
130 3         34 my @dow = @{DAYS_OF_WEEK()};
  3         9  
131 3         3 my @dows = @{DAYS_OF_WEEK_S()};
  3         7  
132 3         4 my $n = $#dow;
133 3         5 my %struct;
134 3         6 my $channels = hash($us => "channel");
135 3   50     117 my $period_global = value($us => "period") || "00:00-23:59";
136 3         102 foreach my $chname (keys %$channels) {
137 9 100 100     25 next if $channel && lc($chname) ne lc($channel);
138 5         9 my $ch = hash($channels => $chname);
139 5 100 66     158 next unless $ch && keys %$ch;
140              
141             #printf("%s\n", Dumper($ch));
142 3   33     7 my $period_channel = value($ch => "period") || $period_global;
143 3         99 for (my $i = 0; $i <= $n; $i++) {
144 21   66     43 my ($s, $f) = _parsePeriod(value($ch => $dow[$i]) || value($ch => $dows[$i]) || $period_channel);
145 21 100       34 next unless defined $s;
146             #printf("%s> %s: %d - %d\n", $chname, $dow[$i], $s, $f);
147 20         36 my $r = $struct{$dow[$i]};
148 20 100       25 if ($r) {
149 6 100 66     19 $struct{$dow[$i]}[0] = $s if $r->[0] && $r->[0] > $s;
150 6 50 33     22 $struct{$dow[$i]}[1] = $f if $r->[1] && $r->[1] < $f;
151             } else {
152 14         43 $struct{$dow[$i]} = [$s, $f];
153             }
154             }
155             }
156 3         7 my $curtime = time();
157 3         67 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($curtime);
158 3         22 my $newtime = timelocal( 0, 0, 0, $mday, $mon, $year );
159              
160 3         158 for (my $i = 0; $i <= $n; $i++) {
161 21         55 my $r = $struct{$dow[$i]};
162 21 100       37 next unless is_array($r);
163 14 50       75 my $j = $i-$wday; $j = 7 + $j if $j < 0; # Real offset index
  14         21  
164 14         17 my $roff = $j * (OFFSET_FINISH + 1);
165 14         18 $r->[0] = $newtime + $roff + $r->[0];
166 14         22 $r->[1] = $newtime + $roff + $r->[1];
167             }
168 3         31 return %struct;
169             }
170             sub _parsePeriod {
171 21     21   1255 my $period = shift;
172             #printf("%s\n", $period);
173 21 50       31 return (undef,undef) unless defined $period;
174 21         22 my $start = OFFSET_START; # 00:00
175 21         23 my $finish = OFFSET_FINISH; # 23:59
176 21 100       156 if ($period =~ /^\-+$/) {
    50          
    50          
    0          
    0          
177 1         3 return (undef,undef);
178             } elsif ($period =~ /none|no|undef|off/i) {
179 0         0 return (undef,undef);
180             } elsif ($period =~ /(\d{1,2})\s*\:\s*(\d{1,2})\s*\-+\s*(\d{1,2})\s*\:\s*(\d{1,2})/) { # 00:00-23:59
181 20         76 my ($sh,$sm,$fh,$fm) = ($1,$2,$3,$4);
182 20         35 $start = $sh*60*60 + $sm*60;
183 20         31 $finish = $fh*60*60 + $fm*60;
184             } elsif ($period =~ /(\d{1,2})\s*\-+\s*(\d{1,2})\s*\:\s*(\d{1,2})/) { # 00-23:59
185 0         0 my ($sh,$fh,$fm) = ($1,$2,$3);
186 0         0 $start = $sh*60*60;
187 0         0 $finish = $fh*60*60 + $fm*60;
188             } elsif ($period =~ /(\d{1,2})\s*\-+\s*(\d{1,2})/) { # 00-23
189 0         0 my ($sh,$fh,$fm) = ($1,$2,59);
190 0         0 $start = $sh*60*60;
191 0         0 $finish = $fh*60*60 + $fm*60;
192             } else { # Errors
193 0         0 return (undef,undef);
194             }
195              
196 20 50 33     51 $start = OFFSET_START if $start < OFFSET_START or $start > OFFSET_FINISH;
197 20 50 33     47 $finish = OFFSET_FINISH if $finish <= OFFSET_START or $finish > OFFSET_FINISH;
198 20         34 return ($start, $finish);
199             }
200              
201             1;