blib/lib/Date/Ordinal.pm

filter: Statementstmt 66.0% 33 / 50 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 7 / 14 Podpod 66.6% 8 / 12 totaltotal 61.2% 57 / 93 CC 12 SCAR 13.2
CC
12
Cov
65%
all_day_ordinations
6.0
all_hour_ordinations
6.0
all_minute_ordinations
6.0
CRAP 3.7
↑ Summary
1 #-----------------------------------------------------------------------
2  
3 =head1 NAME
4  
5 Date::Ordinal - Conversion of dates to ordinal numbers and vice versa
6  
7 =head1 SYNOPSIS
8  
9 use Date::Ordinal;
10
11 $ord = month2ord('January'); # $ord gets 1
12 $mon = ord2month('1'); # $mon gets 'January'
13 $mon = ord2month(1); # $mon gets 'January'
14 $mon = ord2month('01'); # $mon gets 'January'
15 $arryref = all_month_ordinations
16 @arry = all_month_names
17 $arryref = all_month_names_ref
18 $arryref = all_short_month_names_ref
19 $arryref = all_day_ordinations
20  
21 $arryref = all_hour_ordinations
22 $arryref = all_minute_ordinations
23  
24  
25 $hashref = ordination_month_pair # {'01' => 'January', ... }
26 @day = days
27  
28
29 =cut
30  
31 #-----------------------------------------------------------------------
32  
33 package Date::Ordinal;
34 #use strict;
35  
36 #-----------------------------------------------------------------------
37  
38 =head1 DESCRIPTION
39  
40 This module is designed to aid in creation of CGI popup_menus and also
41 interaction with SQL databases.
42  
43  
44 =cut
45  
46 #-----------------------------------------------------------------------
47  
48 1 require Exporter;
49 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 #-----------------------------------------------------------------------
52 # Public Global Variables
53 #-----------------------------------------------------------------------
54 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 $VERSION = '2.9';
56 1 @ISA = qw(Exporter);
57 1 @EXPORT = qw(ord2month month2ord ordination_month_pair all_month_ordinationsall_day_ordinations all_hour_ordinations all_minute_ordinations prezero);
58  
59 #-----------------------------------------------------------------------
60 # Non-Private Global Variables
61 #-----------------------------------------------------------------------
62 1 @month=();
63  
64 #=======================================================================
65  
66 =head1 CONVERSION ROUTINES
67  
68 There are two conversion routines: C<ord2month()> and C<month2ord()>.
69  
70 =over 8
71  
72 =item ord2month()
73  
74 This function takes a month number [1..12] and returns a string
75 which contains the name of the month identified. If the number is
76 not a valid number, then C<undef> will be returned:
77  
78 $mon = ord2month('3');
79  
80 =item C<month2ord()>
81  
82 This function takes a month name and returns the integer
83 corresponding to the month name, if such exists.
84  
85 The match is a regexp match, so both 'Mar' and 'March' will return 3.
86  
87 If the argument could not be identified as a month name,
88 then C<undef> will be returned:
89  
90 $ord = month2ord('March');
91  
92 The case of the month name is not important.
93 See the section L<KNOWN BUGS AND LIMITATIONS> below.
94  
95 =back
96  
97 =cut
98  
99 #=======================================================================
100 sub ord2month
101 1 ▶{
Subroutine ord2month: called CC=4 SCAR=14.5
Pod documented
102 1 my $ord = shift;
103  
104 1 ▶ return undef unless (($ord >=1) && ($ord <=12));
Branch: 1 branch
BranchTrueFalseTotal
unless $ord >= 1 and $ord <= 12 0 1 1
Condition33%1/3
$ord >= 1 and $ord <= 12
!ll&&!rl&&r
001
MC/DC0%0/2
$ord >= 1 and $ord <= 12
$ord >= 1 $ord <= 12
Truth table33%1/3
A and B
A= $ord >= 1B= $ord <= 12
ABresult
0X0
100
111
105  
106 1 my $month_count;
107
108 1 ▶ while (1+$month_count++ != $ord) { ; }
Branch: 1 branch
BranchTrueFalseTotal
if (1 + $month_count++ != $ord) 4 1 5
109  
110 1 return $month[$month_count-1];
111 }
112  
113 sub month2ord
114 1 ▶{
Subroutine month2ord: called CC=3 SCAR=11.2
Pod documented
115 1 my $month = shift;
116  
117 1 $state = "\u\L$state";
118  
119 1 my $month_count;
120  
121 1 ▶ for ($month_count=0; $month_count < 12; ++$month_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($month_count < 12) 5 0 5
122 5 ▶ return ++$month_count if ($month[$month_count] =~ /$month/);
Branch: 1 branch
BranchTrueFalseTotal
if $month[$month_count] =~ /$month/ 1 4 5
123 }
124  
125 0 return undef;
126 }
127  
128 #=======================================================================
129  
130 =head1 QUERY ROUTINES
131  
132 There is one function (and a reference variant) which can be used to
133 obtain a list of all month names:
134  
135  
136 =over 8
137  
138 =item C<all_month_names()>
139  
140 Returns a list of all month names;
141  
142 =item C<all_month_names_ref()>
143  
144 Returns a reference to a list of all month names;
145  
146 =item C<all_short_month_names_ref()>
147  
148 Returns a reference to a list of all month names in 3-letter form
149  
150 =item C<ordination_month_pair()>
151  
152 returns a reference to a hash of the ordination of a month name
153 and the month name
154  
155 =item C<all_month_ordinations()>
156  
157 returns a reference a hash of the ordination of a month name
158 and the month name
159  
160 =back
161  
162 =cut
163  
164 #=======================================================================
165 sub all_month_names
166 0 ▶{
Subroutine all_month_names: not called CC=1 SCAR=6.9
Pod documented
167 0 return @month;
168 }
169  
170 sub all_month_names_ref
171 0 ▶{
Subroutine all_month_names_ref: not called CC=1 SCAR=6.9
Pod documented
172 0 return \@month;
173 }
174  
175 sub all_short_month_names_ref
176 0 ▶{
Subroutine all_short_month_names_ref: not called CC=1 SCAR=6.9
Pod documented
177 0 return \@short_month;
178 }
179  
180 sub ordination_month_pair
181 1 ▶{
Subroutine ordination_month_pair: called CC=3 SCAR=11.0
Pod documented
182 1 my $counter=0;
183
184 1 foreach (@month) {
185 12 ▶ $month_ordination = (++$counter < 10)
Branch: 1 branch
BranchTrueFalseTotal
++$counter < 10 ? : 9 3 12
186 ? "0" . $counter : $counter;
187 12 $ref{$month_ordination}=$_;
188 }
189  
190 1 return \%ref;
191
192 }
193  
194 1 ▶sub all_month_ordinations {
Subroutine all_month_ordinations: called CC=1 SCAR=0.0
Pod documented
195  
196 31 ▶sub bynumber {$a <=> $b}
Subroutine bynumber: called CC=1 SCAR=0.0
Pod undocumented
197  
198 1 return [ (sort bynumber keys %{&ordination_month_pair}) ];
199 }
200  
201 0 ▶sub all_day_ordinations {
Subroutine all_day_ordinations: not called CC=2 SCAR=17.9
Pod undocumented
202  
203 0 my @ary;
204 0 foreach (1..31) {
205 0 push @ary, prezero($_);
206 }
207 0 return \@ary;
208 }
209  
210 0 ▶sub all_hour_ordinations {
Subroutine all_hour_ordinations: not called CC=2 SCAR=17.9
Pod undocumented
211  
212 0 my @ary;
213 0 foreach (1..12) {
214 0 push @ary, prezero($_);
215 }
216 0 return \@ary;
217 }
218  
219 0 ▶sub all_minute_ordinations {
Subroutine all_minute_ordinations: not called CC=2 SCAR=17.9
Pod undocumented
220  
221 0 my @ary;
222 0 foreach (0..59) {
223 0 push @ary, prezero($_);
224 }
225 0 return \@ary;
226 }
227  
228  
229 #=======================================================================
230  
231 =head1 PRETTY-PRINTING ROUTINES
232  
233 =over 8
234  
235 =item C<prezero()>
236  
237 if the current number is single-diit, prefix it with a '0'
238  
239 =back
240  
241 =cut
242  
243 #=======================================================================
244 0 ▶sub prezero() {
Subroutine prezero: not called CC=2 SCAR=17.9
Pod documented
245 return (
246 0 ▶ ($_[0] < 10)
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 10 ? : 0 0 0
247 ? "0$_[0]"
248 : $_[0]
249 )
250 ;
251 }
252  
253  
254 #-----------------------------------------------------------------------
255  
256  
257 =head1 KNOWN BUGS AND LIMITATIONS
258  
259 none
260  
261 =head1 SEE ALSO
262  
263 =over 4
264  
265 =item Locale::US
266  
267  
268 =item Date::Manip
269  
270 =back
271  
272 =head1 AUTHOR
273  
274 Terrence Brannon E<lt>tbrannon@end70.comE<gt>
275  
276 =head1 COPYRIGHT
277  
278 Copyright (c) 2000 End70 Corporation
279  
280 This module is free software; you can redistribute it and/or
281 modify it under the same terms as Perl itself.
282  
283 =cut
284  
285 #-----------------------------------------------------------------------
286  
287 #=======================================================================
288 # initialisation code - stuff the DATA into the CODES hash
289 #=======================================================================
290  
291 1 @month=qw(
292 January February March April May June July
293 August September October November December
294 );
295  
296 1 @short_month = map { substr($_,0,3) } @month;
297  
298  
299 1 1;
300  
↑ Summary