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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.