blib/lib/to_date.pl untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 265 CC 29 SCAR 27.4
CC
29
Cov
0%
rrrr
42.0
rr
30.0
a
20.0
CRAP 15.5
1 use Time::Local;
2
3 my ($s) = $_[0];
4 my ($f) = $_[1];
5
6 @fmts = split(/\b/, $f);
7 @fmts = split(/\b/, $f);
8 @inputs = split(/\b/, $s);
9 @today = localtime(time);
10 $err = '';
11 $rtnTime = '';
12 @tl = ();
13 $begofyear = timelocal(0,0,0,1,0,$today[5]);
14
15
16 for (my $i=0;$i<=$#fmts;$i++)
17 {
18 foreach my $f (qw(month ddd dd yyyymmdd yyyy yy hh24 hh mi mm mon sssss ss a p rm rr))
19 {
20 if ($fmts[$i] =~ /^$f/i)
21 {
22 $err .= &$f($i);
23 last;
24 }
25 }
26 }
27 $tl[3] = '1' unless ($tl[3]);
28 $rtnTime = timelocal(@tl) unless ($rtnTime);
29 $t = localtime($rtnTime);
30
31 sub month
32 {
33 my %mthhash = (
34 'january' => '0',
35 'february' => 1,
36 'march' => 2,
37 'april' => 3,
38 'may' => 4,
39 'june' => 5,
40 'july' => 6,
41 'august' => 7,
42 'september' => 8,
43 'october' => 9,
44 'november' => 10,
45 'december' => 11
46 );
47
48 my $indx = shift;
49 $inputs[$indx] =~ tr/A-Z/a-z/;
50 $tl[4] = $mthhash{$inputs[$indx]};
51 return "Invalid Month ($inputs[$indx])! " unless (length($tl[4]));
52 return '';
53 }
54
55 sub mon
56 {
57 my %mthhash = (
58 'jan' => '0',
59 'feb' => 1,
60 'mar' => 2,
61 'apr' => 3,
62 'may' => 4,
63 'jun' => 5,
64 'jul' => 6,
65 'aug' => 7,
66 'sep' => 8,
67 'oct' => 9,
68 'nov' => 10,
69 'dec' => 11
70 );
71
72 my $indx = shift;
73 $inputs[$indx] =~ tr/A-Z/a-z/;
74 $tl[4] = $mthhash{$inputs[$indx]};
75 return "Invalid Mth ($inputs[$indx])! " unless (length($tl[4]));
76 return '';
77 }
78
79 sub rm
80 {
81 my %mthhash = (
82 'i' => '0',
83 'ii' => 1,
84 'iii' => 2,
85 'iv' => 3,
86 'v' => 4,
87 'vi' => 5,
88 'vii' => 6,
89 'viii' => 7,
90 'ix' => 8,
91 'x' => 9,
92 'xi' => 10,
93 'xii' => 11
94 );
95
96 my $indx = shift;
97 $inputs[$indx] =~ tr/A-Z/a-z/;
98 $tl[4] = $mthhash{$inputs[$indx]};
99 return "Invalid Roman Mth. ($inputs[$indx])! " unless (length($tl[4]));
100 return '';
101 }
102
103 sub mm
104 {
105 my $indx = shift;
106 $inputs[$indx] =~ s/^0//;
107 return "Invalid month ($inputs[$indx])! "
108 unless ($inputs[$indx] > 0 && $inputs[$indx] <= 12);
109 $tl[4] = $inputs[$indx] - 1;
110 return '';
111 }
112
113 sub yyyymmdd
114 {
115 $tl[5] = substr($inputs[$indx],0,4);
116 $tl[4] = substr($inputs[$indx],4,2) - 1;
117 $tl[3] = substr($inputs[$indx],6,2);
118 return '';
119 }
120
121 sub yyyy
122 {
123 my $indx = shift;
124 return "Invalid year ($inputs[$indx])! "
125 unless ($inputs[$indx] =~ /^\d\d\d\d$/);
126 $tl[5] = $inputs[$indx];
127 return '';
128 }
129
130 sub yy
131 {
132 return rr(shift);
133 }
134
135 sub rr
136 {
137 my $indx = shift;
138 return "Invalid year ($inputs[$indx])! "
139 unless ($inputs[$indx] =~ /^\d\d$/);
140 if (($today[5] % 100) > 50)
141 {
142 $inputs[$indx] += 100 if ($inputs[$indx] < 50);
143 }
144 else
145 {
146 #$inputs[$indx] -= 100 if ($inputs[$indx] > 50);
147 $inputs[$indx] += 100 if ($inputs[$indx] < 50);
148 }
149 $tl[5] = $inputs[$indx] + 1900;
150 return '';
151 }
152
153 sub rrrr
154 {
155 my $indx = shift;
156 return &rr($indx) if ($inputs[$indx] =~ /^\d\d?$/);
157 return "Invalid year ($inputs[$indx])! "
158 unless ($inputs[$indx] =~ /^\d\d\d\d?$/);
159 if (($today[5] % 100) > 50)
160 {
161 $inputs[$indx] += 100 if (($inputs[$indx] % 100) < 50);
162 }
163 else
164 {
165 #$inputs[$indx] -= 100 if (($inputs[$indx] % 100) > 50);
166 $inputs[$indx] += 100 if ($inputs[$indx] < 50);
167 }
168 $tl[5] = $inputs[$indx];
169 return '';
170 }
171
172 sub ddd
173 {
174 my $indx = shift;
175 $inputs[$indx] =~ s/^0+//;
176 return "Invalid year-day ($inputs[$indx])! "
177 unless ($inputs[$indx] > 0 and $inputs[$indx] <= 366);
178 $rtnTime = $begofyear + (($inputs[$indx]*86400) - 86400);
179 return '';
180 }
181
182 sub dd
183 {
184 my $indx = shift;
185 return "Invalid day ($inputs[$indx])! "
186 unless ($inputs[$indx] > 0 and $inputs[$indx] <= 31);
187 $inputs[$indx] =~ s/^0//;
188 $tl[3] = $inputs[$indx];
189 return '';
190 }
191
192 sub hh24
193 {
194 my $indx = shift;
195 return "Invalid 24-hr time ($inputs[$indx])! "
196 unless ($inputs[$indx] >= 0 and $inputs[$indx] <= 2400
197 and ($inputs[$indx] % 100) < 60);
198 $tl[1] = ($inputs[$indx] % 100);
199 $inputs[$indx] = int($inputs[$indx] / 100);
200 $tl[2] = $inputs[$indx];
201 return '';
202 }
203
204 sub hh
205 {
206 my $indx = shift;
207 return "Invalid hour ($inputs[$indx])! "
208 unless ($inputs[$indx] > 0 and $inputs[$indx] <= 12);
209 $tl[2] = $inputs[$indx];
210 $rtnTime += ($inputs[$indx] * 3600) if ($rtnTime);
211 return '';
212 }
213
214 sub a
215 {
216 my $indx = shift;
217 if ($tl[2] < 12)
218 {
219 $tl[2] += 12 if ($inputs[$indx] =~ /p/);
220 }
221 else
222 {
223 $tl[2] -= 12 if ($inputs[$indx] =~ /a/);
224 }
225 return '';
226 }
227
228 sub p
229 {
230 return &a;
231 }
232
233 sub mi
234 {
235 my $indx = shift;
236 $inputs[$indx] =~ s/^0//;
237 return "Invalid minutes ($inputs[$indx])! "
238 unless ($inputs[$indx] >= 0 and $inputs[$indx] <= 59);
239 $tl[1] = $inputs[$indx];
240 $rtnTime += ($inputs[$indx] * 60) if ($rtnTime);
241 return '';
242 }
243
244 sub sssss
245 {
246 my $indx = shift;
247 $inputs[$indx] =~ s/^0//;
248 return "Invalid seconds ($inputs[$indx])! "
249 unless ($inputs[$indx] >= 0 and $inputs[$indx] <= 86400);
250 $tl[0] = $inputs[$indx] % 60;
251 $tl[1] = int($inputs[$indx]/60) % 60;
252 $tl[2] = $inputs[$indx] % 3600;
253 $rtnTime += $inputs[$indx] if ($rtnTime);
254 return '';
255 }
256
257 sub ss
258 {
259 my $indx = shift;
260 $inputs[$indx] =~ s/^0//;
261 return "Invalid seconds ($inputs[$indx])! "
262 unless ($inputs[$indx] >= 0 and $inputs[$indx] <= 59);
263 $tl[0] = $inputs[$indx];
264 $rtnTime += $inputs[$indx] if ($rtnTime);
265 return '';
266 }
267
268 1