Slackget/Date.pm

filter: Statementstmt 70.0% 70 / 100 Branchbran 50.0% 32 / 64 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 80.0% 16 / 20 Podpod 100% 16 / 16 totaltotal 64.2% 135 / 210 CC 36 SCAR 29.7
CC
36
Cov
61%
compare
43.8
_fill_undef
22.6
is_equal
12.0
CRAP 19.4
« Slackget/SpecialFiles/PACKAGES.pm ↑ Summary Slackget/PackageList.pm »
1 package Slackware::Slackget::Date;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use overload
6 5 ▶ 'cmp' => \&compare_ng,
Subroutine BEGIN: called CC=1 SCAR=0.0
7 '<=>' => \&compare_ng,
8 5 'fallback' => 1;
9  
10 =head1 NAME
11  
12 Slackware::Slackget::Date - A class to manage date for slack-get.
13  
14 =head1 VERSION
15  
16 Version 1.0.1
17  
18 =cut
19  
20 5 our $VERSION = '1.0.1';
21  
22 =head1 SYNOPSIS
23  
24 This class is an abstraction of a date. It centralyze all operation you can do on a date (like comparisons)
25  
26 use Slackware::Slackget::Date;
27  
28 my $date = Slackware::Slackget::Date->new('day-name' => Mon, 'day-number' => 5, 'year' => 2005);
29 $date->year ;
30 my $status = $date->compare($another_date_object);
31 if($date->is_equal($another_date_object))
32 {
33 print "Nothing to do : date are the same\n";
34 }
35  
36 =head1 CONSTRUCTOR
37  
38 =head2 new
39  
40 The constructor take the followings arguments :
41  
42 day-name => the day name in : Mon, Tue, Wed, Thu, Fri, Sat, Sun
43 day-number => the day number from 1 to 31. WARNINGS : there is no verification about the date validity !
44 month-name => the month name (Jan, Feb, Apr, etc.)
45 month-number => the month number (1 to 12)
46 hour => the hour ( a string like : 12:52:00). The separator MUST BE ':'
47 year => a chicken name...no it's a joke. The year as integer (ex: 2005).
48 use-approximation => in this case the comparisons method just compare the followings : day, month and year. (default: no)
49  
50 You have to manage by yourself the date validity, because this class doesn't check the date validity. The main reason of this, is that this class is use to compare the date of specials files.
51  
52 So I use the predicate that peoples which make thoses files don't try to do a joke by a false date.
53  
54 my $date = Slackware::Slackget::Date->new(
55 'day-name' => Mon,
56 'day-number' => 5,
57 'year' => 2005,
58 'month-number' => 2,
59 'hour' => '12:02:35',
60 'use-approximation' => undef
61 );
62  
63 =cut
64  
65 5 my %equiv_month = (
66 'Non' => 0,
67 'Jan' => 1,
68 'Feb' => 2,
69 'Mar' => 3,
70 'Apr' => 4,
71 'May' => 5,
72 'Jun' => 6,
73 'Jul' => 7,
74 'Aug' => 8,
75 'Sep' => 9,
76 'Oct' => 10,
77 'Nov' => 11,
78 'Dec' => 12,
79 );
80  
81 5 my @equiv_month = ('Non','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
82  
83  
84 sub new
85 6 ▶{
Subroutine new: called CC=7 SCAR=20.5
Pod documented
86 6 my ($class,%args) = @_ ;
87 6 my $self={};
88 6 bless($self,$class);
89 6 ▶ $self->{DATE}->{'day-name'} = $args{'day-name'} if(defined($args{'day-name'})) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"day-name"} 6 0 6
90 6 ▶ $self->{DATE}->{'day-number'} = $args{'day-number'} if(defined($args{'day-number'})) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"day-number"} 6 0 6
91 6 ▶ $self->{DATE}->{'month-name'} = $args{'month-name'} if(defined($args{'month-name'})) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"month-name"} 0 6 6
92 6 ▶ $self->{DATE}->{'month-number'} = $args{'month-number'} if(defined($args{'month-number'})) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"month-number"} 6 0 6
93 6 ▶ $self->{DATE}->{'hour'} = $args{'hour'} if(defined($args{'hour'})) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"hour"} 6 0 6
94 6 ▶ $self->{DATE}->{'year'} = $args{'year'} if(defined($args{'year'})) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"year"} 6 0 6
95 6 $self->{'use-approximation'} = $args{'use-approximation'};
96 6 $self->_fill_undef;
97 6 return $self;
98 }
99  
100  
101 =head1 FUNCTIONS
102  
103 =head2 compare
104  
105 This mathod compare the current date object with a date object passed as parameter.
106  
107 my $status = $date->compare($another_date);
108  
109 The returned status is :
110  
111 0 : $another_date is equal to $date
112 1 : $date is greater than $another_date
113 2 : $date is lesser than $another_date
114  
115 =cut
116  
117 24 ▶sub compare {
Subroutine compare: called CC=17 SCAR=37.8
Pod documented
118 24 my ($self,$date) = @_;
119 24 ▶ return undef if(ref($date) ne 'Slackware::Slackget::Date') ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $date ne "Slackware::Slackget::Date" 0 24 24
120 24 ▶ if($self->year > $date->year){
Branch: 7 branches
BranchTrueFalseTotal
if ($self->year > $date->year) { } 0 24 24
elsif ($self->year < $date->year) { } 0 24 24
elsif ($self->monthnumber > $date->monthnumber) { } 8 16 24
elsif ($self->monthnumber < $date->monthnumber) { } 8 8 16
elsif ($self->daynumber > $date->daynumber) { } 0 8 8
elsif ($self->daynumber < $date->daynumber) { } 0 8 8
elsif (not $self->{'use-approximation'}) { } 8 0 8
121 0 return 1
122 }
123 elsif($self->year < $date->year){
124 0 return 2
125 }
126 elsif($self->monthnumber > $date->monthnumber){
127 8 return 1
128 }
129 elsif($self->monthnumber < $date->monthnumber){
130 8 return 2
131 }
132 elsif($self->daynumber > $date->daynumber){
133 0 return 1
134 }
135 elsif($self->daynumber < $date->daynumber){
136 0 return 2
137 }
138 elsif(!$self->{'use-approximation'}){
139 8 ▶ return 0 unless($self->hour);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hour 0 8 8
140 8 ▶ return 0 unless($date->hour);
Branch: 1 branch
BranchTrueFalseTotal
unless $date->hour 0 8 8
141 8 my @hour_self = $self->hour =~ /^(\d+):(\d+):(\d+)$/g ;
142 8 my @hour_date = $date->hour =~ /^(\d+):(\d+):(\d+)$/g ;
143 8 ▶ if($hour_self[0] > $hour_date[0])
Branch: 6 branches
BranchTrueFalseTotal
if ($hour_self[0] > $hour_date[0]) { } 0 8 8
elsif ($hour_self[0] < $hour_date[0]) { } 0 8 8
elsif ($hour_self[1] > $hour_date[1]) { } 0 8 8
elsif ($hour_self[1] < $hour_date[1]) { } 0 8 8
elsif ($hour_self[2] > $hour_date[2]) { } 0 8 8
elsif ($hour_self[2] < $hour_date[2]) { } 0 8 8
144 {
145 0 return 1;
146 }
147 elsif($hour_self[0] < $hour_date[0])
148 {
149 0 return 2;
150 }
151 elsif($hour_self[1] > $hour_date[1])
152 {
153 0 return 1;
154 }
155 elsif($hour_self[1] < $hour_date[1])
156 {
157 0 return 2;
158 }
159 elsif($hour_self[2] > $hour_date[2])
160 {
161 0 return 1;
162 }
163 elsif($hour_self[2] < $hour_date[2])
164 {
165 0 return 2;
166 }
167
168 }
169 8 return 0;
170 }
171  
172 =head2 compare_ng
173  
174 This method behave exactly the same way than compare() but is compliant with '<=>' and 'cmp' Perl operators.
175  
176 Instead of returning 2 if left operand is lesser than the right one, it return -1.
177  
178 The purpose of not modifying compare() directly is the backward compatibility.
179  
180 =cut
181  
182 24 ▶sub compare_ng {
Subroutine compare_ng: called CC=2 SCAR=6.9
Pod documented
183 24 my $r = compare(@_);
184 24 ▶ return -1 if($r == 2);
Branch: 1 branch
BranchTrueFalseTotal
if $r == 2 8 16 24
185 16 return $r;
186 }
187  
188 =head2 is_equal
189  
190 Take another date object as parameter and return TRUE (1) if this two date object are equal (if compare() return 0), and else return false (0).
191  
192 if($date->is_equal($another_date)){
193 ...do something...
194 }
195  
196 WARNING : this method also return undef if $another_date is not a Slackware::Slackget::Date object, so be carefull.
197  
198 =cut
199  
200 0 ▶sub is_equal {
Subroutine is_equal: not called CC=3 SCAR=24.8
Pod documented
201 0 my ($self,$date) = @_;
202 0 ▶ return undef if(ref($date) ne 'Slackware::Slackget::Date') ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $date ne "Slackware::Slackget::Date" 0 0 0
203 0 ▶ if($self->compare($date) == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->compare($date) == 0) { } 0 0 0
204 0 return 1;
205 }
206 else{
207 0 return 0;
208 }
209 }
210  
211 =head2 _fill_undef [PRIVATE]
212  
213 This method is call by the constructor to resolve the month equivalence (name/number).
214  
215 This method affect 0 to all undefined numerical values.
216  
217 =cut
218  
219 6 ▶sub _fill_undef {
Subroutine _fill_undef: called CC=9 SCAR=31.2
220 6 my $self = shift;
221 6 ▶ unless(defined($self->{DATE}->{'month-number'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"DATE"}{"month-number"}) 0 6 6
222 0 ▶ if(defined($self->{DATE}->{'month-name'}) && exists($equiv_month{$self->{DATE}->{'month-name'}}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'DATE'}{'month-name'} and exists $equiv_month{$self->{'DATE'}{'month-name'}}) { } 0 0 0
Condition0%0/3
defined $self->{"DATE"}{"month-name"} and exists $equiv_month{$self->{"DATE"}{"month-name"}}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"DATE"}{"month-name"} and exists $equiv_month{$self->{"DATE"}{"month-name"}}
defined $self->{"DATE"}{"month-name"} exists $equiv_month{$self->{"DATE"}{"month-name"}}
Truth table0%0/3
A and B
A= defined $self->{"DATE"}{"month-name"}B= exists $equiv_month{$self->{"DATE"}{"month-name"}}
ABresult
0X0
100
111
223 {
224 0 $self->{DATE}->{'month-number'} = $equiv_month{$self->{DATE}->{'month-name'}};
225 }
226 else{
227 0 $self->{DATE}->{'month-number'} = 0;
228 }
229 }
230 6 ▶ unless(defined($self->{DATE}->{'month-name'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"DATE"}{"month-name"}) 6 0 6
231 6 ▶ if(defined($self->{DATE}->{'month-number'}) && defined($equiv_month[$self->{DATE}->{'month-number'}]))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'DATE'}{'month-number'} and defined $equiv_month[$self->{'DATE'}{'month-number'}]) { } 6 0 6
Condition33%1/3
defined $self->{"DATE"}{"month-number"} and defined $equiv_month[$self->{"DATE"}{"month-number"}]
!ll&&!rl&&r
006
MC/DC0%0/2
defined $self->{"DATE"}{"month-number"} and defined $equiv_month[$self->{"DATE"}{"month-number"}]
defined $self->{"DATE"}{"month-number"} defined $equiv_month[$self->{"DATE"}{"month-number"}]
Truth table33%1/3
A and B
A= defined $self->{"DATE"}{"month-number"}B= defined $equiv_month[$self->{"DATE"}{"month-number"}]
ABresult
0X0
100
111
232 {
233 6 $self->{DATE}->{'month-name'} = $equiv_month[$self->{DATE}->{'month-number'}];
234 }
235 else{
236 0 $self->{DATE}->{'month-name'} = 'Non';
237 }
238 }
239 6 ▶ $self->{DATE}->{'day-number'} = 0 unless(defined($self->{DATE}->{'day-number'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"DATE"}{"day-number"} 0 6 6
240 6 ▶ $self->{DATE}->{'year'} = 0 unless(defined($self->{DATE}->{'year'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"DATE"}{"year"} 0 6 6
241 }
242  
243 =head2 today
244  
245 This method fill the Slackware::Slackget::Date object with the today parameters. This method fill the followings object value : day-number, year, month-number,
246  
247 $date->today ;
248 print "Today date is ",$date->to_string,"\n";
249  
250 =cut
251  
252 sub today
253 0 ▶{
Subroutine today: not called CC=1 SCAR=6.9
Pod documented
254 0 my $self = shift;
255 0 my $date_format = '%d/%m/%Y::%H:%M:%S';
256 0 my $date = `date +$date_format`;
257 0 my ($date_tmp,$hour) = split(/::/,$date);
258 0 my ($d,$m,$y) = split(/\//, $date_tmp);
259 0 $self->{DATE}->{'day-number'} = $d;
260 0 $self->{DATE}->{'month-number'} = $m;
261 0 $self->{DATE}->{'year'} = $y;
262 0 $self->{DATE}->{'hour'} = $hour;
263 }
264  
265 =head2 to_xml
266  
267 return the date as an XML encoded string.
268  
269 $xml = $date->to_xml();
270  
271 =cut
272  
273 sub to_xml
274 2 ▶{
Subroutine to_xml: called CC=3 SCAR=11.0
Pod documented
275 2 my $self = shift;
276 2 my $xml = "<date ";
277 2 foreach (keys(%{$self->{DATE}})){
278 12 ▶ $xml .= "$_=\"$self->{DATE}->{$_}\" " if(defined($self->{DATE}->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"DATE"}{$_} 12 0 12
279 }
280 2 $xml .= "/>\n";
281 2 return $xml;
282 }
283  
284 =head2 to_XML (deprecated)
285  
286 same as to_xml() provided for backward compatibility.
287  
288 =cut
289  
290 0 ▶sub to_XML {
Subroutine to_XML: not called CC=1 SCAR=6.9
Pod documented
291 0 return to_xml(@_);
292 }
293  
294  
295 =head2 to_html
296  
297 return the date as an HTML encoded string.
298  
299 $xml = $date->to_html();
300  
301 =cut
302  
303 sub to_html
304 2 ▶{
Subroutine to_html: called CC=1 SCAR=0.0
Pod documented
305 2 my $self = shift;
306 2 my $xml = "<strong>Date :</strong> $self->{DATE}->{'day-number'}/$self->{DATE}->{'month-number'}/$self->{DATE}->{'year'} $self->{DATE}->{'hour'}<br/>\n";
307 # foreach (keys(%{$self->{DATE}})){
308 # $xml .= "<strong>$_ :</strong> $self->{DATE}->{$_}<br/>" if(defined($self->{DATE}->{$_}));
309 # }
310 # $xml .= "</p>\n";
311 2 return $xml;
312 }
313  
314 =head2 to_HTML (deprecated)
315  
316 same as to_html() provided for backward compatibility.
317  
318 =cut
319  
320 0 ▶sub to_HTML {
Subroutine to_HTML: not called CC=1 SCAR=6.9
Pod documented
321 0 return to_html(@_);
322 }
323  
324 =head2 to_string
325  
326 return the date as a plain text string.
327  
328 print "Date of the package is ", $package->date()->to_string,"\n";
329  
330 =cut
331  
332 sub to_string
333 2 ▶{
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
334 2 my $self = shift;
335 2 return "$self->{DATE}->{'day-number'}/$self->{DATE}->{'month-number'}/$self->{DATE}->{'year'} $self->{DATE}->{'hour'}";
336 }
337  
338 =head1 ACCESSORS
339  
340 =cut
341  
342 =head2 year
343  
344 return the year
345  
346 my $string = $date->year;
347  
348 =cut
349  
350 98 ▶sub year {
Subroutine year: called CC=1 SCAR=0.0
Pod documented
351 98 my $self = shift;
352 98 return $self->{DATE}->{'year'};
353 }
354  
355 =head2 monthname
356  
357 return the monthname
358  
359 my $string = $date->monthname;
360  
361 =cut
362  
363 2 ▶sub monthname {
Subroutine monthname: called CC=1 SCAR=0.0
Pod documented
364 2 my $self = shift;
365 2 return $self->{DATE}->{'month-name'};
366 }
367  
368 =head2 dayname
369  
370 return the 'day-name'
371  
372 my $string = $date->'day-name';
373  
374 =cut
375  
376 2 ▶sub dayname {
Subroutine dayname: called CC=1 SCAR=0.0
Pod documented
377 2 my $self = shift;
378 2 return $self->{DATE}->{'day-name'};
379 }
380  
381 =head2 hour
382  
383 return the hour
384  
385 my $string = $date->hour;
386  
387 =cut
388  
389 34 ▶sub hour {
Subroutine hour: called CC=1 SCAR=0.0
Pod documented
390 34 my $self = shift;
391 34 return $self->{DATE}->{'hour'};
392 }
393  
394 =head2 daynumber
395  
396 return the daynumber
397  
398 my $string = $date->daynumber;
399  
400 =cut
401  
402 34 ▶sub daynumber {
Subroutine daynumber: called CC=1 SCAR=0.0
Pod documented
403 34 my $self = shift;
404 34 return $self->{DATE}->{'day-number'};
405 }
406  
407 =head2 monthnumber
408  
409 return the monthnumber
410  
411 my $string = $date->monthnumber;
412  
413 =cut
414  
415 82 ▶sub monthnumber {
Subroutine monthnumber: called CC=1 SCAR=0.0
Pod documented
416 82 my $self = shift;
417 82 return $self->{DATE}->{'month-number'};
418 }
419  
420 =head1 AUTHOR
421  
422 DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >>
423  
424 =head1 BUGS
425  
426 Please report any bugs or feature requests to
427 C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at
428 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>.
429 I will be notified, and then you'll automatically be notified of progress on
430 your bug as I make changes.
431  
432 =head1 SUPPORT
433  
434 You can find documentation for this module with the perldoc command.
435  
436 perldoc Slackware::Slackget
437  
438  
439 You can also look for information at:
440  
441 =over 4
442  
443 =item * Infinity Perl website
444  
445 L<http://www.infinityperl.org/category/slack-get>
446  
447 =item * slack-get specific website
448  
449 L<http://slackget.infinityperl.org>
450  
451 =item * RT: CPAN's request tracker
452  
453 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget>
454  
455 =item * AnnoCPAN: Annotated CPAN documentation
456  
457 L<http://annocpan.org/dist/Slackware-Slackget>
458  
459 =item * CPAN Ratings
460  
461 L<http://cpanratings.perl.org/d/Slackware-Slackget>
462  
463 =item * Search CPAN
464  
465 L<http://search.cpan.org/dist/Slackware-Slackget>
466  
467 =back
468  
469 =head1 ACKNOWLEDGEMENTS
470  
471 Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
472  
473 =head1 SEE ALSO
474  
475 =head1 COPYRIGHT & LICENSE
476  
477 Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
478  
479 This program is free software; you can redistribute it and/or modify it
480 under the same terms as Perl itself.
481  
482 =cut
483  
484 5 1; # End of Slackware::Slackget::Date
« Slackget/SpecialFiles/PACKAGES.pm ↑ Summary Slackget/PackageList.pm »