package ReturnDate;
use strict;
use Date::Manip;
use Carp;
sub new
{
my $self = shift;
return bless \$self;
}
sub Late
{
my $self = shift;
return ( Date_Cmp($_[0], $_[1]) < 0 ) ? $_[1] : $_[0];
}
sub Early
{
my $self = shift;
return ( Date_Cmp($_[0], $_[1]) < 0 ) ? $_[0] : $_[1];
}
sub Earliest
{
my $self = shift;
return &GetDate('Early', @_);
}
sub Latest
{
my $self = shift;
return &GetDate('Late', @_);
}
sub GetDate
{
my $operation = shift;
my @dates = @_;
my $date;
for my $item ( @dates )
{
my $d1 = $date || $item;
my $d2 = $item;
$date = ( $operation =~ /^Early$/ ) ? &Early( 1, $d1, $d2) :
( $operation =~ /^Late$/ ) ? &Late( 1, $d1, $d2) : u
+ndef;
}
return $date or confess "Could not return from GetDate";
}
sub Sorted
{
my $self = shift;
my @dates = @_;
my $sorted;
my %dates;
for (@dates)
{
$dates{UnixDate($_,"%s")} = $_;
}
push @{$sorted}, $dates{$_} for ( sort { $a<=>$b } keys %dates );
return $sorted;
}
1;
=head1 NAME
ReturnDate - Date::Manip subclassed.
=head1 SYNOPSYS
unshift @INC, '/usr/local/perl/lib';
require ReturnDate;
my $dateObj = ReturnDate->new();
my $early = $dateObj->Early('01/01/2000', '01/02/2007');
my $latest = $dateObj->Latest('01/01/2000', '01/02/2007', 'June 3,
+ 2010');
print "Early = $early\n";
print "Late = $late\n";
=head1 DESCRIPTION
Simplifies the need to programatically
figure out which date in a pair is earlier
or later.
=head2 Methods
=over 12
=item C<new>
Returns a ReturnDate object
=item C<Early>
Given two dates, returns the earlier of the two
=item C<Late>
Given two dates, returns the later of the two
=item C<Earliest>
Given a set of dates, returns the earliest
=item C<Latest>
Given a set of dates, returns the latest
=item C<Sorted>
Returns a date sorted array reference, which will accept
a list of dates such as ( 'June 1, 2000', '1/2/2003', '02/22/1999' )
=back
=head1 Author
Ted Fiedler <fiedlert@gmail.com>
=cut
|