File Coverage

blib/lib/Date/Calendar.pm
Criterion Covered Total %
statement 98 142 69.0
branch 18 34 52.9
condition 11 28 39.2
subroutine 15 22 68.1
pod 0 16 0.0
total 142 242 58.6


line stmt bran cond sub pod time code
1              
2             ###############################################################################
3             ## ##
4             ## Copyright (c) 2000 - 2009 by Steffen Beyer. ##
5             ## All rights reserved. ##
6             ## ##
7             ## This package is free software; you can redistribute it ##
8             ## and/or modify it under the same terms as Perl itself. ##
9             ## ##
10             ###############################################################################
11              
12             package Date::Calendar;
13              
14 4     4   225315 BEGIN { eval { require bytes; }; }
  4         153  
15 4     4   127 use strict;
  4         9  
  4         199  
16 4     4   25 use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION );
  4         9  
  4         530  
17              
18             require Exporter;
19              
20             @ISA = qw(Exporter);
21              
22             @EXPORT = qw();
23              
24             @EXPORT_OK = qw();
25              
26             $VERSION = '6.3';
27              
28 4     4   1533 use Carp::Clan qw(^Date::);
  4         2085  
  4         42  
29 4     4   2695 use Date::Calc::Object qw(:ALL);
  4         11  
  4         2762  
30 4     4   2599 use Date::Calendar::Year qw( check_year empty_period );
  4         14  
  4         10160  
31              
32             sub new
33             {
34 177     177 0 18554 my($class) = shift;
35 177         332 my($profile) = shift;
36 177   100     799 my($language) = shift || 0;
37 177         289 my($self);
38              
39 177         329 $self = [ ];
40 177   50     889 $class = ref($class) || $class || 'Date::Calendar';
41 177         633 bless($self, $class);
42 177         483 $self->[0] = { };
43 177         299 $self->[1] = $profile;
44 177         292 $self->[2] = $language;
45 177         338 $self->[3] = [@_];
46 177         459 return $self;
47             }
48              
49             sub year
50             {
51 227     227 0 1247 my($self) = shift;
52 227         1087 my($year) = shift_year(\@_);
53              
54 227         1153 &check_year($year);
55 227 100       687 if (defined $self->[0]{$year})
56             {
57 43         300 return $self->[0]{$year};
58             }
59             else
60             {
61 184         909 return $self->[0]{$year} =
62 184         348 Date::Calendar::Year->new( $year, $self->[1], $self->[2], @{$self->[3]} );
63             }
64             }
65              
66             sub cache_keys
67             {
68 2     2 0 12 my($self) = shift;
69              
70 2         13 return( sort {$a<=>$b} keys(%{$self->[0]}) );
  14         28  
  2         14  
71             }
72              
73             sub cache_vals
74             {
75 0     0 0 0 my($self) = shift;
76 0         0 local($_);
77              
78 0         0 return( map $self->[0]{$_}, sort {$a<=>$b} keys(%{$self->[0]}) );
  0         0  
  0         0  
79             }
80              
81             sub cache_clr
82             {
83 0     0 0 0 my($self) = shift;
84              
85 0         0 $self->[0] = { };
86             }
87              
88             sub cache_add
89             {
90 1     1 0 14 my($self) = shift;
91 1         3 my($year);
92              
93 1         8 while (@_)
94             {
95 5         26 $year = shift_year(\@_);
96 5         23 $self->year($year);
97             }
98             }
99              
100             sub cache_del
101             {
102 1     1 0 17 my($self) = shift;
103 1         2 my($year);
104              
105 1         6 while (@_)
106             {
107 1         6 $year = shift_year(\@_);
108 1 50       6 if (exists $self->[0]{$year})
109             {
110 1         61 delete $self->[0]{$year};
111             }
112             }
113             }
114              
115             sub date2index
116             {
117 0     0 0 0 my($self) = shift;
118 0         0 my(@date) = shift_date(\@_);
119              
120 0         0 return $self->year($date[0])->date2index(@date);
121             }
122              
123             sub labels
124             {
125 5     5 0 18 my($self) = shift;
126 5         10 my($year);
127             my(@date);
128 0         0 my(%result);
129              
130 5 50       11 if (@_)
131             {
132 5         27 @date = shift_date(\@_);
133 5         22 return $self->year($date[0])->labels(@date);
134             }
135             else
136             {
137 0         0 local($_);
138 0         0 %result = ();
139 0         0 foreach $year (keys(%{$self->[0]}))
  0         0  
140             {
141 0         0 grep( $result{$_} = 0, $self->year($year)->labels() );
142             }
143 0 0       0 return wantarray ? (keys %result) : scalar(keys %result);
144             }
145             }
146              
147             sub search
148             {
149 1     1 0 30 my($self,$pattern) = @_;
150 1         2 my($year);
151             my(@result);
152              
153 1         3 @result = ();
154 1         2 foreach $year (sort {$a<=>$b} keys(%{$self->[0]}))
  5         7  
  1         5  
155             {
156 4         18 push( @result, $self->year($year)->search($pattern) );
157             }
158 1 50       12 return wantarray ? (@result) : scalar(@result);
159             }
160              
161             sub tags
162             {
163 0     0 0 0 my($self) = shift;
164 0         0 my(%result) = ();
165 0         0 my(@date);
166              
167 0 0       0 if (@_)
168             {
169 0         0 @date = shift_date(\@_);
170 0         0 return $self->year($date[0])->tags(@date);
171             }
172 0         0 else { return \%result; }
173             }
174              
175             sub delta_workdays
176             {
177 10     10 0 148 my($self) = shift;
178 10         35 my($yy1,$mm1,$dd1) = shift_date(\@_);
179 10         34 my($yy2,$mm2,$dd2) = shift_date(\@_);
180 10         20 my($including1,$including2) = (shift,shift);
181 10         11 my($days,$empty,$year);
182              
183 10         13 $days = 0;
184 10         11 $empty = 1;
185 10 50       27 if ($yy1 == $yy2)
    100          
186             {
187 0         0 return $self->year($yy1)->delta_workdays(
188             $yy1,$mm1,$dd1, $yy2,$mm2,$dd2, $including1,$including2);
189             }
190             elsif ($yy1 < $yy2)
191             {
192 2 50 33     14 unless (($mm1 == 12) && ($dd1 == 31) && (!$including1))
      33        
193             {
194 2         9 $days += $self->year($yy1)->delta_workdays(
195             $yy1,$mm1,$dd1, $yy1,12,31, $including1,1);
196 2         5 $empty = 0;
197             }
198 2 50 33     14 unless (($mm2 == 1) && ($dd2 == 1) && (!$including2))
      33        
199             {
200 2         8 $days += $self->year($yy2)->delta_workdays(
201             $yy2, 1, 1, $yy2,$mm2,$dd2, 1,$including2);
202 2         3 $empty = 0;
203             }
204 2         20 for ( $year = $yy1 + 1; $year < $yy2; $year++ )
205             {
206 0         0 $days += $self->year($year)->delta_workdays(
207             $year,1,1, $year,12,31, 1,1);
208 0         0 $empty = 0;
209             }
210             }
211             else
212             {
213 8 50 33     43 unless (($mm2 == 12) && ($dd2 == 31) && (!$including2))
      33        
214             {
215 8         34 $days -= $self->year($yy2)->delta_workdays(
216             $yy2,$mm2,$dd2, $yy2,12,31, $including2,1);
217 8         13 $empty = 0;
218             }
219 8 50 33     49 unless (($mm1 == 1) && ($dd1 == 1) && (!$including1))
      33        
220             {
221 8         24 $days -= $self->year($yy1)->delta_workdays(
222             $yy1, 1, 1, $yy1,$mm1,$dd1, 1,$including1);
223 8         14 $empty = 0;
224             }
225 8         23 for ( $year = $yy2 + 1; $year < $yy1; $year++ )
226             {
227 0         0 $days -= $self->year($year)->delta_workdays(
228             $year,1,1, $year,12,31, 1,1);
229 0         0 $empty = 0;
230             }
231             }
232 10 50       22 &empty_period() if ($empty);
233 10         32 return $days;
234             }
235              
236             sub add_delta_workdays
237             {
238 7     7 0 97 my($self) = shift;
239 7         30 my($yy,$mm,$dd) = shift_date(\@_);
240 7         17 my($days) = shift;
241 7         9 my($date,$rest,$sign);
242              
243 7 50       13 if ($days == 0)
244             {
245 0         0 $rest = $self->year($yy)->date2index($yy,$mm,$dd); # check date
246 0         0 $date = Date::Calc->new($yy,$mm,$dd);
247 0 0       0 return wantarray ? ($date,$days) : $date;
248             }
249             else
250             {
251 7 100       17 $sign = ($days > 0) ? +1 : -1;
252 7         24 ($date,$rest,$sign) = $self->year($yy)->add_delta_workdays($yy,$mm,$dd,$days,$sign);
253 7         23 while ($sign)
254             {
255 7         23 ($date,$rest,$sign) = $self->year($date)->add_delta_workdays($date,$rest,$sign);
256             }
257 7 100       44 return wantarray ? ($date,$rest) : $date;
258             }
259             }
260              
261             sub is_full
262             {
263 0     0 0   my($self) = shift;
264 0           my(@date) = shift_date(\@_);
265 0           my($year) = $self->year($date[0]);
266              
267 0           return $year->vec_full->bit_test( $year->date2index(@date) );
268             }
269              
270             sub is_half
271             {
272 0     0 0   my($self) = shift;
273 0           my(@date) = shift_date(\@_);
274 0           my($year) = $self->year($date[0]);
275              
276 0           return $year->vec_half->bit_test( $year->date2index(@date) );
277             }
278              
279             sub is_work
280             {
281 0     0 0   my($self) = shift;
282 0           my(@date) = shift_date(\@_);
283 0           my($year) = $self->year($date[0]);
284              
285 0           return $year->vec_work->bit_test( $year->date2index(@date) );
286             }
287              
288             1;
289              
290             __END__