Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

ReturnDate.pm

by tcf03 (Deacon)
on Sep 27, 2007 at 18:04 UTC ( #641387=sourcecode: print w/replies, xml ) Need Help??
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 (Chancellor) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://641387]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2022-01-25 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?