File Coverage

blib/lib/Date/Simple/Month.pm
Criterion Covered Total %
statement 26 45 57.7
branch 7 16 43.7
condition 0 2 0.0
subroutine 8 13 61.5
pod 6 6 100.0
total 47 82 57.3


line stmt bran cond sub pod time code
1             package Date::Simple::Month;
2              
3 2     2   53232 use strict;
  2         5  
  2         79  
4 2     2   11 use base qw (Date::Range);
  2         3  
  2         1855  
5 2     2   3764 use vars qw($VERSION);
  2         11  
  2         119  
6             $VERSION = '0.02';
7              
8 2     2   1745 use Date::Simple;
  2         18591  
  2         7146  
9              
10             sub new {
11 4     4 1 1887 my $class = shift;
12 4 50       9 my $ds = eval{ _ds(@_) } or return;
  4         18  
13 4         285 my $first = $ds - $ds->day + 1;
14 4         169 my $last = $first + Date::Simple::days_in_month($first->year, $first->month) -1;
15 4         164 return $class->SUPER::new($first, $last);
16             }
17              
18             sub _ds{
19 4 100   4   18 if ( @_ == 1 ) {
    100          
20 2 100       13 if (UNIVERSAL::isa($_[0], 'Date::Simple')) {
21 1         41 return Date::Simple->new( $_[0] );
22             } else {
23 1         5 return Date::Simple->new(Date::Simple->new->year, $_[0], 1);
24             }
25             } elsif ( @_ == 2 ) {
26 1         4 return Date::Simple->new($_[0], $_[1], 1);
27             }
28 1         8 return Date::Simple->new;
29             }
30              
31 4     4 1 2481 sub year {shift->start->year}
32 4     4 1 2137 sub month {shift->start->month}
33              
34             sub prev_month {
35 0     0 1   my $self = shift;
36 0           $self->new( $self->start - 1 );
37             }
38              
39             sub next_month {
40 0     0 1   my $self = shift;
41 0           $self->new( $self->end + 1 );
42             }
43              
44             sub wraparound_dates {
45 0     0 1   my $self = shift;
46 0   0       my $start_day = shift || 0;
47              
48 0           my $nof_preceed = $self->_nof_preceed($start_day);
49 0 0         my @preceed = $nof_preceed ? ($self->prev_month->dates)[ 0 - $nof_preceed .. -1] : ();
50              
51 0           my $nof_follow = $self->_nof_follow($start_day);
52 0 0         my @follow = $nof_follow ? ($self->next_month->dates)[0 .. $nof_follow -1] : ();
53 0           return @preceed , ($self->dates), @follow;
54             }
55              
56             sub _nof_preceed {
57 0     0     my ($self, $start_day) = @_;
58 0           my $nof_preceed = $self->start->day_of_week - $start_day;
59 0 0         $nof_preceed += 7 if $nof_preceed < 0;
60 0           return $nof_preceed;
61             }
62              
63             sub _nof_follow{
64 0     0     my ($self, $start_day) = @_;
65 0           my $nof_follow = $start_day + 6 - $self->end->day_of_week;
66 0 0         $nof_follow -= 7 if $nof_follow > 6;
67 0           return $nof_follow;
68             }
69              
70             1;
71             __END__