http://qs1969.pair.com?node_id=641387
Category: Miscellaneous
Author/Contact Info Ted Fiedler <fiedlert@gmail.com>
Description: Returns the Earlier/Later Dates. I could not find anything on CPAN which provides this exact functionality.

upddate added a Sorted method to return a sorted list of dates
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
Replies are listed 'Best First'.
Re: ReturnDate.pm
by ikegami (Patriarch) on Sep 27, 2007 at 18:25 UTC

    Alternatively, you can use core module List::Util's min and max functions on the values returned by various functions including

Re: ReturnDate.pm
by jdporter (Paladin) on Sep 28, 2007 at 00:59 UTC
    I could not find anything on CPAN...

    That's because it's overkill for a module. As ikegami said:

    use Date::Manip; use List::Util qw(reduce); sub earliest { reduce { Date_Cmp( $a , $b ) < 0 ? $a : $b } map { ParseDate($_) } + @_ } sub latest { reduce { Date_Cmp( $a , $b ) > 0 ? $a : $b } map { ParseDate($_) } + @_ } my $e = earliest( @dates ); my $l = latest( @dates );
    A word spoken in Mind will reach its own level, in the objective world, by its own weight