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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: calculate time(days)
by moodster (Hermit) on Apr 04, 2002 at 17:08 UTC
    Given that you've supplied almost no background information whatsoever, I'd say that you have several options.

    If you have two perl scalars with strings representing dates you could go with the various Decode_Date_XX functions of Date::Calc, calculate the number of days each date represent (Date_to_Days) and compare these values. This is all covered in perldoc Date::Calc

    There are several date manipulation modules to choose from on CPAN, but Date::Calc is one of the most popular. You might want to look into Date::Simple as well.

    If your data is stored in a database you may be able to do the comparison on the database level, depending on what database flavor you are using. For example, MS SQL will let you subtract one date from another calculate the difference in days. The SQL for calculating the number of days between the START and STOP values for all rows in the EXAMPLE table looks something like this:



Re: calculate time(days)
by ls (Scribe) on Apr 04, 2002 at 16:38 UTC
    In which format are these dates?

    Maybe Date::Calc could help you.

Re: calculate time(days)
by Stegalex (Chaplain) on Apr 04, 2002 at 17:20 UTC
    How is DBI germaine to your question? By your question, I am wondering if you meant to ask how you compute the difference between two dates using SQL. If that's your question then we need to know the DBMS you are using, no? Otherwise, if you are asking how to do it in Perl, use Date::Calc. I like chicken.
Re: calculate time(days)
by data67 (Monk) on Apr 04, 2002 at 21:58 UTC
    Date::Calc is definately the way to go if you are doing date computations and you can try using "SYSDATE" on your RDBMS side. Instead of worrying about date or time stamps by your perl, always use SYSDATE instead, works much better.

    $SQL = "INSERT INTO DB (name, date) VALUES (?, SYSDATE)"; $sth = $dbh->prepare($SQL); $sth->execute ( $name);
Re: calculate time(days)
by SageMusings (Beadle) on Apr 04, 2002 at 20:05 UTC
    Fellow Perl disciple,

    I have no idea what type of dates you are working with. Where I work, we use Julian calendar dates in our databases. I have developed a method for resolving the span of dates between to Julian dates. It took no thought and 13 lines of code.

    Also, you are not going to implement such a function in SQL. Forget the database integration; it is just an abstraction layer.

    I apologise for not including the code, I mentioned, but I am logging on today from an alien machine in an alien office. So far, no one has noticed my meditation robes or heard my chanting of Perl mantras.
      I always code these things in either PL/SQL or PL/Perl, which both provide database integration, as they run inside the database server and not on the database client.
      Snazzy tagline here
Re: calculate time(days)
by seanbo (Chaplain) on Apr 04, 2002 at 21:36 UTC
    you could:
    use Date::Manip; $d=&DateCalc($start,$end); if($d eq "+0:0:0:0:2:0:0") #2 hours { print "two hours\n"; } else { print "not two hours\n"; }

    ...or something like that

    perl -e 'print reverse qw/o b n a e s/;'