The simple answer? See perlfaq4.
The following provides examples (and a tweak) to the answers found there. It also shows different ways to use Date::Calc and Date::Manip.
#!/usr/bin/perl -w
use strict;
use Date::Calc ( ":all" );
use Date::Manip;
my ( $date, $yy, $dd, $mm );
print "There are various ways to get yesterday's date. Here are\n",
"are a few alternatives and their results:\n\n";
# perlfaq4 - simple
$date = scalar localtime( ( time() - ( 24 * 60 * 60 ) ) );
print "1. The simple calculation in perlfaq4 yields: ",
"$date.\n\n";
# perlfaq4 - DST aware
$date = scalar localtime( yesterday() );
print "2. The DST subroutine in perlfaq4 reports: ",
"$date.\n\n";
# One way using Date::Calc
( $yy, $mm, $dd ) = Today();
( $yy, $mm, $dd ) = Add_Delta_Days( $yy, $mm, $dd, -1 );
$mm = Month_to_Text( $mm );
print "3. A simple use of Date::Calc gives: ",
"$dd $mm $yy.\n\n";
# Date::Manip; note that the timezone is my local;
# change as needed.
$ENV{TZ} = "PST8PDT";
$date = ParseDate( "yesterday" );
print "4. Date::Manip returns $date, as well as ",
UnixDate( "yesterday", "%e %b %Y"),
"\n\n";
print "Other approaches are certainly possible.\n";
exit 1;
sub yesterday
{ # Borrowed from perlfaq4; note changes below.
my $now = defined $_[0] ? $_[0] : time;
my $then = $now - 60 * 60 * 24;
my $ndst = (localtime $now)[8] > 0;
my $tdst = (localtime $then)[8] > 0;
# Added '=' to avoid warning (and return)
$then -= ($tdst - $ndst) * 60 * 60;
return $then
}
--f
|