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

Respected Monks,

I want to be able to subtract and add the dates in my program,

example:- find a day 35 days before today. I could do that in a lengthy way, but I was wondering if there is a module that could make the coding easier and shorter.

I checked out with the Date::Calc module and found that it didn't have any function that could let me do that directly. The reason I need to add/subtract the days is I have the data files in the format $data_file="$DataDir/$month_names{$month}\_$day\_$year\.dat"; and I build html file today.html using the $data_file. I use localtime function to generate $month,$day and $year. If i don't find todays $data_file I have to use yesterday's data file or else day before yesterday data file to build today.html.

I appreciate your input.

Thank you,
nagesh

Edited 2001-12-13 by dvergin per author request

Replies are listed 'Best First'.
Re: Date related question
by Beatnik (Parson) on Dec 14, 2001 at 03:22 UTC
    Date::Manip and Date::Calc come to mind.
    Date::Calcs Add_Delta_Days subroutine.
    ($year,$month,$day) = Add_Delta_Days($year,$month,$day,$Dd); #$Dd can +be negative, no??

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Date related question
by rbc (Curate) on Dec 14, 2001 at 03:54 UTC
    Boy they are fast around here ... Here's a starting point of a possible solution right from the Camel book
    #!/usr/bin/perl use Getopt::Std; getopts( 'd:' ); if( $opt_d ) { $no_seconds_ago = $opt_d * 60 * 60 *24; } ( $sec, $min, $hr, $mday, $mon, $year, $wday, $yday, $isdst ) = localt +ime(time-$no_seconds_ago); print<\<EOT sec = $sec min = $min hr = $hr mday = $mday mon = $mon yr = $year wday = $wday yday = $yday isdst = $isdst EOT
(shockme) Re: Date related question
by shockme (Chaplain) on Dec 14, 2001 at 03:23 UTC
    There are plenty of date manipulation modules on CPAN. You might start with Class::Date, as it allows you to add and subtract days, years, etc.

    Update: After reading Beatnik's post, I recall that I have used Date::Calc before, and as he points out, it will do what you're needing to do. It's also far more straightforward and easy to use (IMO) than Class::Date.

    If things get any worse, I'll have to ask you to stop helping me.

(Ovid - formatting) Re: Date related question
by Ovid (Cardinal) on Dec 14, 2001 at 03:44 UTC

    Just a little request. I could edit your node to "fix" your formatting, but that strikes me as being very innapropriate, so I won't. However, I think you'll find that people may be more receptive if you simply slap <p></p> tags around your paragraphs. The random breaks you have hurt my eyes :) Compare:


    I want to be able to subtract and add the dates in my program, example:- find a day 35 days before today. I could do that in a lengthy way, but I was wondering if there is a module that could make the coding easier and shorter. I checked out with the Date::Calc module and found that it didn't have any function that could let me do that directly. The reason I need to add or subtract the days is I have the data files in the format

    $data_file="$DataDir/$month_names{$month}\_$day\_$year\.dat";

    and I build html file today.html using the $data_file. I use localtime function to generate $month,$day and $year If i don't find todays data_file I have to use yesterday's data file or else day before yesterday data file to build today.html.

    Separating out code with <code></code> tags is also helpful.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      I apologize for the bad formatting there. I will defenitely keep your tips in mind. Since I cannot directly change the format of the posting. I will request the editor about it.

      Thanks

      nagesh

Re: Date related question
by nagesh (Novice) on Dec 14, 2001 at 03:41 UTC
    Thanks for your help.

    nagesh