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

hi all,

I have following two dates

my $date_1="Aug 26 23:10:59"; my $date_2="Aug 27 10:59:02";
how to find the difference between these 2 dates in seconds.

Replies are listed 'Best First'.
Re: Finding the difference between 2 dates in seconds
by bruceb3 (Pilgrim) on Sep 17, 2007 at 09:23 UTC
    Or try Date::Calc for similar effect as the previous response.
    #!/usr/bin/env perl # use strict; use warnings; use Date::Calc qw/ Mktime /; my $date1 = "Aug 26 23:10:59"; my $date2 = "Aug 27 10:59:02"; # assume that the year needed is 2007; my $year1 = my $year2 = 2007; sub split_date_str { my %months = ( Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, J +un => 6, Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, + Dec => 12 ); my $str = shift; my @f = split /\s+/, $str; my $month = $months{$f[0]} || die "invalid month in string: '$str' +\n"; ($month, $f[1], split(/:/, $f[2])); } my $diff = Mktime( $year2, split_date_str($date2) ) - Mktime( $year1, + split_date_str($date1) ); print $diff,"\n";
Re: Finding the difference between 2 dates in seconds
by johngg (Canon) on Sep 17, 2007 at 08:57 UTC
    Have a look at the documentation for Time::Local.

    Cheers,

    JohnGG

Re: Finding the difference between 2 dates in seconds
by almut (Canon) on Sep 17, 2007 at 11:29 UTC

    Or, for the lazy...

    use Date::Manip; my @sec = map UnixDate($_, "%s"), "Aug 26 23:10:59", "Aug 27 10:59:02" +; printf "diff secs: %d\n", $sec[1] - $sec[0]; # = 42483

      I'm even lazier :)

      use Date::Parse; print str2time( 'Aug 27 10:59:02' ) - str2time( 'Aug 26 23:10:59' );

      Alternately,

      use Date::Manip; my $date_1="Aug 26 23:10:59"; my $date_2="Aug 27 10:59:02"; my $sec = Delta_Format(DateCalc($date_1, $date_2), 0, "%st"); printf "diff secs: %d\n", $sec;
Re: Finding the difference between 2 dates in seconds
by oxone (Friar) on Sep 17, 2007 at 11:29 UTC
    Your example case is a relatively simple one in that the two dates in question are within the same month.

    If you wanted this to be absolutely correct for ANY two dates, there are plenty of complications to consider: not least of them being leap years and worldwide time zones.

    I recently had to solve a similar problem and looked into various modules out there, and concluded that DateTime was the best one to use. May be worth you taking a look.

Re: Finding the difference between 2 dates in seconds
by qsl (Scribe) on Sep 17, 2007 at 10:27 UTC
    #!/usr/bin/perl use Time::Local; my $val= "Aug 26 23:10:59"; $val =~ tr/:/ /; my $val1="Aug 27 10:59:02"; $val1 =~ tr/:/ /; my %MONTH_NAMES = ( "Jan" => '01', "Feb"=> '02', "Mar" => '03', "Apr"=> '04',"May" => '05', "Jun" => '06', "Jul" => '07', "Aug" => '08', "sep" => '09', "oct" => '10', "Nov" => '11', "Dec" => '12' ); my $sysdate= `date`; chomp($sysdate); my $year=(split(/ /,$sysdate))[-1]; my($mon,$mday,$hour,$minute,$sec)=split(/ /,$val); $time_1 = timelocal($sec,$minute,$hour,$mday,$mon,$year); my($mon,$mday,$hour,$minute,$sec)=split(/ /,$val1); $time_2 = timelocal($sec,$minute,$hour,$mday,$mon,$year); print "1 :$time_1:\n"; print "2 :$time_2:\n"; $diff_time = $time_2 - $time_1; print "diff_time :$diff_time:\n"; output : 1 :1169824259: 2 :1169866742: diff_time :42483:
    Is the above out put write ? ( Date Difference in Seconds )
Re: Finding the difference between 2 dates in seconds
by andreas1234567 (Vicar) on Sep 28, 2007 at 19:40 UTC
    The number of date and time modules on CPAN can be overwhelming, and finding the right one for one particular situation can be quite a challenge. The modules all have their unique properties, from highly specialized (and possibly very performant) to very general and customizable. I suggest you try out a couple of them before choosing the right one for you.
    $ cat 639358.pl use strict; use warnings; use Date::Calc qw(Delta_YMDHMS); use Date::Manip; use Date::Parse; use DateTime; use Benchmark qw(cmpthese); my ($d1, $d2) = ("Aug 26 23:10:59", "Aug 27 10:59:02"); sub date_manip { return UnixDate($d1, "%s") - UnixDate($d2, "%s"); } sub date_calc { return Delta_YMDHMS(2007, 8, 26, 23, 10, 59, 2007, 8, 27, 10, 59, 02 +); } sub date_parse { return str2time($d1) - str2time($d2); } sub datetime { return DateTime->new( year => 2007, month => 8, day => 26, hour => 23, minute => 10, second => 59 ) - DateTime->new( year => 2007, month => 8, day => 27, hour => 10, minute => 59, second => 02 ); } cmpthese( -1, { date_manip => \&date_manip, date_parse => \&date_parse, date_calc => \&date_calc, datetime => \&datetime } ); __END__ $ perl 639358.pl Rate date_manip datetime date_parse date_calc date_manip 310/s -- -65% -89% -100% datetime 888/s 186% -- -68% -100% date_parse 2774/s 794% 212% -- -99% date_calc 374797/s 120730% 42109% 13413% --
    --
    Andreas