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

Hi esteemed monks!

I just want to know if there exist any CPAN module which allows me to do complex date arithmetic.
Say, for example,I have a date 20031105 (as you gueesed in yyyymmdd format):)

And I want to know what would be the date after three months from this date (by simple lookup on calendar we can say it is 20040205)
It is very simple to program it and presently I have coded the script with the use of all regex and simple addition,but am sure that there exists a CPAN module to do this for me in a much more abstracted and neat manner.

Please help me out guys!

Thanks
Karthik

Replies are listed 'Best First'.
Re: doubt with date arithmetic
by Aristotle (Chancellor) on Aug 03, 2004 at 14:49 UTC

    I have come to love the DateTime modules for this kind of work. They're lean, offer a clean interface, and allow you to do just about anything you'd ever want to, without much hassle (so long as you don't mind occasionally reading the docs).

    If you want a sledgehammer solution, try Date::Manip — its original intent was to let people enter complex time specifications like "second Wednesday this month" on the commandline and have them work. It's rather heavy and the author himself discourages its use for more narrowly defined scenarios, though.

    Makeshifts last the longest.

Re: doubt with date arithmetic
by davorg (Chancellor) on Aug 03, 2004 at 15:07 UTC

    The DateTime project is supposed to make all other Perl date and time handling modules obsolete and replace them with a consistant set of modules.

    To do what you want with the DateTime modules, you'd use DateTime::Format::Strptime like this:

    #!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; my $str = '20031105'; my $parser = DateTime::Format::Strptime->new(pattern => '%Y%m%d'); my $dt = $parser->parse_datetime($str); $dt->add(months => 3); # standard date format print $dt->date, "\n"; # or in your original formay print $parser->format_datetime($dt), "\n";
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: doubt with date arithmetic
by friedo (Prior) on Aug 03, 2004 at 14:47 UTC
    The superb Date::Calc module handles lots of calendar arithmetic pretty easily.
Re: doubt with date arithmetic
by ysth (Canon) on Aug 03, 2004 at 17:36 UTC
    As mentioned by others, DateTime, Date::Calc, or Date::Manip can do this. There were some benchmark posts recently on the datetime list. I think these are fairly reflective of just about anything all three modules do, though, depending on your needs, any of them may be perfectly fine.