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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on 'Difference between two dates in a ss:mm:hh etc format?'

Replies are listed 'Best First'.
Re: 'Difference between two dates in a ss:mm:hh etc format?'
by davorg (Chancellor) on May 01, 2002 at 05:59 UTC

    Once again I repeat my recommendation for Time::Piece. It's also worth noting that of all the Date/Time modules, this is the one that is most likely to make it into the core distribution (it was going to be in 5.8, but was removed at the last minute).

    I should also point out this section from the documentation for Date::Manip

    SHOULD I USE DATE::MANIP If you look in CPAN, you'll find that there are a number of Date and Time packages. Is Date::Manip the one you should be using? In my opinion, the answer is no most of the time. This sounds odd coming from the author of the software, but read on. Date::Manip is written entirely in perl. It's the most powerful of the date modules. It's also the biggest and slowest.

    For this reason, I avoid Date::Manip most of the time.

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

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

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: 'Difference between two dates in a ss:mm:hh etc format?'
by mojotoad (Monsignor) on Apr 30, 2002 at 22:04 UTC
    It's still not entirely clear to me what you are looking for -- your restated question seems ambiguous because you say "5 seconds between them" is what you are looking for, however, Date::Manip returning a number of seconds between two dates is insufficient.

    At any rate, Date::Manip (does not require compilation) appears to cover all of the contingencies in your problem. From the Date::Manip docs:

    4. The amount of time between two dates. $date1=&ParseDate($string1); $date2=&ParseDate($string2); $delta=&DateCalc($date1,$date2,\$err); => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes, and seconds between the two $delta=&DateCalc($date1,$date2,\$err,1); => YY:MM:WK:DD:HH:MM:SS the years, months, etc. between the two

    In the interests of being specific, how exactly is this not meeting your needs?

    Cheers,
    Matt

Re: 'Difference between two dates in a ss:mm:hh etc format?'
by coreolyn (Parson) on Apr 30, 2002 at 22:00 UTC

    I think ar0n already gave you the best answer here when you first asked it.

    coreolyn (not looking for a flame war -- It's just the right answer)

      And I thought I gave you the 2nd best answer here with the Date::Manip solution. To get into more detail:

      use Date::Manip; # This would give you h:m:s difference Delta_Format(DateCalc($day1, $day2), 2, '%hh h:%mv m:%sv s');

      If you could post a concrete example of what you are looking for, or what has been wrong with the solutions offered to you, that would make it easier for us to help you.

Re: 'Difference between two dates in a ss:mm:hh etc format?'
by rbc (Curate) on May 01, 2002 at 00:05 UTC
    You might try this ...
    $ cat datediff.pl #!/usr/bin/perl -w use strict; use Time::JulianDay; use vars qw/ $opt_a $opt_b /; use Getopt::Std; my $dateA = "59:59:23:31:11:2002"; # 1 second before 2003 my $dateB = "00:00:00:01:00:2003"; # 2003 getopt( "a:b:"); if( $opt_a ) { $dateA = $opt_a; } if( $opt_b ) { $dateB = $opt_b; } print "dateA = $dateA\n"; print "dateB = $dateB\n"; my ($sec,$min,$hours,$mday,$month_0_to_11,$year) = split /:/, $dateA; print "($sec,$min,$hours,$mday,$month_0_to_11,$year)\n"; ### ssA is the seconds since JAN 1st 1970 for dateA my $ssA = jd_timelocal($sec,$min,$hours,$mday,$month_0_to_11,$year); print "seconds from Jan 1st 1970 to 1 second before 2003 = $ssA\n"; ($sec,$min,$hours,$mday,$month_0_to_11,$year) = split /:/, $dateB; print "($sec,$min,$hours,$mday,$month_0_to_11,$year)\n"; ### ssB is the seconds since JAN 1st 1970 for dateB my $ssB = jd_timelocal($sec,$min,$hours,$mday,$month_0_to_11,$year); print "number of seconds from Jan 1st 1970 to midnight 2003 = $ssB\n"; my $diff = $ssB - $ssA; print "The diffence in seconds between $dateB and $dateA = $diff\n";
    Here's soe output ...
    $ ./datediff.pl dateA = 59:59:23:31:11:2002 dateB = 00:00:00:01:00:2003 (59,59,23,31,11,2002) number of seconds from Jan 1st 1970 to 1 second before 2003 = 10414079 +99 (00,00,00,01,00,2003) number of seconds from Jan 1st 1970 to midnight 2003 = 1041408000 The diffence in seconds between 00:00:00:01:00:2003 and 59:59:23:31:11 +:2002 = 1

    the module Time::JulianDay is very to install.
Re: 'Difference between two dates in a ss:mm:hh etc format?'
by BUU (Prior) on May 01, 2002 at 03:43 UTC
    Much thanks to everyone who pointed out Date::Manip; this does exactly what i want, i must of missed it or something. My apologies to all of you who had to restate their opinion, i may have missed it in the first thread, i got slightly distracted and was trying to stay away from that thread to avoid further incitement.