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

Date::Calc Module

by skirrow (Novice)
on Dec 29, 2001 at 22:15 UTC ( [id://135133]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm trying to write a script that will read a database, check the date in the DB, add X days to that date, compare it with today's date and if the dates match (todays and modfied date from db) then perform a function. If they don't it just needs to move to the next line of the DB. I think the Date::Calc module will do this for me but the documentation is really confusing and I can't figure out how to do what I want. My question is, how do I add X days to one date, match it against today's date and then perform a function if it matches. All help is welcome as I'm pretty stuck! - Neil

Replies are listed 'Best First'.
(jeffa) Re: Date::Calc Module
by jeffa (Bishop) on Dec 29, 2001 at 22:35 UTC
    I like maverick's suggestion better, but here is some code for you to toy with to get the hang of the somewhat hard to get the hang of Date::Calc
    use strict; use Date::Calc qw(Add_Delta_YMD Delta_Days); my $date1 = '2001/12/25/'; # Dec 25, 2001 my ($d,$m,$y) = (localtime)[3..5]; my $today = join('/', $y+1900, $m+1, $d); # add x days to date1 my $date2 = join('/', Add_Delta_YMD( split('/',$date1), # date 1 0,0,4 # plus 4 days ) ); # compare it to today's date print "same day\n" if Delta_Days( split('/',$date2), split('/',$today), ) == 0;
    I also recommend getting a copy of the Perl Cookbook, chapter 3 is devoted solely to working with dates.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      Hi, No matter what I change $date1 to, it returns 'same day'!? - Neil
        If you change the day portion, you will not get 'same day'. However, if you change the year or the month without changing the day, you will. The reason is because i chose to compare the dates by the number of days - not the year and/or month.

        Like I said, this is just to get you going.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        F--F--F--F--F--F--F--F--
        (the triplet paradiddle)
        
(maverick) Re: Date::Calc Module
by maverick (Curate) on Dec 29, 2001 at 22:24 UTC
    Most databases have date manipulation functions built in. If you're using mysql for example (your database probably has different commands for this) you write your sql query as:
    SELECT something FROM some_table WHERE TO_DAYS(NOW()) - TO_DAYS(date_column) = 10;
    That would return all the matching rows where date_column is 10 days less that today's date. NOW give you the current date, TO_DAYS converts the date to a number of days so that you can do a subtraction. This would be significantly faster than selecting out every row and then doing the date manipulation stuff in perl.

    HTH (I can provide a more complete example if you want)

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

      Hi, I'm using a flat-file text db but thanks anyway. :o) - Neil
(shockme) Re: Date::Calc Module
by shockme (Chaplain) on Dec 29, 2001 at 22:39 UTC
    maverick's suggestion is definitely the efficient way to go. However, if you still find yourself needing Date::Calc, here's the format to add 5 days to your current date:

    $NumDays = 5; ($NewYear, $NewMon, $NewYear) = Add_Delta_Days($CurrYear, $CurrMon, $CurrDay, $NumDays);

    Update: Doh! jeffa beat me to the punch. And, as usual, he's response is more complete. ++

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

      That did exactly what I need. Thank you! I'd found that code before but got confused with how to use it I think but now i've figured it out! Thanks a lot everyone for all your help! - Neil
Date::Calc Alternative
by Jazz (Curate) on Dec 30, 2001 at 08:03 UTC

    I like Class::Date instead of Date::Calc for this kind of simple date math (where sql statements can't be used).

    use Class::Date qw/ date localdate now /; my $cmpday = date( date( '2001-11-9' ) + '1M' + '20D' ); # add 1 month +, 20 days my $today = date( localdate( now ))->truncate; # truncate makes time +midnight. if ( $cmpday == $today ){ print "Today is $cmpday.\n"; } else { print +( Class::Date->new( $today ) - $cmpday )->day, " days away. +\n"; }

    Date::Calc is definitely faster, so if you're needing lots of speedy date calcs, stick with Date::Calc. Docs for Class::Date is here.

Re: Date::Calc Module
by rbc (Curate) on Dec 30, 2001 at 07:43 UTC
    I would do this in the DBMS.
    Assuming PL/SQL
    procedure foo ( Xday number ) is nmatch number := 0; begin select count(*) into nmatchs from Whatever a where trunc(a.theDBdate + Xday) = trunc(sysdate); if nmatch > 0 then callSomeFunction(); end if; end;

    This assumes that the function you are calling
    is a stored function, and your DB is oracle and
    well ... it assumes a lot. But I think it is fair
    to ask why not do this with a SQL query?

    I don't know ... sorry if this isn't the
    answer you are looking for but sometimes
    you can't beat a good select statement.

    --
    Its like a dog that can sing and dance.
    It's remarkable because it can do it.
    Not that it can do it well.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found