File Coverage

blib/lib/Date/Ordinal.pm
Criterion Covered Total %
statement 24 41 58.5
branch 5 8 62.5
condition 1 3 33.3
subroutine 7 14 50.0
pod 8 12 66.6
total 45 78 57.6


line stmt bran cond sub pod time code
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             require Exporter;
49 1     1   2602 use Carp;
  1         2  
  1         132  
50              
51             #-----------------------------------------------------------------------
52             # Public Global Variables
53             #-----------------------------------------------------------------------
54 1     1   6 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         1  
  1         876  
55             $VERSION = '2.9';
56             @ISA = qw(Exporter);
57             @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             @month=();
63              
64             #=======================================================================
65              
66             =head1 CONVERSION ROUTINES
67              
68             There are two conversion routines: C and C.
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 will be returned:
77              
78             $mon = ord2month('3');
79              
80             =item C
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 will be returned:
89              
90             $ord = month2ord('March');
91              
92             The case of the month name is not important.
93             See the section L below.
94              
95             =back
96              
97             =cut
98              
99             #=======================================================================
100             sub ord2month
101             {
102 1     1 1 8 my $ord = shift;
103              
104 1 50 33     11 return undef unless (($ord >=1) && ($ord <=12));
105              
106 1         2 my $month_count;
107            
108 1         9 while (1+$month_count++ != $ord) { ; }
109              
110 1         20 return $month[$month_count-1];
111             }
112              
113             sub month2ord
114             {
115 1     1 1 32 my $month = shift;
116              
117 1         4 $state = "\u\L$state";
118              
119 1         2 my $month_count;
120              
121 1         5 for ($month_count=0; $month_count < 12; ++$month_count) {
122 5 100       39 return ++$month_count if ($month[$month_count] =~ /$month/);
123             }
124              
125 0         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
139              
140             Returns a list of all month names;
141              
142             =item C
143              
144             Returns a reference to a list of all month names;
145              
146             =item C
147              
148             Returns a reference to a list of all month names in 3-letter form
149              
150             =item C
151              
152             returns a reference to a hash of the ordination of a month name
153             and the month name
154              
155             =item C
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             {
167 0     0 1 0 return @month;
168             }
169              
170             sub all_month_names_ref
171             {
172 0     0 1 0 return \@month;
173             }
174              
175             sub all_short_month_names_ref
176             {
177 0     0 1 0 return \@short_month;
178             }
179              
180             sub ordination_month_pair
181             {
182 1     1 1 3 my $counter=0;
183            
184 1         3 foreach (@month) {
185 12 100       22 $month_ordination = (++$counter < 10)
186             ? "0" . $counter : $counter;
187 12         29 $ref{$month_ordination}=$_;
188             }
189              
190 1         12 return \%ref;
191            
192             }
193              
194             sub all_month_ordinations {
195              
196 26     26 0 26 sub bynumber {$a <=> $b}
197              
198 1     1 1 5 return [ (sort bynumber keys %{&ordination_month_pair}) ];
  1         4  
199             }
200              
201             sub all_day_ordinations {
202              
203 0     0 0   my @ary;
204 0           foreach (1..31) {
205 0           push @ary, prezero($_);
206             }
207 0           return \@ary;
208             }
209              
210             sub all_hour_ordinations {
211              
212 0     0 0   my @ary;
213 0           foreach (1..12) {
214 0           push @ary, prezero($_);
215             }
216 0           return \@ary;
217             }
218              
219             sub all_minute_ordinations {
220              
221 0     0 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
236              
237             if the current number is single-diit, prefix it with a '0'
238              
239             =back
240              
241             =cut
242              
243             #=======================================================================
244             sub prezero() {
245             return (
246 0 0   0 1   ($_[0] < 10)
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 Etbrannon@end70.comE
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             @month=qw(
292             January February March April May June July
293             August September October November December
294             );
295              
296             @short_month = map { substr($_,0,3) } @month;
297              
298              
299             1;
300