File Coverage

blib/lib/Slackware/Slackget/Date.pm
Criterion Covered Total %
statement 66 96 68.7
branch 32 64 50.0
condition 1 6 16.6
subroutine 16 20 80.0
pod 16 16 100.0
total 131 202 64.8


line stmt bran cond sub pod time code
1             package Slackware::Slackget::Date;
2              
3 5     5   101686 use warnings;
  5         6  
  5         131  
4 5     5   16 use strict;
  5         4  
  5         154  
5             use overload
6 5         32 'cmp' => \&compare_ng,
7             '<=>' => \&compare_ng,
8 5     5   1926 'fallback' => 1;
  5         1660  
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             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             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             my @equiv_month = ('Non','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
82              
83              
84             sub new
85             {
86 6     6 1 214 my ($class,%args) = @_ ;
87 6         11 my $self={};
88 6         8 bless($self,$class);
89 6 50       65 $self->{DATE}->{'day-name'} = $args{'day-name'} if(defined($args{'day-name'})) ;
90 6 50       15 $self->{DATE}->{'day-number'} = $args{'day-number'} if(defined($args{'day-number'})) ;
91 6 50       15 $self->{DATE}->{'month-name'} = $args{'month-name'} if(defined($args{'month-name'})) ;
92 6 50       13 $self->{DATE}->{'month-number'} = $args{'month-number'} if(defined($args{'month-number'})) ;
93 6 50       14 $self->{DATE}->{'hour'} = $args{'hour'} if(defined($args{'hour'})) ;
94 6 50       11 $self->{DATE}->{'year'} = $args{'year'} if(defined($args{'year'})) ;
95 6         7 $self->{'use-approximation'} = $args{'use-approximation'};
96 6         10 $self->_fill_undef;
97 6         13 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             sub compare {
118 24     24 1 22 my ($self,$date) = @_;
119 24 50       46 return undef if(ref($date) ne 'Slackware::Slackget::Date') ;
120 24 50       31 if($self->year > $date->year){
    50          
    100          
    100          
    50          
    50          
    50          
121 0         0 return 1
122             }
123             elsif($self->year < $date->year){
124 0         0 return 2
125             }
126             elsif($self->monthnumber > $date->monthnumber){
127 8         14 return 1
128             }
129             elsif($self->monthnumber < $date->monthnumber){
130 8         10 return 2
131             }
132             elsif($self->daynumber > $date->daynumber){
133 0         0 return 1
134             }
135             elsif($self->daynumber < $date->daynumber){
136 0         0 return 2
137             }
138             elsif(!$self->{'use-approximation'}){
139 8 50       10 return 0 unless($self->hour);
140 8 50       11 return 0 unless($date->hour);
141 8         11 my @hour_self = $self->hour =~ /^(\d+):(\d+):(\d+)$/g ;
142 8         13 my @hour_date = $date->hour =~ /^(\d+):(\d+):(\d+)$/g ;
143 8 50       104 if($hour_self[0] > $hour_date[0])
    50          
    50          
    50          
    50          
    50          
144             {
145 0         0 return 1;
146             }
147             elsif($hour_self[0] < $hour_date[0])
148             {
149 0         0 return 2;
150             }
151             elsif($hour_self[1] > $hour_date[1])
152             {
153 0         0 return 1;
154             }
155             elsif($hour_self[1] < $hour_date[1])
156             {
157 0         0 return 2;
158             }
159             elsif($hour_self[2] > $hour_date[2])
160             {
161 0         0 return 1;
162             }
163             elsif($hour_self[2] < $hour_date[2])
164             {
165 0         0 return 2;
166             }
167            
168             }
169 8         12 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             sub compare_ng {
183 24     24 1 49 my $r = compare(@_);
184 24 100       102 return -1 if($r == 2);
185 16         44 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             sub is_equal {
201 0     0 1 0 my ($self,$date) = @_;
202 0 0       0 return undef if(ref($date) ne 'Slackware::Slackget::Date') ;
203 0 0       0 if($self->compare($date) == 0){
204 0         0 return 1;
205             }
206             else{
207 0         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             sub _fill_undef {
220 6     6   7 my $self = shift;
221 6 50       12 unless(defined($self->{DATE}->{'month-number'})){
222 0 0 0     0 if(defined($self->{DATE}->{'month-name'}) && exists($equiv_month{$self->{DATE}->{'month-name'}}))
223             {
224 0         0 $self->{DATE}->{'month-number'} = $equiv_month{$self->{DATE}->{'month-name'}};
225             }
226             else{
227 0         0 $self->{DATE}->{'month-number'} = 0;
228             }
229             }
230 6 50       14 unless(defined($self->{DATE}->{'month-name'})){
231 6 50 33     26 if(defined($self->{DATE}->{'month-number'}) && defined($equiv_month[$self->{DATE}->{'month-number'}]))
232             {
233 6         10 $self->{DATE}->{'month-name'} = $equiv_month[$self->{DATE}->{'month-number'}];
234             }
235             else{
236 0         0 $self->{DATE}->{'month-name'} = 'Non';
237             }
238             }
239 6 50       10 $self->{DATE}->{'day-number'} = 0 unless(defined($self->{DATE}->{'day-number'}));
240 6 50       13 $self->{DATE}->{'year'} = 0 unless(defined($self->{DATE}->{'year'}));
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             {
254 0     0 1 0 my $self = shift;
255 0         0 my $date_format = '%d/%m/%Y::%H:%M:%S';
256 0         0 my $date = `date +$date_format`;
257 0         0 my ($date_tmp,$hour) = split(/::/,$date);
258 0         0 my ($d,$m,$y) = split(/\//, $date_tmp);
259 0         0 $self->{DATE}->{'day-number'} = $d;
260 0         0 $self->{DATE}->{'month-number'} = $m;
261 0         0 $self->{DATE}->{'year'} = $y;
262 0         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             {
275 2     2 1 3 my $self = shift;
276 2         3 my $xml = "<date ";
277 2         3 foreach (keys(%{$self->{DATE}})){
  2         7  
278 12 50       29 $xml .= "$_=\"$self->{DATE}->{$_}\" " if(defined($self->{DATE}->{$_}));
279             }
280 2         4 $xml .= "/>\n";
281 2         6 return $xml;
282             }
283              
284             =head2 to_XML (deprecated)
285              
286             same as to_xml() provided for backward compatibility.
287              
288             =cut
289              
290             sub to_XML {
291 0     0 1 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             {
305 2     2 1 3 my $self = shift;
306 2         11 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         11 return $xml;
312             }
313              
314             =head2 to_HTML (deprecated)
315              
316             same as to_html() provided for backward compatibility.
317              
318             =cut
319              
320             sub to_HTML {
321 0     0 1 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             {
334 2     2 1 4 my $self = shift;
335 2         13 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             sub year {
351 98     98 1 961 my $self = shift;
352 98         198 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             sub monthname {
364 2     2 1 3 my $self = shift;
365 2         8 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             sub dayname {
377 2     2 1 2 my $self = shift;
378 2         7 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             sub hour {
390 34     34 1 21 my $self = shift;
391 34         109 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             sub daynumber {
403 34     34 1 20 my $self = shift;
404 34         62 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             sub monthnumber {
416 82     82 1 50 my $self = shift;
417 82         112 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             1; # End of Slackware::Slackget::Date