File Coverage

blib/lib/Calendar/Simple.pm
Criterion Covered Total %
statement 70 70 100.0
branch 29 30 96.7
condition 20 26 76.9
subroutine 11 11 100.0
pod 2 2 100.0
total 132 139 95.0


line stmt bran cond sub pod time code
1             # $Id$
2              
3             =head1 NAME
4            
5             Calendar::Simple - Perl extension to create simple calendars
6            
7             =head1 SYNOPSIS
8            
9             use Calendar::Simple;
10            
11             my @curr = calendar; # get current month
12             my @this_sept = calendar(9); # get 9th month of current year
13             my @sept_2002 = calendar(9, 2002); # get 9th month of 2002
14             my @monday = calendar(9, 2002, 1); # get 9th month of 2002,
15             # weeks start on Monday
16            
17             my @span = date_span(mon => 10, # returns span of dates
18             year => 2006,
19             begin => 15,
20             end => 28);
21            
22             =cut
23              
24             package Calendar::Simple;
25              
26 4     4   435441 use 5.006;
  4         11  
  4         61  
27 4     4   11 use strict;
  4         3  
  4         61  
28 4     4   11 use warnings;
  4         5  
  4         100  
29              
30 4     4   14 use base 'Exporter';
  4         3  
  4         268  
31              
32             our @EXPORT = qw(calendar);
33             our @EXPORT_OK = qw(date_span);
34             our $VERSION = '1.20';
35              
36 4     4   1176 use Time::Local;
  4         3641  
  4         152  
37 4     4   16 use Carp;
  4         6  
  4         1688  
38              
39 4     4   1623 eval 'use DateTime';
  4         401874  
  4         60  
40             my $dt = ! $@;
41             $dt = 0 if $ENV{CAL_SIMPLE_NO_DT};
42              
43             my @days = qw(31 xx 31 30 31 30 31 31 30 31 30 31);
44              
45             =head1 DESCRIPTION
46            
47             A very simple module that exports one function called C<calendar>.
48            
49             =head2 calendar
50            
51             This function returns a data structure representing the dates in a month.
52             The data structure returned is an array of array references. The first
53             level array represents the weeks in the month. The second level array
54             contains the actual days. By default, each week starts on a Sunday and
55             the value in the array is the date of that day. Any days at the beginning
56             of the first week or the end of the last week that are from the previous or
57             next month have the value C<undef>.
58            
59             If the month or year parameters are omitted then the current month or
60             year are assumed.
61            
62             A third, optional parameter, start_day, allows you to set the day each
63             week starts with, with the same values as localtime sets for wday
64             (namely, 0 for Sunday, 1 for Monday and so on).
65            
66             =cut
67              
68             sub calendar {
69 31     31 1 12896   my ($mon, $year, $start_day) = @_;
70              
71 31         222   my @now = (localtime)[4, 5];
72              
73 31 100       100   $mon = ($now[0] + 1) unless $mon;
74 31 100       46   $year = ($now[1] + 1900) unless $year;
75 31 100       83   $start_day = 0 unless defined $start_day;
76              
77 31 100 100     147   croak "Year $year out of range" if $year < 1970 && !$dt;
78 30 100 100     595   croak "Month $mon out of range" if ($mon < 1 || $mon > 12);
79 26 100 100     409   croak "Start day $start_day out of range"
80                 if ($start_day < 0 || $start_day > 6);
81              
82 22         19   my $first;
83              
84 22 100       29   if ($dt) {
85 14         86     $first = DateTime->new(year => $year,
86             month => $mon,
87             day => 1)->day_of_week % 7;
88               } else {
89 8         44     $first = (localtime timelocal 0, 0, 0, 1, $mon -1, $year - 1900)[6];
90               }
91              
92 22         3763   $first -= $start_day;
93 22 100       52   $first += 7 if ($first < 0);
94              
95 22         31   my @mon = (1 .. _days($mon, $year));
96              
97 22         52   my @first_wk = (undef) x 7;
98 22         56   @first_wk[$first .. 6] = splice @mon, 0, 6 - $first + 1;
99              
100 22         32   my @month = (\@first_wk);
101              
102 22         49   while (my @wk = splice @mon, 0, 7) {
103 90         374     push @month, \@wk;
104               }
105              
106 22         21   $#{$month[-1]} = 6;
  22         64  
107              
108 22 100       143   return wantarray ? @month : \@month;
109             }
110              
111             =head2 date_span
112            
113             This function returns a cur-down version of a month data structure which
114             begins and ends on dates other than the first and last dates of the month.
115             Any weeks that fall completely outside of the date range are removed from
116             the structure and any days within the remaining weeks that fall outside
117             of the date range are set to C<undef>.
118            
119             As there are a number of parameters to this function, they are passed
120             using a named parameter interface. The parameters are as follows:
121            
122             =over 4
123            
124             =item year
125            
126             The required year. Defaults to the current year if omitted.
127            
128             =item mon
129            
130             The required month. Defaults to the current month if omitted.
131            
132             =item begin
133            
134             The first day of the required span. Defaults to the first if omitted.
135            
136             =item end
137            
138             The last day of the required span. Defaults to the last day of the month
139             if omitted.
140            
141             =item start_day
142            
143             Indicates the day of the week that each week starts with. This takes the same
144             values as the optional third parameter to C<calendar>. The default is 0
145             (for Sunday).
146            
147             =back
148            
149             This function isn't exported by default, so in order to use it in your
150             program you need to use the module like this:
151            
152             use Calendar::Simple 'date_span';
153            
154             =cut
155              
156             sub date_span {
157 3     3 1 1885   my %params = @_;
158              
159 3         25   my @now = (localtime)[4, 5];
160              
161 3   33     10   my $mon = $params{mon} || ($now[0] + 1);
162 3   33     5   my $year = $params{year} || ($now[1] + 1900);
163 3   100     7   my $begin = $params{begin} || 1;
164 3   66     8   my $end = $params{end} || _days($mon, $year);
165 3 50       4   my $start_day = defined $params{start_day} ? $params{start_day} : 0;
166              
167 3         5   my @cal = calendar($mon, $year, $start_day);
168              
169 3         6   while ($cal[0][6] < $begin) {
170 4         7     shift @cal;
171               }
172              
173 3         3   my $i = 0;
174 3   66     14   while (defined $cal[0][$i] and $cal[0][$i] < $begin) {
175 2         6     $cal[0][$i++] = undef;
176               }
177              
178 3         6   while ($cal[-1][0] > $end) {
179 2         8     pop @cal;
180               }
181              
182 3         3   $i = -1;
183 3   100     9   while (defined $cal[-1][$i] and $cal[-1][$i] > $end) {
184 4         12     $cal[-1][$i--] = undef;
185               }
186              
187 3         20   return @cal;
188             }
189              
190             sub _days {
191 23     23   27   my ($mon, $yr) = @_;
192              
193 23 100       92   return $days[$mon - 1] unless $mon == 2;
194 8 100       11   return _isleap($yr) ? 29 : 28;
195             }
196              
197             sub _isleap {
198 8 100   8   17   return 1 unless $_[0] % 400;
199 6 100       12   return unless $_[0] % 100;
200 5 100       17   return 1 unless $_[0] % 4;
201 2         8   return;
202             }
203              
204             1;
205             __END__
206            
207             =head2 EXAMPLE
208            
209             A simple C<cal> replacement would therefore look like this:
210            
211             #!/usr/bin/perl -w
212            
213             use strict;
214             use Calendar::Simple;
215            
216             my @months = qw(January February March April May June July August
217             September October November December);
218            
219             my $mon = shift || (localtime)[4] + 1;
220             my $yr = shift || (localtime)[5] + 1900;
221            
222             my @month = calendar($mon, $yr);
223            
224             print "\n$months[$mon -1] $yr\n\n";
225             print "Su Mo Tu We Th Fr Sa\n";
226             foreach (@month) {
227             print map { $_ ? sprintf "%2d ", $_ : ' ' } @$_;
228             print "\n";
229             }
230            
231             A version of this example, called C<pcal>, is installed when you install this
232             module.
233            
234             =head2 Date Range
235            
236             This module will make use of DateTime.pm if it is installed. By using
237             DateTime.pm it can use any date that DateTime can represent. If DateTime
238             is not installed it uses Perl's built-in date handling and therefore
239             can't deal with dates before 1970 and it will also have problems with dates
240             after 2038 on a 32-bit machine.
241            
242             =head2 EXPORT
243            
244             C<calendar>
245            
246             =head1 AUTHOR
247            
248             Dave Cross <dave@mag-sol.com>
249            
250             =head1 ACKNOWLEDGEMENTS
251            
252             With thanks to Paul Mison <cpan@husk.org> for the start day patch.
253            
254             =head1 COPYRIGHT
255            
256             Copyright (C) 2002-2008, Magnum Solutions Ltd. All Rights Reserved.
257            
258             =head1 LICENSE
259            
260             This script is free software; you can redistribute it and/or
261             modify it under the same terms as Perl itself.
262            
263             =head1 SEE ALSO
264            
265             L<perl>, L<localtime>, L<DateTime>
266            
267             =cut
268