Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Package to calculate time diff

Reply
Thread Tools

Package to calculate time diff

 
 
* Tong *
Guest
Posts: n/a
 
      09-21-2005
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:ate 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.

--
Tong (remove underscore(s) to reply)
*niX Power Tools Project: http://xpt.sourceforge.net/
- All free contribution & collection
 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      09-21-2005

* Tong * wrote:
> 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:ate 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.
>
> --
> Tong (remove underscore(s) to reply)
> *niX Power Tools Project: http://xpt.sourceforge.net/
> - All free contribution & collection


use Date::Manip;

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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-21-2005
* Tong * wrote:
>
> 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:ate 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-21-2005
* Tong * wrote:
> 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:ate 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.
>
> --
> Tong (remove underscore(s) to reply)
> *niX Power Tools Project: http://xpt.sourceforge.net/
> - All free contribution & collection


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' )

 
Reply With Quote
 
l v
Guest
Posts: n/a
 
      09-22-2005
* Tong * wrote:
> 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:ate 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;

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert time to ruby time to calculate the time difference. Ruwan Budha Ruby 4 03-09-2011 04:43 PM
Diff CSS styles for diff INPUT TYPE='s? A Traveler ASP .Net 6 08-31-2004 09:17 PM
[ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4 Austin Ziegler Ruby 3 08-09-2004 06:34 AM
diff Process under diff users Cyril Vi?ville Perl 1 06-29-2004 06:22 PM
Same sessionID retuned to diff browsers in diff machines Berrucho ASP .Net 2 12-05-2003 02:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57