File Coverage

blib/lib/Time/DayOfWeek.pm
Criterion Covered Total %
statement 32 38 84.2
branch 7 18 38.8
condition 5 15 33.3
subroutine 8 9 88.8
pod 5 5 100.0
total 57 85 67.0


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