File Coverage

blib/lib/Time/DayOfWeek.pm
Criterion Covered Total %
statement 29 35 82.8
branch 7 18 38.8
condition 5 15 33.3
subroutine 7 8 87.5
pod 5 5 100.0
total 53 81 65.4


line stmt bran cond sub pod time code
1             # 3C7Exdx - Time::DayOfWeek.pm created by Pip@CPAN.Org to simply tell what day of the week a specific date is.
2             package Time::DayOfWeek;
3             require Exporter;
4 2     2   10860 use strict;
  2         4  
  2         69  
5 2     2   9 use warnings;
  2         5  
  2         69  
6 2     2   11 use base qw( Exporter );
  2         3  
  2         1457  
7             our $VERSION = '1.6.A6FFxZB'; our $PTVR = $VERSION; $PTVR =~ s/^\d+\.\d+\.//; # Please see `perldoc Time::PT` for an explanation of $PTVR.
8             our @EXPORT = qw( DoW ); # only export DoW() for 'use Time::DayOfWeek;' and all other stuff optionally
9             our @EXPORT_OK = qw( Dow DayOfWeek DayNames MonthNames );
10             our %EXPORT_TAGS = ( 'all' => [ qw( DoW Dow DayOfWeek DayNames MonthNames ) ],
11             'dow' => [ qw( DoW Dow DayOfWeek ) ],
12             'nam' => [ qw( DayNames MonthNames ) ],
13             'day' => [ qw( DoW Dow DayOfWeek DayNames ) ]);
14             my @Days = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
15             my @Day = (); push(@Day, substr($_, 0, 3)) foreach(@Days);
16             my @Months = ( qw( January February March April May June
17             July August September October November December ) );
18             sub DoW { # calculate the day-of-the-week from the Year, Month, and Day
19 6 50   6 1 2136 my $year = shift; $year = 2000 unless(defined($year));
  6         20  
20 6 50 33     8 my $mont = shift; $mont = 1 unless(defined($mont) && $mont); # 1..12
  6         33  
21 6 50 33     8 my $daay = shift; $daay = 1 unless(defined($daay) && $daay); # 1..31
  6         27  
22 6 50       33 if($mont !~ /^\d+$/) { # match a named month if param not a number 1..12
23 0 0       0 for(my $i = 0; $i < @Months; $i++) { if($Months[$i] =~ /^$mont/i) { $mont = ($i + 1); last; } }
  0         0  
  0         0  
  0         0  
24             }
25 6         13 my $mndx = int((14 - $mont) / 12); my $yshf = $year - $mndx; my $ys4h = $yshf / 400; $daay += $yshf + int($ys4h) - int($ys4h * 4) + int($ys4h * 100);
  6         7  
  6         9  
  6         14  
26 6 50 33     33 $daay++ if(($year == 2008 && $mont >= 3) || ($year == 2009 && $mont <= 2)); # silly kludge hack to shift right between Feb.29..28leap-dayz for 2008..2009
      33        
      33        
27 6         27 return(($daay + (31 * int((12 * $mndx) + $mont - 2)) / 12) % 7);
28             }
29 3     3 1 332 sub Dow { return($Day[ DoW(@_)]); } # return 3-letter abbrev.
30 1     1 1 175 sub DayOfWeek { return($Days[DoW(@_)]); } # return full day name
31 1 50   1 1 159 sub DayNames { @Days = @_ if(@_ >= @Days); @Day = (); # assign a new day names list if there aren't too few day names
  1         4  
32 1 50       4 for(@Days) { (length($_) > 3) ? push(@Day, substr($_, 0, 3)) : push(@Day, $_); } # redo abbrevs
  7         18  
33 1         2 return( @Days );
34             }
35 0 0   0 1   sub MonthNames { @Months = @_ if(@_ >= @Months); return(@Months); } # assign a new month names list if there aren't too few month names
  0            
36              
37             127;
38              
39             =head1 NAME
40              
41             Time::DayOfWeek - calculate which Day-of-Week a date is
42              
43             =head1 VERSION
44              
45             This documentation refers to version 1.6.A6FFxZB of Time::DayOfWeek, which was released on Tue Jun 15 15:59:35:11 2010.
46              
47             =head1 SYNOPSIS
48              
49             #!/usr/bin/perl
50             use strict; use warnings;
51             use Time::DayOfWeek qw(:dow);
52              
53             my($year, $month, $day)=(2003, 12, 7);
54              
55             print "The Day-of-Week of $year/$month/$day (YMD) is: ",
56             DayOfWeek($year, $month, $day), "\n";
57             print 'The 3-letter abbreviation of the Dow is: ',
58             Dow( $year, $month, $day), "\n";
59             print 'The 0-based index of the DoW is: ',
60             DoW( $year, $month, $day), "\n";
61              
62             =head1 DESCRIPTION
63              
64             This module just calculates the Day-of-Week for any particular date. It was inspired by the clean L module written by David Muir
65             Sharnoff .
66              
67             =head1 2DO
68              
69             =over 2
70              
71             =item - What else does DayOfWeek need?
72              
73             =back
74              
75             =head1 PURPOSE
76              
77             The reason I created DayOfWeek was to support other Time modules which would like to have a Day-of-Week calculated.
78              
79             =head1 USAGE
80              
81             =head2 DoW(, , )
82              
83             Time::DayOfWeek's core function which does the calculation and returns the weekday index answer in 0..6. If no Year is supplied, 2000 C.E. is assumed. If no
84             Month or Day is supplied, they are set to 1. Months are 1-based in 1..12.
85              
86             DoW() is the only function that is exported from a normal 'use Time::DayOfWeek;' command. Other functions can be imported to local namespaces explicitly or
87             with the following tags:
88              
89             :all - every function described here
90             :dow - only DoW(), Dow(), and DayOfWeek()
91             :nam - only DayNames() and MonthNames()
92             :day - everything but MonthNames()
93              
94             =head2 Dow(, , )
95              
96             same as above but returns 3-letter day abbreviations in 'Sun'..'Sat'.
97              
98             =head2 DayOfWeek(, , )
99              
100             same as above but returns full day names in 'Sunday'..'Saturday'.
101              
102             =head2 DayNames(<@NewDayNames>)
103              
104             can override default day names with the strings in @NewDayNames. The current list of day names is returned so call DayNames() with no parameters
105             to obtain a list of the default day names.
106              
107             An example call is:
108              
109             DayNames('Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado');
110              
111             =head2 MonthNames(<@NewMonthNames>)
112              
113             has also been included to provide a centralized name set. Just like DayNames(), this function returns the current list of month names so call
114             MonthNames() with no parameters to obtain a list of the default month names.
115              
116             =head1 CHANGES
117              
118             Revision history for Perl extension Time::DayOfWeek:
119              
120             =over 4
121              
122             =item - 1.6.A6FFxZB Tue Jun 15 15:59:35:11 2010
123              
124             * had to bump minor version to keep them ascending
125              
126             =item - 1.4.A6FCO7V Tue Jun 15 12:24:07:31 2010
127              
128             * added hack to shift days right one between Feb2008..2009 (still not sure why algorithm skewed)
129              
130             =item - 1.4.75R5ulZ Sun May 27 05:56:47:35 2007
131              
132             * added kwalitee && POD tests, bumped minor version
133              
134             * condensed code && moved POD to bottom
135              
136             =item - 1.2.4CCMRd5 Sun Dec 12 22:27:39:05 2004
137              
138             * updated License
139              
140             =item - 1.0.429BmYk Mon Feb 9 11:48:34:46 2004
141              
142             * updated DoW param tests to turn zero month or day to one
143              
144             * updated POD to contain links
145              
146             =item - 1.0.41M4ecn Thu Jan 22 04:40:38:49 2004
147              
148             * made bin/dow as EXE_FILES && added named month param detection
149              
150             =item - 1.0.3CNH7Fs Tue Dec 23 17:07:15:54 2003
151              
152             * removed most eccentric misspellings
153              
154             =item - 1.0.3CCA4sO Fri Dec 12 10:04:54:24 2003
155              
156             * removed indenting from POD NAME field
157              
158             =item - 1.0.3CB7PxT Thu Dec 11 07:25:59:29 2003
159              
160             * added month name data and tidied up for release
161              
162             =item - 1.0.3C7IOam Sun Dec 7 18:24:36:48 2003
163              
164             * wrote pod and made tests
165              
166             =item - 1.0.3C7Exdx Sun Dec 7 14:59:39:59 2003
167              
168             * original version
169              
170             =back
171              
172             =head1 INSTALL
173              
174             Please run:
175              
176             `perl -MCPAN -e "install Time::DayOfWeek"`
177              
178             or uncompress the package && run:
179              
180             `perl Makefile.PL; make; make test; make install`
181             or if you don't have `make` but Module::Build is installed
182             `perl Build.PL; perl Build; perl Build test; perl Build install`
183              
184             =head1 LICENSE
185              
186             Most source code should be Free! Code I have lawful authority over is && shall be!
187             Copyright: (c) 2003-2007, Pip Stuart.
188             Copyleft : This software is licensed under the GNU General Public License (version 2). Please consult the Free Software Foundation (HTTP://FSF.Org)
189             for important information about your freedom.
190              
191             =head1 AUTHOR
192              
193             Pip Stuart
194              
195             =cut