ferum has asked for the wisdom of the Perl Monks concerning the following question:

Hello...
Let say I have this code...
$currentDate = `date "+%Y_%m_%d"`; $currentDate =~ s/\s/ /g; chop($currentDate);
I want to get today's date and the date 3 days before today's...
This will become problems when the month is February in a leap year...
Anybody can contribute idea for this...?

Replies are listed 'Best First'.
Re: How to substract date in perl?
by graff (Chancellor) on Feb 26, 2003 at 06:32 UTC
    Have you tried typing "date arithmetic" into the search box? Or looking into the "dates and times" section of "Q&A"? That should help...

    update: (noting with some personal remorse that this was your first post...) Sorry to be less than helpful. Welcome! The short answer would be something like:

    use Date::Manip; $currentDate = UnixDate( "today", "%Y_%m_%d" ); $currentMinusThree = UnixDate( "3 days ago", "%Y/%m/%d" ); print join " ", $currentDate, $currentMinusThree, "\n";
    In other words, don't bother with trying to munge the output of the shell "date" command -- perl modules make things a lot easier than that.
      thankss for all of you!!! your helps will be appreciated... i'm a newbie here... so, need to learn a lot... ;)
Re: How to substract date in perl?
by JayBonci (Curate) on Feb 26, 2003 at 06:56 UTC
    Date::Calc is really useful for this sort of item.
    use strict; use Date::Calc qw(Add_Delta_Days); my (undef, undef, undef, $day, $month,$year) = localtime(); $year+=1900; $month+=1; ($year,$month,$day) = Add_Delta_Days($year,$month,$day, -3); print join("-", $year,$month,$day)
    Using localtime() is a little bit easier (and more portable, if that matters) than calling date from the shell. Good luck.

        --jaybonci
Re: How to substract date in perl?
by hotshot (Prior) on Feb 26, 2003 at 06:48 UTC
    You can always get the current time in seconds since the epoc (time function), substruct 3*24*60*60 (3 days in seconds), and use existing commands for tranforming the numOfSeconds you got into a date.

    Hotshot
Re: How to substract date in perl?
by caedes (Pilgrim) on Feb 26, 2003 at 07:40 UTC
    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

      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