File Coverage

blib/lib/PlotCalendar/DateDesc.pm
Criterion Covered Total %
statement 34 56 60.7
branch 0 2 0.0
condition 1 3 33.3
subroutine 8 9 88.8
pod 0 5 0.0
total 43 75 57.3


line stmt bran cond sub pod time code
1             package PlotCalendar::DateDesc;
2              
3 2     2   1281 use strict;
  2         4  
  2         97  
4 2     2   11 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  2         5  
  2         314  
5              
6             require Exporter;
7              
8             @ISA = qw(Exporter);
9             # Items to export into callers namespace by default. Note: do not export
10             # names by default without a very good reason. Use EXPORT_OK instead.
11             # Do not simply export all your public functions/methods/constants.
12             @EXPORT = qw( getdates getdom);
13              
14             $VERSION = sprintf "%d.%02d", q$Revision: 1.0 $ =~ m#(\d+)\.(\d+)#;
15              
16 2     2   28 use Carp;
  2         3  
  2         186  
17 2     2   1151 use PlotCalendar::DateTools qw(Add_Delta_Days Day_of_Week Day_of_Year Days_in_Month Decode_Day_of_Week Day_of_Week_to_Text Month_to_Text);
  2         8  
  2         1506  
18              
19             sub new {
20 1     1 0 57 my $proto = shift;
21 1   33     8 my $class = ref($proto) || $proto;
22 1         3 my $self = {};
23              
24             # Values to apply to all cells
25              
26 1         3 $self->{MONTH} = shift; # Month (1-12)
27 1         2 $self->{YEAR} = shift; # 4 digit (yes, we are y2k compliant)
28              
29 1         3 bless $self, $class;
30              
31 1         3 return $self;
32              
33             }
34              
35             # ****************************************************************
36             sub getdates {
37 0     0 0 0 my $self = shift;
38 0         0 my $desc = shift;
39              
40 0         0 my @doms = parse_desc($self,$desc);
41              
42 0         0 my $mm = $self->{MONTH};
43 0         0 my $yy = $self->{YEAR};
44              
45 0         0 my @dates = map {$mm . "/" . $_ . "/" . $yy} @doms;
  0         0  
46              
47             return \@dates
48 0         0 }
49              
50             # ****************************************************************
51             sub getdom {
52 1     1 0 10 my $self = shift;
53 1         2 my $desc = shift;
54              
55 1         3 my @doms = parse_desc($self,$desc);
56              
57 0         0 return \@doms;
58             }
59              
60             # ****************************************************************
61             sub parse_desc {
62             # parse the descriptions
63 1     1 0 1 my $self = shift;
64 1         2 my $desc = shift;
65              
66 1         6 my %ords = ( 'first' => '0',
67             'second' => '1',
68             'third' => '2',
69             'fourth' => '3',
70             'fifth' => '4',
71             'last' => '-1',
72             );
73 1         2 my @doms;
74             my $component;
75 1         4 foreach $component ( (split(/ and /,$desc))) { # split on 'and'
76             # this is either a single dayname or a qualified dayname
77 1         4 $component =~ s/^\s*//;
78 1         6 $component =~ s/\s*$//;
79 1         3 my @desc = (split(/\s+/,$component));
80 1         9 my @temp = days_of_month($self->{YEAR},$self->{MONTH},$desc[$#desc]);
81 0 0       0 if ($#desc == 0) { # just a dayname
82 0         0 push @doms,@temp;
83             }
84             else { # dayname and qualifier
85 0         0 push @doms,$temp[$ords{$desc[0]}];
86             }
87             }
88 0         0 return @doms;
89             }
90              
91             # ****************************************************************
92             sub days_of_month {
93 1     1 0 2 my ($yr, $mon, $dayname) = @_;
94 1         5 my $dow = Decode_Day_of_Week($dayname);
95 1         4 my $dowfirst = Day_of_Week($yr,$mon,1);
96 0           my $days = Days_in_Month($yr,$mon);
97              
98 0           my @dom;
99              
100 0           my $first = Add_Delta_Days($yr,$mon,1, ($dow - $dowfirst)%7);
101 0           $dom[0]=$first;
102 0           my $j=7;
103 0           for (my $i=7+$first;$i<=$days;$i+=7) {
104 0           push @dom,$first+$j;
105 0           $j+=7;
106             }
107              
108 0           return @dom;
109            
110             }
111              
112              
113             1;
114             __END__