File Coverage

blib/lib/Date/DayOfNthWeek.pm
Criterion Covered Total %
statement 9 67 13.4
branch 0 66 0.0
condition 0 33 0.0
subroutine 3 6 50.0
pod 0 3 0.0
total 12 175 6.8


line stmt bran cond sub pod time code
1             package Date::DayOfNthWeek;
2              
3             our $VERSION = '1.0';
4              
5 1     1   61954 use 5.005;
  1         5  
  1         44  
6 1     1   6 use strict;
  1         2  
  1         41  
7 1     1   6 use warnings;
  1         169  
  1         3624  
8              
9             require Exporter;
10              
11             our @ISA = qw(Exporter);
12              
13             our %EXPORT_TAGS = ( 'all' => [ qw(day_week last_week first_week) ] );
14              
15             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16              
17             our @EXPORT = qw();
18              
19             sub day_week($$) {
20              
21 0     0 0   my $day = shift;
22 0           my $week = shift;
23            
24 0 0 0       die "your day is out of the range 0 - 6 Sunday==0\n" unless ((0 <= $day) && ( $day <= 6));
25 0 0 0       die "Your week of the month is out of range (Range is 1-6)\n" unless ((1 <= $week) && ( $week <= 6));
26              
27 0           my (undef,undef,undef,$mday,undef,undef,$wday,undef,undef) = localtime;
28              
29             # return unless the days match
30 0 0         return 0 unless $wday == $day;
31              
32 0           my $date = $mday-1;
33              
34 0           my %hash = ();
35              
36 0           for my $c (0 ..6 ) {
37 0           my $a = $date+$c;
38 0           my $key = $a%7;
39 0           my $w = (int($a/7))+1;
40 0           $hash{$key} = $w;
41             }
42            
43 0           my $q = $hash{$wday};
44              
45 0 0         return 0 unless $q == $week;
46              
47 0           return 1;
48             }
49              
50             sub last_week($) {
51              
52 0     0 0   my $day = shift;
53              
54 0 0 0       die "your day is out of the range 0 - 6 Sunday==0\n" unless ((0 <= $day) && ( $day <= 6));
55              
56 0           my (undef,undef,undef,$mday,$mon,$year,$wday,undef,undef) = localtime;
57              
58 0 0         return 0 unless $wday == $day; # return unless the days match
59              
60 0           my $max = 0;
61              
62             # how many days in the month?
63             # 0 1 2 3 4 5 6 7 8 9 10 11
64             # Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
65             # 31 28 31 30 31 30 31 31 30 31 30 31
66              
67             # This is laid out like this because using || was not giving me the correct
68             # answer each time.
69              
70 0 0         if ($mon == 0 ) { $max=31;} # January
  0 0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
71             # if the month is February is it a leap year?
72 0           elsif ($mon == 1 ) { # This is to account for leap
73 0 0         if ( $year % 4 ) { $max = 28; } # years. Which works like this:
  0            
74             else { # if year%4 != 0 it is a
75 0 0         if ( $year % 100 ) { $max = 28; } # non-leap year, so Feb has 28 days.
  0            
76 0           else { $max = 29; } # If year%4 == 0 it could be a leap
77             } # year. If the year ends in 00,
78             } # Feb has 28 days, otherwise it
79             # has 29 days and is a leap year.
80 0           elsif ($mon == 2 ) {$max=31; } # March
81 0           elsif ($mon == 3 ) {$max=30; } # April
82 0           elsif ($mon == 4 ) {$max=31; } # May
83 0           elsif ($mon == 5 ) {$max=30; } # June
84 0           elsif ($mon == 6 ) {$max=31; } # July
85 0           elsif ($mon == 7 ) {$max=31; } # August
86 0           elsif ($mon == 8 ) {$max=30; } # September
87 0           elsif ($mon == 9 ) {$max=31; } # October
88 0           elsif ($mon == 10 ) {$max=30; } # November
89             elsif ($mon == 11 ) {$max=31; } # December
90 0           else { die "your month is out of the range 0 - 11\n"; }
91              
92              
93 0 0         return 1 if $mday == $max; # if it is the last day of the month, it has to be the last week of the month.
94              
95 0           my $diff = $max - $mday;
96              
97 0 0         return 0 if ($diff > 6); # date can't be in the last week because
98             # there is more than 7 days left in the month.
99              
100 0 0 0       if (($wday == 6) && ($diff >0)) {
    0 0        
    0 0        
    0 0        
    0 0        
    0 0        
    0 0        
101 0           return 0;
102             }
103             elsif (($wday == 5) && ($diff >1)) {
104 0           return 0;
105             }
106             elsif (($wday == 4) && ($diff >2)) {
107 0           return 0;
108             }
109             elsif (($wday == 3) && ($diff >3)) {
110 0           return 0;
111             }
112             elsif (($wday == 2) && ($diff >4)) {
113 0           return 0;
114             }
115             elsif (($wday == 1) && ($diff >5)) {
116 0           return 0;
117             }
118             elsif (($wday == 0) && ($diff >6)) {
119 0           return 0;
120             }
121             else {
122 0           return 1;
123             }
124             }
125              
126             sub first_week($) {
127              
128 0     0 0   my $day = shift;
129              
130 0 0 0       die "your day is out of the range 0 - 6 Sunday==0\n" unless ((0 <= $day) && ( $day <= 6));
131              
132 0           my (undef,undef,undef,$mday,undef,undef,$wday,undef,undef) = localtime;
133              
134 0 0         return 0 if $mday > 7; # can't be the first week of the month if it is after the 7th
135              
136 0 0         return 0 unless $wday == $day; # return unless the days match
137              
138 0 0         return 0 if ($mday-$day) > 1; #
139              
140 0           return 1;
141             }
142              
143              
144             1;
145             __END__