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

here i am trying to comapre two dates and like this. can anybody tell me how to do that.
$dateslots[$len-1] = 8/11/2002; $i = 8/5/2002; for (;$i<=$dateslots[$len-1];($y,$m,$d)=$object->split_date($i), $i=$o +bject->get_next_date($y,$m,$d,1)) { $ret->[0] = $object->clean( $ret->[0] ); $ret->[1] = $object->hours_time( $ret->[1], 1 ); $ret->[2] = $object->hours_time( $ret->[2], 2 ); if ($ret->[0] > $i){ $i = $object->dateslot($i); my ($mdt,$dn,$s,$e) =($i,$i,'',''); if ($mdate) # finish existing date { colSpans( $roomSched, $end, $last ); push @roomSched, ($roomSched); } $roomSched = $object->blankRow($first,$last,$i,$i,''); ($start,$end ) = ( $last, $first ); $e = $first if ( !$e ); ($mdate,$start,$end ) = ($mdt,$s,$e);

Code tags - dvergin 2002-08-06

Replies are listed 'Best First'.
Re: date comparing
by DamnDirtyApe (Curate) on Aug 06, 2002 at 05:50 UTC

    Whoa there. That's a scary way to compare dates indeed. Take a look at the Date::Manip module on CPAN; it's got several functions for comparing two dates. It will undoubtedly save you many a headache.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: date comparing
by mem (Acolyte) on Aug 06, 2002 at 07:43 UTC

    Just a tip: if you have to compare dates and want to do it yourself, the general algorithm goes like this:

    • Parse the date
    • Convert to seconds since the epoch
    • Compare that. It can't get easier than $t1 <=> $t2

    Since the parsing task might be non-trivial, I like the Date::Parse module from TimeDate, but there are other date parsing modules in CPAN, and a bunch of them do date comparisons, too.

      Actually what you say is correct for determining time elapsed between two dates, but not for simply determining if two dates are inorder (what "compare" normally means). If the dates are in ISO or DIN compliant format then they are readily compared using the eq operator.

      Just another reason for discarding outdated conventional date formats like dd/mm/yy or the even more bizarre mm/dd/yy.

      print POSIX::strftime("%Y%m%d%H%M%S",localtime);
      Sorry, I guess i'm in a pedantic mood this morning. :-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

Re: date comparing
by demerphq (Chancellor) on Aug 06, 2002 at 09:15 UTC
    Hi,

    I have a hard time understanding this code. First off, theres this:

    $dateslots[$len-1] = 8/11/2002; $i = 8/5/2002;
    Do you realize that it puts the value 0.000363273090545818 into $dateslots[$len-1]? And 0.000799200799200799 into $i? Normally if you want to deal with dates in such a format you quote them: "8/11/2002".

    Next thing is that what format are these dates? Are they in the US format m/d/y or in the european format d/m/y? Perhaps if you used the ISO/DIN (and iirc US Military standard) YYYY/MM/DD there would be less ambiguity (and you could sort your dates easily.)

    for (;$i<=$dateslots[$len-1];($y,$m,$d)=$object->split_date($i), $i=$o +bject->get_next_date( +$y,$m,$d,1))
    Im assuming you come from a C background, but even there I dont see why you used a for(;;) loop and not a while() loop. For instance the above is written much clearer as (although I wonder if strict and warnings are on or not. You dont seem to declare any variables.)
    while ($i<=$dateslots[$len-1]) { ($y,$m,$d)=$object->split_date($i); $i=$object->get_next_date(+$y,$m,$d,1))
    Im sorry to say but besides this I cant see much more to do than to rethink your problem, clean up your code and review the Date modules on CPAN.

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.