File Coverage

blib/lib/Date/Calc/Iterator.pm
Criterion Covered Total %
statement 43 43 100.0
branch 13 24 54.1
condition 8 24 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 73 100 73.0


line stmt bran cond sub pod time code
1             package Date::Calc::Iterator;
2              
3             # $Id: Iterator.pm,v 1.2 2004/03/17 19:44:40 bronto Exp $
4              
5 1     1   23771 use 5.006;
  1         2  
  1         33  
6 1     1   6 use strict;
  1         2  
  1         32  
7 1     1   4 use warnings;
  1         6  
  1         59  
8              
9             our @ISA = qw();
10              
11             our $VERSION = '1.00';
12              
13 1     1   894 use Date::Calc qw(Delta_Days Add_Delta_Days) ;
  1         43674  
  1         2366  
14              
15             sub new {
16 2     2 1 572 my $self = shift ;
17 2         10 my %parms = @_ ;
18              
19 2   33     13 my $class = ref $self || $self ;
20              
21 2 50 33     17 die "Need a start date" unless exists $parms{from} and ref $parms{from} eq 'ARRAY' ;
22 2 50 33     21 die "Need an end date" unless exists $parms{to} and ref $parms{to} eq 'ARRAY' ;
23 2 50 33     10 die "Need an integer step" if exists $parms{step} and not $parms{step} =~ /^\d+$/ ;
24              
25 2 50       9 _check_date($parms{from}) or die "Invalid start date" ;
26 2 50       8 _check_date($parms{to}) or die "Invalid end date" ;
27              
28 2 50       9 $parms{step} = 1 unless exists $parms{step} ;
29 2         4 $parms{delta} = 0 ;
30 2         3 $parms{maxdelta} = Delta_Days(@{$parms{from}},@{$parms{to}}) ;
  2         5  
  2         10  
31              
32 2         132 my @object_keys = qw(from to step delta maxdelta) ;
33 2         4 my %object_hash ;
34 2         10 @object_hash{@object_keys} = @parms{@object_keys} ;
35 2         12 return bless \%object_hash,$class ;
36             }
37              
38             sub next {
39 12     12 1 939 my $self = shift ;
40              
41 12         14 eval { $self->isa('Date::Calc::Iterator') } ;
  12         31  
42 12 50       23 die "next() is an object method" if $@ ;
43              
44 12 100       31 return undef if $self->{delta} > $self->{maxdelta} ;
45              
46 11         11 my @next_date = Add_Delta_Days(@{$self->{from}},$self->{delta}) ;
  11         37  
47 11         533 $self->{delta} += $self->{step} ;
48 11 50       36 return wantarray ? @next_date : \@next_date ;
49             }
50              
51             sub _check_date {
52 4     4   5 my $date = shift ;
53 4         6 my ($y,$m,$d) = @$date ;
54              
55 4 50       26 return undef unless $y =~ /^\d+$/ ;
56 4 50 33     38 return undef unless $m =~ /^\d{1,2}$/ and $m >= 1 and $m <= 12 ;
      33        
57 4 50 33     44 return undef unless $d =~ /^\d{1,2}$/ and $d >= 1 and $d <= 31 ;
      33        
58             }
59              
60             1;
61             __END__