Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Add A Number of Days to Today's Date

by clemburg (Curate)
on Sep 28, 2001 at 13:14 UTC ( [id://115344]=note: print w/replies, xml ) Need Help??


in reply to Add A Number of Days to Today's Date

You should also have a look at Date::Manip and Date::Calc. Both offer a large range of features that will probably cover your needs. Especially Date::Manip's ParseDate() function is a real time saver for tasks like this.

Example:

> perl -MDate::Manip -e 'print UnixDate(ParseDate("October 2nd"), "%Y- +% m-%d"), "\n"' 2001-10-02 > perl -MDate::Manip -e 'print UnixDate(DateCalc("October 2nd", "+ 20 +days", \$err), "%Y-%m-%d"), "\n"' 2001-10-22 > perl -MDate::Manip -e 'print UnixDate(ParseDate("today"), "%Y-%m-%d" +) , "\n"' 2001-09-28 > perl -MDate::Manip -e 'print UnixDate(ParseDate("10 days"), "%Y-%m-% +d "), "\n"' 2001-10-08 > perl -MDate::Manip -e 'print UnixDate(ParseDate("10 days ago"), "%Y- +% m-%d"), "\n"' 2001-09-18 > perl -MDate::Manip -e 'print UnixDate(ParseDate("Monday 1 week ago") +, "%Y-%m-%d"), "\n"' 2001-09-17

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com

Replies are listed 'Best First'.
Re: Re: Add A Number of Days to Today's Date
by davorg (Chancellor) on Sep 28, 2001 at 13:45 UTC

    I really don't like Date::Manip. Sure it does a lot, but that's it's downfall. In tests I've seen it take fifteen times as long to do simple date manipulations than an equivalent script written using Perl's built-in functions.

    There are times when that's a worthwhile hit to take, but in cases like this where it's very simple to do with built-ins I really don't think it's worthwhile.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      Sure, Date::Manip is slow compared to hand-rolled stuff. It even says so in the POD (see below). So there is no reason to complain about this, you have been warned.

      OTOH, I really think re-solving all those common date calculating problems again and again is a big waste of time, and a big source of errors in programs, too.

      From the Date::Manip POD:

      Date::Manip is certainly the most powerful of the Date modules. To the best of my knowledge, it will do everything that any other date module will do (not just the ones I listed above), and there are a number of features that Date::Manip has that none of the other modules have. Date::Manip is the "Swiss Army Knife" of Date modules. I'm trying to build a library which can do _EVERY_ conceivable date/time manipulation that you'll run into in everyday life. Although I am working on making Date::Manip faster, it will never be as fast as other modules. And before anyone asks, Date::Manip will never be translated to C (at least by me). I write C because I have to. I write perl because I like to. Date::Manip is something I do because it interests me, not something I'm paid for. Date::Manip is also big. The last time I looked, it's one of the largest CPAN modules there is. If you ignore modules like Tk, LWP, etc. which are actually packages of modules, it may be the largest. It's true that Date::Manip will do almost every date operation you could imagine... but you rarely need all that power. I'm working on reducing the footprint of Date::Manip, but even at it's slimmest, it'll outweigh the other modules by a good bit. If you are going to be using the module in cases where performance is an important factor (started up in a CGI program being run by your web server 5,000 times a second), you should check out one of the other Date or Time modules in CPAN. If you're only doing fairly simple date operations (parsing common date formats, finding the difference between two dates, etc.), the other modules will almost certainly suffice. If you're doing one operation very repetitively (parsing 10,000 dates from a database), you are probably better off writing your own functions (perhaps bypassing all date modules entirely) designed specifically for your needs. On the other hand, if you want one solution for all your date needs, don't need peak speed, or are trying to do more exotic date operations, Date::Manip is for you. Operations on things like business dates, foreign language dates, holidays and other recurring events, etc. are available more-or-less exclusively in Date::Manip.

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://115344]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-29 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found