Re: getting a time diff from strings
- From: jag.robin18@xxxxxxxxxx (Gerard Robin)
- Date: Thu, 29 Dec 2005 11:25:05 +0100
On Wed, Dec 28, 2005 at 09:18:55PM -0500, Robert wrote:
From: Robert <sigzero@xxxxxxxxx> To: beginners@xxxxxxxx Subject: getting a time diff from strings
I have a log that I am parsing and I can get the login and logout time string parsed out. It looks like this:
13:50:01 # this is the when the user logged in 14:14:35 # this is when the user logged out
I need to get how much time that is but I am not sure how to go about it since it is a string (that I assume I have to convert to "real" time to do the math with).
#!/usr/bin/perl #timelogged.pl
use warnings; use strict; use POSIX;
use constant Hs => 3600; use constant Hmn => 60;
my $login = '13:50:01'; my $logout = '14:14:35'; my @Login = split ':', $login; my @Logout = split ':', $logout;
my $sumin = $Login[0]*Hs + $Login[1]*Hmn + $Login[2]; my $sumout = $Logout[0]*Hs + $Logout[1]*Hmn + $Logout[2];
my $diff = $sumout - $sumin;
my $hours = floor($diff/Hs);
my $rest1 = $diff % Hs; my $mn = floor($rest1/Hmn); my $sec = $rest1 % Hmn;
print "Time spends : $hours h $mn mn $sec sec\n";
__END__
hth if you don't have the Time::Piece module
You can make subroutines ....
-- Gérard
.
- Follow-Ups:
- Re: getting a time diff from strings
- From: Robert Hicks
- Re: getting a time diff from strings
- References:
- getting a time diff from strings
- From: Robert
- getting a time diff from strings
- Prev by Date: strange problem with STDIN- need help
- Next by Date: Re: strange problem with STDIN- need help
- Previous by thread: getting a time diff from strings
- Next by thread: Re: getting a time diff from strings
- Index(es):
Relevant Pages
|