in reply to Simple date calc

Simple version

Since time is the current moment in time, as a Unix Epoch second value, just subtract 86400 from that, and use localtime in a scalar context to make a date string:
my $yesterday = localtime (time - 86400);
You can extract month/day/year from that string, or use the more general list context version and do the processing yourself:
my @yesterday_values = localtime(time - 86400);

More precise version

Those versions suffer from the "daylight savings time" switch, and so it fails once or twice a year when a day has 25 hours or 23 hours in it. To get around that, aim for something "mid-day" yesterday, since you don't care about the specific time:
my $current_hour = (localtime)[2]; my $yesterday_around_noon = localtime(time - ($current_hour + 12) * 36 +00);
This works by getting us back to roughly midnight today, then going back another 12 hours. Even if the day is 23 or 25 hours, this'll still be within the mid-day span.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Simple date calc
by aennen (Acolyte) on Dec 19, 2002 at 18:30 UTC
    Ahhh what I just found interesting is that if I inlcude Time::localtime i get
    Time::tm=ARRAY(0x400978cc)
    in the interpolation of the array. Code ::
    use Time::localtime; $offset = time(); @yesterday_values = localtime($offset); print "@yesterday_values\n"