Lately I have been using a particular set of code over and over again, and I finally decided to put it into a module. I would like everyone's overall opinion of this code, and whether or not they think it might even be worthy of CPAN.
package Date::Lastday;
$VERSION=0.01;
use strict;
## GLOBALS ##
my %Max_Days=("Jan"=>"31","Feb"=>"28","Mar"=>"31","Apr"=>"30",
"May"=>"31","Jun"=>"30","Jul"=>"31","Aug"=>"31",
"Sep"=>"30","Oct"=>"31","Nov"=>"30","Dec"=>"31");
my %Months=("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr",
"05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug",
"09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec");
## Functions ##
sub new{
my $self=shift;
return bless {} $self;
}
sub last{
my $self=shift;
my ($Day,$Month,$Year)=(localtime)[3,4,5];
$Month=sprintf("%02d,($Month+1));
$Year=sprintf("%04d,($Year+1900));
## Provide for leap years ##
my $Leap=$Year % 4;
if($Leap==0){
$Max_Days{"Feb"}=29;
}
if($Day==1 and $Month==1){ # Cover January 1st
$Month=12;
$Day=31;
$Year=$Year-1;
}elsif($Day==1){ # Cover the 1st of any month
$Month-=1;
$Month=sprintf("%02d",$Month);
$Day=$Max_Days{$Months{$Month}};
}else{
$Day-=1;
$Day=sprintf("%02d",$Day);
}
return $Month,$Day,$Year;
}
sub cal{
my $self=shift;
my ($Day,$Month,$Year)=(localtime)[3,4,5];
$Month=sprintf("%02d,($Month+1));
$Year=sprintf("%04d,($Year+1900));
## Provide for leap years ##
my $Leap=$Year % 4;
if($Leap==0){
$Max_Days{"Feb"}=29;
}
if($Day==1 and $Month==1){ # Cover January 1st
$Month="Dec";
$Day=31;
$Year=$Year-1;
}elsif($Day==1){ # Cover the 1st of any month
$Month-=1;
$Month=sprintf("%02d",$Month);
$Month=$Months{$Month};
$Day=$Max_Days{$Months};
}else{
$Day-=1;
$Day=sprintf("%02d",$Day);
$Month=$Months{$Month};
}
return $Month,$Day,$Year;
}
1;
__END__;
=pod
=head1 NAME
Date::Lastday - Perl extension to calculate previous day's date
=head1 SYNOPSIS
use Date::Lastday;
my $Last=new Date::Lastday;
my ($Month,$Day,$Year)=$Last->last();
## OR ##
my ($Month,$Day,$Year)=$Last->cal();
=head1 DESCRIPTION
Date::Lastday is used to calculate the date of the previous day. It us
+es the localtime function of Perl to get the date, then manipulate it
+ from there.
The last function returns the month and day as 2 digit numbers, and th
+e year as a 4 digit number. If you wish to have the month returned as
+ the 3 letter abbreviation (i.e. Jan, Feb, Mar, etc.), use the cal fu
+nction.
=head2 EXPORT
None by default
=head1 AUTHOR
Thomas Stanley
Thomas_J_Stanley@msn.com
=head1 COPYRIGHT
Copyright (C) 2001 Thomas Stanley. All rights reserved. This program i
+s free software; you can redistribute it and/or modify it under the s
+ame terms as Perl itself.
=head1 SEE ALSO
perl(1)
=cut
TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
about this script for Hamlet they've worked out
-- Douglas Adams/Hitchhiker's Guide to the Galaxy