in reply to How to substract date in perl?

You can always do this with a simple localtime:
my $time = time(); my ($d1, $m1, $y1) = (localtime($time))[3..5]; $y1 += 1900; $m1 += 1; $time -= 24*3600*3; # this is three days of seconds my ($d2, $m2, $y2) = (localtime($time))[3..5]; $y2 += 1900; $m2 += 1; print "$y1\_$m1\_$d1\n"; print "$y2\_$m2\_$d2\n";
Just subtract three days worth of seconds from the argument to localtime and you'll get the date three days ago. It'll account for February and any other weird months that come along too. ;-)

Update: forgot to add 1 to the month.

-caedes

Replies are listed 'Best First'.
Re: How to substract date in perl?
by Abigail-II (Bishop) on Feb 26, 2003 at 09:39 UTC
    However, that will give the wrong date about 6 hours a year. You didn't take into account that two days a year, the day isn't 24 * 3600 seconds, but either 25 * 3600 or 23 * 3600 seconds. That means for the 3 days after daylight switch over, you're be wrong either the hour before midnight, or the hour after.

    Abigail

      Ah ha :-) But that would only happen if one was to do something crazy like daylight savings time now wouldn't it? =)

      Daylight savings is the exception and not the rule. I could just as easily declare time to reverse during a full moon, but I'm not going to add that functionality to my program at this time. =P

      I can almost always rely on someone to think of an aspect of a problem that had not occured to me at first. I guess that's one of the really valuable parts of the monastery.

      -caedes

        Well, this issue is is discussed in perlfaq4.pod....

        Abigail