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

I have two dates and times I have to compare. I have current_date and requested_date(in the format of mm/dd/yr hr:min:sec). I have to check the difference between the two and do some processing if the time difference is >= 1hr. I was doing it initially with compaing the times but later realized that if requested date is between 23:00 and 24:00 of a day then this condition will nevere be reached. Any other ideas on how can I get the diffrence?
$cur_time = GetCurrentTime(); ($cdate, $ctime) = split(/ /, $cur_time); ($chour,$cmin,$csec) = split(/:/, $ctime); $current_time = ($chour*3600) + ($cmin*60) + $csec; same way for the req time: $requested_time = ($hour*3600) + ($min*60) + $sec; if (($current_time - $requested_time) >= 3600 Do processing.....
I tried the above code....

Replies are listed 'Best First'.
Re: Compare Date and Time
by marto (Cardinal) on May 09, 2006 at 15:22 UTC
    Hi anne,

    Check out the module Date::Calc.
    In the documentation there is a recipe titled "How do I compare two dates with times? How do I check whether two dates and times lie more or less than a given time interval apart?" which should be of use to you.

    Hope this helps.

    Martin
Re: Compare Date and Time
by davorg (Chancellor) on May 09, 2006 at 16:10 UTC

    This is why Time::Local is included in the standard Perl distribution.

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

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

Re: Compare Date and Time
by blazar (Canon) on May 09, 2006 at 15:23 UTC

    This kind of question get asked so often... and the general answer is to use a suitable date/time managing module. Two such modules (that I have tried) are DateTime and Date::Calc. Try reading their respective documentations to see if they suit your needs.

Re: Compare Date and Time
by McDarren (Abbot) on May 09, 2006 at 15:48 UTC
    There are also plenty of answers to this type of question in the Q&A section of the Monastery.