File Coverage

blib/lib/Schedule/ByClock.pm
Criterion Covered Total %
statement 12 77 15.5
branch 0 32 0.0
condition 0 9 0.0
subroutine 4 13 30.7
pod 6 6 100.0
total 22 137 16.0


line stmt bran cond sub pod time code
1             package Schedule::ByClock;
2 1     1   16995 use strict;
  1         3  
  1         49  
3              
4 1     1   1088 use Time::localtime;
  1         18382  
  1         81  
5 1     1   11 use Carp;
  1         8  
  1         63  
6              
7 1     1   6 use vars qw($VERSION);
  1         2  
  1         1031  
8              
9             $VERSION='1.01';
10              
11             ########################################################
12             sub new {
13 0     0 1   my $class=shift;
14 0           my $self=[];
15              
16 0           bless($self,$class);
17 0           $self->_init(@_);
18             }
19              
20             ########################################################
21             sub get_version {
22 0     0 1   $VERSION;
23             }
24              
25             ########################################################
26             sub get_control_list {
27 0     0 1   my $self=shift;
28 0           @$self;
29             }
30              
31             ########################################################
32             sub set_control_list {
33 0     0 1   my $self=shift;
34              
35 0           @$self=();
36 0           foreach ($self->_check_params(@_)) {
37 0           push(@$self,$_);
38             }
39 0           @$self;
40             }
41              
42             ########################################################
43             sub get_control_on_second {
44 0     0 1   my $self=shift;
45 0           my($tm,$until);
46              
47 0           $tm=localtime;
48              
49 0 0         if(@_) {
50 0           $until=$self->_find_next($tm->sec,$self->_check_params(@_));
51             }
52             else {
53 0           $until=$self->_find_next($tm->sec,@$self);
54             }
55 0 0         return undef unless defined($until);
56              
57 0 0         if($until>$tm->sec) {
    0          
58 0           sleep($until-$tm->sec);
59             }
60             elsif($until<$tm->sec) {
61 0           sleep($until+60-$tm->sec);
62             }
63             else {
64 0           sleep(60);
65             }
66              
67 0           $until;
68             }
69              
70             ########################################################
71             sub get_control_on_minute {
72 0     0 1   my $self=shift;
73 0           my($tm,$until,$until_sec);
74              
75 0           $tm=localtime;
76 0 0         if(@_) {
77 0           $until=$self->_find_next($tm->min,$self->_check_params(@_));
78             }
79             else {
80 0           $until=$self->_find_next($tm->min,@$self);
81             }
82 0 0         return undef unless defined($until);
83              
84             # Syncronize on top of a minute.
85 0 0         if($tm->sec) {
86 0           $until_sec=$self->get_control_on_second(0);
87             }
88              
89             # Which minute?
90 0           $tm=localtime;
91              
92             # Wait until...
93 0 0         if($until>$tm->min) {
    0          
94 0           sleep(($until-$tm->min)*60);
95             }
96             elsif($until<$tm->min) {
97 0           sleep(($until+60-$tm->min)*60);
98             }
99             else {
100             # On the minute.
101 0 0         return $until if(defined($until_sec)); # We have slept for less than a minute.
102 0           sleep(3600); # Wait for a full hour.
103             }
104              
105 0           $until;
106             }
107              
108             ########################################################
109             # 'Internal' subs. Don't call these, since they may,
110             # and will, change without notice.
111             ########################################################
112             sub _init {
113 0     0     my $self=shift;
114              
115 0           foreach ($self->_check_params(@_)) {
116 0           push(@$self,$_);
117             }
118 0           $self;
119             }
120              
121             sub _check_params {
122 0     0     my $self=shift;
123 0           my @control;
124              
125 0           foreach (@_) {
126 0 0 0       if(m/\D/||$_<0||$_>59) {
      0        
127 0           carp("Control value $_ out of bounds")
128             }
129             else {
130 0           push(@control,$_);
131             }
132             }
133 0           @control;
134             }
135              
136             sub _find_next {
137 0     0     my ($self,$now,@control)=(@_);
138 0           my ($low,$high,$next);
139              
140 0 0         return undef unless @control;
141 0           $low=$high=$control[0]; # Must have an initial value.
142              
143             # Find highest and lowest values in list.
144 0           foreach (@control) {
145 0 0         if($_>$high) {
    0          
146 0           $high=$_;
147             }
148             elsif($_<$low) {
149 0           $low=$_;
150             }
151             }
152              
153 0 0         if($now<$high) {
154             # Search the closest that is higher than now.
155 0           $next=$high;
156 0 0 0       foreach (@control) { $next=$_ if($_>$now&&$_<$next); }
  0            
157             }
158             else {
159             # Grab low value.
160 0           $next=$low;
161             }
162              
163 0           $next;
164             }
165              
166             'Ymer';
167              
168             __END__