File Coverage

blib/lib/Time/Piece/Month.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition 2 2 100.0
subroutine 9 9 100.0
pod 4 4 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Time::Piece::Month;
2              
3 1     1   1133 use strict;
  1         2  
  1         40  
4 1     1   6 use warnings;
  1         2  
  1         37  
5 1     1   16 use base 'Time::Piece::Range';
  1         1  
  1         978  
6 1     1   34854 use Time::Seconds;
  1         2  
  1         447  
7              
8             our $VERSION = '1.00';
9              
10             =head1 NAME
11              
12             Time::Piece::Month - a month of Time::Piece objects
13              
14             =head1 SYNOPSIS
15              
16             use Time::Piece::Month;
17              
18             my $month = Time::Piece::Month->new(Time::Piece $tp);
19             my $month = Time::Piece::Month->new("2002-01-03");
20              
21             my Time::Piece::Month $prev = $month->prev_month;
22             my Time::Piece::Month $next = $month->next_month;
23              
24             my @dates = $month->dates;
25             my @dates = $month->wraparound_dates;
26              
27             =head1 DESCRIPTION
28              
29             This is an extension to Time::Piece::Range that represents a complete
30             calendar month.
31              
32             =head1 CONSTRUCTOR
33              
34             =head2 new
35              
36             my $month = Time::Piece::Month->new(Time::Piece $tp);
37              
38             my $month = Time::Piece::Month->new("2002-01-03");
39              
40             A Month object can be instantiated from either a Time::Piece object,
41             or a Y-m-d format string.
42              
43             =cut
44              
45             sub new {
46 15     15 1 11176 my ($class, $date) = @_;
47 15 100       51 my $tp = ref $date ? $date : _tp($date);
48 15         182 my $first = _tp($tp->strftime("%Y-%m-01"));
49 15         389 my $last = _tp($tp->strftime("%Y-%m-" . $first->month_last_day));
50 15         346 return $class->SUPER::new($first, $last);
51             }
52              
53 35     35   2171 sub _tp { Time::Piece->strptime(shift, "%Y-%m-%d") }
54              
55             =head1 METHODS
56              
57             As well as the inherited Time::Piece::Range methods, we also include:
58              
59             =head2 prev_month / next_month
60              
61             my Time::Piece::Month $prev = $month->prev_month;
62             my Time::Piece::Month $next = $month->next_month;
63              
64             The next and previous months.
65              
66             =cut
67              
68             sub prev_month {
69 5     5 1 9 my $self = shift;
70 5         15 $self->new($self->start - ONE_DAY);
71             }
72              
73             sub next_month {
74 5     5 1 14239 my $self = shift;
75 5         16 $self->new($self->end + ONE_DAY);
76             }
77              
78             =head2 wraparound_dates
79              
80             This returns a list of Time::Piece objects representing each day in
81             the month, but also including the days on either side that ensure that
82             the full list runs from a Sunday to a Saturday. This is useful for
83             displaying calendars.
84              
85             =cut
86              
87             sub wraparound_dates {
88 5     5 1 22276 my $self = shift;
89 5   100     20 my $preceed = 0 - ($self->start->day_of_week || 7);
90 5         67 my $follow = 6 - ($self->end + ONE_DAY)->day_of_week;
91 5         374 return ($self->prev_month->dates)[ $preceed .. -1 ], ($self->dates),
92             ($self->next_month->dates)[ 0 .. $follow ];
93             }
94              
95             =head1 AUTHOR
96              
97             Tony Bowden
98              
99             =head1 BUGS and QUERIES
100              
101             Please direct all correspondence regarding this module to:
102             bug-Business-Barcode-EAN13@rt.cpan.org
103              
104             =head1 COPYRIGHT AND LICENSE
105              
106             Copyright (C) 2002-2005 Kasei
107              
108             This program is free software; you can redistribute it and/or modify it under
109             the terms of the GNU General Public License; either version 2 of the License,
110             or (at your option) any later version.
111              
112             This program is distributed in the hope that it will be useful, but WITHOUT
113             ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
114             FOR A PARTICULAR PURPOSE.
115              
116             =cut
117              
118             1;