Package to calculate time diff

T

* Tong *

Hi,

I'm wondering which package can do time diff calculation. I.e., something
that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.

I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
of them can. Maybe I'm too blunt, so I'm not going to show my stupid
trials here.

Thanks for your help.
 
I

it_says_BALLS_on_your forehead

* Tong * said:
Hi,

I'm wondering which package can do time diff calculation. I.e., something
that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.

I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
of them can. Maybe I'm too blunt, so I'm not going to show my stupid
trials here.

Thanks for your help.

use Date::Manip;

and here's a link that provides some examples of its use:
http://www.icewalkers.com/Perl/5.8.0/lib/Date/Manip.html
 
J

John W. Krahn

* Tong * said:
I'm wondering which package can do time diff calculation. I.e., something
that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.

I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
of them can. Maybe I'm too blunt, so I'm not going to show my stupid
trials here.


$ perl -le'
use Time::Local;
my $end = q/0:40:33/;
my $start = q/0:28:50/;

sub time_diff { join q/:/, ( gmtime timegm( reverse 1, $_[1] =~ /\d+/g ) -
timegm( reverse 1, $_[0] =~ /\d+/g ) )[2,1,0] }

print "Start: $start End: $end Diff: ", time_diff $start, $end;
'
Start: 0:28:50 End: 0:40:33 Diff: 0:11:43


Or if you want to use sprintf to get leading zeros:

sub time_diff { sprintf q/%3$d:%2$02d:%1$02d/, gmtime timegm( reverse 1, $_[1]
=~ /\d+/g ) - timegm( reverse 1, $_[0] =~ /\d+/g ) }




John
 
W

William James

* Tong * said:
Hi,

I'm wondering which package can do time diff calculation. I.e., something
that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.

I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
of them can. Maybe I'm too blunt, so I'm not going to show my stupid
trials here.

Thanks for your help.

Perhaps you would prefer to use Ruby:

class String
def to_time
Time.local(2000,1,1,*self.split(':'))
end
end

def timediff( t1, t2 )
Time.at(t2.to_time - t1.to_time).gmtime.strftime("%H:%M:%S")
end

puts timediff( '0:28:50', '0:40:33' )
 
L

l v

* Tong * said:
Hi,

I'm wondering which package can do time diff calculation. I.e., something
that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.

I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
of them can. Maybe I'm too blunt, so I'm not going to show my stupid
trials here.

Thanks for your help.

I don't know how the Ruby solution will help you out, but Date::Calc is
my preferred *Perl* module. However, when comparing times with
Date::Calc you need to pass it the date (year, month, day) as well. For
this example I used Date::Calc's Today function for that. If you
determine the correct date, then Date::Calc will correctly calculate the
time diff when spanning different days.

use Date::Calc qw(Today Delta_YMDHMS);

my $end = q/0:40:33/;
my $start = q/0:28:50/;

my ($D_y,$D_m,$D_d, $Dh,$Dm,$Ds) =
Delta_YMDHMS(Today(), (split /:/, $start) ,
Today(), (split /:/, $end) );

printf "Start: %s End: %s Diff: %02d:%02d:%02d",
$start, $end, $Dh, $Dm, $Ds;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top